Measuring Portfolio risk using Monte Carlo simulation in python — Part 2
Introduction
Let’s do a quick recap of our previous discussion on implementing the Monte Carlo simulation model for a portfolio.
We talked about the definition of Monte Carlo and why is it an important process to consider in many different industries, such as Finance, Retail, investment, etc. We also explained all the essential functions needed to process and calculate the result, which in our case is the output that iterates a list of portfolios that shows the best possible and worst-case scenario performance. These functions are related to calculating mean returns, volatility, and the relationship between two stocks and more in the portfolio by using Covariance. I visualized these results in a line chart based on a pre-defined initial portfolio value.
Ultimately I mentioned that visualization of Monte Carlo simulation was not enough, as we need to use additional parameters such as VaR and cVaR values to quantify unrealized and projected losses for the portfolio, so without further ado, Let’s go with explaining and implementing VaR and cVaR with our already implemented Monte Carlo model.
Important: To follow up and understand the remaining tasks associated with the previous Monte Carlo model simulation , please revisit the previous work on the model for the VaR and cVaR function to work .Click below or copy paste the following link: https://medium.com/codex/measuring-portfolio-risk-using-monte-carlo-simulation-in-python-part-1-ac69ea9802f
What is Var and cVar ?
Before we jump into implementing VaR and cVaR in our model, We need to know what are VaR and cVaR.
VaR and cVaR stand for Value at risk and Conditional value at risk respectively.
VaR (Value at Risk) is a statistical method that quantifies the extent of possible financial losses within a portfolio, organization, or position over a specific time frame, while cVaR (Conditional Value at Risk) known as the expected shortfall, is a risk assessment measure that quantifies the amount of tail risk an investment portfolio has. cVaR is calculated by taking a weighted average of the extreme losses in the tail of the distribution of possible returns, beyond the value at risk (VaR) cutoff point, in another word, cVaR will give you the value of losses for the worst-case scenario of your portfolio or position. Hence, cVaR value tends to be mathematically larger than VaR , that’s why a Fund manager will always consider the readings from both Var and cVaR to optimize and measure risk for effective risk management.
What is an easy way of understanding the difference between Value-at-Risk (VaR) and Conditional-Value-at-Risk (CVaR) of a portfolio?
To answer these questions, let me refresh your memories on some important concepts in statistics, do you remember the normal (or Gaussian) distribution, confidence interval, and Z-values.
Please do visit the following link or click here < https://medium.com/swlh/a-simple-refresher-on-confidence-intervals-1e29a8580697> to read about these concepts and have a quick refresher on these concepts before we proceed to explain the relationship of these concepts and our attempt to calculate cVaR and VaR.
Figure 1 clearly illustrates the different confidence interval for every Z-value
A confidence interval is an interval in which we expect the actual outcome to fall with a given probability (confidence). There are four common used confidence intervals for normal distribution , and they are :
· 68% of values fall within 1 standard deviation of the mean .
· 90% of values fall within 1.65 standard deviations of the mean .
· 95% of values fall within 1.96 standard deviations of the mean.
· 99% of values fall within 2.58 standard deviations of the mean.
This means our confidence interval determines to which z-values our portfolio represents. Check the below figure, Figure 2
For example, A stock portfolio has mean returns of 10% per year and the returns have a standard deviation of 20%. The returns are normally distribution. Calculate the 99% confidence interval. 95% confidence interval = 10% +/- 2.58*20%.
Hence, the confidence interval is -41.6% to 61.6%. This means with 99% confidence, the returns will range from -41.6% to 61.6%.
You don’t have to worry about all of this , as we can easily calculate z-scores with python , given of course all the data from normal distribution we ‘ve done earlier .
Python code of VaR and cVaR
Now we can define a function to what level of uncertainty of percentile from the Monte Carlo simulation distribution, gives us what the value of risk is.
def mcVaR(returns, alpha):
""" Input: pandas series of returns
Output: percentile on return distribution to a given confidence level alpha
"""
if isinstance(returns, pd.Series):
return np.percentile(returns, alpha)
else:
raise TypeError("Expected a pandas data series.")
def mcCVaR(returns, alpha):
""" Input: pandas series of returns
Output: CVaR or Expected Shortfall to a given confidence level alpha
"""
if isinstance(returns, pd.Series):
belowVaR = returns <= mcVaR(returns, alpha=alpha)
return returns[belowVaR].mean()
else:
raise TypeError("Expected a pandas data series.")
# You will probably want to print results!!
portResults = pd.Series(portfolio_sims[-1,:])
VaR = initialPortfolio - mcVaR(portResults, 5)
CVaR = initialPortfolio - mcCVaR(portResults, 5)
print('VaR ${:,.2f}'.format(VaR,2))
print('CVaR ${:,.2f}'.format(CVaR,2))
I think the code is straightforward and easy to understand. Here we simply will use 95% confidence interval with 5% percentile if our portfolio falls to the worst territory or side of performance.
Results
Based on the results populated by the pandas series, our results are as follows
· VaR is at 9,281.26
· cVaR is at 10,972.41
So in the worst case scenario, assuming the market will drop by 5% percentile of our confidence interval, our portfolio which was valued at $ 47,000, will be worth =47,000–10,972 approximately $ 36,000.
To put it in simple terms, based on the stock selection (Portfolio asset allocation composition) of this portfolio, the expected value at risk of the past 100 days was a loss of $10,972.41.
A seasoned and experienced portfolio manager will play around with different asset classes to reach an overall consensus on the most optimal portfolio for a given level of risk.
For example, assuming a Fund/Portfolio manager constructed Portfolio A , B, and C . they all have a different collection of stocks profile (Apple , Meta , Exxon , etc). Portfolio A shows a cVaR value of USD10k, B and C show a cVaR value of USD 8k and USD 5k respectively.
Based on the above, the Fund manager shall go for Portfolio C as it offers fewer losses compared to Portfolio A and B, however, this is shouldn’t be the only factor to select an optimal portfolio. There are other factors to consider such as the yield profile for these stocks, investment objective, and risk, sectoral analysis, Geographical analysis,etc.
It’s always a good idea to use cVaR value as your reference for risk, While VaR measures the maximum loss in a given confidence level and period, CVaR measures the amount of loss exceeding VaR in a given confidence level.
I hope you all enjoyed reading this article, Please follow me so you won’t miss out on Fintech, Artificial intelligence, and other related Data Science projects. I am always a big fan of the real-life implementation of concepts concerning Data Science and the internet of things.
Thanks again, don’t forget to follow my account and drop me a message if you ever have any questions regarding this article.