{ "cells": [ { "cell_type": "markdown", "id": "cooked-faith", "metadata": {}, "source": [ "# Implementation\n", "\n", "Implementing common pricing algorithms in C++." ] }, { "cell_type": "markdown", "id": "objective-chorus", "metadata": {}, "source": [ "## Carr–Madan FFT Pricing" ] }, { "cell_type": "code", "execution_count": 1, "id": "controversial-spirituality", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6,0), (-2,2), (-2,0), (-2,-2), " ] } ], "source": [ "// FFT by Eigen, copied from ds and algo\n", "\n", "#pragma cling add_include_path(\"/srv/conda/envs/notebook/include/eigen3/\")\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "Eigen::FFT fft;\n", "std::vector a = {0, 1, 2, 3};\n", "std::vector > out;\n", "\n", "fft.fwd(out, a);\n", "\n", "for (auto c : out)\n", " std::cout << c << \", \";" ] }, { "cell_type": "markdown", "id": "tracked-clearance", "metadata": {}, "source": [ "## Black-Scholes Formula" ] }, { "cell_type": "code", "execution_count": 1, "id": "underlying-importance", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.6062921" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#include \n", "#include \n", "\n", "using namespace boost::math;\n", "\n", "normal norm;\n", "\n", "double s0 = 100;\n", "double K = 120;\n", "double T = 1;\n", "double r = 0.1;\n", "double s = 0.3;\n", "\n", "double d1 = (std::log(s0/K) + (r+0.5*s*s)*T)/(s*std::sqrt(T));\n", "double d2 = d1 - s*std::sqrt(T);\n", "\n", "s0*cdf(norm, d1) - K*std::exp(-r*T)*cdf(norm, d2)" ] }, { "cell_type": "markdown", "id": "secure-symphony", "metadata": {}, "source": [ "## Gauss Quadrature" ] }, { "cell_type": "code", "execution_count": 1, "id": "daily-harvard", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.21065725" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#include \n", "#include \n", "\n", "using namespace boost::math;\n", "\n", "auto f = [](const double& t) { return t * t * std::atan(t); };\n", "\n", "quadrature::gauss::integrate(f, 0, 1)" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xcpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "17" } }, "nbformat": 4, "nbformat_minor": 5 }