#include <cmath> // mathematical C library
using namespace std;
double N(const double& z); // declare cumulative distribution function.
//欧式看涨期权:
double option_price_call_black_scholes(const double& S,
const double& K,
const double& r,
const double& sigma,
const double& time) // time to maturity
{
double time_sqrt = sqrt(time);
double d1 = (log(S/K)+r*time)/(sigma*time_sqrt)+0.5*sigma*time_sqrt;
double d2 = d1-sigma*time_sqrt;
return S*N(d1)-K*exp(-r*time)*N(d2);
};
//欧式看跌期权:
double option_price_put_black_scholes(const double& S,
const double& K,
const double& r,
const double& sigma,
const double& time) // time to maturity
{
double time_sqrt = sqrt(time);
double d1 = (log(S/K)+r*time)/(sigma*time_sqrt)+0.5*sigma*time_sqrt;
double d2 = d1-sigma*time_sqrt;
return K*exp(-r*time)*N(-d2)-S*N(-d1);
};
//累积概率函数(标准正态分布)
double N(const double& z) {
if (z > 6.0) { return 1.0; }; // this guards against overflow
if (z < -6.0) { return 0.0; };