Options(Monte Carlo Simulation)


vector<double> myVec2;
for(int i = 0; i < 1000; i++){
myBarrier.generatePath();
myVec2.push_back(myBarrier.thisPath.back());
}

cout << "mean of myVec is " << mean(myVec2) << "\n";
cout << "stddev of myVec is " << stdDev(myVec2) << "\n";



//cout << "\nPress Enter to continue...";
//cin.get();
system ("Pause");
return 0;
}

Last edited on
This is your generatePath method:

1
2
3
4
5
6
7
8
9
10
11
12
//method definition
void BarrierOption::generatePath() {
    double thisDrift = (r * expiry - 0.5 * vol * vol * expiry) / double(nInt);
    double cumShocks = 0;
    thisPath.clear();

    for(int i = 0; i < nInt; i++) {
        cumShocks += (thisDrift + vol * sqrt(expiry / double(nInt)) * 
                             GetOneGaussianByBoxMuller());
        thisPath.push_back(spot * exp(cumShocks));
    }
}


How does one determine when a barrier is hit?
To be honestly I have no idea. However, the teacher told me I have to do something in the function I have mentioned. I assume that when it generate numbers it stores them in a vector this.path. So maybe I could write small syntax that will get the largest number in that vector and if there is a number that is over 250 then it could stop and return zero,meaning that the option costs nothing (zero). I hope that somehow helps, I am not very good ad advanced c++.
I think your friend might have been suggesting to iterate through the vector and search until you find the number 250. So something like:

1
2
3
4
typedef vector<double>::iterator dviter;

for (dviter iter = thisPath.begin(); iter != thisPath.end(); ++iter)
{ /* Search for the number 250 */ }


The above method is all well and good, but since this is a monte carlo simulation you don't want to be incurring extra overhead just because you need to search for one number. So what I would suggest it to make the generatePath method return a boolean. It will return true if 250 was not appended to the path and it will return false if 250 is appended to the path.

So this means before you add each value to the vector, you check if it is 250. If it is you return false. The default return value of the function should be true

Then in your function:
double BarrierOption::arithmeticBarrierPut(int)

You do
...
1
2
3
if (!generatePath()) {
    return 0;
}
Smac89:
Thank you very much for reply.So if I did use boolean syntax, how exactly would my function look like. I mean how would my Arithmetic BarrierPut function would look like exactly? Again thank you very much.
That function will look exactly the same as before, but instead of just having the line: generatePath();, you will replace this with the if statement I wrote out in my last comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool BarrierOption::generatePath() {
    double thisDrift = (r * expiry - 0.5 * vol * vol * expiry) / double(nInt);
    double cumShocks = 0;
    thisPath.clear();

    for(int i = 0; i < nInt; i++) {
        cumShocks += (thisDrift + vol * sqrt(expiry / double(nInt)) * 
                             GetOneGaussianByBoxMuller());
        double pathvalue = spot * exp(cumShocks);
        if (pathvalue >= 250)
        	return false;
        thisPath.push_back(pathvalue);
    }
    
    return true;
}

//method definition
double BarrierOption::arithmeticBarrierPut(double nReps){
	double rollingSum = 0.0;
	double thisMean = 0.0;
	//int barrier=250;

	for(int i = 0; i < nReps; i++){
		if (!generatePath())
			return 0;
		thisMean = getArithmeticMean();
		rollingSum += (thisMean < strike) ? (strike - thisMean) : 0;
	}

	return rollingSum / nReps;
}
Last edited on
thank you very much. I appreciate it
Topic archived. No new replies allowed.