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