-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwoLevelModel.cpp
116 lines (101 loc) · 3.22 KB
/
TwoLevelModel.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// #include "TwoLevelModel.h"
// #include "BlackScholesModel.h"
// using namespace std;
// #include "matlib.h"
// TwoLevelModel::TwoLevelModel() {
// setDrift(0.0);
// setStockPrice(0.0);
// setVolatility(0.0);
// setVolatility2(0.0);
// setRiskFreeRate(0.0);
// setDate(0.0);
// }
// /**
// * Creates a price path according to the model parameters
// */
// vector<double> TwoLevelModel::
// generateRiskNeutralPricePath(
// double toDate,
// int nSteps ) const {
// return generatePricePath( toDate, nSteps, getRiskFreeRate() );
// }
// /**
// * Creates a price path according to the model parameters
// */
// vector<double> TwoLevelModel::generatePricePath(
// double toDate,
// int nSteps ) const {
// return generatePricePath( toDate, nSteps, getDrift() );
// }
// /**
// * Creates a price path according to the model parameters
// */
// vector<double> TwoLevelModel::generatePricePath(
// double toDate,
// int nSteps,
// double drift ) const {
// vector<double> path(nSteps,0.0);
// vector<double> epsilon = randn( nSteps );
// double dt = (toDate-getDate())/nSteps;
// double a = (drift - getVolatility()*getVolatility()*0.5)*dt;
// double b = getVolatility()*sqrt(dt);
// double currentLogS = log( getStockPrice() );
// double dLogS, logS;
// for (int i=0; i<nSteps/2; i++) {
// dLogS = a + b*epsilon[i];
// logS = currentLogS + dLogS;
// path[i] = exp( logS );
// currentLogS = logS;
// }
// a = (drift - getVolatility2()*getVolatility2()*0.5)*dt;
// b = getVolatility2()*sqrt(dt);
// for (int i=nSteps/2; i<nSteps; i++) {
// dLogS = a + b*epsilon[i];
// logS = currentLogS + dLogS;
// path[i] = exp( logS );
// currentLogS = logS;
// }
// return path;
// }
// ////////////////////////////////
// //
// // TESTS
// //
// ////////////////////////////////
// void testAgainstBSM() {
// rng("default");
// BlackScholesModel bsm;
// bsm.setRiskFreeRate(0.05);
// bsm.setVolatility(0.1);
// bsm.setStockPrice(100.0);
// bsm.setDate(2.0);
// int nPaths = 10000;
// int nsteps = 5;
// double maturity = 4.0;
// vector<double> finalPricesBSM(nPaths,0.0);
// for (int i=0; i<nPaths; i++) {
// vector<double> path =
// bsm.generateRiskNeutralPricePath( maturity,
// nsteps );
// finalPricesBSM[i] = path.back();
// }
// double bsmMeanPrice = mean( finalPricesBSM );
// TwoLevelModel tlm;
// tlm.setRiskFreeRate(0.05);
// tlm.setVolatility(0.1);
// tlm.setVolatility2(0.1);
// tlm.setStockPrice(100.0);
// tlm.setDate(2.0);
// vector<double> finalPricesTLM(nPaths,0.0);
// for (int i=0; i<nPaths; i++) {
// vector<double> path =
// bsm.generateRiskNeutralPricePath( maturity,
// nsteps );
// finalPricesTLM[i] = path.back();
// }
// double tlmMeanPrice = mean( finalPricesTLM );
// ASSERT_APPROX_EQUAL( tlmMeanPrice, bsmMeanPrice, 0.5);
// }
// void testTwoLevelModel() {
// TEST( testAgainstBSM );
// }