Implementation

Implementing common pricing algorithms in C++.

Carr–Madan FFT Pricing

[1]:
// FFT by Eigen, copied from ds and algo

#pragma cling add_include_path("/srv/conda/envs/notebook/include/eigen3/")

#include <unsupported/Eigen/FFT>
#include <vector>
#include <complex>
#include <iostream>

Eigen::FFT<double> fft;
std::vector<double> a = {0, 1, 2, 3};
std::vector<std::complex<double> > out;

fft.fwd(out, a);

for (auto c : out)
    std::cout << c << ", ";
(6,0), (-2,2), (-2,0), (-2,-2),

Black-Scholes Formula

[1]:
#include <boost/math/distributions/normal.hpp>
#include <cmath>

using namespace boost::math;

normal norm;

double s0 = 100;
double K = 120;
double T = 1;
double r = 0.1;
double s = 0.3;

double d1 = (std::log(s0/K) + (r+0.5*s*s)*T)/(s*std::sqrt(T));
double d2 = d1 - s*std::sqrt(T);

s0*cdf(norm, d1) - K*std::exp(-r*T)*cdf(norm, d2)
[1]:
8.6062921

Gauss Quadrature

[1]:
#include <boost/math/quadrature/gauss.hpp>
#include <cmath>

using namespace boost::math;

auto f = [](const double& t) { return t * t * std::atan(t); };

quadrature::gauss<double, 7>::integrate(f, 0, 1)
[1]:
0.21065725