-
Notifications
You must be signed in to change notification settings - Fork 0
/
lattice_utility.h
358 lines (292 loc) · 10.6 KB
/
lattice_utility.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
352
353
354
355
356
357
358
#pragma once
#if !defined(_LATTICE_UTILITY)
#define _LATTICE_UTILITY
#include"lattice_structure.h"
#include"lattice_traits.h"
#include"lattice_types.h"
#include<tuple>
#include<iostream>
namespace lattice_utility {
using lattice_types::LatticeType;
using lattice_traits::PrintTraits;
using lattice_types::DiscountingStyle;
using lattice_types::BarrierType;
// ==============================================================================
// ===================================== Logger =================================
// ==============================================================================
class Logger {
protected:
explicit Logger() {};
void inline what(std::ostream &out, std::string text)const {
out << text;
}
public:
virtual ~Logger() {}
Logger(Logger const &) = delete;
Logger(Logger &&) = delete;
Logger& operator=(Logger const &) = delete;
Logger& operator=(Logger &&) = delete;
static inline Logger& get() {
static Logger logger;
return logger;
}
void inline warning(std::ostream &out, std::string text)const {
this->what(out, std::move(std::string{ "WARNING: " + text }));
}
void inline critical(std::ostream &out, std::string text)const {
this->what(out, std::move(std::string{ "CRITICAL: " + text }));
}
};
// ==============================================================================
// ================================ BarrierComparer =============================
// ==============================================================================
template<typename T>
struct BarrierComparer {
static std::function<bool(T, T)> const comparer(BarrierType BType) {
switch (BType)
{
case lattice_types::BarrierType::DownAndOut:
return [](T stock, T barrier)->bool {
return (stock > barrier);
};
break;
case lattice_types::BarrierType::UpAndIn:
return [](T stock, T barrier)->bool {
return (stock >= barrier);
};
break;
case lattice_types::BarrierType::DownAndIn:
return [](T stock, T barrier)->bool {
return (stock <= barrier);
};
break;
case lattice_types::BarrierType::UpAndOut:
return [](T stock, T barrier)->bool{
return (stock < barrier);
};
break;
}
}
};
// ==============================================================================
// ================================ DermanKaniErgenerAdjuster ===================
// ==============================================================================
template<typename T>
using CheckAdjusterPair = std::pair<std::function<bool(T, T, T, T)>, std::function<T(T, T, T, T, T, T)>>;
template<typename T>
struct DermanKaniErgenerAdjuster {
static CheckAdjusterPair<T> const adjuster(lattice_types::BarrierType BType) {
auto cmp = BarrierComparer<T>::comparer(BType);
switch (BType)
{
case lattice_types::BarrierType::DownAndOut:
case lattice_types::BarrierType::UpAndIn:
{
auto checker = [=](T stock, T stockDown,T stockUp, T barrier)->bool {
return (cmp(stock,barrier) && (stockDown <= barrier));
};
auto adjuster = [](T stock,T stockDown,T stockUp,T barrier,T rebate,T optionPrice)->T {
return (std::max(rebate - optionPrice, optionPrice - rebate) / (stockDown - stock))*(barrier - stock);
};
return std::make_pair(checker, adjuster);
}
break;
case lattice_types::BarrierType::DownAndIn:
case lattice_types::BarrierType::UpAndOut:
{
auto checker = [=](T stock, T stockDown, T stockUp, T barrier)->bool {
return (cmp(stock, barrier) && (stockUp >= barrier));
};
auto adjuster = [](T stock, T stockDown, T stockUp, T barrier, T rebate, T optionPrice)->T {
return (std::max(rebate - optionPrice, optionPrice - rebate) / (stockUp - stock))*(barrier - stock);
};
return std::make_pair(checker, adjuster);
}
break;
}
}
};
// ==============================================================================
// ================================== print =====================================
// ==============================================================================
template<typename LatticeObject,
typename cIter,
typename Traits = PrintTraits<typename LatticeObject::Node_type, LatticeObject::type()>>
void _print_impl(LatticeObject const &lattice, cIter cbegin, cIter cend,std::ostream &out, std::true_type) {
for (auto itr = cbegin; itr != cend; ++itr) {
out << "["
<< Traits::printLine(*itr);
out << "]\n";
}
}
template<typename LatticeObject,
typename cIter,
typename Traits = PrintTraits<typename LatticeObject::Node_type, LatticeObject::type()>>
void _print_impl(LatticeObject const &lattice, cIter cbegin,cIter cend, std::ostream &out, std::false_type) {
std::string bl{ "" };
if (LatticeObject::type() == LatticeType::TwoVariableBinomial)
bl = "\n";
for (auto itr = cbegin; itr != cend; ++itr) {
out << "(" << (*itr).first << "):" << bl << "["
<< Traits::printLine((*itr).second);
out << "]\n";
}
}
template<typename LatticeObject,
typename cIter,
typename Traits = PrintTraits<typename LatticeObject::Node_type, LatticeObject::type()>>
void print(LatticeObject const &lattice, cIter cbegin,cIter cend,std::ostream &out = std::cout) {
_print_impl(lattice, cbegin, cend, out, std::is_integral<typename LatticeObject::TimeAxis_type>());
}
// ==============================================================================
// ========================== DiscountingFactor =================================
// ==============================================================================
template<typename T>
struct DiscountingFactor {
static std::function<T(T, T)> function(DiscountingStyle style) {
if (style == DiscountingStyle::Continuous) {
return [=](T rate, T delta)->T {
return std::exp(-1.0*rate*delta);
};
}
return [=](T rate, T delta)->T {
return (1.0 / (1.0 + rate * delta));
};
}
};
// ==============================================================================
// ======================== DeltaTimeHolder =====================================
// ==============================================================================
template<typename DeltaTime>
struct DeltaTimeHolder {
private:
static auto const _deltaTime_impl(std::size_t idx, DeltaTime const &deltaTime, std::true_type) {
return deltaTime.at(idx);
}
static DeltaTime const _deltaTime_impl(std::size_t idx, DeltaTime const &deltaTime, std::false_type) {
return deltaTime;
}
public:
static auto const deltaTime(std::size_t idx, DeltaTime const &deltaTime) {
return _deltaTime_impl(idx, deltaTime, std::is_compound<DeltaTime>());
}
};
// ==============================================================================
// ===================== RiskFreeRateHolder =====================================
// ==============================================================================
template<typename RiskFreeRate>
struct RiskFreeRateHolder {
private:
static auto const _rate_impl(std::size_t idx, RiskFreeRate const &riskFreeRate, std::true_type) {
return riskFreeRate.at(idx);
}
static auto const _rate_impl(std::size_t idx, RiskFreeRate const &riskFreeRate, std::false_type) {
return riskFreeRate;
}
public:
static auto const rate(std::size_t idx, RiskFreeRate const &riskFreeRate) {
return _rate_impl(idx, riskFreeRate, std::is_compound<RiskFreeRate>());
}
};
// ==============================================================================
// =================================== sign =====================================
// ==============================================================================
template<typename T>
T sign(T x) {
if (x < 0)
return -1.0;
else if (x > 0)
return 1.0;
else
return 0.0;
}
// ==============================================================================
// ================================ probFloorCapper =============================
// ==============================================================================
/* If this function is used it should be logged so one can see when the probability
is not within <0.0,1.0> */
template<typename T>
T probFloorCapper(T x) {
if (x < 0.0 || x > 1.0) {
std::stringstream ss{};
ss << "Probability value " << x << " has been modified to fit <0,1>\n";
Logger::get().critical(std::cout, ss.str());
}
return std::min(1.0, std::max(0.0, x));
}
// ==============================================================================
// ============================= LinearInterpolator =============================
// ==============================================================================
template<typename Container>
class LinearInterpolator{
private:
Container x_;
Container y_;
public:
typedef Container Container_type;
typedef typename Container::value_type Container_value_type;
LinearInterpolator()
:x_(),y_(){}
LinearInterpolator(LinearInterpolator const &other)
:x_(other.x_),y_(other.y_){}
LinearInterpolator(LinearInterpolator &&other)noexcept
:x_(std::move(other.x_)),y_(std::move(other.y_)){}
~LinearInterpolator(){}
LinearInterpolator& operator=(LinearInterpolator const &other){
if (this != &other) {
x_ = other.x_;
y_ = other.y_;
}
return *this;
}
LinearInterpolator& operator=(LinearInterpolator &&other)noexcept {
if (this != &other) {
x_ = std::move(other.x_);
y_ = std::move(other.y_);
}
return *this;
}
void setPoints(Container const &xpoints, Container const &ypoints,bool sortXPoints = false) {
x_ = xpoints;
y_ = ypoints;
if(sortXPoints)
std::sort(x_.begin(), x_.end());
for (std::size_t i = 0; i < x_.size(); ++i) {
for (std::size_t j = 0; j < x_.size(); ++j) {
if (x_[i] == xpoints[j]) {
y_[i] = ypoints[j];
break;
}
}
}
}
Container_value_type const getValue(Container_value_type const &x)const {
Container_value_type x0{}, y0{}, x1{}, y1{};
if (x < x_[0]) {
std::stringstream ss{};
ss << "Lower constant extrapolation for x ( " << x << " ) occured.\n";
Logger::get().warning(std::cout, ss.str());
return y_[0];
}
if ((x > x_[x_.size() - 1])) {
std::stringstream ss{};
ss << "Upper constant extrapolation for x ( " << x << " ) occured.\n";
Logger::get().warning(std::cout, ss.str());
return y_[x_.size() - 1];
}
for (std::size_t i = 0; i < x_.size(); ++i) {
if (x_[i] < x) {
x0 = x_[i];
y0 = y_[i];
}
else if (x_[i] >= x) {
x1 = x_[i];
y1 = y_[i];
break;
}
}
return ((y0*(x - x1) / (x0 - x1)) + (y1 * (x - x0) / (x1 - x0)));
}
};
};
#endif ///_LATTICE_UTILITY