COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Variance Covariance Matrix

• Email
• Follow

```Hello!

I have to generate a Variance Covariance Matrix of assets returns using as parameter a decay factor of 0.97.
Does anyone knows how can i include the decay factor? (the cov function does not allow me to do that)

Giorgio
```
 0

See related articles to this posting

```On 12/27/2010 10:26 AM, Giorgio Solfaroli wrote:
> I have to generate a Variance Covariance Matrix of assets returns using
> as parameter a decay factor of 0.97.
> Does anyone knows how can i include the decay factor? (the cov function
> does not allow me to do that)

The COV function is for computing a sample covariance matrix, given
data.  There are no model parameters involved at all.  It sounds like
you want to compute a theoretical covariance.  You should be able to do
that based on whatever model you are using.
```
 0

```Peter Perkins <Peter.Perkins@MathRemoveThisWorks.com> wrote in message <ifajop\$egt\$1@fred.mathworks.com>...
> On 12/27/2010 10:26 AM, Giorgio Solfaroli wrote:
> > I have to generate a Variance Covariance Matrix of assets returns using
> > as parameter a decay factor of 0.97.
> > Does anyone knows how can i include the decay factor? (the cov function
> > does not allow me to do that)
>
> The COV function is for computing a sample covariance matrix, given
> data.  There are no model parameters involved at all.  It sounds like
> you want to compute a theoretical covariance.  You should be able to do
> that based on whatever model you are using.

I want to compute a GBM with a theoretical mean using a Variance Covariance Matrix computed with a decay factor of 0.97..Do you have any suggestion to give me?
```
 0

```On Dec 27, 10:26 am, "Giorgio Solfaroli"
<giorgio.solfar...@uniqagroup.it> wrote:
> Hello!
>
> I have to generate a Variance Covariance Matrix of
> assets returns using as parameter a decay factor of 0.97.
> Does anyone knows how can i include the decay factor?
>(the cov function does not allow me to do that)
>
>
> Giorgio

M      No. of assets
N      No. of time instants (N is index of latest asset)
X0     Original assets return matrix

size(X0) = [N M]

W  diagonal exponential decay weight matrix

W0i = a^(N-i)       % i = 1:N, a = 0.97
Wi  = W01/sum(W0i)

X     Exponentially weighted assets return matrix

X = W*X0

covX = cov(X);

Hope this helps.

Greg
```
 0

```Greg Heath <heath@alumni.brown.edu> wrote in message <1efad805-f60f-460b-8764-5c3e2f434956@30g2000yql.googlegroups.com>...
> On Dec 27, 10:26 am, "Giorgio Solfaroli"
> <giorgio.solfar...@uniqagroup.it> wrote:
> > Hello!
> >
> > I have to generate a Variance Covariance Matrix of
> > assets returns using as parameter a decay factor of 0.97.
> > Does anyone knows how can i include the decay factor?
> >(the cov function does not allow me to do that)
> >
> >
> > Giorgio
>
> M      No. of assets
> N      No. of time instants (N is index of latest asset)
> X0     Original assets return matrix
>
> size(X0) = [N M]
>
> W  diagonal exponential decay weight matrix
>
> W0i = a^(N-i)       % i = 1:N, a = 0.97
> Wi  = W01/sum(W0i)
>
> X     Exponentially weighted assets return matrix
>
> X = W*X0
>
> covX = cov(X);
>
> Hope this helps.
>
> Greg

Hello Greg!

I wrote down this code:

for i=1:N
W0(i) = 0.97^(N-i);
W(i) = W0(1)/sum(W0(i));
end

X = W*X0;

covX = cov(X);

Where M=43 and N=74

Anyway what i get is a single covariance number (cov(X) is a 1x1 matrix, instead of 43)
Where is my mistake?

```
 0

