Calculating Value At Risk or "most probable loss", for a given distribution of returns

7.8k views Asked by At

Given historical daily returns, how can I calculate the portfolio allocation for a single stock position, based on not losing more than 10% of the starting portfolio value over 21 days? (with 95% confidence.)

Based on some starting code of e.g.

import numpy as np
from scipy.stats import norm

returns = [-0.01, -0.02, -0.01, 0.04, 0.02, 0.01, -0.03]
mu = np.mean(returns)
std = np.std(returns)
valueAtRisk = norm.ppf(0.05, mu, sigma)

However, the above only tells me my risk for 1 day. My question goes in the other direction; what can I allocate given the distribution of returns, assuming that I don't want to lose more than 10% over 21 days.

I would prefer an answer that can be computed directly, but a Monte Carlo answer would be acceptable.

Thanking you kindly for your help.

1

There are 1 answers

1
Jianxun Li On
import numpy as np

returns = np.random.randn(1000)

Assuming returns are independently and identically distributed (i.i.d.), then the volatility of T days equals to the product of sqrt(T) times one-day-volatility.

# one-way 5% quantile, critical value is 1.64
VaR_21 = returns.std() * np.sqrt(21) * 1.645
VaR_21
Out[72]: 7.4161618430166989

Alternatively, you can do bootstraps. That's randomly select 21 days from historical dataset, calculate the return over this randomly drawed 21 days. Plot the histogram and get the 5% quantile.

def generate_random_index(n=21):
    # could set replace to False as well
    return np.random.choice(np.arange(1000), size=n, replace=False)  

VaR_simulated_21 = []
n_bootstrap = 10000
for _ in range(n_bootstrap):
    VaR = returns[generate_random_index(21)].sum()
    VaR_simulated_21.append(VaR)

plt.hist(VaR_simulated_21)
np.percentile(VaR_simulated_21, q=5)
Out[108]: -8.0686958215041216

enter image description here