API
Applications
Utilities
API
Handbook
GitHub
Home
Loading...
Searching...
No Matches
recursive_least_squares.cpp
Go to the documentation of this file.
1
#include "
recursive_least_squares.hpp
"
2
#include <string>
3
4
namespace
DDSPC
5
{
6
7
RecursiveLeastSquares::RecursiveLeastSquares
(
int
num_predictors,
int
num_features,
realT
forgetting_factor,
realT
initial_covariance){
8
9
_gamma
= forgetting_factor;
10
_inverse_gamma
= 1 /
_gamma
;
11
12
_initial_covariance
= initial_covariance;
13
_num_features
= num_features;
14
_num_predictors
= num_predictors;
15
16
// Set size of all the arrays
17
prediction_matrix
.resize(
_num_predictors
,
_num_features
);
18
prediction_matrix
.setZero();
19
20
prediction_output
.resize(
_num_predictors
, 1);
21
prediction_output
.setZero();
22
23
inverse_covariance
.resize(
_num_features
,
_num_features
);
24
inverse_covariance
.setZero();
25
for
(
int
i=0; i <
_num_features
; i++)
26
inverse_covariance
(i, i) =
_initial_covariance
;
27
28
err
.resize(
_num_predictors
, 1);
29
err
.setZero();
30
31
K
.resize(1,
_num_features
);
32
K
.setZero();
33
};
34
35
36
RecursiveLeastSquares::~RecursiveLeastSquares
(){
37
38
}
39
40
void
RecursiveLeastSquares::reset
(){
41
prediction_matrix
.setZero();
42
err
.setZero();
43
K
.setZero();
44
45
inverse_covariance
.resize(
_num_features
,
_num_features
);
46
inverse_covariance
.setZero();
47
for
(
int
i=0; i <
_num_features
; i++)
48
inverse_covariance
(i, i) =
_initial_covariance
;
49
}
50
51
// I want to change this interface to make it easier to use.
52
void
RecursiveLeastSquares::update
(eigenImage<realT> *x, eigenImage<realT> *y){
53
Matrix
_x = (*x).matrix();
54
err
= (*y).matrix();
55
err
-=
prediction_matrix
* _x;
56
57
xtP
= (
_inverse_gamma
* _x).transpose() *
inverse_covariance
;
58
realT
cn = 1 + (
xtP
* _x)(0,0);
59
K
=
xtP
;
60
K
/= cn;
61
prediction_matrix
+=
err
*
K
;
62
63
inverse_covariance
*=
_inverse_gamma
;
64
inverse_covariance
-=
K
.transpose() *
xtP
;
65
}
66
67
void
RecursiveLeastSquares::update
(
Matrix
*x,
Matrix
*y){
68
Matrix
_x = (*x);
69
err
= (*y);
70
err
-=
prediction_matrix
* _x;
71
72
xtP
= (
_inverse_gamma
* _x).transpose() *
inverse_covariance
;
73
realT
cn = 1 + (
xtP
* _x)(0,0);
74
K
=
xtP
;
75
K
/= cn;
76
prediction_matrix
+=
err
*
K
;
77
78
inverse_covariance
*=
_inverse_gamma
;
79
inverse_covariance
-=
K
.transpose() *
xtP
;
80
}
81
82
83
// Matrix RecursiveLeastSquares::predict(eigenImage<realT> *x){
84
// return prediction_matrix * (*x).matrix();
85
//}
86
87
void
RecursiveLeastSquares::save_state
(std::string filename){
88
89
}
90
91
}
DDSPC::RecursiveLeastSquares::_initial_covariance
realT _initial_covariance
Definition
recursive_least_squares.hpp:26
DDSPC::RecursiveLeastSquares::inverse_covariance
Matrix inverse_covariance
Definition
recursive_least_squares.hpp:36
DDSPC::RecursiveLeastSquares::prediction_output
Matrix prediction_output
Definition
recursive_least_squares.hpp:38
DDSPC::RecursiveLeastSquares::_num_predictors
int _num_predictors
Definition
recursive_least_squares.hpp:28
DDSPC::RecursiveLeastSquares::update
void update(eigenImage< realT > *x, eigenImage< realT > *y)
Definition
recursive_least_squares.cpp:52
DDSPC::RecursiveLeastSquares::prediction_matrix
Matrix prediction_matrix
Definition
recursive_least_squares.hpp:35
DDSPC::RecursiveLeastSquares::~RecursiveLeastSquares
~RecursiveLeastSquares()
Definition
recursive_least_squares.cpp:36
DDSPC::RecursiveLeastSquares::_num_features
int _num_features
Definition
recursive_least_squares.hpp:27
DDSPC::RecursiveLeastSquares::_inverse_gamma
realT _inverse_gamma
Definition
recursive_least_squares.hpp:25
DDSPC::RecursiveLeastSquares::err
Matrix err
Definition
recursive_least_squares.hpp:31
DDSPC::RecursiveLeastSquares::save_state
void save_state(std::string filaname)
Definition
recursive_least_squares.cpp:87
DDSPC::RecursiveLeastSquares::xtP
Matrix xtP
Definition
recursive_least_squares.hpp:32
DDSPC::RecursiveLeastSquares::_gamma
realT _gamma
Definition
recursive_least_squares.hpp:24
DDSPC::RecursiveLeastSquares::reset
void reset()
Definition
recursive_least_squares.cpp:40
DDSPC::RecursiveLeastSquares::K
Matrix K
Definition
recursive_least_squares.hpp:30
DDSPC::RecursiveLeastSquares::RecursiveLeastSquares
RecursiveLeastSquares(int num_predictors, int num_features, realT forgetting_factor, realT inverse_covariance)
Definition
recursive_least_squares.cpp:7
DDSPC
Definition
ar_controller.cpp:4
DDSPC::Matrix
Eigen::Matrix< realT, Eigen::Dynamic, Eigen::Dynamic > Matrix
Definition
utils.hpp:12
DDSPC::realT
float realT
Definition
utils.hpp:11
recursive_least_squares.hpp
apps
loPredCtrl
recursive_least_squares.cpp
Generated by
1.9.8