Portfolio standard deviation from variance-covariance matrix in a rolling window | Stata


Portfolio standard deviation 

Assume that you have a dataset of stocks for weekly or monthly frequency. Each stock is assigned to a given portfolio. So our the data looks like:

  Security_id month returns portfolio
  18558 1987m1 .0220113 1
  18513 1987m1 -.01355276 1
  18249 1987m1 .02199931 1
  18513 1987m2 -.15505397 1
  18069 1987m2 -.06250625 1
  18558 1987m2 .07093613 1
  18249 1987m2 -.04000542 1
  18558 1987m3 .06831241 1
  18069 1987m3 -.00486903 1
  18147 1987m3 -.03961496 1

If we were to find the portfolio standard deviation over the last 60 days, this will pose a considerable programming challenge. The solution can be decomposed into several steps. Specifically, I list these steps below:

1.  Make a group from two variables, portfolio and months, so we have a total of N groups in each month, identify which stocks is included in which group

2. Then for each stock, we shall go back and find its last 60 months returns

3. Since many stocks might have missing data, we need to rectangularize the data by including missing values, and filling the months series so that each stock has similar number of months, even though they might have no returns in some months. Later we shall delete the missing values

4. This is necessary as  the variance-covariances need to be calculated on matching dates

5. Next, we shall make a matrix in each month for the included stocks from their available past 60 months returns

6. Then find variance -covariance matrix in Stata or Mata (We can make a Mata function for that)

7. Find standard deviation by assigning equal weights to each stock

8. Get the SD from Mata and write it to the relevant portfolio

9. Delete the extra data points created in step 3


Our Stata Code

We have developed easy to use yet robust codes for the above steps.  The codes need just a basic understanding of Stata. Further, our comments on each line of code will surely help you to not only apply the code but also understand the process more clearly.



The code is available for $ 150, plus a $50 for raw data processing (in case the data is not in Stata format and variables are not already constructed). For further details, please contact us at:



See our full list of completed projects