Last active
August 14, 2018 22:26
-
-
Save mpersano/56b82f9178afc1468d2a1f2a8aebf016 to your computer and use it in GitHub Desktop.
Black-Scholes option pricing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <tuple> | |
// mostly taken from http://www.espenhaug.com/black_scholes.html | |
// the cumulative normal distribution function | |
auto CND(double value) | |
{ | |
return 0.5 * std::erfc(-value * M_SQRT1_2); | |
} | |
// Black-Scholes (1973) formula | |
// S: stock price | |
// X: strike price | |
// T: years to maturity | |
// r: risk-free rate | |
// v: volatility | |
auto BlackScholes(double S, double X, double T, double R, double V) | |
{ | |
const auto sqrtT = std::sqrt(T); | |
const auto d1 = (std::log(S / X) + (R + 0.5 * V * V) * T) / (V * sqrtT); | |
const auto d2 = d1 - V * sqrtT; | |
const auto CNDD1 = CND(d1); | |
const auto CNDD2 = CND(d2); | |
const auto expRT = std::exp(-R * T); | |
const auto call = S * CNDD1 - X * expRT * CNDD2; | |
const auto put = X * expRT * (1.0 - CNDD2) - S * (1.0 - CNDD1); | |
return std::make_tuple(call, put); | |
} | |
#include <iostream> | |
#include <iomanip> | |
int main() | |
{ | |
double call, put; | |
std::tie(call, put) = BlackScholes(10, 12, 1, 0.003, 0.4); | |
std::cout << std::fixed << std::setprecision(4) << call << ' ' << put << '\n'; | |
// expected: 0.9291 2.8921 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment