# Math Help - Kalman filtering application

1. [quote=atlove;272782]OK, i got what you mean. What we can do now is to learn the knowledge of the system. Otherwise, we need to try and test with the given data to make such a model. Can you show me an example that how to found those variable with enough information?[quote]

Not really

Is the E(x) purely the mean of state ?
If i have a matrix
....|x11 x12 x13|
x= |x21 x22 x23|
....|x31 x32 x33|

how to find the E(x)?
Thank You
E(.) is the expectation operator, which gives the mean of its argument. If x is the state E(x) is the expected value of the state. The expectation of a matrix is the matrix of expectations.

CB

2. [quote=CaptainBlack;273585][quote=atlove;272782]OK, i got what you mean. What we can do now is to learn the knowledge of the system. Otherwise, we need to try and test with the given data to make such a model. Can you show me an example that how to found those variable with enough information?

Not really

E(.) is the expectation operator, which gives the mean of its argument. If x is the state E(x) is the expected value of the state. The expectation of a matrix is the matrix of expectations.

CB

Thank You. I am waiting the information about the system. If I get that , i will post here asap. Thanks your reply so long.

3. Dear CaptainBlack,

Besides the system knowledge, I want to ask sth.

I am getting confuse on the innovation part.Is the z(k) in the State estimate update(x=x+K(z-Hx)) measured from the system directly? Since according to the latest matlab code you posted before, you used the z value from the data(function rv=KalmanRun(data)) to find each set of estimation of state.

If I want to know the estimation of z in k instant. Am i supposed to use the process equation y(k)=H(k)x(k)+w(k)?

4. Originally Posted by atlove
Dear CaptainBlack,

Besides the system knowledge, I want to ask sth.

I am getting confuse on the innovation part.Is the z(k) in the State estimate update(x=x+K(z-Hx)) measured from the system directly? Since according to the latest matlab code you posted before, you used the z value from the data(function rv=KalmanRun(data)) to find each set of estimation of state.

If I want to know the estimation of z in k instant. Am i supposed to use the process equation y(k)=H(k)x(k)+w(k)?
z(k) is the measurement at epoc k.

CB

5. Originally Posted by CaptainBlack
z(k) is the measurement at epoc k.

CB
So, z(k) is measured from the system.

If i use the esitmation of state (x1...x5) to find the z (x6) by z=Hx+v. What is the meaning of this z? Is it the optimal estiamtion of z (x6) and more accurate than the measure one?

CaptainBlack, I have got some new information from my consultant.

1. Five variables can be adjusted by the student using rheostats and they are:
Condensing water temperature into chiller, adjustable between 20C to 30C
Condensing water flow into chiller, adjustable between 50 l/s to 80 l/s
Chilled water temperature into chiller, adjustable between 10C to 16C
Chilled water temperature out of chiller, adjustable between 6C to 9C
Chilled water flow, adjustable btween 40 l/s to 70 l/s
2. Then, the simulator can calculate the power consumption real-time.
3. All these six variables will be available on a 10-second frequency on the NAE so that the student can read them continuously.
4. The student's job is to predict the power consumption continuously by using techniques in Kalam Filtering.
5. The way of calculating the power consumption inside our NAE will be confidential.
6. The student is considered doing a great job by tracing the power consumption closely with the value from our simulator.

Suggest the student use a moving window of say 10 minutes, i.e. 60 sets of data. During these 10 minutes, he should adjust the five parameters slightly to have a wider dynamical span. Then, he starts to trace the power consumption from the 61st set of data onwards. With 2-61st sets of date, he can then predict the power consumption of the 62nd set of data.

P.S. The simulator is a software used to get value of parameters.
NAE is a control unit so that i can get the information by my computer through this unit.

One thing i don't understand, Why 2-61st sets of date data are necessary to predict the power consumption? Kalman filtering told that we just need the previous estimation of state (t=k-1) and measurement at this instant (t=k) to predict the new estimation of state.

6. Originally Posted by atlove
So, z(k) is measured from the system.

If i use the esitmation of state (x1...x5) to find the z (x6) by z=Hx+v. What is the meaning of this z? Is it the optimal estiamtion of z (x6) and more accurate than the measure one?
Hx is the prediction of the measurement from the current state, v is an unknown noise term, the z that appears in the equations is your x6.

CB

7. Originally Posted by atlove
So, z(k) is measured from the system.

If i use the esitmation of state (x1...x5) to find the z (x6) by z=Hx+v. What is the meaning of this z? Is it the optimal estiamtion of z (x6) and more accurate than the measure one?

CaptainBlack, I have got some new information from my consultant.

