bin_eur_call.cc

来自「Financial Recipes」· CC 代码 · 共 31 行

CC
31
字号
#include <cmath>             // standard mathematical library#include <algorithm>             // defining the max() operator#include <vector>           // STL vector templatesusing namespace std;double option_price_call_european_binomial( double S,     // spot price					    double X,     // exercice price					    double r,     // interest rate					    double sigma, // volatility					    double t,     // time to maturity					    int steps){  // no steps in binomial tree   double R = exp(r*(t/steps));            // interest rate for each step   double Rinv = 1.0/R;                    // inverse of interest rate   double u = exp(sigma*sqrt(t/steps));    // up movement   double uu = u*u;   double d = 1.0/u;   double p_up = (R-d)/(u-d);   double p_down = 1.0-p_up;   vector<double> prices(steps+1);       // price of underlying   prices[0] = S*pow(d, steps);  // fill in the endnodes.   for (int i=1; i<=steps; ++i) prices[i] = uu*prices[i-1];   vector<double> call_values(steps+1);       // value of corresponding call    for (int i=0; i<=steps; ++i) call_values[i] = max(0.0, (prices[i]-X)); // call payoffs at maturity   for (int step=steps-1; step>=0; --step) {      for (int i=0; i<=step; ++i) {	 call_values[i] = (p_up*call_values[i+1]+p_down*call_values[i])*Rinv;      };   };   return call_values[0];};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?