FTQuant  0.1
LocalVolatility.cpp
Go to the documentation of this file.
1 #include <LocalVolatility.hpp>
3 #include <random>
4 
6  sigma = [](double a, double b) {
7  return 0.;
8  };
9 }
10 
11 std::vector<std::vector<double>> LocalVolatility::generate_paths(int n_paths,
12  double spot,
13  int steps,
14  double T) {
15  std::random_device rd{};
16  std::mt19937 gen(rd());
17 
18  std::vector<std::vector<double>> paths;
19  std::normal_distribution<double> Normal(0, 1);
20  double dt = T / steps;
21  double sqdt = sqrt(dt);
22 
23  for (int i = 0; i < n_paths; ++i) {
24 
25  std::vector<double> path;
26  path.push_back(0);
27  double ti = 0.;
28 
29  for (int j = 1; j <= steps; ++j) {
30  double sigma_val = this->sigma(ti, exp(path[j]));
31  path.push_back(path[j - 1] +
32  (this->r - 0.5 * sigma_val * sigma_val) * dt +
33  sigma_val * sqdt * Normal(gen));
34  ti += dt;
35  path[j - 1] = spot * exp(path[j - 1]);
36  }
37  path[path.size() - 1] = spot * exp(path[path.size() - 1]);
38  paths.push_back(path);
39  }
40  return paths;
41 }
42 
43 int LocalVolatility::calibrate_dupire(std::vector<std::vector<double>>& omega,
44  std::vector<double>& T,
45  std::vector<double>& y, double spot) {
47  w.fit(T, y, omega);
48  double r_ = r;
49 
50  auto glambda = [w, r_, spot](double t, double k) {
51  double y_ = log(k / exp(r_ * t) * spot);
52  double w_ev = w.eval(t, y_);
53  double Dx_w = w.partial_derivative(t, k, 0, 1);
54  double D2x_w = w.partial_derivative(t, k, 0, 2);
55  double Dt_w = w.partial_derivative(t, k, 1, 0);
56 
57  double y_over_w_ev = y_ / w_ev;
58  double denom =
59  1 - y_over_w_ev * Dx_w +
60  0.25 * (-0.25 - 1. / w_ev + y_over_w_ev * y_over_w_ev) * Dx_w * Dx_w +
61  0.5 * D2x_w;
62 
63  return sqrt(Dt_w / denom);
64  };
65  this->sigma = glambda;
66  return 0;
67 }
Contains the definition of the Dupire's local volatility model class.
int calibrate_dupire(std::vector< std::vector< double >> &w, std::vector< double > &T, std::vector< double > &y, double spot)
std::vector< std::vector< double > > generate_paths(int n_paths, double spot, int steps, double T)
Implementation of 2d spline interpolation on a rectangular grid.
void fit(const std::vector< double > &x, const std::vector< double > &y, const std::vector< std::vector< double >> &f)
double partial_derivative(double x_, double y_, int dx, int dy) const
double eval(double x_, double y_) const