This post presents how to estimate Value at Risk via a variance – covariance method. The following steps outline how to calculate Value at Risk using this method.

1.) Gather stock data and calculate periodic returns (Including the average return of each asset).

2.) Generate a covariance matrix based upon the periodic returns.

3.) Calculate the portfolio mean and standard deviation based upon the defined weights in each asset and amount invested in the portfolio.

4.) Calculate the inverse of the normal cumulative distribution with a specified probability, standard deviation, and mean.

5.) Estimate the value at risk for the portfolio by subtracting the initial investment from the calculation in step 4.

Let’s jump into Python to see how this is implemented.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
from pandas_datareader import data as pdr import datetime as dt import fix_yahoo_finance as yf import numpy as np #Create Multiple Portfolios tickers = ["AAPL", "T", "JPM", "WFC", "V"] #Weights in each stock weights = np.array([.2, .2, .2, .2, .2]) #initial investment initial_investment = 100000 #Override API yf.pdr_override() # download Data data = pdr.get_data_yahoo(tickers, start="2017-01-01", end=dt.date.today())['Close'] #Calculate periodic returns returns = data.pct_change() #Generate Var-Cov matrix cov_matrix = returns.cov() #Calculate mean returns for each stock avg_rets = returns.mean() #Calculate Portfolio Mean port_mean = avg_rets.dot(weights) #Calculate Portfolio STDEV port_stdev = np.sqrt(weights.T.dot(cov_matrix).dot(weights)) #Mean Investment mean_investment = (1+port_mean) * initial_investment #Standard Deviation Investment stdev_investment = initial_investment * port_stdev #Cutoff Point conf_level0 = 0.001 conf_level1 = 0.01 conf_level2 = 0.05 from scipy.stats import norm cutoff0 = norm.ppf(conf_level0, mean_investment, stdev_investment) cutoff1 = norm.ppf(conf_level1, mean_investment, stdev_investment) cutoff2 = norm.ppf(conf_level2, mean_investment, stdev_investment) #Calculate PDF (If Desired) #print(norm.cdf(cutoff0, mean_investment, stdev_investment)) #Calculate 1 Day VaR at different confidence intervals var_1d0 = initial_investment - cutoff0 var_1d1 = initial_investment - cutoff1 var_1d2 = initial_investment - cutoff2 #-------------------------Optional--------------------------# #Calculate n Day VaR in loop num_days = 100 for x in range(1, num_days): print(str(x) + " day VaR at 99.99% confidence level: " + str(np.round(var_1d0 * np.sqrt(x),2))) #Plot Results On Bar Chart import matplotlib.pyplot as plt import pandas as pd data = {'99.99%': np.round(var_1d0,2), '99%': np.round(var_1d1, 2), '95%': np.round(var_1d2, 2)} df = pd.DataFrame(data,index=[0]) df.plot(kind = 'bar') plt.suptitle('Portfolio VaR', fontsize=20) plt.ylabel('VaR ($)', fontsize=16) plt.show() |

Below are some graphical results: