-
Notifications
You must be signed in to change notification settings - Fork 0
/
lattice_calibrator.h
173 lines (126 loc) · 5.66 KB
/
lattice_calibrator.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
#pragma once
#if! defined(_LATTICE_CALIBRATOR)
#define _LATTICE_CALIBRATOR
#include"lattice_types.h"
#include"lattice_macros.h"
#include"lattice_calibrator_results.h"
#include"lattice_calibrator_ir.h"
#include"lattice_calibrator_equity.h"
#include<memory>
namespace lattice_calibrator {
using lattice_types::AssetClass;
using lattice_types::LatticeType;
using lattice_calibrator_results::CalibratorIRResultsPtr;
using lattice_calibrator_results::CalibratorBinomialEquityResultsPtr;
using lattice_calibrator_results::CalibratorTrinomialEquityResultsPtr;
using lattice_calibrator_ir::CalibratorIR;
using lattice_calibrator_equity::CalibratorEquity;
// ==============================================================================
// =============================== Calibrator ===================================
// ==============================================================================
template<LatticeType Type,
AssetClass AClass,
typename TimeAxis,
typename DeltaTime,
typename ...Others>
class Calibrator {};
// ==============================================================================
// ============= Calibrator Partial Specialization for InterestRate =============
// ==============================================================================
template<typename TimeAxis,
typename DeltaTime,
typename DiscountCurve>
class Calibrator<LatticeType::Binomial,AssetClass::InterestRate,
TimeAxis,DeltaTime, DiscountCurve> {
private:
DiscountCurve discountCurve_;
public:
explicit Calibrator(DiscountCurve const &discountCurve)
:discountCurve_{ discountCurve }{}
template<typename LatticeObject,typename Generator>
CalibratorIRResultsPtr<LatticeObject> const
operator()(LatticeObject &rateLattice,
Generator &&generator,
DeltaTime const &deltaTime) const {
LASSERT(rateLattice.type() == generator.latticeType(), "Mismatch between lattice types");
LASSERT(generator.assetClass() == AssetClass::InterestRate, "Mismatch between asset classes");
return CalibratorIR<LatticeType::Binomial, TimeAxis, DeltaTime, DiscountCurve>::
calibrate(rateLattice, std::forward<Generator>(generator), deltaTime, this->discountCurve_);
}
};
template<typename TimeAxis,
typename DeltaTime,
typename DiscountCurve>
class Calibrator<LatticeType::Trinomial, AssetClass::InterestRate,
TimeAxis, DeltaTime, DiscountCurve> {
private:
DiscountCurve discountCurve_;
public:
explicit Calibrator(DiscountCurve const &discountCurve)
:discountCurve_{ discountCurve } {}
template<typename LatticeObject, typename Generator>
CalibratorIRResultsPtr<LatticeObject> const
operator()(LatticeObject &rateLattice,
Generator &&generator,
DeltaTime const &deltaTime) const {
LASSERT(rateLattice.type() == generator.latticeType(), "Mismatch between lattice types");
LASSERT(generator.assetClass() == AssetClass::InterestRate, "Mismatch between asset classes");
return CalibratorIR<LatticeType::Trinomial, TimeAxis, DeltaTime, DiscountCurve>::
calibrate(rateLattice, std::forward<Generator>(generator), deltaTime, this->discountCurve_);
}
};
// ==============================================================================
// ================= Calibrator Partial Specialization for Equity ===============
// ==============================================================================
template<typename TimeAxis,
typename DeltaTime,
typename RiskFreeRate,
typename OptionData>
class Calibrator<LatticeType::Binomial, AssetClass::Equity,
TimeAxis, DeltaTime, RiskFreeRate, OptionData> {
private:
OptionData optionData_;
RiskFreeRate rate_;
public:
explicit Calibrator(OptionData const &optionData,RiskFreeRate const &rate)
:rate_{rate},optionData_{optionData}{}
template<typename LatticeObject>
CalibratorBinomialEquityResultsPtr<LatticeObject> const
operator()(LatticeObject &stockPriceLattice, DeltaTime const &deltaTime,
RiskFreeRate const &riskFreeRate, typename LatticeObject::Node_type const &apexPrice) {
return lattice_calibrator_equity::CalibratorEquity<LatticeType::Binomial, TimeAxis,
DeltaTime, RiskFreeRate, OptionData>::
implyTree(stockPriceLattice, deltaTime, riskFreeRate, apexPrice, this->optionData_);
}
};
template<typename TimeAxis,
typename DeltaTime,
typename RiskFreeRate,
typename OptionData>
class Calibrator<LatticeType::Trinomial, AssetClass::Equity,
TimeAxis, DeltaTime, RiskFreeRate, OptionData> {
private:
OptionData optionData_;
RiskFreeRate rate_;
public:
explicit Calibrator(OptionData const &optionData,RiskFreeRate const &rate)
:rate_{ rate }, optionData_{ optionData } {}
template<typename LatticeObject>
CalibratorTrinomialEquityResultsPtr<LatticeObject> const
operator()(LatticeObject &stockPriceLattice, DeltaTime const &deltaTime,
typename LatticeObject::Node_type const &apexPrice,bool areCallPricesLiquid = true)const {
return lattice_calibrator_equity::CalibratorEquity<LatticeType::Trinomial, TimeAxis,
DeltaTime, RiskFreeRate, OptionData>::
statePriceLattice(stockPriceLattice, deltaTime, apexPrice, this->optionData_, areCallPricesLiquid);
}
template<typename LatticeObject>
CalibratorTrinomialEquityResultsPtr<LatticeObject> const
operator()(LatticeObject const &statePriceLattice, LatticeObject const &stockPriceLattice,
DeltaTime const &deltaTime, RiskFreeRate const &riskFreeRate, bool areCallPricesLiquid = true) {
return lattice_calibrator::CalibratorEquity<LatticeType::Trinomial, TimeAxis,
DeltaTime, RiskFreeRate, OptionData>::
impliedProbability(statePriceLattice, stockPriceLattice, deltaTime, riskFreeRate, areCallPricesLiquid);
}
};
}
#endif ///_LATTICE_CALIBRATOR