Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

expectation calculation in matlab

Status
Not open for further replies.

rameshrai

Full Member level 3
Joined
Aug 16, 2010
Messages
158
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Activity points
2,272
Hi,

How can I calculate the expectation value in matlab?

Code:
M  = Σ P(i)*X(i)

where the sum is over i = 1 to n and X(i) is a 2x2 matrix

what function to use to integrate, is trapz ok?

thanks
 

Depending on your requirements, either sum() or trapz() are often fine to approximate integrals. (This won't generally allow you to calculate an expectation... just approximate it, assuming ergodicity and stationarity of any random signals).

What are you trying to calculate the expectation of? M? If so, you will need multiple realisations of M. It's easiest to store these in a 3D array, so M:),:,1), M:),:,2), ... , are the different realisations.

- - - Updated - - -

Oh wait... are you trying to calculate the expectation of a discrete random variable, X? Does P denote probability in your equation?

For a discrete random variable, you don't need to calculate an integral... just the sum in your equation. Let's start with a scalar random variable, Y, before considering the matrix X. In Matlab, we can just write:
Code:
M = 0;
for i = 1 : N
[INDENT]M = M + P(i)*Y(i);[/INDENT]
end
where N is the number of possible discrete outcomes.
Or, we can do exactly the same thing much more compactly using column vectors:
Code:
M = P.'*Y;

As an example, let's consider a fair die. When we roll dice, there are six possible values, each with probability 1/6. So, we have:
Code:
Y = [1;2;3;4;5;6];
P = (1/6)*ones(6,1);
M = P.'*Y
and Matlab will tell you the correct answer that the expected value of Y is 3.5.

Now, we just need to generalise this so X can be a matrix. As I said above, the easiest way to store X is so that X:),:,1), X:),:,2), ..., are the different realisations. Then, the first way to calculate the expectation is almost the same as before:
Code:
M = zeros(2,2);
for i = 1 : N
[INDENT]M = M + P(i)*X(:,:,i);[/INDENT]
end
If you want to do this very quickly and efficiently for large numbers of realisations, please let me know. This will be a little trickier, as Matlab does not optimise so neatly for higher-dimensional arrays. We could come up with a solution using something like this or this, so it would not be too difficult.
 
Last edited:
Depending on your requirements, either sum() or trapz() are often fine to approximate integrals. (This won't generally allow you to calculate an expectation... just approximate it, assuming ergodicity and stationarity of any random signals).

What are you trying to calculate the expectation of? M? If so, you will need multiple realisations of M. It's easiest to store these in a 3D array, so M:),:,1), M:),:,2), ... , are the different realisations.

- - - Updated - - -

Oh wait... are you trying to calculate the expectation of a discrete random variable, X? Does P denote probability in your equation?

For a discrete random variable, you don't need to calculate an integral... just the sum in your equation. Let's start with a scalar random variable, Y, before considering the matrix X. In Matlab, we can just write:
Code:
M = 0;
for i = 1 : N
[INDENT]M = M + P(i)*Y(i);[/INDENT]
end
where N is the number of possible discrete outcomes.
Or, we can do exactly the same thing much more compactly using column vectors:
Code:
M = P.'*Y;

As an example, let's consider a fair die. When we roll dice, there are six possible values, each with probability 1/6. So, we have:
Code:
Y = [1;2;3;4;5;6];
P = (1/6)*ones(6,1);
M = P.'*Y
and Matlab will tell you the correct answer that the expected value of Y is 3.5.

Now, we just need to generalise this so X can be a matrix. As I said above, the easiest way to store X is so that X:),:,1), X:),:,2), ..., are the different realisations. Then, the first way to calculate the expectation is almost the same as before:
Code:
M = zeros(2,2);
for i = 1 : N
[INDENT]M = M + P(i)*X(:,:,i);[/INDENT]
end
If you want to do this very quickly and efficiently for large numbers of realisations, please let me know. This will be a little trickier, as Matlab does not optimise so neatly for higher-dimensional arrays. We could come up with a solution using something like this or this, so it would not be too difficult.

