## Step-by-Step: Portfolio Risk in Stata and Excel

Category:Uncategorized**Portfolio Risk in Excel**

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.

A B .249917 .819483 .739069 .821416 .895491 .276843 .902722 .001586 .078344 .714815 .429804 .027261 .239556 .736011

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

=STDEV(range)

And the formula for covariance is

=COVARIANCE.S(range1, range2)

Thus, standard deviation of asset A = 0.3387

the standard deviation of asset B = 0.3713

Covariance between the two = -0.0683

And if we invest equally in both assets, then

the weight of A = 0.5

and weight of B = 0.5

Portfolio SD =( (0.5^2*0.3387^2)+(0.5^2*0.3713^2)+(2*0.5*0.5*-0.0683))^0.5 =(0.02897)^0.5 =0.1702

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 .736011`

`end`

### 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.