autodiff.qbk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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)]
  5. [section:autodiff Automatic Differentiation]
  6. [template autodiff_equation[name] '''<inlinemediaobject><imageobject><imagedata fileref="../equations/autodiff/'''[name]'''"></imagedata></imageobject></inlinemediaobject>''']
  7. [h1:synopsis Synopsis]
  8. #include <boost/math/differentiation/autodiff.hpp>
  9. namespace boost {
  10. namespace math {
  11. namespace differentiation {
  12. // Function returning a single variable of differentiation. Recommended: Use auto for type.
  13. template <typename RealType, size_t Order, size_t... Orders>
  14. autodiff_fvar<RealType, Order, Orders...> make_fvar(RealType const& ca);
  15. // Function returning multiple independent variables of differentiation in a std::tuple.
  16. template<typename RealType, size_t... Orders, typename... RealTypes>
  17. auto make_ftuple(RealTypes const&... ca);
  18. // Type of combined autodiff types. Recommended: Use auto for return type (C++14).
  19. template <typename RealType, typename... RealTypes>
  20. using promote = typename detail::promote_args_n<RealType, RealTypes...>::type;
  21. namespace detail {
  22. // Single autodiff variable. Use make_fvar() or make_ftuple() to instantiate.
  23. template <typename RealType, size_t Order>
  24. class fvar {
  25. public:
  26. // Query return value of function to get the derivatives.
  27. template <typename... Orders>
  28. get_type_at<RealType, sizeof...(Orders) - 1> derivative(Orders... orders) const;
  29. // All of the arithmetic and comparison operators are overloaded.
  30. template <typename RealType2, size_t Order2>
  31. fvar& operator+=(fvar<RealType2, Order2> const&);
  32. fvar& operator+=(root_type const&);
  33. // ...
  34. };
  35. // Standard math functions are overloaded and called via argument-dependent lookup (ADL).
  36. template <typename RealType, size_t Order>
  37. fvar<RealType, Order> floor(fvar<RealType, Order> const&);
  38. template <typename RealType, size_t Order>
  39. fvar<RealType, Order> exp(fvar<RealType, Order> const&);
  40. // ...
  41. } // namespace detail
  42. } // namespace differentiation
  43. } // namespace math
  44. } // namespace boost
  45. [h1:description Description]
  46. Autodiff is a header-only C++ library that facilitates the [@https://en.wikipedia.org/wiki/Automatic_differentiation
  47. automatic differentiation] (forward mode) of mathematical functions of single and multiple variables.
  48. This implementation is based upon the [@https://en.wikipedia.org/wiki/Taylor_series Taylor series] expansion of
  49. an analytic function /f/ at the point ['x[sub 0]]:
  50. [/ Thanks to http://www.tlhiv.org/ltxpreview/ for LaTeX-to-SVG conversions. ]
  51. [/ \Large\begin{align*}
  52. f(x_0+\varepsilon) &= f(x_0) + f'(x_0)\varepsilon + \frac{f''(x_0)}{2!}\varepsilon^2 + \frac{f'''(x_0)}{3!}\varepsilon^3 + \cdots \\
  53. &= \sum_{n=0}^N\frac{f^{(n)}(x_0)}{n!}\varepsilon^n + O\left(\varepsilon^{N+1}\right).
  54. \end{align*} ]
  55. [:[:[autodiff_equation taylor_series.svg]]]
  56. The essential idea of autodiff is the substitution of numbers with polynomials in the evaluation of ['f(x[sub 0])].
  57. By substituting the number ['x[sub 0]] with the first-order polynomial ['x[sub 0]+\u03b5], and using the same
  58. algorithm to compute ['f(x[sub 0]+\u03b5)], the resulting polynomial in ['\u03b5] contains the function's derivatives
  59. ['f'(x[sub 0])], ['f''(x[sub 0])], ['f\'\'\'(x[sub 0])], ... within the coefficients. Each coefficient is equal to the
  60. derivative of its respective order, divided by the factorial of the order.
  61. In greater detail, assume one is interested in calculating the first /N/ derivatives of /f/ at ['x[sub 0]]. Without
  62. loss of precision to the calculation of the derivatives, all terms ['O(\u03b5[super N+1])] that include powers
  63. of ['\u03b5] greater than /N/ can be discarded. (This is due to the fact that each term in a polynomial depends
  64. only upon equal and lower-order terms under arithmetic operations.) Under these truncation rules, /f/ provides a
  65. polynomial-to-polynomial transformation:
  66. [/ \Large$$f \qquad : \qquad x_0+\varepsilon \qquad \mapsto \qquad
  67. \sum_{n=0}^Ny_n\varepsilon^n=\sum_{n=0}^N\frac{f^{(n)}(x_0)}{n!}\varepsilon^n.$$ ]
  68. [:[:[autodiff_equation polynomial_transform.svg]]]
  69. C++'s ability to overload operators and functions allows for the creation of a class `fvar` ([_f]orward-mode autodiff
  70. [_var]iable) that represents polynomials in ['\u03b5]. Thus the same algorithm /f/ that calculates the numeric value
  71. of ['y[sub 0]=f(x[sub 0])], when written to accept and return variables of a generic (template) type, is also used
  72. to calculate the polynomial ['\u03a3[sub n]y[sub n]\u03b5[super n]=f(x[sub 0]+\u03b5)]. The derivatives
  73. ['f[super (n)](x[sub 0])] are then found from the product of the respective factorial ['n!] and coefficient
  74. ['y[sub n]]:
  75. [/ \Large$$\frac{d^nf}{dx^n}(x_0)=n!y_n.$$ ]
  76. [:[:[autodiff_equation derivative_formula.svg]]]
  77. [h1:examples Examples]
  78. [h2:example-single-variable Example 1: Single-variable derivatives]
  79. [h3 Calculate derivatives of ['f(x)=x[super 4]] at /x/=2.]
  80. In this example, `make_fvar<double, Order>(2.0)` instantiates the polynomial 2+['\u03b5]. The `Order=5`
  81. means that enough space is allocated (on the stack) to hold a polynomial of up to degree 5 during the proceeding
  82. computation.
  83. Internally, this is modeled by a `std::array<double,6>` whose elements `{2, 1, 0, 0, 0, 0}` correspond
  84. to the 6 coefficients of the polynomial upon initialization. Its fourth power, at the end of the computation, is
  85. a polynomial with coefficients `y = {16, 32, 24, 8, 1, 0}`. The derivatives are obtained using the formula
  86. ['f[super (n)](2)=n!*y[n]].
  87. #include <boost/math/differentiation/autodiff.hpp>
  88. #include <iostream>
  89. template <typename T>
  90. T fourth_power(T const& x) {
  91. T x4 = x * x; // retval in operator*() uses x4's memory via NRVO.
  92. x4 *= x4; // No copies of x4 are made within operator*=() even when squaring.
  93. return x4; // x4 uses y's memory in main() via NRVO.
  94. }
  95. int main() {
  96. using namespace boost::math::differentiation;
  97. constexpr unsigned Order = 5; // Highest order derivative to be calculated.
  98. auto const x = make_fvar<double, Order>(2.0); // Find derivatives at x=2.
  99. auto const y = fourth_power(x);
  100. for (unsigned i = 0; i <= Order; ++i)
  101. std::cout << "y.derivative(" << i << ") = " << y.derivative(i) << std::endl;
  102. return 0;
  103. }
  104. /*
  105. Output:
  106. y.derivative(0) = 16
  107. y.derivative(1) = 32
  108. y.derivative(2) = 48
  109. y.derivative(3) = 48
  110. y.derivative(4) = 24
  111. y.derivative(5) = 0
  112. */
  113. The above calculates
  114. [/ \Large\begin{alignat*}{3}
  115. {\tt y.derivative(0)} &=& f(2) =&& \left.x^4\right|_{x=2} &= 16\\
  116. {\tt y.derivative(1)} &=& f'(2) =&& \left.4\cdot x^3\right|_{x=2} &= 32\\
  117. {\tt y.derivative(2)} &=& f''(2) =&& \left.4\cdot 3\cdot x^2\right|_{x=2} &= 48\\
  118. {\tt y.derivative(3)} &=& f'''(2) =&& \left.4\cdot 3\cdot2\cdot x\right|_{x=2} &= 48\\
  119. {\tt y.derivative(4)} &=& f^{(4)}(2) =&& 4\cdot 3\cdot2\cdot1 &= 24\\
  120. {\tt y.derivative(5)} &=& f^{(5)}(2) =&& 0 &
  121. \end{alignat*} ]
  122. [:[:[autodiff_equation example1.svg]]]
  123. [h2:example-multiprecision
  124. Example 2: Multi-variable mixed partial derivatives with multi-precision data type]
  125. [/ \Large$\frac{\partial^{12}f}{\partial w^{3}\partial x^{2}\partial y^{4}\partial z^{3}}(11,12,13,14)$]
  126. [/ \Large$f(w,x,y,z)=\exp\left(w\sin\left(\frac{x\log(y)}{z}\right)+\sqrt{\frac{wz}{xy}}\right)+\frac{w^2}{\tan(z)}$]
  127. [h3 Calculate [autodiff_equation mixed12.svg] with a precision of about 50 decimal digits,
  128. where [autodiff_equation example2f.svg].]
  129. In this example, `make_ftuple<float50, Nw, Nx, Ny, Nz>(11, 12, 13, 14)` returns a `std::tuple` of 4
  130. independent `fvar` variables, with values of 11, 12, 13, and 14, for which the maximum order derivative to
  131. be calculated for each are 3, 2, 4, 3, respectively. The order of the variables is important, as it is the same
  132. order used when calling `v.derivative(Nw, Nx, Ny, Nz)` in the example below.
  133. #include <boost/math/differentiation/autodiff.hpp>
  134. #include <boost/multiprecision/cpp_bin_float.hpp>
  135. #include <iostream>
  136. using namespace boost::math::differentiation;
  137. template <typename W, typename X, typename Y, typename Z>
  138. promote<W, X, Y, Z> f(const W& w, const X& x, const Y& y, const Z& z) {
  139. using namespace std;
  140. return exp(w * sin(x * log(y) / z) + sqrt(w * z / (x * y))) + w * w / tan(z);
  141. }
  142. int main() {
  143. using float50 = boost::multiprecision::cpp_bin_float_50;
  144. constexpr unsigned Nw = 3; // Max order of derivative to calculate for w
  145. constexpr unsigned Nx = 2; // Max order of derivative to calculate for x
  146. constexpr unsigned Ny = 4; // Max order of derivative to calculate for y
  147. constexpr unsigned Nz = 3; // Max order of derivative to calculate for z
  148. // Declare 4 independent variables together into a std::tuple.
  149. auto const variables = make_ftuple<float50, Nw, Nx, Ny, Nz>(11, 12, 13, 14);
  150. auto const& w = std::get<0>(variables); // Up to Nw derivatives at w=11
  151. auto const& x = std::get<1>(variables); // Up to Nx derivatives at x=12
  152. auto const& y = std::get<2>(variables); // Up to Ny derivatives at y=13
  153. auto const& z = std::get<3>(variables); // Up to Nz derivatives at z=14
  154. auto const v = f(w, x, y, z);
  155. // Calculated from Mathematica symbolic differentiation.
  156. float50 const answer("1976.319600747797717779881875290418720908121189218755");
  157. std::cout << std::setprecision(std::numeric_limits<float50>::digits10)
  158. << "mathematica : " << answer << '\n'
  159. << "autodiff : " << v.derivative(Nw, Nx, Ny, Nz) << '\n'
  160. << std::setprecision(3)
  161. << "relative error: " << (v.derivative(Nw, Nx, Ny, Nz) / answer - 1) << '\n';
  162. return 0;
  163. }
  164. /*
  165. Output:
  166. mathematica : 1976.3196007477977177798818752904187209081211892188
  167. autodiff : 1976.3196007477977177798818752904187209081211892188
  168. relative error: 2.67e-50
  169. */
  170. [h2:example-black_scholes
  171. Example 3: Black-Scholes Option Pricing with Greeks Automatically Calculated]
  172. [h3 Calculate greeks directly from the Black-Scholes pricing function.]
  173. Below is the standard Black-Scholes pricing function written as a function template, where the price, volatility
  174. (sigma), time to expiration (tau) and interest rate are template parameters. This means that any greek based on
  175. these 4 variables can be calculated using autodiff. The below example calculates delta and gamma where the variable
  176. of differentiation is only the price. For examples of more exotic greeks, see `example/black_scholes.cpp`.
  177. ```
  178. #include <boost/math/differentiation/autodiff.hpp>
  179. #include <iostream>
  180. using namespace boost::math::constants;
  181. using namespace boost::math::differentiation;
  182. // Equations and function/variable names are from
  183. // https://en.wikipedia.org/wiki/Greeks_(finance)#Formulas_for_European_option_Greeks
  184. // Standard normal cumulative distribution function
  185. template <typename X>
  186. X Phi(X const& x) {
  187. return 0.5 * erfc(-one_div_root_two<X>() * x);
  188. }
  189. enum class CP { call, put };
  190. // Assume zero annual dividend yield (q=0).
  191. template <typename Price, typename Sigma, typename Tau, typename Rate>
  192. promote<Price, Sigma, Tau, Rate> black_scholes_option_price(CP cp,
  193. double K,
  194. Price const& S,
  195. Sigma const& sigma,
  196. Tau const& tau,
  197. Rate const& r) {
  198. using namespace std;
  199. auto const d1 = (log(S / K) + (r + sigma * sigma / 2) * tau) / (sigma * sqrt(tau));
  200. auto const d2 = (log(S / K) + (r - sigma * sigma / 2) * tau) / (sigma * sqrt(tau));
  201. switch (cp) {
  202. case CP::call:
  203. return S * Phi(d1) - exp(-r * tau) * K * Phi(d2);
  204. case CP::put:
  205. return exp(-r * tau) * K * Phi(-d2) - S * Phi(-d1);
  206. }
  207. }
  208. int main() {
  209. double const K = 100.0; // Strike price.
  210. auto const S = make_fvar<double, 2>(105); // Stock price.
  211. double const sigma = 5; // Volatility.
  212. double const tau = 30.0 / 365; // Time to expiration in years. (30 days).
  213. double const r = 1.25 / 100; // Interest rate.
  214. auto const call_price = black_scholes_option_price(CP::call, K, S, sigma, tau, r);
  215. auto const put_price = black_scholes_option_price(CP::put, K, S, sigma, tau, r);
  216. std::cout << "black-scholes call price = " << call_price.derivative(0) << '\n'
  217. << "black-scholes put price = " << put_price.derivative(0) << '\n'
  218. << "call delta = " << call_price.derivative(1) << '\n'
  219. << "put delta = " << put_price.derivative(1) << '\n'
  220. << "call gamma = " << call_price.derivative(2) << '\n'
  221. << "put gamma = " << put_price.derivative(2) << '\n';
  222. return 0;
  223. }
  224. /*
  225. Output:
  226. black-scholes call price = 56.5136
  227. black-scholes put price = 51.4109
  228. call delta = 0.773818
  229. put delta = -0.226182
  230. call gamma = 0.00199852
  231. put gamma = 0.00199852
  232. */
  233. ```
  234. [h1 Advantages of Automatic Differentiation]
  235. The above examples illustrate some of the advantages of using autodiff:
  236. * Elimination of code redundancy. The existence of /N/ separate functions to calculate derivatives is a form
  237. of code redundancy, with all the liabilities that come with it:
  238. * Changes to one function require /N/ additional changes to other functions. In the 3rd example above,
  239. consider how much larger and inter-dependent the above code base would be if a separate function were
  240. written for [@https://en.wikipedia.org/wiki/Greeks_(finance)#Formulas_for_European_option_Greeks
  241. each Greek] value.
  242. * Dependencies upon a derivative function for a different purpose will break when changes are made to
  243. the original function. What doesn't need to exist cannot break.
  244. * Code bloat, reducing conceptual integrity. Control over the evolution of code is easier/safer when
  245. the code base is smaller and able to be intuitively grasped.
  246. * Accuracy of derivatives over finite difference methods. Single-iteration finite difference methods always include
  247. a ['\u0394x] free variable that must be carefully chosen for each application. If ['\u0394x] is too small, then
  248. numerical errors become large. If ['\u0394x] is too large, then mathematical errors become large. With autodiff,
  249. there are no free variables to set and the accuracy of the answer is generally superior to finite difference
  250. methods even with the best choice of ['\u0394x].
  251. [h1 Manual]
  252. Additional details are in the [@../differentiation/autodiff.pdf autodiff manual].
  253. [endsect]