Changeset 625
- Timestamp:
- 09/18/09 00:17:05 (15 years ago)
- Location:
- library/bdm/estim
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/arx.cpp
r585 r625 5 5 double lnc; 6 6 7 7 8 if ( frg < 1.0 ) { 8 9 est.pow ( frg ); … … 11 12 } 12 13 } 13 V.opupdt ( dt, w ); 14 if (have_constant) { 15 _dt.set_subvector(0,dt); 16 V.opupdt ( _dt, w ); 17 } else { 18 V.opupdt ( dt, w ); 19 } 14 20 nu += w; 15 21 … … 78 84 mlnorm<ldmat>* ARX::predictor ( ) const { 79 85 int dim = est.dimension(); 80 int dif = V.rows() - dim ;///<----------- TODO 81 bdm_assert_debug ( ( dif == 0 ) || ( dif == 1 ), "Give RVs do not match" ); 82 86 83 87 mat mu ( dim, V.rows() - dim ); 84 88 mat R ( dim, dim ); … … 90 94 //correction for student-t -- TODO check if correct!! 91 95 92 if ( dif == 0 ) { // no constant term 93 tmp->set_parameters ( mu, zeros ( dim ), ldmat ( R ) ); 94 } else { 96 if ( have_constant) { // constant term 95 97 //Assume the constant term is the last one: 96 98 tmp->set_parameters ( mu.get_cols ( 0, mu.cols() - 2 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ) ); 99 } else { 100 tmp->set_parameters ( mu, zeros ( dim ), ldmat ( R ) ); 97 101 } 98 102 return tmp; … … 101 105 mlstudent* ARX::predictor_student ( ) const { 102 106 int dim = est.dimension(); 103 int dif = V.rows() - est.dimension();//-------------TODO104 bdm_assert_debug ( ( dif == 0 ) || ( dif == 1 ), "Give RVs do not match" );105 107 106 108 mat mu ( dim, V.rows() - dim ); … … 117 119 118 120 119 if ( dif == 0 ) { // no constant term 120 tmp->set_parameters ( mu, zeros ( xdim ), ldmat ( R ), Lam ); 121 } else { 121 if ( have_constant) { // no constant term 122 122 //Assume the constant term is the last one: 123 123 if ( mu.cols() > 1 ) { … … 126 126 tmp->set_parameters ( mat ( dim, 0 ), mu.get_col ( mu.cols() - 1 ), ldmat ( R ), Lam ); 127 127 } 128 } else { 129 // no constant term 130 tmp->set_parameters ( mu, zeros ( xdim ), ldmat ( R ), Lam ); 128 131 } 129 132 return tmp; … … 203 206 204 207 void ARX::from_setting ( const Setting &set ) { 205 shared_ptr<RV> yrv = UI::build<RV> ( set, " y", UI::compulsory );208 shared_ptr<RV> yrv = UI::build<RV> ( set, "rv", UI::compulsory ); 206 209 shared_ptr<RV> rrv = UI::build<RV> ( set, "rgr", UI::compulsory ); 207 210 int ylen = yrv->_dsize(); 211 // rgrlen - including constant!!! 208 212 int rgrlen = rrv->_dsize(); 209 213 214 set_rv ( *yrv, *rrv ); 215 210 216 string opt; 211 217 if ( UI::get(opt, set, "options", UI::optional) ) { 212 218 BM::set_options(opt); 213 219 } 220 if (!UI::get(have_constant, set, "constant", UI::optional)){ 221 have_constant=true; 222 } 223 if (have_constant) {rgrlen++;_dt=ones(rgrlen+ylen);} 214 224 215 225 //init 216 226 mat V0; 217 227 vec dV0; 218 if ( !UI::get ( dV0, set, "dV0" ) ) 219 dV0 = concat ( 1e-3 * ones ( ylen ), 1e-5 * ones ( rgrlen ) ); 220 V0 = diag ( dV0 ); 221 228 if (!UI::get(V0, set, "V0",UI::optional)){ 229 if ( !UI::get ( dV0, set, "dV0" ) ) 230 dV0 = concat ( 1e-3 * ones ( ylen ), 1e-5 * ones ( rgrlen ) ); 231 V0 = diag ( dV0 ); 232 } 222 233 double nu0; 223 234 if ( !UI::get ( nu0, set, "nu0" ) ) … … 230 241 set_parameters ( frg ); 231 242 set_statistics ( ylen, V0, nu0 ); 232 set_drv ( concat ( *yrv, *rrv ) ); 233 243 234 244 //name results (for logging) 235 set_rv ( RV ( "{theta r }", vec_2 ( ylen*rgrlen, ylen*ylen ) ) ); 236 237 } 238 239 } 245 shared_ptr<RV> rv_par=UI::build<RV>(set, "rv_param",UI::optional ); 246 if (!rv_par){ 247 est.set_rv ( RV ( "{theta r }", vec_2 ( ylen*rgrlen, ylen*ylen ) ) ); 248 } else { 249 est.set_rv ( *rv_par ); 250 } 251 validate(); 252 } 253 254 } -
library/bdm/estim/arx.h
r585 r625 48 48 //! Do NOT access directly, only via \c get_yrv(). 49 49 RV _yrv; 50 //! rv of regressor 51 RV rgrrv; 50 52 //! Posterior estimate of \f$\theta,r\f$ in the form of Normal-inverse Wishart density 51 53 egiw est; … … 54 56 //! cached value of est.nu 55 57 double ν 58 //! switch if constant is modelled or not 59 bool have_constant; 60 //! cached value of data vector for have_constant =true 61 vec _dt; 56 62 public: 57 63 //! \name Constructors … … 73 79 //!@} 74 80 75 // //! Set parameters given by moments, \c mu (mean of theta), \c R (mean of R) and \c C (variance of theta)76 // void set_parameters ( const vec &mu, const mat &R, const mat &C, double dfm){};77 81 //! Set sufficient statistics 78 82 void set_statistics ( const BMEF* BM0 ); 79 // //! Returns sufficient statistics 80 // void get_parameters ( mat &V0, double &nu0 ) {V0=est._V().to_mat(); nu0=est._nu();} 83 81 84 //!\name Mathematical operations 82 85 //!@{ … … 121 124 //!\name Connection 122 125 //!@{ 123 void set_drv ( const RV &drv0 ) { 124 drv = drv0; 126 void set_rv ( const RV &yrv0 , const RV &rgrrv0 ) { 127 _yrv = yrv0; 128 rgrrv=rgrrv0; 129 set_drv(concat(yrv0, rgrrv)); 125 130 } 126 131 … … 140 145 //!@} 141 146 142 // TODO dokumentace - aktualizovat143 147 /*! UI for ARX estimator 144 148 145 The ARX is constructed from a structure with fields:146 149 \code 147 estimator = { 148 class = "ARX"; 149 y = {type="rv", ...} // description of output variables 150 rgr = {type="rv", ...} // description of regressor variables 151 constant = true; // boolean switch if the constant term is modelled or not 150 class = 'ARX'; 151 rv = RV({names_of_dt} ) // description of output variables 152 rgr = RV({names_of_regressors}, [-1,-2]} // description of regressor variables 153 constant = true; // boolean switch if the constant term is modelled or not 152 154 153 //optional fields 154 dV0 = [1e-3, 1e-5, 1e-5, 1e-5]; 155 // default: 1e-3 for y, 1e-5 for rgr 156 nu0 = 6; // default: rgrlen + 2 157 frg = 1.0; // forgetting, default frg=1.0 158 }; 155 --- optional --- 156 V0 = [1 0;0 1]; // Initial value of information matrix V 157 --- OR --- 158 dV0 = [1e-3, 1e-5, 1e-5, 1e-5]; // Initial value of diagonal of information matrix V 159 // default: 1e-3 for rv, 1e-5 for rgr 160 nu0 = 6; // initial value of nu, default: rgrlen + 2 161 frg = 1.0; // forgetting, default frg=1.0 162 163 rv_param = RV({names_of_parameters}} // description of parametetr names 164 // default: ["theta_i" and "r_i"] 159 165 \endcode 160 161 The estimator will assign names of the posterior in the form ["theta_i" and "r_i"]162 166 */ 163 167 void from_setting ( const Setting &set ); 164 168 169 void validate() { 170 bdm_assert(dimx == _yrv._dsize(), "RVs of parameters and regressor do not match"); 171 172 } 165 173 }; 166 174 -
library/bdm/estim/kalman.h
r620 r625 378 378 public: 379 379 //! set up this object to match given mlnorm 380 void connect_mlnorm(const mlnorm<fsqmat 381 //get ids of yrv380 void connect_mlnorm(const mlnorm<fsqmat> &ml){ 381 //get ids of yrv 382 382 const RV &yrv = ml._rv(); 383 383 //need to determine u_t - it is all in _rvc that is not in ml._rv()