辅导C++、辅导C++ barrierCallPayoff程序、讲解C++、讲解C ++ barrierCallPayoff
- 首页 >> C/C++编程#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;
}
}