-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmcMdlBS.h
351 lines (295 loc) · 9.47 KB
/
mcMdlBS.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: AAD and Parallel Simulations
Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
// Black and Scholes model, chapter 6
#pragma once
template <class T>
class BlackScholes : public Model<T>
{
// Model parameters
// Today's spot
// That would be today's linear market in a production system
T mySpot;
// Constant rate and dividend yield
T myRate;
T myDiv;
// Constant vol
T myVol;
// false = risk neutral measure
// true = spot measure
const bool mySpotMeasure;
// Similuation timeline = today + event dates
vector<Time> myTimeline;
// Is today on product timeline?
bool myTodayOnTimeline;
// The pruduct's defline byref
const vector<SampleDef>* myDefline;
// Pre-calculated on initialization
// pre-calculated stds
vector<T> myStds;
// pre-calculated drifts
vector<T> myDrifts;
// pre-caluclated numeraires exp(-r * t)
vector<T> myNumeraires;
// pre-calculated discounts exp(r * (T - t))
vector<vector<T>> myDiscounts;
// forward factors exp((r - d) * (T - t))
vector<vector<T>> myForwardFactors;
// and rates = (exp(r * (T2 - T1)) - 1) / (T2 - T1)
vector<vector<T>> myLibors;
// Exported parameters
vector<T*> myParameters;
vector<string> myParameterLabels;
public:
// Constructor: store data
template <class U>
BlackScholes(
const U spot,
const U vol,
const bool spotMeasure = false,
const U rate = U(0.0),
const U div = U(0.0)) :
mySpot(spot),
myVol(vol),
myRate(rate),
myDiv(div),
mySpotMeasure(spotMeasure),
myParameters(4),
myParameterLabels(4)
{
// Set parameter labels once
myParameterLabels[0] = "spot";
myParameterLabels[1] = "vol";
myParameterLabels[2] = "rate";
myParameterLabels[3] = "div";
setParamPointers();
}
private:
// Must reset on copy
void setParamPointers()
{
myParameters[0] = &mySpot;
myParameters[1] = &myVol;
myParameters[2] = &myRate;
myParameters[3] = &myDiv;
}
public:
// Read access to parameters
T spot() const
{
return mySpot;
}
const T vol() const
{
return myVol;
}
const T rate() const
{
return myRate;
}
const T div() const
{
return myDiv;
}
// Access to all the model parameters
const vector<T*>& parameters() override
{
return myParameters;
}
const vector<string>& parameterLabels() const override
{
return myParameterLabels;
}
// Virtual copy constructor
unique_ptr<Model<T>> clone() const override
{
auto clone = make_unique<BlackScholes<T>>(*this);
clone->setParamPointers();
return clone;
}
// Initialize timeline
void allocate(
const vector<Time>& productTimeline,
const vector<SampleDef>& defline)
override
{
// Simulation timeline = today + product timeline
myTimeline.clear();
myTimeline.push_back(systemTime);
for (const auto& time : productTimeline)
{
if (time > systemTime) myTimeline.push_back(time);
}
// Is today on the timeline?
myTodayOnTimeline = (productTimeline[0] == systemTime);
// Take a reference on the product's defline
myDefline = &defline;
// Allocate the standard devs and drifts
// over simulation timeline
myStds.resize(myTimeline.size() - 1);
myDrifts.resize(myTimeline.size() - 1);
// Allocate the numeraires, discount and forward factors
// over product timeline
const size_t n = productTimeline.size();
myNumeraires.resize(n);
myDiscounts.resize(n);
for (size_t j = 0; j < n; ++j)
{
myDiscounts[j].resize(defline[j].discountMats.size());
}
myForwardFactors.resize(n);
for (size_t j = 0; j < n; ++j)
{
myForwardFactors[j].resize(defline[j].forwardMats.size());
}
myLibors.resize(n);
for (size_t j = 0; j < n; ++j)
{
myLibors[j].resize(defline[j].liborDefs.size());
}
}
void init(
const vector<Time>& productTimeline,
const vector<SampleDef>& defline)
override
{
// Pre-compute the standard devs and drifts over simulation timeline
const T mu = myRate - myDiv;
const size_t n = myTimeline.size() - 1;
for (size_t i = 0; i < n; ++i)
{
const double dt = myTimeline[i + 1] - myTimeline[i];
// Var[logST2 / ST1] = vol^2 * dt
myStds[i] = myVol * sqrt(dt);
if (mySpotMeasure)
{
// under spot measure
// E[logST2 / ST1] = logST1 + ( (r - d) + 0.5 * vol ^ 2 ) * dt
myDrifts[i] = (mu + 0.5*myVol*myVol)*dt;
}
else
{
// under risk neutral measure
// E[logST2 / ST1] = logST1 + ( (r - d) - 0.5 * vol ^ 2 ) * dt
myDrifts[i] = (mu - 0.5*myVol*myVol)*dt;
}
}
// Pre-compute the numeraires, discount and forward factors
// on event dates
const size_t m = productTimeline.size();
for (size_t i = 0; i < m; ++i)
{
// Numeraire
if (defline[i].numeraire)
{
if (mySpotMeasure)
{
// Under the spot measure,
// the numeraire is the spot with reinvested dividend
// num(t) = spot(t) / spot(0) * exp(div * t)
// we precalculate exp(div * t) / spot(0)
myNumeraires[i] = exp(myDiv * productTimeline[i]) / mySpot;
}
else
{
// Under the risk neutral measure,
// numeraire is deterministic in Black-Scholes = exp(rate * t)
myNumeraires[i] = exp(myRate * productTimeline[i]);
}
}
// Discount factors
const size_t pDF = defline[i].discountMats.size();
for (size_t j = 0; j < pDF; ++j)
{
myDiscounts[i][j] =
exp(-myRate * (defline[i].discountMats[j] - productTimeline[i]));
}
// Forward factors
const size_t pFF = defline[i].forwardMats.front().size();
for (size_t j = 0; j < pFF; ++j)
{
myForwardFactors[i][j] =
exp(mu * (defline[i].forwardMats.front()[j] - productTimeline[i]));
}
// Libors
const size_t pL = defline[i].liborDefs.size();
for (size_t j = 0; j < pL; ++j)
{
const double dt
= defline[i].liborDefs[j].end - defline[i].liborDefs[j].start;
myLibors[i][j] = (exp(myRate*dt) - 1.0) / dt;
}
} // loop on event dates
}
// MC Dimension
size_t simDim() const override
{
return myTimeline.size() - 1;
}
private:
// Helper function, fills a Sample given the spot
inline void fillScen(
const size_t idx, // index on product timeline
const T& spot, // spot
Sample<T>& scen, // Sample to fill
const SampleDef& def) // and its definition
const
{
if (def.numeraire)
{
scen.numeraire = myNumeraires[idx];
if (mySpotMeasure) scen.numeraire *= spot;
}
transform(myForwardFactors[idx].begin(), myForwardFactors[idx].end(),
scen.forwards.front().begin(),
[&spot](const T& ff)
{
return spot * ff;
}
);
copy(myDiscounts[idx].begin(), myDiscounts[idx].end(),
scen.discounts.begin());
copy(myLibors[idx].begin(), myLibors[idx].end(),
scen.libors.begin());
}
public:
// Generate one path, consume Gaussian vector
// path must be pre-allocated
// with the same size as the product timeline
void generatePath(
const vector<double>& gaussVec,
Scenario<T>& path)
const override
{
// The starting spot
// We know that today is on the timeline
T spot = mySpot;
// Next index to fill on the product timeline
size_t idx = 0;
// Is today on the product timeline?
if (myTodayOnTimeline)
{
fillScen(idx, spot, path[idx], (*myDefline)[idx]);
++idx;
}
// Iterate through timeline, apply sampling scheme
const size_t n = myTimeline.size() - 1;
for (size_t i = 0; i < n; ++i)
{
// Apply known conditional distributions
// Black-Scholes
spot = spot * exp(myDrifts[i]
+ myStds[i] * gaussVec[i]);
// Store on the path
fillScen(idx, spot, path[idx], (*myDefline)[idx]);
++idx;
}
}
};