📄 mcbystratify.h
字号:
#define MCbyStratify_H
class MCbyStratify
{
public:
MCbyStratify(double Expiry,double Strike,double Spot,double Vol,double rate,double d, double PathNumber)
{
T=Expiry; // time to maturity
K=Strike; // strike price
S=Spot; // spot price
vol=Vol; // volatility
r=rate; // interest rate
div=d; // dividend
NumberOfPaths=PathNumber;
}
void StratifyPricing(
double NumStra,
double NumDates,
double& VanillaCallPrice,
double& GeometricAsianCallPrice,
double& ArithmeticAsianCallPrice
)
{
double callsum =0;
double GeometricCallSum=0;
double ArithmeticCallSum=0;
double stockK; // The simulated stock price at maturity
double rootT = sqrt(T);
// Calculate the fixed part of the price.
double base = S*exp(r-div - vol*vol/2)* T;
BrownianBridgePath Path1(NumStra,NumDates,NumberOfPaths);
Path1.GetVanillaPath();
//ofstream outfile("ddd.txt");
//for(int i=1;i<=NumberOfPaths;i++)
// outfile<<Path1.getW(i,1)<<endl;
BrownianBridgePath Path2(NumStra,NumDates,NumberOfPaths);
Path2.GetAsianPath();
//ofstream outfile1("ddd1.txt");
//for(int i=1;i<=NumberOfPaths;i++)
//outfile1<<Path2.getW(i,1)<<'\t'<<Path2.getW(i,2)<<'\t'<<Path2.getW(i,3)<<'\t'<<Path2.getW(i,4)<<'\t'<<Path2.getW(i,5)<<'\t'<<Path2.getW(i,6)<<endl;
for (unsigned long i=1; i <= NumberOfPaths; i++)
{
double tGaussian = Path1.getW(i,1);
stockK =S* exp((r-div-vol*vol/2.0)*T+ vol*tGaussian);
double Gaussian[7];
for(int j=1;j<=6;j++)
Gaussian[j] = Path2.getW(i,j);
double s[7];s[0]=100;
double TT;
double SumGeometric=1;
double SumArithmetic=0;
//ofstream outfile3("aaa.txt",ios::app);
//outfile3<<stockK<<endl;
for(int k=1;k<=6;k++)
{
TT=T*k/6.0;
rootT = sqrt(TT);
s[k] = s[0]* exp((r-div-vol*vol/2.0)*TT+ vol*Gaussian[k]);
SumGeometric*=s[k];
SumArithmetic+=s[k];
}
double GeometricMean=pow(SumGeometric,1.0/6);
double ArithmeticMean=SumArithmetic/6.0;
double tPayoff = stockK - K;
double GeometricAsianPayoff=GeometricMean-K;
double ArithmeticAsianPayoff=ArithmeticMean-K;
if (tPayoff > 0){
// We will exercise the call option
callsum += tPayoff;
}
if (GeometricAsianPayoff> 0){
// We will exercise the call option
GeometricCallSum += GeometricAsianPayoff;
}
if (ArithmeticAsianPayoff > 0){
// We will exercise the call option
ArithmeticCallSum += ArithmeticAsianPayoff;
}
}
VanillaCallPrice = callsum / NumberOfPaths * exp(-r*T) ;
GeometricAsianCallPrice=GeometricCallSum/NumberOfPaths * exp(-r*T) ;
ArithmeticAsianCallPrice=ArithmeticCallSum/NumberOfPaths * exp(-r*T) ;
}
private:
double T; // time to maturity
double K; // strike price
double S; // spot price
double vol; // volatility
double r; // interest rate
double div; // dividend
unsigned long NumberOfPaths;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -