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