returns = data.pct_change()
meanReturns = returns.mean()
covMatrix = returns.cov()
weights = np.random.random(len(returns.columns))
weights /= np.sum(weights)
returns = returns.dropna()
returns['portfolio'] = returns.dot(weights)
#Monte Carlo Method
mc_sims = 400 # number of simulations
T = 100 #timeframe in days
meanM = np.full(shape=(T, len(weights)), fill_value=meanReturns)
meanM = meanM.T
portfolio_sims = np.full(shape=(T, mc_sims), fill_value=0.0)
initialPortfolio = 10000
for m in range(0, mc_sims):
# MC loops
Z = np.random.normal(size=(T, len(weights)))
L = np.linalg.cholesky(covMatrix)
dailyReturns = meanM + np.inner(L, Z)
portfolio_sims[:,m] = np.cumprod(np.inner(weights, dailyReturns.T)+1)*initialPortfolio
def mcVaR(returns, alpha):
""" Input: pandas series of returns
Output: percentile on return distribution to a given confidence level alpha
"""
return np.percentile(returns, alpha)
def mcCVaR(returns, alpha):
""" Input: pandas series of returns
Output: CVaR or Expected Shortfall to a given confidence level alpha
"""
belowVaR = returns <= mcVaR(returns, alpha)
return returns[belowVaR].mean()
portResults = pd.Series(portfolio_sims[-1,:])
VaR = (initialPortfolio - mcVaR(portResults, 1))
CVaR = (initialPortfolio - mcCVaR(portResults, 1))
print('VaR {}%'.format(round(VaR,2)))
print('CVaR {}%'.format(round(CVaR,2)))
If I want to apply kelly criterion to the portfolio, shouldnt I use the kelly weights for the calculation of the value at risk? It would seem people use random weights to get monte carlo cvar and var. Should I use kelly 2 times? One without monte carlo and with ?
Hi Jane,
Whether you should use Kelly once or twice (with and without Monte Carlo simulations) depends on the goals of your strategy and the level of risk you are willing to take.
Thanks,
Akshay
Is there a standard way to approach using monte carlo simulation with kelly on the porfolio? And how does it vary from the sample code above?
Hi Jane,
Yes. There is a standard approach to use Monte Carlo simulation with the Kelly criterion for portfolio optimisation. You can follow the following approach:
- Define the portfolio
- Generate random scenarios
- Calculate the Kelly fraction for each investment
- Evaluate the performance of the portfolio
- Select the optimal portfolio
Hope this helps!
Thanks,
Akshay
Hi Jane,
Yes. There is a standard approach to use Monte Carlo simulation with the Kelly criterion for portfolio optimisation. You can follow the following approach:
- Define the portfolio
- Generate random scenarios
- Calculate the Kelly fraction for each investment
- Evaluate the performance of the portfolio
- Select the optimal portfolio
Hope this helps!
Thanks,
Akshay