-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heston.cpp
39 lines (34 loc) · 1.24 KB
/
Heston.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
//
// Heston.cpp
// HandIn1
//
// Created by Mikkel Larsen on 13/09/2020.
// Copyright © 2020 Mikkel Larsen. All rights reserved.
//
#include "Heston.hpp"
#include <iostream>
Heston::Heston(double spot_, double rate_, double kappa_, double theta_, double sigma_, double sigma0_, double rho_) :
spot(spot_), rate(rate_), kappa(kappa_), theta(theta_), sigma(sigma_), sigma0(sigma0_), rho(rho_) {}
double Heston::genPath(std::vector<double> &n, unsigned int T) const {
double s = spot;
unsigned size = (int)n.size();
double dt = 2*T/(double)size;
double v = sigma0;
for (int i = 0; i != size/2; ++i) {
s += s*rate*dt + s*sqrt(max0(v)*dt)*n[i];
v += kappa*(theta-max0(v))*dt + sigma*sqrt(max0(v)*dt)*(n[i]*rho+sqrt(1-pow(rho, 2))*n[i+size/2]);
}
return s;
}
double Heston::getRate() const {
return rate;
}
double monteCarlo(Heston& heston, CallOption& call, RndDevice& rndDevice, unsigned steps, unsigned paths){
double payoff = 0;
std::vector<double> n(steps*2, 0);
for (int i = 0; i != paths; ++i) {
rndDevice.genNormal(n);
payoff += fmax(heston.genPath(n, call.getExpiry()) - call.getStrike(), 0);
}
return exp(-heston.getRate()*call.getExpiry()) * payoff / (double)paths;
}