]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/example/autodiff_black_scholes_brief.cpp
1 // Copyright Matthew Pulver 2018 - 2019.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // https://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/math/differentiation/autodiff.hpp>
10 using namespace boost::math::constants
;
11 using namespace boost::math::differentiation
;
13 // Equations and function/variable names are from
14 // https://en.wikipedia.org/wiki/Greeks_(finance)#Formulas_for_European_option_Greeks
16 // Standard normal cumulative distribution function
19 return 0.5 * erfc(-one_div_root_two
<X
>() * x
);
22 enum class CP
{ call
, put
};
24 // Assume zero annual dividend yield (q=0).
25 template <typename Price
, typename Sigma
, typename Tau
, typename Rate
>
26 promote
<Price
, Sigma
, Tau
, Rate
> black_scholes_option_price(CP cp
,
33 auto const d1
= (log(S
/ K
) + (r
+ sigma
* sigma
/ 2) * tau
) / (sigma
* sqrt(tau
));
34 auto const d2
= (log(S
/ K
) + (r
- sigma
* sigma
/ 2) * tau
) / (sigma
* sqrt(tau
));
37 return S
* Phi(d1
) - exp(-r
* tau
) * K
* Phi(d2
);
39 return exp(-r
* tau
) * K
* Phi(-d2
) - S
* Phi(-d1
);
41 throw std::runtime_error("Invalid CP value.");
46 double const K
= 100.0; // Strike price.
47 auto const S
= make_fvar
<double, 2>(105); // Stock price.
48 double const sigma
= 5; // Volatility.
49 double const tau
= 30.0 / 365; // Time to expiration in years. (30 days).
50 double const r
= 1.25 / 100; // Interest rate.
51 auto const call_price
= black_scholes_option_price(CP::call
, K
, S
, sigma
, tau
, r
);
52 auto const put_price
= black_scholes_option_price(CP::put
, K
, S
, sigma
, tau
, r
);
54 std::cout
<< "black-scholes call price = " << call_price
.derivative(0) << '\n'
55 << "black-scholes put price = " << put_price
.derivative(0) << '\n'
56 << "call delta = " << call_price
.derivative(1) << '\n'
57 << "put delta = " << put_price
.derivative(1) << '\n'
58 << "call gamma = " << call_price
.derivative(2) << '\n'
59 << "put gamma = " << put_price
.derivative(2) << '\n';
64 black-scholes call price = 56.5136
65 black-scholes put price = 51.4109
68 call gamma = 0.00199852
69 put gamma = 0.00199852