| 353 | //! conversion of outer ARX model (mlnorm) to state space model |
| 354 | /*! |
| 355 | The model is constructed as: |
| 356 | \f[ x_{t+1} = Ax_t + B u_t + R^{1/2} e_t, y_t=Cx_t+Du_t + R^{1/2}w_t, \f] |
| 357 | For example, for: |
| 358 | \f[ y_t = a y_{t-1} + b u_{t-1}\f] |
| 359 | the state \f$ x_t = [y_{t-1}, u_{t-1}] \f$ |
| 360 | */ |
| 361 | template<class sq_T> |
| 362 | shared_ptr<StateSpace<fsqmat> > to_state_space(const mlnorm<sq_T > &ml, ivec &theta_in_A, ivec &theta_in_C){ |
| 363 | //get ids of yrv |
| 364 | |
| 365 | ivec yids= unique(ml._rv()._ids()); //yrv is now stored in _yrv |
| 366 | ivec dids= unique(ml._rvc()._ids()); //yrv is now stored in _yrv |
| 367 | ivec uids=unique_complement(dids, yids); |
| 368 | |
| 369 | RV yrv_uni = RV(yids,zeros_i(yids.length())); |
| 370 | RV urv_uni = RV(uids,zeros_i(yids.length())); |
| 371 | //We can do only 1d now... :( |
| 372 | bdm_assert_debug(yrv_uni._dsize()==urv_uni._dsize()==1, "Only for SISO so far..." ); |
| 373 | |
| 374 | RV xrv; //empty |
| 375 | RV Crv; //empty |
| 376 | int td=ml._rvc().mintd(); |
| 377 | for (int t=-1;t>=td;t--){ |
| 378 | xrv.add(yrv_uni); |
| 379 | Crv.add(urv_uni); |
| 380 | } |
| 381 | |
| 382 | int dimx = xrv._dsize(); |
| 383 | |
| 384 | theta_in_A = ml._rv().dataind(xrv); |
| 385 | theta_in_C = ml._rvc().dataind(xrv); |
| 386 | // some chcek of corretness |
| 387 | |
| 388 | vec A1row = zeros(xrv._dsize()); |
| 389 | vec C1row = zeros(xrv._dsize()); |
| 390 | vec theta = ml._A().get_row(0); // this |
| 391 | set_subvector( A1row, theta_in_A, theta); |
| 392 | set_subvector( C1row, theta_in_C, theta); |
| 393 | |
| 394 | StateSpace<fsqmat> stsp=new StateSpace<fsqmat>(); |
| 395 | mat A=zeros(dimx,dimx); |
| 396 | A.set_row(0,A1row); |
| 397 | for (int j=1; j<dimx; j++){A(j,j-1)=1.0;} // off diagonal |
| 398 | mat B=zeros(dimx,1); |
| 399 | B(0) = 1.0; |
| 400 | mat C=zeros(1,dimx); |
| 401 | C.set_row(0,C1row); |
| 402 | stsp.set_parameters(A,B,C,zeros(1,1), zeros(dimx,dimx), ml._R()); |
| 403 | return stsp; |
| 404 | } |
| 405 | |