[97] | 1 | #include "arx.h" |
---|
[270] | 2 | namespace bdm { |
---|
[13] | 3 | |
---|
[170] | 4 | void ARX::bayes ( const vec &dt, const double w ) { |
---|
[97] | 5 | double lnc; |
---|
[625] | 6 | |
---|
[477] | 7 | if ( frg < 1.0 ) { |
---|
[665] | 8 | est.pow ( frg ); // multiply V and nu |
---|
[639] | 9 | |
---|
| 10 | //stabilize |
---|
[665] | 11 | ldmat V0=alter_est._V(); //$ copy |
---|
| 12 | double &nu0=alter_est._nu(); |
---|
| 13 | V0*=(1-frg); |
---|
[639] | 14 | V += V0; //stabilization |
---|
[665] | 15 | nu +=(1-frg)*nu0; |
---|
[639] | 16 | |
---|
[665] | 17 | // recompute loglikelihood of new "prior" |
---|
[170] | 18 | if ( evalll ) { |
---|
[162] | 19 | last_lognc = est.lognc(); |
---|
| 20 | } |
---|
| 21 | } |
---|
[625] | 22 | if (have_constant) { |
---|
| 23 | _dt.set_subvector(0,dt); |
---|
| 24 | V.opupdt ( _dt, w ); |
---|
| 25 | } else { |
---|
| 26 | V.opupdt ( dt, w ); |
---|
| 27 | } |
---|
[477] | 28 | nu += w; |
---|
[97] | 29 | |
---|
[204] | 30 | // log(sqrt(2*pi)) = 0.91893853320467 |
---|
[97] | 31 | if ( evalll ) { |
---|
| 32 | lnc = est.lognc(); |
---|
[204] | 33 | ll = lnc - last_lognc - 0.91893853320467; |
---|
[97] | 34 | last_lognc = lnc; |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
[170] | 38 | double ARX::logpred ( const vec &dt ) const { |
---|
| 39 | egiw pred ( est ); |
---|
[477] | 40 | ldmat &V = pred._V(); |
---|
| 41 | double &nu = pred._nu(); |
---|
[170] | 42 | |
---|
| 43 | double lll; |
---|
| 44 | |
---|
[477] | 45 | if ( frg < 1.0 ) { |
---|
[170] | 46 | pred.pow ( frg ); |
---|
| 47 | lll = pred.lognc(); |
---|
[477] | 48 | } else//should be save: last_lognc is changed only by bayes; |
---|
| 49 | if ( evalll ) { |
---|
| 50 | lll = last_lognc; |
---|
| 51 | } else { |
---|
| 52 | lll = pred.lognc(); |
---|
| 53 | } |
---|
[170] | 54 | |
---|
[477] | 55 | V.opupdt ( dt, 1.0 ); |
---|
| 56 | nu += 1.0; |
---|
[201] | 57 | // log(sqrt(2*pi)) = 0.91893853320467 |
---|
[477] | 58 | return pred.lognc() - lll - 0.91893853320467; |
---|
[170] | 59 | } |
---|
| 60 | |
---|
[283] | 61 | ARX* ARX::_copy_ ( ) const { |
---|
[477] | 62 | ARX* Tmp = new ARX ( *this ); |
---|
[170] | 63 | return Tmp; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void ARX::set_statistics ( const BMEF* B0 ) { |
---|
[477] | 67 | const ARX* A0 = dynamic_cast<const ARX*> ( B0 ); |
---|
[170] | 68 | |
---|
[565] | 69 | bdm_assert_debug ( V.rows() == A0->V.rows(), "ARX::set_statistics Statistics differ" ); |
---|
[270] | 70 | set_statistics ( A0->dimx, A0->V, A0->nu ); |
---|
[170] | 71 | } |
---|
[180] | 72 | |
---|
[270] | 73 | enorm<ldmat>* ARX::epredictor ( const vec &rgr ) const { |
---|
[477] | 74 | int dim = dimx;//est.dimension(); |
---|
| 75 | mat mu ( dim, V.rows() - dim ); |
---|
| 76 | mat R ( dim, dim ); |
---|
[270] | 77 | |
---|
[198] | 78 | enorm<ldmat>* tmp; |
---|
[477] | 79 | tmp = new enorm<ldmat> ( ); |
---|
[270] | 80 | //TODO: too hackish |
---|
[477] | 81 | if ( drv._dsize() > 0 ) { |
---|
[270] | 82 | } |
---|
[198] | 83 | |
---|
[477] | 84 | est.mean_mat ( mu, R ); //mu = |
---|
[198] | 85 | //correction for student-t -- TODO check if correct!! |
---|
| 86 | //R*=nu/(nu-2); |
---|
[477] | 87 | mat p_mu = mu.T() * rgr; //the result is one column |
---|
| 88 | tmp->set_parameters ( p_mu.get_col ( 0 ), ldmat ( R ) ); |
---|
[198] | 89 | return tmp; |
---|
| 90 | } |
---|
| 91 | |
---|
[270] | 92 | mlnorm<ldmat>* ARX::predictor ( ) const { |
---|
[477] | 93 | int dim = est.dimension(); |
---|
[625] | 94 | |
---|
[477] | 95 | mat mu ( dim, V.rows() - dim ); |
---|
| 96 | mat R ( dim, dim ); |
---|
[198] | 97 | mlnorm<ldmat>* tmp; |
---|
[477] | 98 | tmp = new mlnorm<ldmat> ( ); |
---|
[198] | 99 | |
---|
[477] | 100 | est.mean_mat ( mu, R ); //mu = |
---|
[198] | 101 | mu = mu.T(); |
---|
| 102 | //correction for student-t -- TODO check if correct!! |
---|
| 103 | |
---|
[625] | 104 | if ( have_constant) { // constant term |
---|
[198] | 105 | //Assume the constant term is the last one: |
---|
[477] | 106 | tmp->set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ) ); |
---|
[625] | 107 | } else { |
---|
| 108 | tmp->set_parameters ( mu, zeros ( dim ), ldmat ( R ) ); |
---|
[198] | 109 | } |
---|
| 110 | return tmp; |
---|
| 111 | } |
---|
| 112 | |
---|
[270] | 113 | mlstudent* ARX::predictor_student ( ) const { |
---|
| 114 | int dim = est.dimension(); |
---|
[198] | 115 | |
---|
[477] | 116 | mat mu ( dim, V.rows() - dim ); |
---|
| 117 | mat R ( dim, dim ); |
---|
[198] | 118 | mlstudent* tmp; |
---|
[477] | 119 | tmp = new mlstudent ( ); |
---|
[198] | 120 | |
---|
[477] | 121 | est.mean_mat ( mu, R ); // |
---|
[198] | 122 | mu = mu.T(); |
---|
[270] | 123 | |
---|
| 124 | int xdim = dimx; |
---|
[477] | 125 | int end = V._L().rows() - 1; |
---|
| 126 | ldmat Lam ( V._L() ( xdim, end, xdim, end ), V._D() ( xdim, end ) ); //exp val of R |
---|
[198] | 127 | |
---|
| 128 | |
---|
[625] | 129 | if ( have_constant) { // no constant term |
---|
[198] | 130 | //Assume the constant term is the last one: |
---|
[477] | 131 | if ( mu.cols() > 1 ) { |
---|
| 132 | tmp->set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ), Lam ); |
---|
| 133 | } else { |
---|
| 134 | tmp->set_parameters ( mat ( dim, 0 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ), Lam ); |
---|
[270] | 135 | } |
---|
[625] | 136 | } else { |
---|
| 137 | // no constant term |
---|
| 138 | tmp->set_parameters ( mu, zeros ( xdim ), ldmat ( R ), Lam ); |
---|
[198] | 139 | } |
---|
[180] | 140 | return tmp; |
---|
| 141 | } |
---|
| 142 | |
---|
[585] | 143 | |
---|
| 144 | |
---|
[97] | 145 | /*! \brief Return the best structure |
---|
| 146 | @param Eg a copy of GiW density that is being examined |
---|
| 147 | @param Eg0 a copy of prior GiW density before estimation |
---|
| 148 | @param Egll likelihood of the current Eg |
---|
| 149 | @param indeces current indeces |
---|
| 150 | \return best likelihood in the structure below the given one |
---|
| 151 | */ |
---|
| 152 | double egiw_bestbelow ( egiw Eg, egiw Eg0, double Egll, ivec &indeces ) { //parameter Eg is a copy! |
---|
| 153 | ldmat Vo = Eg._V(); //copy |
---|
| 154 | ldmat Vo0 = Eg._V(); //copy |
---|
| 155 | ldmat& Vp = Eg._V(); // pointer into Eg |
---|
| 156 | ldmat& Vp0 = Eg._V(); // pointer into Eg |
---|
[477] | 157 | int end = Vp.rows() - 1; |
---|
[97] | 158 | int i; |
---|
| 159 | mat Li; |
---|
| 160 | mat Li0; |
---|
[477] | 161 | double maxll = Egll; |
---|
| 162 | double tmpll = Egll; |
---|
| 163 | double belll = Egll; |
---|
[97] | 164 | |
---|
| 165 | ivec tmpindeces; |
---|
[477] | 166 | ivec maxindeces = indeces; |
---|
[97] | 167 | |
---|
[115] | 168 | |
---|
[477] | 169 | cout << "bb:(" << indeces << ") ll=" << Egll << endl; |
---|
[115] | 170 | |
---|
[97] | 171 | //try to remove only one rv |
---|
[477] | 172 | for ( i = 0; i < end; i++ ) { |
---|
[97] | 173 | //copy original |
---|
| 174 | Li = Vo._L(); |
---|
| 175 | Li0 = Vo0._L(); |
---|
| 176 | //remove stuff |
---|
[477] | 177 | Li.del_col ( i + 1 ); |
---|
| 178 | Li0.del_col ( i + 1 ); |
---|
| 179 | Vp.ldform ( Li, Vo._D() ); |
---|
| 180 | Vp0.ldform ( Li0, Vo0._D() ); |
---|
| 181 | tmpll = Eg.lognc() - Eg0.lognc(); // likelihood is difference of norm. coefs. |
---|
[115] | 182 | |
---|
[477] | 183 | cout << "i=(" << i << ") ll=" << tmpll << endl; |
---|
[170] | 184 | |
---|
[97] | 185 | // |
---|
| 186 | if ( tmpll > Egll ) { //increase of the likelihood |
---|
| 187 | tmpindeces = indeces; |
---|
| 188 | tmpindeces.del ( i ); |
---|
| 189 | //search for a better match in this substructure |
---|
[477] | 190 | belll = egiw_bestbelow ( Eg, Eg0, tmpll, tmpindeces ); |
---|
| 191 | if ( belll > maxll ) { //better match found |
---|
[97] | 192 | maxll = belll; |
---|
| 193 | maxindeces = tmpindeces; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | indeces = maxindeces; |
---|
| 198 | return maxll; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | ivec ARX::structure_est ( egiw est0 ) { |
---|
[477] | 202 | ivec ind = linspace ( 1, est.dimension() - 1 ); |
---|
| 203 | egiw_bestbelow ( est, est0, est.lognc() - est0.lognc(), ind ); |
---|
[97] | 204 | return ind; |
---|
| 205 | } |
---|
[254] | 206 | |
---|
[577] | 207 | |
---|
| 208 | |
---|
| 209 | ivec ARX::structure_est_LT ( egiw est0 ) { |
---|
| 210 | //some stuff with beliefs etc. |
---|
[585] | 211 | // ivec ind = bdm::straux1(V,nu, est0._V(), est0._nu()); |
---|
| 212 | return ivec();//ind; |
---|
[577] | 213 | } |
---|
| 214 | |
---|
[477] | 215 | void ARX::from_setting ( const Setting &set ) { |
---|
[625] | 216 | shared_ptr<RV> yrv = UI::build<RV> ( set, "rv", UI::compulsory ); |
---|
[527] | 217 | shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr", UI::compulsory ); |
---|
[357] | 218 | int ylen = yrv->_dsize(); |
---|
[625] | 219 | // rgrlen - including constant!!! |
---|
[357] | 220 | int rgrlen = rrv->_dsize(); |
---|
[625] | 221 | |
---|
[665] | 222 | dimx = ylen; |
---|
[625] | 223 | set_rv ( *yrv, *rrv ); |
---|
| 224 | |
---|
[585] | 225 | string opt; |
---|
| 226 | if ( UI::get(opt, set, "options", UI::optional) ) { |
---|
| 227 | BM::set_options(opt); |
---|
| 228 | } |
---|
[631] | 229 | int constant; |
---|
| 230 | if (!UI::get(constant, set, "constant", UI::optional)){ |
---|
[625] | 231 | have_constant=true; |
---|
[631] | 232 | } else { |
---|
| 233 | have_constant=constant>0; |
---|
[625] | 234 | } |
---|
| 235 | if (have_constant) {rgrlen++;_dt=ones(rgrlen+ylen);} |
---|
[585] | 236 | |
---|
[357] | 237 | //init |
---|
[665] | 238 | shared_ptr<egiw> pri=UI::build<egiw>(set, "prior", UI::optional); |
---|
| 239 | if (pri) { |
---|
| 240 | bdm_assert(pri->_dimx()==ylen,"prior is not compatible"); |
---|
| 241 | bdm_assert(pri->_V().rows()==ylen+rgrlen,"prior is not compatible"); |
---|
| 242 | est.set_parameters( pri->_dimx(),pri->_V(), pri->_nu()); |
---|
| 243 | }else{ |
---|
| 244 | est.set_parameters( ylen, zeros(ylen+rgrlen)); |
---|
| 245 | set_prior_default(est); |
---|
[625] | 246 | } |
---|
[665] | 247 | |
---|
| 248 | shared_ptr<egiw> alt=UI::build<egiw>(set, "alternative", UI::optional); |
---|
| 249 | if (alt) { |
---|
| 250 | bdm_assert(alt->_dimx()==ylen,"alternative is not compatible"); |
---|
| 251 | bdm_assert(alt->_V().rows()==ylen+rgrlen,"alternative is not compatible"); |
---|
| 252 | alter_est.set_parameters( alt->_dimx(),alt->_V(), alt->_nu()); |
---|
| 253 | } else { |
---|
| 254 | alter_est = est; |
---|
| 255 | } |
---|
[357] | 256 | |
---|
| 257 | double frg; |
---|
[477] | 258 | if ( !UI::get ( frg, set, "frg" ) ) |
---|
[357] | 259 | frg = 1.0; |
---|
| 260 | |
---|
[477] | 261 | set_parameters ( frg ); |
---|
[625] | 262 | |
---|
[357] | 263 | //name results (for logging) |
---|
[625] | 264 | shared_ptr<RV> rv_par=UI::build<RV>(set, "rv_param",UI::optional ); |
---|
| 265 | if (!rv_par){ |
---|
| 266 | est.set_rv ( RV ( "{theta r }", vec_2 ( ylen*rgrlen, ylen*ylen ) ) ); |
---|
| 267 | } else { |
---|
| 268 | est.set_rv ( *rv_par ); |
---|
| 269 | } |
---|
| 270 | validate(); |
---|
[270] | 271 | } |
---|
[357] | 272 | |
---|
| 273 | } |
---|