```On Dec 29, 4:38 am, "Giorgio Solfaroli"
<giorgio.solfar...@uniqagroup.it> wrote:
> > On Dec 27, 10:26 am, "Giorgio Solfaroli"
> > <giorgio.solfar...@uniqagroup.it> wrote:
> > > Hello!
>
> > > I have to generate a Variance Covariance Matrix of
> > > assets returns using as parameter a decay factor of 0.97.
> > > Does anyone knows how can i include the decay factor?
> > >(the cov function does not allow me to do that)
>
> > > Thank you very much in advance for your attention.
>
> > > Giorgio
>
> > M      No. of assets
> > N      No. of time instants (N is index of latest asset)

CORRECTION:  ... (N is index of the latest TIME)...

> > X0     Original assets return matrix
>
> > size(X0) = [N M]
>
> > W  diagonal exponential decay weight matrix
>
> > W0i = a^(N-i)       % i = 1:N, a = 0.97
> > Wi  = W01/sum(W0i)
>
> > X     Exponentially weighted assets return matrix
>
> > X = W*X0
>
> > covX = cov(X);
>
> > Hope this helps.
>
> > Greg
>
> Hello Greg!
>
>
> I wrote down this code:
>
> for i=1:N
> W0(i) = 0.97^(N-i);
> W(i) = W0(1)/sum(W0(i));
> end
>
> X = W*X0;
>
> covX = cov(X);
>
> Where M=43 and N=74
>
> Anyway what i get is a single covariance number (cov(X) is a 1x1 matrix, instead of 43)
> Where is my mistake?

1. Using my comments without trying to understand them,
e.g., why did you use W0(1) and why did you
normalize with the sum before having all the terms?
2. Assuming my comments were error free code
3. Not using small N,M with semicolons removed so
that you can see the result of each command
4. Not checking the dimensions of each matrix
via whos or size, e.g, what is the size of your X?
5. Using an unnecessary for loop instead of vectorization
(not really a mistake, but if you are going to
use MATLAB, learn how to vectorize!)

close all, clear all,  clc

% Debugging: Small N,M and no semicolons

N = 7, M = 4, a = 0.97

rand('state',0) % Old version
X0 = rand(N,M)  % [7 4] meaningless placeholder

i = (1:N)'      % [7 1]
Wu  = a.^(N-i)   % [7 1] Unnormalized
Wn = Wu/sum(Wu)  % [7 1] Normalized
whos            % Shows all dimensions

% UNDERSTANDING: You have to multiply
% each column of X0 with Wn (i.e.,
% Wn.*X0(:,j), j=1:M) and end up
% with  size(X) = size(X0) = [N M]
% and size(cov(X)) = [M M]

W     = diag(Wn)
X     = W*X0
covX  = cov(X)

% Simple check:

Xc     = X - repmat(mean(X),N,1)
dcovX  = abs( covX - Xc'*Xc/(N-1) )

Hope this helps.

Greg

```
 0

```Greg Heath <heath@alumni.brown.edu> wrote in message <540aa0b1-dd16-498b-bfc6-9333b7808c0f@s4g2000yql.googlegroups.com>...
> On Dec 29, 4:38 am, "Giorgio Solfaroli"
> <giorgio.solfar...@uniqagroup.it> wrote:
> > > On Dec 27, 10:26 am, "Giorgio Solfaroli"
> > > <giorgio.solfar...@uniqagroup.it> wrote:
> > > > Hello!
> >
> > > > I have to generate a Variance Covariance Matrix of
> > > > assets returns using as parameter a decay factor of 0.97.
> > > > Does anyone knows how can i include the decay factor?
> > > >(the cov function does not allow me to do that)
> >
> > > > Thank you very much in advance for your attention.
> >
> > > > Giorgio
> >
> > > M      No. of assets
> > > N      No. of time instants (N is index of latest asset)
>
> CORRECTION:  ... (N is index of the latest TIME)...
>
> > > X0     Original assets return matrix
> >
> > > size(X0) = [N M]
> >
> > > W  diagonal exponential decay weight matrix
> >
> > > W0i = a^(N-i)       % i = 1:N, a = 0.97
> > > Wi  = W01/sum(W0i)
> >
> > > X     Exponentially weighted assets return matrix
> >
> > > X = W*X0
> >
> > > covX = cov(X);
> >
> > > Hope this helps.
> >
> > > Greg
> >
> > Hello Greg!
> >
> >
> > I wrote down this code:
> >
> > for i=1:N
> > W0(i) = 0.97^(N-i);
> > W(i) = W0(1)/sum(W0(i));
> > end
> >
> > X = W*X0;
> >
> > covX = cov(X);
> >
> > Where M=43 and N=74
> >
> > Anyway what i get is a single covariance number (cov(X) is a 1x1 matrix, instead of 43)
> > Where is my mistake?
>
> 1. Using my comments without trying to understand them,
>    e.g., why did you use W0(1) and why did you
>    normalize with the sum before having all the terms?
> 2. Assuming my comments were error free code
> 3. Not using small N,M with semicolons removed so
>    that you can see the result of each command
> 4. Not checking the dimensions of each matrix
>    via whos or size, e.g, what is the size of your X?
> 5. Using an unnecessary for loop instead of vectorization
>    (not really a mistake, but if you are going to
>     use MATLAB, learn how to vectorize!)
>
>
> close all, clear all,  clc
>
> % Debugging: Small N,M and no semicolons
>
> N = 7, M = 4, a = 0.97
>
> rand('state',0) % Old version
> X0 = rand(N,M)  % [7 4] meaningless placeholder
>
> i = (1:N)'      % [7 1]
> Wu  = a.^(N-i)   % [7 1] Unnormalized
> Wn = Wu/sum(Wu)  % [7 1] Normalized
> whos            % Shows all dimensions
>
> % UNDERSTANDING: You have to multiply
> % each column of X0 with Wn (i.e.,
> % Wn.*X0(:,j), j=1:M) and end up
> % with  size(X) = size(X0) = [N M]
> % and size(cov(X)) = [M M]
>
> W     = diag(Wn)
> X     = W*X0
> covX  = cov(X)
>
> % Simple check:
>
> Xc     = X - repmat(mean(X),N,1)
> dcovX  = abs( covX - Xc'*Xc/(N-1) )
>
> Hope this helps.
>
> Greg

Thanks! Your help has been really usefull for me!

Giorgio
```
 0