# Computing Montecarlo VaR for a single asset

I'm trying to understand the procedure to compute the Value-at-Risk for a single asset by implementing the Montecarlo technique.

Here it follows the procedure step-by-step in 5 points:

1. selecting the probability density function f(x) which best approximates the distribution of the market returns
2. estimating the parameters for the selected distribution f(x)
3. simulating N scenarios from the distribution f(x)
4. calculating the position's market value for each scenario
5. cutting off the resulting probability distribution at the percentile corresponding to the desired confidence level

First of all, I would like to understand whether the procedure is theoretically correct; according to you, is this process correct to compute the VaR?

Moreover, for the point (3), I broke down such point in 2 sub-steps:

• extracting a number u from the Uniform distribution
• computing the value x, such that x = $$F^{-1}$$(u)

How can I compute properly the inverse cumulative distribution in order to get the value $$F^{-1}$$(u)?

I did it by using the quantile SAS function within the following data step, by assuming that the returns follow a Gamma distribution, $$\Gamma(\alpha,\theta)$$, where $$\alpha$$ is the shape parameter and $$\theta$$ the scaling parameter.

The data step is as follows:

data distribution_sample (keep = _U_ x);
call streaminit(&seed.);
/* (3) simulate x scenarios from the selected distribution */
do i = 1 to %eval(10*&nmc.);
/* Extracting a number _U_ from the uniform distribution */
_U_ = rand("Uniform");
x = QUANTILE('GAMMA', _U_,&alpha.,&theta.); /*the QUANTILE function returns the inverse of the CDF function*/
output;
end;
run;


According to you, is correct such procedure to solve the step (3)?

The process you describe of random sampling from a distribution is correct. Calculate a uniform random variable $$u$$ and then convert that to a value $$x$$ by requiring that $$x$$ is the minimum value for which $$F(x) \geq u$$. Then $$x$$ can be said to be sampled from $$f(x)$$. You are right that in practice this can be hard depending upon the underlying $$f(x)$$. Take, for example, the normal distribution. The box-muller algorithm is an effective and accurate way to sample from this distribution but it is a sufficiently clever (in my opinion) concept that highlights the difficult in generalising the process. Of course there are numerical, and computationally intensive ways I'm sure but they don't necessarily scale so often sampling algorithms have been developed for different distributions. Indeed the gamma distribution, I'm sure will have its own its own implementation: http://www.hongliangjie.com/2012/12/19/how-to-generate-gamma-random-variables/

When sampling you should be concerned about the variance of your estimator. One technique to reduce the variance of your estimator (and thereby have a higher notion of confidence it is predictive power of your population statistic) is to use importance sampling. When sampling a very low probability of events (and hence infrequently sampled values) might contribute a reasonably large amount of the expected value (for example very low probability of complete default on bonds). In these cases sampling more frequently from the lower probabilities but scaling the results according to the true distribution yields a more consistently accurate estimator.

Edit:

In your script you explicitly generate samples from the $$\Gamma(\alpha, \theta)$$ distribution using the quantile for a uniform RV. You don't explicitly show any code that does anything with your sample, but presumably calculate some profit and loss value assuming that market movement. But lets say that specific points of interest are in the regions of the distribution which have low pdf, i.e. around zero perhaps and in the right hand tail. What you can do is generate samples from distributions that have higher density in those regions. Suppose instead you generated samples from a normal distribution, $$\mathcal{N}(\alpha \theta, 2k\theta^2)$$ you can obtain the ratio: $$\frac{f(x)}{g(x)}$$, where f(x) is your underlying true (gamma) distribution pdf and g(x) is your sample (normal) distribution pdf. Most often this technique is used to calculate means and you multiply all data values by this fraction to obtain a weighted average.

Now in your case when you are attempting to find the VaR you are interested in say the lowest 5% of values. If you sampled from the Gamma distribution 100 times and ordered your values you would progress 5 along and read out that value: you have inherently assigned each datapoint the unit value 1 and progressed until the sum of 5. When using importance sampling try assigning weights to your values by the above ratio. You will get more samples in the wider regions but their weight is lower so to discover the 5% quantile you might take more into account:

Sampling 100 from Gamma:

weight: [1,1,1,1,1,1,......]

pnl: [1,3,5,7,8,9,10,11,....] Var = 9

Sampling from Normal:

weight: [0,0,0.1,0.5,0.6,0.7,0.8,0.9,0.9,0.9,0.9,0.95,...]

pnl: [-100,-100,0.1,0.4,0.5,0.8,1,1,2,4,7,8...] Var = 4.

The art is of course choosing a sample distribution that optimises the lowest variance and is computationally efficient. Wiki has a good page on this.

• Hi @Attack68 and thanks for your answer! Could you provide an example of the importance sampling technique applied to my case? For instance, practically, how do you modify my script for sampling by applying importance sampling? – Quantopik Nov 1 '18 at 10:19