nice and complete, thank you

- - - Updated - - -

hi,

i have another question to add here, X is a 2x2 matrix which gets different values for each k in a k loop, how do I write the X matrix that will have different element values for each loop. I am currently using the form below-


Code:
Y = zeros(2,2);
for k=1:N

....

X(2*k-1:2*k,1:2) = ....;
Y = Y + P(i)*X(2*k-1:2*k,1:2);

....

end

also when i use X(2*k-1:2*k,1:2) form for X then the output is quite unreadable, any advice?
Code:
    0         0
    0    1.0000
    0.5000   -0.5000
   -0.5000    0.5000
    0.5000    0.5000
    0.5000    0.5000
    1.0000         0
         0         0
    0.5000    0.5000
    0.5000    0.5000
    0.5000    0.5000
    0.5000    0.5000
    0.5000    0.5000
    0.5000    0.5000

thanks
 

In my first post I said that:
the easiest way to store X is so that X:),:,1), X:),:,2), ..., are the different realisations
So, let me clarify that. In Matlab, we want to create a 3D array of size (2 x 2 x N) to store the N different realisations of X:
Code:
X = zeros(2,2,N);
Y = zeros(2,2);
for k=1:N

....

X(:,:,k) = ....;
Y = Y + P(k)*X(:,:,k);

....

end
You will find that this gives a slightly more convenient output in Matlab.

There was nothing technically wrong with your approach and it is actually really useful to understand how to do that type of "sliced" indexing (e.g. 2*k-1:2*k). The only comment I would have is that you should always pre-allocate your arrays to the correct size:
Code:
X = zeros(2,2*N);
Y = zeros(2,2);
...
Otherwise, your code will execute slowly as memory is reallocated every time the array grows. (And also I think your P(i) should be P(k)).
 

In my first post I said that:

So, let me clarify that. In Matlab, we want to create a 3D array of size (2 x 2 x N) to store the N different realisations of X:
Code:
X = zeros(2,2,N);
Y = zeros(2,2);
for k=1:N

....

X(:,:,k) = ....;
Y = Y + P(k)*X(:,:,k);

....

end
You will find that this gives a slightly more convenient output in Matlab.

There was nothing technically wrong with your approach and it is actually really useful to understand how to do that type of "sliced" indexing (e.g. 2*k-1:2*k). The only comment I would have is that you should always pre-allocate your arrays to the correct size:
Code:
X = zeros(2,2*N);
Y = zeros(2,2);
...
Otherwise, your code will execute slowly as memory is reallocated every time the array grows. (And also I think your P(i) should be P(k)).

hi,

i want to have X a 2x2 matric but X:),:,k) gives 3x3 matrix. what i want to do is and what i am looking for is a best possible way to have a 2x2 matrix assigned in each loop

Code:
for k=1:N
X(k) = [(2x2matrix)]
end

but this gives error dimension mismatch

i have been using-

Code:
for k=1:N
X(2*k-1:2*k,1:2) = [(2x2matrix)]
end

which gives me unreadable output as shown previously

any other suggestion for creating X?

thanks
 

X:),:,k) gives a 2x2 matrix. You can check this as follows:
Code:
N = 5;
X = zeros(2,2,N);
for k = 1:N
    disp(['X(:,:,', num2str(k), ') = ']);
    disp(X(:,:,k));
end
Alternatively, I would often prefer not to store all value of X (unless they are specifically needed at the end of the loop). In this case you can just us a temporary (2 x 2) matrix called Xk:
Code:
Xk = zeros(2,2);
Y = zeros(2,2);
for k=1:N

....

Xk = ....;
Y = Y + P(k)*Xk;

....

end
(You don't really need to pre-allocate Xk or Y since they do not grow in the loop, but it's not so bad to do it anyway).
 
Last edited:
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top