-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathql_imps.cpp
202 lines (143 loc) · 7.35 KB
/
ql_imps.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
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
// //
// Dale Roberts <dale.o.roberts@gmail.com>
//
// Call QuantLib implementations
#include <stdio.h>
#include <ql/quantlib.hpp>
#include "ql_imps.h"
using namespace QuantLib;
boost::shared_ptr<YieldTermStructure>
flatRate(const Date& today, const boost::shared_ptr<Quote>& forward, const DayCounter &dc) {
return boost::shared_ptr<YieldTermStructure>(new FlatForward(today, Handle<Quote>(forward),dc));
}
boost::shared_ptr<YieldTermStructure>
flatRate(const boost::shared_ptr<Quote>& forward, const DayCounter &dc) {
return boost::shared_ptr<YieldTermStructure>(new FlatForward(0, NullCalendar(), Handle<Quote>(forward), dc));
}
boost::shared_ptr<YieldTermStructure>
flatRate(Rate forward, const DayCounter &dc) {
return flatRate(boost::shared_ptr<Quote>(new SimpleQuote(forward)), dc);
}
boost::shared_ptr<QuantLib::BlackVolTermStructure>
flatVol(const QuantLib::Date& today, const boost::shared_ptr<Quote>& vol, const QuantLib::DayCounter& dc) {
return boost::shared_ptr<QuantLib::BlackVolTermStructure>(new QuantLib::BlackConstantVol(today, NullCalendar(), Handle<Quote>(vol), dc));
}
boost::shared_ptr<QuantLib::BlackVolTermStructure>
flatVol(const QuantLib::Date& today, Real vol, const DayCounter &dc) {
return flatVol(today, boost::shared_ptr<Quote>(new SimpleQuote(vol)), dc);
}
Real HestonCallAnalytic(Real r, Real rho, Real kappa, Real theta, Real sigma, Real v0, Real S0, Real T, Real K) {
// Square root process
// dv(t) = \kappa (\theta - v(t))dt + \sigma \sqrt{v(t)}dW(t)
// v(0) = v0.
Calendar calendar = NullCalendar();
DayCounter dayCounter = SimpleDayCounter();
Date todaysDate = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = todaysDate;
Date maturityDate = calendar.advance(todaysDate, T*Years);
printf("\nT:%f\n", dayCounter.yearFraction(todaysDate, maturityDate));
Option::Type type(Option::Call);
Rate riskFreeRate = r;
boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(type, K));
boost::shared_ptr<Exercise> europeanExercise(new EuropeanExercise(maturityDate));
VanillaOption option(payoff, europeanExercise);
Handle<Quote> underlyingH(boost::shared_ptr<Quote>(new SimpleQuote(S0)));
Handle<YieldTermStructure> riskFreeTS(flatRate(riskFreeRate, dayCounter));
Handle<YieldTermStructure> dividendTS(flatRate(0, dayCounter));
boost::shared_ptr<HestonProcess> process(new HestonProcess(riskFreeTS, dividendTS, underlyingH, v0, kappa, theta, sigma, rho));
boost::shared_ptr<AnalyticHestonEngine> engine(new AnalyticHestonEngine(boost::shared_ptr<HestonModel>(new HestonModel(process)),64));
option.set#Engine(engine);
return (Real) option.NPV();
}
Real HestonCallMC(Real r, Real rho, Real kappa, Real theta, Real sigma, Real v0, Real S0, Real T, Real K, int discretization, int trials, int steps, int seed) {
HestonProcess::Discretization d;
switch (discretization)
{
case 1:
d = HestonProcess::PartialTruncation;
break;
case 2:
d = HestonProcess::FullTruncation;
break;
case 3:
d = HestonProcess::Reflection;
break;
case 4:
d = HestonProcess::NonCentralChiSquareVariance;
break;
case 5:
d = HestonProcess::QuadraticExponential;
break;
case 6:
d = HestonProcess::QuadraticExponentialMartingale;
break;
}
Calendar calendar = NullCalendar();
DayCounter dayCounter = SimpleDayCounter();
Date todaysDate = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = todaysDate;
Date maturityDate = calendar.advance(todaysDate, T*Years);
Option::Type type(Option::Call);
Rate riskFreeRate = r;
boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(type, K));
boost::shared_ptr<Exercise> europeanExercise(new EuropeanExercise(maturityDate));
VanillaOption option(payoff, europeanExercise);
Handle<Quote> underlyingH(boost::shared_ptr<Quote>(new SimpleQuote(S0)));
Handle<YieldTermStructure> riskFreeTS(flatRate(riskFreeRate, dayCounter));
Handle<YieldTermStructure> dividendTS(flatRate(0, dayCounter));
boost::shared_ptr<HestonProcess> process(new HestonProcess(riskFreeTS, dividendTS, underlyingH, v0, kappa, theta, sigma, rho, d));
// boost::shared_ptr<#Engine> engine =
// MakeMCEuropeanHestonEngine<PseudoRandom>(process)
// .withSteps(10)
// .withAntitheticVariate()
// .withSamples(5000)
// .withSeed(1234);
boost::shared_ptr<#Engine> engine =
MakeMCEuropeanHestonEngine<PseudoRandom>(process)
.withSteps(steps)
.withSamples(trials)
.withSeed(seed);
option.set#Engine(engine);
return (Real) option.NPV();
}
Real BlackScholesCall(Real r, Real sigma, Real S0, Real T, Real K) {
Calendar calendar = NullCalendar();
DayCounter dayCounter = SimpleDayCounter();
Date todaysDate = calendar.adjust(Date::todaysDate());
Date maturityDate = calendar.advance(todaysDate, T*Years);
Settings::instance().evaluationDate() = todaysDate;
Option::Type type(Option::Call);
boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(type, K));
boost::shared_ptr<Exercise> europeanExercise(new EuropeanExercise(maturityDate));
VanillaOption option(payoff, europeanExercise);
Handle<Quote> S0H(boost::shared_ptr<Quote>(new SimpleQuote(S0)));
Handle<BlackVolTermStructure> volatilityTS(flatVol(todaysDate, sigma, dayCounter));
Handle<YieldTermStructure> riskFreeTS(flatRate(r, dayCounter));
Handle<YieldTermStructure> dividendTS(flatRate(0, dayCounter));
boost::shared_ptr<BlackScholesMertonProcess> process(new BlackScholesMertonProcess(S0H, dividendTS, riskFreeTS, volatilityTS));
boost::shared_ptr<#Engine> engine(new AnalyticEuropeanEngine(process));
option.set#Engine(engine);
return (Real) option.NPV();
}
Real HestonFdBarrierDownOutCall(Real r, Real rho, Real kappa, Real theta, Real sigma, Real v0, Real S0, Real T, Real K, Real H)
{
Calendar calendar = NullCalendar();
DayCounter dayCounter = SimpleDayCounter();
Date todaysDate = calendar.adjust(Date::todaysDate());
Settings::instance().evaluationDate() = todaysDate;
Date maturityDate = calendar.advance(todaysDate, T*Years);
Option::Type type(Option::Call);
Rate riskFreeRate = r;
boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(type, K));
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(maturityDate));
Handle<Quote> underlyingH(boost::shared_ptr<Quote>(new SimpleQuote(S0)));
Handle<YieldTermStructure> riskFreeTS(flatRate(riskFreeRate, dayCounter));
Handle<YieldTermStructure> dividendTS(flatRate(0, dayCounter));
boost::shared_ptr<HestonProcess> process(new HestonProcess(riskFreeTS, dividendTS, underlyingH, v0, kappa, theta, sigma, rho));
boost::shared_ptr<#Engine> engine(new FdHestonBarrierEngine(
boost::shared_ptr<HestonModel>(new HestonModel(process)),
200,400,100));
BarrierOption option(Barrier::DownOut, H, 0.0, payoff, exercise);
option.set#Engine(engine);
return (Real) option.NPV();
}