Exercises - Chapter 5

This forum is to discuss the book "C++ design patterns and derivatives pricing."

Exercises - Chapter 5

Exercise 5.2 is about the calculation of the Value At Risk of a sample.
Do we only need to use the Variance-Covariance method (using the first moments calculated in ex. 5.1), or do we need to sort all the payoff outcomes of the radom draws to get the x% lowest values for a 100-x confidence interval?

In the latter case, since we need to sort the payoff outcomes, what is the most memory-optimal strategy (creating a vector of all the draws outcomes seems a bit irrealistic)? A vector of only the x% lowest values, for example?

akbar

Posts: 28
Joined: Fri Aug 10, 2007 7:12 pm

well ultimately it's up to you!

but the solution in my mind was to store all samples and sort.
mj

Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Exercises - Chapter 5

Hi, I was also struggling a little with this partly cause I was not familiar with the value of risk.
Is it correct that I should simply determine the entry in the ordered vector that is for example x% of the total number of values in the list?

I have included what I see as a solution to this problem any comments would be greatly appreciated.

cheers

Mark
Code: Select all
`////////                          ValueAtRisk.cpp////#include <vector>#include <iterator>#include <algorithm>#include <ValueAtRisk.h>#include <cmath>#include <iostream>using namespace std;ValueAtRisk::ValueAtRisk(double alpha_) : alpha(alpha_){   PathsDone=0;}StatisticsMC* ValueAtRisk::clone() const{    return new ValueAtRisk(*this);}void ValueAtRisk::DumpOneResult(double result){   PathData.push_back(result);    ++PathsDone;}vector<vector<double> >  ValueAtRisk::GetResultsSoFar() const{   vector<vector<double> > Results(1);    Results[0].resize(1);   vector<double> tmp(PathData);      sort(tmp.begin(), tmp.end());   int n = tmp.size();   int var_slot = ceil(n*alpha);      Results[0][0] = tmp[var_slot];    return Results;}`
emza0114

Posts: 8
Joined: Sun Nov 25, 2012 2:26 am

that is correct.
mj