Changeset 83
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/libKF.cpp
r67 r83 130 130 vec u = dt.get ( dimy,dimy+dimu-1 ); 131 131 vec y = dt.get ( 0,dimy-1 ); 132 132 vec pom(dimy); 133 133 134 //TODO get rid of Q in qr()! 134 135 // mat Q; 135 136 136 137 //R and Q are already set in set_parameters() 137 preA.set_submatrix ( dimy,0, ( _P ->_Ch() ) *C.T() );138 preA.set_submatrix ( dimy,0, ( _P._Ch() ) *C.T() ); 138 139 //Fixme can be more efficient if .T() is not used 139 preA.set_submatrix ( dimy,dimy, ( _P ->_Ch() ) *A.T() );140 preA.set_submatrix ( dimy,dimy, ( _P._Ch() ) *A.T() ); 140 141 141 142 // if ( !qr ( preA,Q,postA ) ) it_warning ( "QR in kalman unstable!" ); 142 143 if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 143 144 144 ( _Ry->_Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 145 _K = ( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 146 ( _P->_Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 147 _Ry->inv ( *_iRy ); 148 fy._cached ( true ); 149 *_mu = A* ( *_mu ) + B*u + ( _K ) * ( _iRy->_Ch() ).T() * ( y-C* ( *_mu )-D*u ); 150 151 /* cout << "P:" <<_P->to_mat() <<endl; 152 cout << "Ry:" <<_Ry->to_mat() <<endl; 153 cout << "iRy:" <<_iRy->to_mat() <<endl;*/ 145 ( _Ry._Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 146 _K = inv(A)*( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 147 ( _P._Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 148 149 _mu = A* ( _mu ) + B*u; 150 _yp = C*_mu -D*u; 151 152 backward_substitution(_Ry._Ch(),( y-_yp),pom); 153 _mu += ( _K ) * pom; 154 155 cout << "P:" <<_P.to_mat() <<endl; 156 cout << "Ry:" <<_Ry.to_mat() <<endl; 157 cout << "_K:" <<_K <<endl; 158 154 159 if ( evalll==true ) { //likelihood of observation y 155 160 ll=fy.evalpdflog ( y ); … … 165 170 166 171 //initialize matrices A C, later, these will be only updated! 167 pfxu->dfdx_cond ( *_mu,zeros ( dimu ),A,true );172 pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true ); 168 173 // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); 169 174 B.clear(); 170 phxu->dfdx_cond ( *_mu,zeros ( dimu ),C,true );175 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true ); 171 176 // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); 172 177 D.clear(); … … 184 189 void EKFCh::bayes ( const vec &dt ) { 185 190 186 vec u = dt.get ( dimy,dimy+dimu-1 ); 187 vec y = dt.get ( 0,dimy-1 ); 188 189 pfxu->dfdx_cond ( *_mu,u,A,false ); //update A by a derivative of fx 190 phxu->dfdx_cond ( *_mu,u,C,false ); //update A by a derivative of fx 191 vec pom(dimy); 192 vec u = dt.get ( dimy,dimy+dimu-1 ); 193 vec y = dt.get ( 0,dimy-1 ); 194 195 pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx 196 phxu->dfdx_cond ( _mu,u,C,false ); //update A by a derivative of fx 191 197 192 198 //R and Q are already set in set_parameters() 193 preA.set_submatrix ( dimy,0, ( _P ->_Ch() ) *C.T() );199 preA.set_submatrix ( dimy,0, ( _P._Ch() ) *C.T() ); 194 200 //Fixme can be more efficient if .T() is not used 195 preA.set_submatrix ( dimy,dimy, ( _P ->_Ch() ) *A.T() );201 preA.set_submatrix ( dimy,dimy, ( _P._Ch() ) *A.T() ); 196 202 197 203 // mat Sttm = _P->to_mat(); … … 200 206 if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 201 207 202 ( _Ry ->_Ch() ) =postA ( 0,dimy-1, 0,dimy-1 );208 ( _Ry._Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 203 209 _K = inv(A)*( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 204 ( _P->_Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 205 _Ry->inv ( *_iRy ); 206 fy._cached ( true ); 210 ( _P._Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 207 211 208 212 // mat iRY = inv(_Ry->to_mat()); … … 211 215 212 216 // prediction 213 *_mu = pfxu->eval ( *_mu ,u ); 214 215 *_yp = phxu->eval ( *_mu,u ); 217 _mu = pfxu->eval ( _mu ,u ); 218 _yp = phxu->eval ( _mu,u ); 216 219 217 220 /* vec mu2 = *_mu + ( _K2 ) * ( y-*_yp );*/ 218 221 219 222 //correction //= initial value is already prediction! 220 *_mu += ( _K ) * ( _iRy->_Ch() ).T() * ( y-*_yp ); 223 backward_substitution(_Ry._Ch(),( y-_yp ),pom); 224 _mu += ( _K ) *pom ; 221 225 222 226 /* _K = (_P->to_mat())*C.transpose() * ( _iRy->to_mat() ); 223 227 *_mu = pfxu->eval ( *_mu ,u ) + ( _K )* ( y-*_yp );*/ 224 228 225 /* cout << "P:" <<_P->to_mat() <<endl;226 cout << "Ry:" <<_Ry ->to_mat() <<endl;227 cout << " iRy:" <<_iRy->to_mat() <<endl;*/229 cout << "P:" <<_P.to_mat() <<endl; 230 cout << "Ry:" <<_Ry.to_mat() <<endl; 231 cout << "_K:" <<_K <<endl; 228 232 229 233 if ( evalll==true ) { //likelihood of observation y -
bdm/estim/libKF.h
r67 r83 95 95 mat _K; 96 96 //! cache of fy.mu 97 vec *_yp;97 vec& _yp; 98 98 //! cache of fy.R 99 sq_T* _Ry; 100 //! cache of fy.iR 101 sq_T* _iRy; 99 sq_T& _Ry; 102 100 //!cache of est.mu 103 vec *_mu;101 vec& _mu; 104 102 //!cache of est.R 105 sq_T* _P; 106 //!cache of est.iR 107 sq_T* _iP; 103 sq_T& _P; 108 104 109 105 public: … … 266 262 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ), 267 263 Q(dimx), R(dimy), 268 est ( rv ), fy ( rvy ) {264 est ( rv ), fy ( rvy ), _mu(est._mu()), _P(est._R()), _yp(fy._mu()),_Ry(fy._R()) { 269 265 270 266 this->set_parameters ( K0.A, K0.B, K0.C, K0.D, K0.R, K0.Q ); 271 267 272 //establish pointers273 _mu = est._mu();274 est._R ( _P,_iP );275 276 //fy277 _yp = fy._mu();278 fy._R ( _Ry,_iRy );279 280 268 // copy values in pointers 281 *_mu = *K0._mu; 282 *_P = *K0._P; 283 *_iP = *K0._iP; 284 *_yp = *K0._yp; 285 *_iRy = *K0._iRy; 286 *_Ry = *K0._Ry; 269 _mu = K0._mu; 270 _P = K0._P; 271 _yp = K0._yp; 272 _Ry = K0._Ry; 287 273 288 274 } … … 293 279 A ( dimx,dimx ), B ( dimx,dimu ), C ( dimy,dimx ), D ( dimy,dimu ), 294 280 Q(dimx), R (dimy), 295 est ( rvx ), fy ( rvy ) { 296 //assign cache 297 //est 298 _mu = est._mu(); 299 est._R ( _P,_iP ); 300 301 //fy 302 _yp = fy._mu(); 303 fy._R ( _Ry,_iRy ); 281 est ( rvx ), fy ( rvy ), _mu(est._mu()), _P(est._R()), _yp(fy._mu()),_Ry(fy._R()) { 304 282 }; 305 283 … … 325 303 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" ); 326 304 305 sq_T iRy(dimy); 327 306 vec u = dt.get ( dimy,dimy+dimu-1 ); 328 307 vec y = dt.get ( 0,dimy-1 ); 329 308 //Time update 330 *_mu = A* ( *_mu )+ B*u;309 _mu = A* _mu + B*u; 331 310 //P = A*P*A.transpose() + Q; in sq_T 332 _P ->mult_sym ( A );333 ( *_P )+=Q;311 _P.mult_sym ( A ); 312 _P +=Q; 334 313 335 314 //Data update 336 315 //_Ry = C*P*C.transpose() + R; in sq_T 337 _P->mult_sym ( C, *_Ry ); 338 ( *_Ry ) +=R; 339 340 mat Pfull = _P->to_mat(); 341 342 _Ry->inv ( *_iRy ); // result is in _iRy; 343 fy._cached ( true ); 344 _K = Pfull*C.transpose() * ( _iRy->to_mat() ); 316 _P.mult_sym ( C, _Ry ); 317 _Ry +=R; 318 319 mat Pfull = _P.to_mat(); 320 321 _Ry.inv ( iRy ); // result is in _iRy; 322 _K = Pfull*C.transpose() * ( iRy.to_mat() ); 345 323 346 324 sq_T pom ( ( int ) Pfull.rows() ); 347 _iRy->mult_sym_t ( C*Pfull,pom );348 ( *_P ) -= pom; // P = P -PC'iRy*CP;349 ( *_yp ) = C* ( *_mu )+D*u; //y prediction350 ( *_mu ) += _K* ( y- ( *_yp ));325 iRy.mult_sym_t ( C*Pfull,pom ); 326 (_P ) -= pom; // P = P -PC'iRy*CP; 327 (_yp ) = C* _mu +D*u; //y prediction 328 (_mu ) += _K* ( y- _yp ); 351 329 352 330 … … 372 350 373 351 //initialize matrices A C, later, these will be only updated! 374 pfxu->dfdx_cond ( *_mu,zeros ( dimu ),A,true );352 pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true ); 375 353 // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); 376 354 B.clear(); 377 phxu->dfdx_cond ( *_mu,zeros ( dimu ),C,true );355 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true ); 378 356 // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); 379 357 D.clear(); … … 387 365 it_assert_debug ( dt.length() == ( dimy+dimu ),"KalmanFull::bayes wrong size of dt" ); 388 366 367 sq_T iRy(dimy,dimy); 389 368 vec u = dt.get ( dimy,dimy+dimu-1 ); 390 369 vec y = dt.get ( 0,dimy-1 ); 391 370 //Time update 392 *_mu = pfxu->eval ( *_mu, u );393 pfxu->dfdx_cond ( *_mu,u,A,false ); //update A by a derivative of fx371 _mu = pfxu->eval ( _mu, u ); 372 pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx 394 373 395 374 //P = A*P*A.transpose() + Q; in sq_T 396 _P ->mult_sym ( A );397 ( *_P )+=Q;375 _P.mult_sym ( A ); 376 _P +=Q; 398 377 399 378 //Data update 400 phxu->dfdx_cond ( *_mu,u,C,false ); //update C by a derivative hx379 phxu->dfdx_cond ( _mu,u,C,false ); //update C by a derivative hx 401 380 //_Ry = C*P*C.transpose() + R; in sq_T 402 _P->mult_sym ( C, *_Ry ); 403 ( *_Ry ) +=R; 404 405 mat Pfull = _P->to_mat(); 406 407 _Ry->inv ( *_iRy ); // result is in _iRy; 408 fy._cached ( true ); 409 _K = Pfull*C.transpose() * ( _iRy->to_mat() ); 381 _P.mult_sym ( C, _Ry ); 382 ( _Ry ) +=R; 383 384 mat Pfull = _P.to_mat(); 385 386 _Ry.inv ( iRy ); // result is in _iRy; 387 _K = Pfull*C.transpose() * ( iRy.to_mat() ); 410 388 411 389 sq_T pom ( ( int ) Pfull.rows() ); 412 _iRy->mult_sym_t ( C*Pfull,pom );413 ( *_P ) -= pom; // P = P -PC'iRy*CP;414 *_yp = phxu->eval ( *_mu,u ); //y prediction415 ( *_mu ) += _K* ( y-*_yp );390 iRy.mult_sym_t ( C*Pfull,pom ); 391 (_P ) -= pom; // P = P -PC'iRy*CP; 392 _yp = phxu->eval ( _mu,u ); //y prediction 393 ( _mu ) += _K* ( y-_yp ); 416 394 417 395 if ( evalll==true ) {ll+=fy.evalpdflog ( y );}