# Matlab: How do I implement the Expectation operator for time series

I have the output of a time series y(t) which is a vector for t = 1,2,...m denoting the number of data points.

Q1: If E[y(t)] = mean of a process y. This can be invoked using the command mean(y) or writing the statement

m = size(y,1);
Expectation_y = sum(y)/m;


Then, how do I find the Expectation of the following expression as

 [E[y(t-1)*y(t-1)']]^-1


These kinds of related Expressions are found in the Slide2 mentions the Autocorrelation matrix. In the formula, there is the Expectation operator. So how do I implement the expectation of the product of the lagged random variable with itself and other such expressions without using the inbuilt commands?

My problem is how do I implement them?

Q2: My confusion is what is the different between $E[\sum_{t = 1}^ N y(t-1)*y(t-1)^T]$ and expression $E[y(t-1)*{y(t-1)}^T]$ which looks like the Autocorrelation of the lag and again how to implement them.

Thank you!

## 1 Answer

I'm not sure I understand the question. If I'm missing something, leave a comment and I'll try to edit/expand/clarify as needed.

First of all, "mean" and "expected value" are related concepts, but are not the same thing. You find the mean of a set of samples, or observations. If you sampled/observed properly, the mean will be close to the expected value, which is the theoretical, "true mean" of the underlying random process.

When you sample a random process, you end up with a set of random numbers. If you sample the same process again, you end up with a different set of numbers. So, you can think of each sample as a random variable (a black box that produces random numbers); the random process is then a sequence of random variables.

These random variables may be correlated; that means, intuitively, that if one of them has a certain value, you can use it to predict the values of some of the other samples. For example, you may find that if one sample is positive, its neighboring samples tend to be positive too.

In your question you say that you have one set of samples, y(t). In general, you don't have enough information to generate the correlation matrix. However, you could do it like this:

M = length(y);
C = zeros(M,M);
for i = 1:M
for j = 1:M
C(i,j) = mean(y(i)*y(j));  % Note that the mean is useless here,
% since y(i)*y(j) is scalar
end
end


To find a closer approximation to the actual correlation matrix of the underlying process, you need to gather many observations. Let's say that you have stored the observations in the rows of a matrix Y (every row is a different set of observations). Then you could do:

M = size(Y,2);
C = zeros(M,M);
for i = 1:M
for j = 1:M
C(i,j) = mean(Y(:,i).*Y(:,j));
end
end


To test this, let's try sampling a Gaussian process of zero mean and variance 1. Every time I look at the process, I'll gather three samples, and I'll look at the process 1000 times:

Y = randn(1000,3);


If I run the code above with matrix Y, I obtain this correlation matrix:

0.9598    0.0590    0.0238
0.0590    0.9675   -0.0286
0.0238   -0.0286    1.0055


which is expected (the "true" correlation matrix is the identity).

Regarding calculating a time shift in Matlab, it's helpful to arrange vectors in a table as follows (I've assumed $t=1,2,\ldots$ and $y(t)=10t$):

t    | y(t)  |  y(t-1)  | y(t-2)
|       |          |
1    |  10   |  --      | --
2    |  20   |  10      | --
3    |  30   |  20      | 10
4    |  40   |  30      | 20
...


It's clear from the table that, to find yn=y(t-n), you cut n elements from the end of y, and shift the time vector:

tn = t(n:end);    % shifted time vector
yn = y(1:end-n);  % shifted signal


The following code provides an example, which doesn't assume integer time or integer delay:

t = 0:0.1:3;  % time vector
y = exp(t/5); % y(t)
n = 0.8;      % time delay
tn = t(t>n);  % delayed time vector
yn = y(1:length(tn));  % delayed signal
plot(t,y,'r',tn,yn,'g');  % plot


This produces the following plot: • Thank you for your attempt in answering my Question. But I think my Question is vague for this is not what I was looking for, although I have noted down many key insights which I was unaware before. The thing is how can I obtain the answer to this multiplication = $y(t-1)*y(t-1)'$ for t = 1 to 1000 (say)? This will yield a matrix, I guess. Then I need to take the Expectation and later trace of the Expectation. This is very similar to computing the individual elements that constitute the Correlation Matrix, $E[y(t)*y(t)^T]$. – SKM Nov 25 '14 at 0:51
• In the current problem, I don't know how I can implement the analytical expression $E[y(t-1)*y(t-1)^T]$. If $E[y*y^T]$ is feasible, then surely it is possible to implement the lagged version also. Is there a way to do? Please help. – SKM Nov 25 '14 at 0:52
• @SKM, $y(t-1)$ in Matlab is simply y(2:end). You need to start at $t=2$ because you don't have $y(0)$. Then, you can simply do y(2:end)*y(2:end)' in Matlab. The result is a matrix. Since the elements are scalars, its mean is equal to the matrix itself. – MBaz Nov 25 '14 at 2:14
• Last Question if you may kindly clarify. For t =3,4,5,...,N, If y(t-1) = y(2:end), then will y(t) = y(3:end); y(t-2) = y(1:end)? – SKM Nov 25 '14 at 10:32
• @SKM, I added an explanation on shifting at the end of my question. I hope this helps; add a comment if it doesn't. By the way, my comment on shifting above was not accurate; disregard in favor of my main answer. – MBaz Nov 25 '14 at 18:34