1. Five variables can be adjusted by the student using rheostats and they are:
Condensing water temperature into chiller, adjustable between 20C to 30C
Condensing water flow into chiller, adjustable between 50 l/s to 80 l/s
Chilled water temperature into chiller, adjustable between 10C to 16C
Chilled water temperature out of chiller, adjustable between 6C to 9C
Chilled water flow, adjustable btween 40 l/s to 70 l/s
2. Then, the simulator can calculate the power consumption real-time.
3. All these six variables will be available on a 10-second frequency on the NAE so that the student can read them continuously.
4. The student's job is to predict the power consumption continuously by using techniques in Kalam Filtering.
5. The way of calculating the power consumption inside our NAE will be confidential.
6. The student is considered doing a great job by tracing the power consumption closely with the value from our simulator.

Suggest the student use a moving window of say 10 minutes, i.e. 60 sets of data. During these 10 minutes, he should adjust the five parameters slightly to have a wider dynamical span. Then, he starts to trace the power consumption from the 61st set of data onwards. With 2-61st sets of date, he can then predict the power consumption of the 62nd set of data.

P.S. The simulator is a software used to get value of parameters.
NAE is a control unit so that i can get the information by my computer through this unit.

One thing i don't understand, Why 2-61st sets of date data are necessary to predict the power consumption? Kalman filtering told that we just need the previous estimation of state (t=k-1) and measurement at this instant (t=k) to predict the new estimation of state.
The filter will run from the first data set perfectly well, you just won't have very good estimates untill the system has processed enough data to have settled down.

As a miniimum you would expect to need 5 data points to estimate 5 coefficients, assuming that the 5 data points constitute a linearly independednt set.

By the look of this you should be able to get the filter to work by setting the initial values to be some small values and the covariance to be diagonal with values of the order of 1,000,000 on the diagonal (assuming power is measured in something like watts).

CB

8. Originally Posted by CaptainBlack
The filter will run from the first data set perfectly well, you just won't have very good estimates untill the system has processed enough data to have settled down.

As a miniimum you would expect to need 5 data points to estimate 5 coefficients, assuming that the 5 data points constitute a linearly independednt set.
It means that the 2-61st dataset are used to intialize the estimation? Only previous estimation of state are necessary for further estimation? But why not 1-61st dataset?

By the look of this you should be able to get the filter to work by setting the initial values to be some small values and the covariance to be diagonal with values of the order of 1,000,000 on the diagonal (assuming power is measured in something like watts).

CB
How to find that small values, by taking average of data or other else? Is there any problem to assume that to be 0?
Is the covariance values larger is better?
are you mean:
$
\begin{bmatrix} 1000000 & 0 & 0 \\ 0 & 1000000 & 0 \\ 0 & 0 & 100000 \end{bmatrix}
$

Besides , he didn't give me any information on the variance to determine R. Can i assume it to be anything?

9. I 've done some modification base on the latest matlab code which plot the curve of estimation of z against the real measured z

Innovation
Code:
function [X,P]=KalmanInov(Data,R,X,P)
%
% Kalman Filter Inovation Processing
%
% data is a row vector with the independent vars and the depended var
%
ll=length(Data)
H=Data(1:ll-1);          %extract the H matrix or vector from the data
z=Data(ll);                %extract the measurement from the data

% Below is the standard Kalman inovation equations in matrix form

S=H*P*H'+R; %note this is a scalar
K=P*H'*inv(S);
X=X+K*(z-H*X);
P=(eye(ll-1,ll-1)-K*H)*P;
Main Kalman filtering running
Code:
function P=KalmanRun(data)
%
%  function to run simulation of the Kalman filter for
%  MHF question
%
%  data is a matrix each row of which contains the vector of data
%
%  The system model is:
%
%  z=sum(x(i)*aa(i)) + w
%
%  where z is the measurement to be explained, the x's are the
%  independent variables corresponding to z and w is a scalar zero
%  mean gaussian noise term.
%
%  Initial values, the state estimate is set to zero, and the covariance
%  to a diagonal matrix of the right dimension with 10^2 down the diagonal
%
%  the measurement noise variance R (of w in the model) is set to 1.
%
a=data;
sz=size(data);
niter=sz(1);    %number of rows of data
ll=sz(2);       %length of each row of data
P=eye(ll-1,ll-1).*1000000; %initial covariance of state estimate
X=zeros(ll-1,1);            %initial state estimate
R=0.2;                 %measurement variance
rv=X;
%
% loop
%
for idx=1:niter
D=data(idx,:);     % load the nest line of data into D consists of [x(1),..,x(n),z]
[X,P]=KalmanInov(D,R,X,P);  % inovate the filter with the data vactor D
rv=[rv,X];%accumulate the state estimate
end

