VaR with normally distributed returns using Boost

In what follows, we work out an exercise on value-at-risk with normally distributed returns that is largely inspired by Carol Alexander‘s Market Risk Analysis, Value at Risk Models (Vol. IV) . Just as it was the case in a recent post, all the interest here lies in the use of C++/Boost.

Exercise“What is the 10% VaR over a 1-year horizon of $3,5 million invested in a fund whose annual returns in excess of the risk free rate are assumed to be normally distributed with mean 7% and volatility 14%?”

One main point is here is computing the inverse normal distribution. We thus resort on the Boost quantile function.

// Édouard Tallent @ TaGoMa.Tech
// September 2012

using std::cout;
using std::endl;

double inverseNormal(double prob, double mean, double sd){
        boost::math::normal_distribution<>myNormal (mean, sd);
        return quantile(myNormal, prob);

int main (int, char*[])
                double myProb = 0.1;	// the 10% quantile
                double myMean = 0.07;	// a 7% mean 
                double myVol = 0.14;	// a 14% volatility 

		cout << inverseNormal(myProb, myMean, myVol)  << endl;

                catch(std::exception& e)
                cout << "Error message: " << e.what() << endl;
return 0;

The compiler should return a -0109417 value (with no surprise a negative value is returned).

Checking this result with R is straightforward:

# Édouard Tallent @ TaGoMa.Tech
# September 2012

# Computes the inverse normal distribution function
VaR <- qnorm(0.1, 0.07, 0.14)

# Prints out the result
print(paste("10% 1-year Var with mean = 7% and sd = 14%: ", VaR))

Put in words, the 10% 1-year Var is 10,94% of the portfolio value. With $3,5 million invested the VaR then is $3,5  x 0,1094 = $382 900. According to our calculations, we are 90% confident that the investor will lose no more than $382 900 over the next year.

About these ads

One thought on “VaR with normally distributed returns using Boost

  1. Pingback: Analytical credit exposure, an example code in C++ | Quant Corner

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s