前面的推導省略,該公式爲期權定價公式
參數解釋:
r:riskless 利率 ==r
T:期限 ==Expire
S0:起始spot price ==Spot
:Vol ==Vol
K:strike price即成交價 ==Strike
算法核心在於求期望,解決方法爲多次計算f(S)並求其平均值來得到期望。 用變量x來表示每一次實驗N(0,1)的取值,這裏用到Box Muller算法(該算法根據標準正態分佈產生隨機數)。
附核心算法代碼:
*******************************************主過程*****************************************
double SimpleMonteCarlo1(double Expiry,
double Strike,
double Spot,
double Vol,
double r,
unsigned long NumberOfPaths)
{
double variance = Vol*Vol*Expiry;
double rootVariance = sqrt(variance);
double itoCorrection = -0.5*variance;
double movedSpot = Spot*exp(r*Expiry +itoCorrection);
double thisSpot;
double runningSum=0;
for (unsigned long i=0; i < NumberOfPaths; i++)
{
double thisGaussian = GetOneGaussianByBoxMuller();
thisSpot = movedSpot*exp( rootVariance*thisGaussian);
double thisPayoff = thisSpot - Strike;
thisPayoff = thisPayoff >0 ? thisPayoff : 0;
runningSum += thisPayoff;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
return mean;
}
*******************************************Box Muller*********************************************
double GetOneGaussianByBoxMuller()
{
double result;
double x;
double y;
double sizeSquared;
do
{
x = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
y = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
sizeSquared = x*x + y*y;
}
while
( sizeSquared >= 1.0);
result = x*sqrt(-2*log(sizeSquared)/sizeSquared);
return result;
}