-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMonteCarloPricer.cpp
149 lines (125 loc) · 4.19 KB
/
MonteCarloPricer.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "MonteCarloPricer.h"
#include "CallOption.h"
#include "Executor.h"
#include "matlib.h"
using namespace std;
MonteCarloPricer::MonteCarloPricer()
: nScenarios(100000), nSteps(10), nTasks(1) {}
double MonteCarloPricer::price(const ContinuousTimeOption &option,
const BlackScholesModel &model) const {
auto stocks = option.getStocks();
assert(stocks.size() == 1);
MultiStockModel msm(model);
return price(option, msm);
}
double MonteCarloPricer::delta(const ContinuousTimeOption &option,
const BlackScholesModel &model, double h) const {
auto stocks = option.getStocks();
assert(stocks.size() == 1);
BlackScholesModel model1(model);
BlackScholesModel model2(model);
model1.stockPrice = model1.stockPrice - h;
model2.stockPrice = model2.stockPrice + h;
MultiStockModel msm1(model1);
MultiStockModel msm2(model2);
return (price(option, msm2) - price(option, msm1)) / (2 * h);
}
double singleThreadedPrice(int taskNumber, int nScenarios, int nSteps,
const ContinuousTimeOption &option,
const MultiStockModel &model) {
if (!option.isPathDependent()) {
nSteps = 1;
}
double total = 0.0;
MultiStockModel subModel = model.getSubmodel(option.getStocks());
long long randSize = subModel.randSize(nScenarios, nSteps);
mt19937 rng;
rng.discard(randSize * taskNumber);
// We price at most one million scenarios at a time to avoid running out of
// memory
int batchSize = 1000000 / nSteps;
if (batchSize <= 0) {
batchSize = 1;
}
int scenariosRemaining = nScenarios;
while (scenariosRemaining > 0) {
int thisBatch = batchSize;
if (scenariosRemaining < batchSize) {
thisBatch = scenariosRemaining;
}
MarketSimulation sim = subModel.generateRiskNeutralPricePaths(
rng, option.getMaturity(), thisBatch, nSteps);
Matrix payoffs = option.payoff(sim);
total += sumCols(payoffs).asScalar();
scenariosRemaining -= thisBatch;
}
double mean = total / nScenarios;
double r = model.getRiskFreeRate();
double T = option.getMaturity() - model.getDate();
return exp(-r * T) * mean;
}
class PriceTask : public Task {
public:
/* Amount of random numbers to skip */
int taskNumber;
int nScenarios, nSteps;
const ContinuousTimeOption &option;
const MultiStockModel &model;
/* Output data */
double result;
PriceTask(int taskNumber, int nScenarios, int nSteps,
const ContinuousTimeOption &option, const MultiStockModel &model)
: taskNumber(taskNumber), nScenarios(nScenarios), nSteps(nSteps),
option(option), model(model) {}
void execute() {
result = singleThreadedPrice(taskNumber, nScenarios, nSteps, option, model);
}
};
/**
* Price the option by Monte Carlo
*/
double MonteCarloPricer::price(const ContinuousTimeOption &option,
const MultiStockModel &model) const {
ASSERT(nTasks >= 1);
vector<shared_ptr<PriceTask>> tasks;
shared_ptr<Executor> executor = Executor::newInstance(nTasks);
for (int i = 0; i < nTasks; i++) {
shared_ptr<PriceTask> task(
new PriceTask(i, nScenarios / nTasks, nSteps, option, model));
tasks.push_back(task);
executor->addTask(task);
}
executor->join();
double total = 0.0;
for (int i = 0; i < nTasks; i++) {
total += tasks[i]->result;
}
return total / nTasks;
}
//////////////////////////////////////
//
// Tests
//
//////////////////////////////////////
static void testPriceCallOption() {
rng("default");
CallOption c;
c.setStrike(110);
c.setMaturity(2);
BlackScholesModel m;
m.volatility = 0.1;
m.riskFreeRate = 0.05;
m.stockPrice = 100.0;
m.drift = 0.1;
m.date = 1;
MultiStockModel msm(m);
MonteCarloPricer pricer;
pricer.nTasks = 1;
double price = pricer.price(c, m);
double expected = c.price(msm);
pricer.nTasks = 10;
double price2 = pricer.price(c, m);
ASSERT_APPROX_EQUAL(price, expected, 0.1);
ASSERT_APPROX_EQUAL(price2, price, 0.000001);
}
void testMonteCarloPricer() { TEST(testPriceCallOption); }