#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <fstream>
#include <random>
#include <algorithm>
using namespace std;
double barrierCallPayoff(double S0, double T, double sigma, double r, double d, int M, int N, double B)
{
// declare a random number generator
// here we choose the merton twister engine(32bit)
static mt19937 rng;
normal_distribution<> ND(0., 1.);
double sum = 0.;
double dt = T / M; //step size of path
for (int n = 0; n < N; n++)
{
vector<double> stockPath(M + 1);
stockPath[0] = S0;
// create a path with the initial value
for (int i = 1; i <= M; i++)
{
double phi = ND(rng);
stockPath[i] = stockPath[i - 1] * exp((r - d - 0.5*sigma*sigma)*dt + phi * sigma*sqrt(dt));
}
// plot it
double X = 100;
double A = S0;
double ST = stockPath[M];
for (int i = 0; i <= M; i++)
{
A = min(stockPath[i], A);
}
if (A < B)
{
sum = sum + 0.;
}
else sum = sum + max(ST - X, 0.);
}
return sum / N * exp(-r * T);
}
int main()
{
for (int b = 85;b <= 99;b++)
{
double T = 0.2;
double sigma = 0.30;
double r = 0.10;
double d = 0.0;
double S0 = 100;
int M = 50;
int N = 100000;
double B = b;
cout << barrierCallPayoff(S0, T, sigma, r, d, M, N, B) << endl;
}
}
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。