a1=a(1:niter,1:ll-1);
b1=rv(1:ll-1,2:niter+1);
d1=[];
for idx=1:niter;
c1=a1(idx:idx,1:ll-1)*b1(1:ll-1,idx:idx);
d1=[d1,c1];
end
e1=a(1:niter,ll:ll)';
d1=[d1;e1];
plot([1:niter],d1);
You may randomly input some data matrix to test.
$
\begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 100 \\ 2 & 3 & 4 & 5 & 6 & 130 \\ 3 & 4 & 5 & 6 & 7 & 155 \\ 4 & 5 & 6 & 7 & 8 & 175 \\5 & 6 & 7 & 8 & 9 & 200 \end{bmatrix}
$

The picture attached is the result.
Can i do that in this way?

10. Originally Posted by atlove
It means that the 2-61st dataset are used to intialize the estimation? Only previous estimation of state are necessary for further estimation? But why not 1-61st dataset?

How to find that small values, by taking average of data or other else? Is there any problem to assume that to be 0?
Is the covariance values larger is better?
are you mean:
$
\begin{bmatrix} 1000000 & 0 & 0 \\ 0 & 1000000 & 0 \\ 0 & 0 & 100000 \end{bmatrix}
$

Besides , he didn't give me any information on the variance to determine R. Can i assume it to be anything?
Zeros should be OK for the initial state estimate.

The diagonal terms of the initial covariance matrix should be as big as they can be without causing numerical problems (so they can probably be bigger without too much trouble but I would not go beyond about 10,000,000,000).

You may have to make some assumptions about the measurement variance. One way of doing this is to start by assuming the SD it is say 10% of the first measurement. Run the filer over the first block of data then go back and calculate the SD or variance of the residuals about the back predictions from the final state, the rerun the data with that as a basis for the measurement variance. (possibly repeating this procedure if necessary)

CB

11. Originally Posted by CaptainBlack

Zeros should be OK for the initial state estimate.

The diagonal terms of the initial covariance matrix should be as big as they can be without causing numerical problems (so they can probably be bigger without too much trouble but I would not go beyond about 10,000,000,000).

You may have to make some assumptions about the measurement variance. One way of doing this is to start by assuming the SD it is say 10% of the first measurement. Run the filer over the first block of data then go back and calculate the SD or variance of the residuals about the back predictions from the final state, the rerun the data with that as a basis for the measurement variance. (possibly repeating this procedure if necessary)

CB

For the measurement covariance, do you mean to run the filter to the final state with assumming SD? Then use the dataset which is used before to find the variance?

12. Dear CaptainBlack,

How to find the state(a,b,c,d,e) with a large set of data by kalman filter?
for example, if i have 60 set of data. So that
$
\begin{bmatrix} y(1) \\ y(2) \\ .\\ . \\ y(60) \end{bmatrix}
$
=
$
\begin{bmatrix} x1(1) & x2(1) & x3(1) & x4(1) & x5(1) \\ x1(2) & x2(2) & x3(2) & x4(2) & x5(2) \\ . & . & . & . & . \\ . & . & . & . & . \\ x1(60) & x2(60) & x3(60) & x4(60) & x5(60) \end{bmatrix}
$
x $
\begin{bmatrix} a \\ b \\ c\\ d \\ e \end{bmatrix}
$

13. Originally Posted by atlove
The state covariance is adjusted automatically

For the measurement covariance, do you mean to run the filter to the final state with assumming SD? Then use the dataset which is used before to find the variance?
Something like that, with a lot of these things one of the best things to do is run it and see what happens.

CB

14. Originally Posted by atlove
Dear CaptainBlack,

How to find the state(a,b,c,d,e) with a large set of data by kalman filter?
for example, if i have 60 set of data. So that
$
\begin{bmatrix} y(1) \\ y(2) \\ .\\ . \\ y(60) \end{bmatrix}
$
=
$
\begin{bmatrix} x1(1) & x2(1) & x3(1) & x4(1) & x5(1) \\ x1(2) & x2(2) & x3(2) & x4(2) & x5(2) \\ . & . & . & . & . \\ . & . & . & . & . \\ x1(60) & x2(60) & x3(60) & x4(60) & x5(60) \end{bmatrix}
$
x $
\begin{bmatrix} a \\ b \\ c\\ d \\ e \end{bmatrix}
$
You treat each row as a seperate measurement with given parameters x1, .. x5 and measurement x6, That is you have 60 updates.

CB

15. hello CaptainBlack,
With your assistance before, i have build a basic model and programmed with vb.net already. Later on i will post some result of the estimation estimated by my program. Thank you very much.

Besides that, I was asked to do some modification about to optimize my model. My consultant said that there is forgetting factor for kalman filtering. What exact this means? Do you have related information?

Page 3 of 4 First 1234 Last