Skip to content

Instantly share code, notes, and snippets.

@39mikufans
Last active November 10, 2020 09:48
Show Gist options
  • Save 39mikufans/09fdc3bafab19f4b154cf556a2d8d6a9 to your computer and use it in GitHub Desktop.
Save 39mikufans/09fdc3bafab19f4b154cf556a2d8d6a9 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <tuple>
// Copyright (c) <2020> <TouhouSupport(Ameame)>
// "Anti 996" License Version 1.0 (Draft)
// Permission is hereby granted to any individual or legal entity
// obtaining a copy of this licensed work (including the source code,
// documentation and/or related items, hereinafter collectively referred
// to as the "licensed work"), free of charge, to deal with the licensed
// work for any purpose, including without limitation, the rights to use,
// reproduce, modify, prepare derivative works of, distribute, publish
// and sublicense the licensed work, subject to the following conditions:
// 1. The individual or the legal entity must conspicuously display,
// without modification, this License and the notice on each redistributed
// or derivative copy of the Licensed Work.
// 2. The individual or the legal entity must strictly comply with all
// applicable laws, regulations, rules and standards of the jurisdiction
// relating to labor and employment where the individual is physically
// located or where the individual was born or naturalized; or where the
// legal entity is registered or is operating (whichever is stricter). In
// case that the jurisdiction has no such laws, regulations, rules and
// standards or its laws, regulations, rules and standards are
// unenforceable, the individual or the legal entity are required to
// comply with Core International Labor Standards.
// 3. The individual or the legal entity shall not induce, suggest or force
// its employee(s), whether full-time or part-time, or its independent
// contractor(s), in any methods, to agree in oral or written form, to
// directly or indirectly restrict, weaken or relinquish his or her
// rights or remedies under such laws, regulations, rules and standards
// relating to labor and employment as mentioned above, no matter whether
// such written or oral agreements are enforceable under the laws of the
// said jurisdiction, nor shall such individual or the legal entity
// limit, in any methods, the rights of its employee(s) or independent
// contractor(s) from reporting or complaining to the copyright holder or
// relevant authorities monitoring the compliance of the license about
// its violation(s) of the said license.
// THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
// LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.
// the cumulative normal distribution function
auto CND(double value)
{
return 0.5 * std::erfc(-value * M_SQRT1_2);
}
// Black-Scholes 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 answer is 0.9291 2.8921
}
@39mikufans
Copy link
Author

I should add a custom numeric input function to these codes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment