To build our concept of the portfolio risk, we shall calculate it first manually in EXCEL, then we shall replicate the results using matrix notations in Stata. Consider the following set of returns for two assets, i.e asset A and B.
As we know, the portfolio standard deviation under the modern portfolio theory is calculated as =
Thus, we need the standard deviation of the two assets, the proportion of investment in each asset (weights), and the covariance term between the two assets. In Excel, we can find the standard deviation by
Excel sheet showing the above example can be downloaded from here.
Portfolio Risk in Stata
Finding portfolio standard deviation under the Modern Portfolio theory using matrix algebra requires three matrices 1. Weights of the assets in the portfolio, in row format = W 2. Variance-Covariance matrix of assets returns = S 3. Weights of the assets in the portfolio, in column format = W’
Portfolio SD = W * S * W'
NOTE: In order to find the variance-covariance matrix, you can install varrets program from ssc with:
ssc install mvport
Step 1 : Copy the example data to stata
You can do it either by copying the data from the excel file and pasting it to the stata editor. Alternatively, you can copy the following and past it in the stata command line, or download the data file from here.
input a b.249917 .819483.739069 .821416.895491 .276843.902722 .001586.078344 .714815.429804 .027261.239556 .736011end
Step 2: Make variance-covariance matrix
varrets a bmat S = r(cov)
Step 3: Make a weight matrix
Assuming that we assign equal weights, we define matrix W
mat W = (0.5, 0.5)
Step 4 : Multiply the weight and variance-covariance matrix
mat VAR = W * S * W'
Step 5: Show variance of the portfolio
mat list VAR
Complete .do file of the example can be downloaded from here.