Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Clarification st: Compute portfolio variance

 From André Gyllenram To , Subject Clarification st: Compute portfolio variance Date Mon, 17 Sep 2012 11:37:21 +0200

```Thank you for your answer Alberto. It made me realize that I have to solve this problem in a different way.

I have the historical return data of each of the N stocks in a separate file. It looks like this.

date    valueStock1    valueStock2   valueStock3 ... valueStock99
19980101    4          19            100              33
19980102    4          49            109              88
19980103    5          60            99               44
...         ...        ...           ...              ...
20070101    9          55            126              66

In another file I have information about the stocks each individual owns at the beginning of every year during a ten year period. I also have the portfolio weights  of the stocks that each individual owns. A small part of the data looks  like this.

individual    date        portfolioweight    Stock
1             20000101    0.3                Stock1
1             20000101    0.4                stock9
1             20000101    0.2                stock56
1             20000101    0.1                stock77
...
1             20010101    0.8                stock88
1             20010101    0.2                stock1
2             20000101    0.3                stock3
2             20000101    0.2                stock25
2             20000101    0.4                stock1
2             20000101    0.1                stock88
2             20010101    1.0                stock22
...
1897          20000101    0.6                stock23
1897          20000101    0.2                stock33
1897          20000101    0.2                stock77

I want to compute the portfolio variance for every individual in every time period. Every individual often only own a small number of stocks in every time period.

Is it easy to compute the Var-Cov Matrix if the data is in this format? How do I do that?

I am not familiar with Mata, so any code you have is of interest.

Kind regards
André Gyllenram

>Hi André.
> It is hard to see how you are using your dataset to compute your
> portfolio variance. For example, I do not see where are the weights in

> If you had something like a set of vectors (a matrix) of different
> combinations of portfolio weights for N stocks, and also if you have
> the Var-Cov Matrix (or the historical return data of each of the N
> stocks, you can easily compute the Var-Cov matrix), then it is  simple
> to compute all portfolio variances in only one Matrix multiplication.
> It is faster to use MATA for this. MATA is a computer language that
> can be invoked from Stata.

> In matrix notation, the variance of a portfolio is:
> Var(Portfolio) = w' * COV * w
> Where COV is the Var-Covar matrix of the N returns considered in the
> portfolio, and w can be either a vector or a set of vectors (matrix)
> of different portfolio combinations.
> If N is too big, you can have problems with Mata to do the matrix
> multiplication, but you can use a loop to do N matrix multiplications
> using w as a different vector each time.

> Let me know if you want to compute different variance of potfolio
> combinations, and I can share with you a Mata code I have for doing
> this.

> I hope this help... Alberto Dorantes

----------------------------------------
> From: a_gyllenram@hotmail.com
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Compute portfolio variance
> Date: Mon, 10 Sep 2012 10:56:21 +0200
>
> Hello,
>
> I want to compute the portfolio variance for each individual in every time-period.
>
> Portfolio variance = (weight(1)^2*variance(1) + weight(2)^2*variance(2) + 2*weight(1)*weight(2)*covariance(1,2)
>
> My data-material is in this format:
>
>
>
>
>
> individual  date                 STOCK    varISIN1    varISIN2   ...   varISIN199    corrSTOCK1STOCK2  corrSTOCK1STOCK3   ...   corrSTOCK99STOCK198
> 1          20000101              stock1   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 1          20000101              stock2   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 1          20000102              stock3   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 1          20000102              stock77  .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 1          20000103              stock1   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000101              stock100 .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000101              stock3   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000101              stock2   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000102              stock66  .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000103              stock3   .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
> 2          20000103              stock22  .3333333    450.3333         30.33333     .7073684          -.5765567                .1696948
>
>
>
>
> The problem is that every individual do not own every stock. I also have a very large number of individuals so I cannot compute the portfolio variance for every individual and date manually.
>
>
> Does anyone have an idea how to do this?
>
> Kind regards
> André Gyllenram
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```