Hello,
I was trying to figure out my problem alone, but looks like I am not able to do it, so..
My code is supposed to be Monte Carlo (MC) simulation to evaluate option price obtained with Heston model.
I have the original code in VBA, but was trying to translate it into C++ to see how much faster it could be.
In order to generate random variable, the Box Muller algorithme is used.
Here is the code of MC function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
double heston (double kappa,double theta,double lambda,double rho, double sigmav,double startS, double r, double startv, double strike, int ITER, int daynum)
{
srand(time(0));
double sum=0;
vector<double> Q(ITER*daynum);
vector<double> payoffs(ITER);
Q.clear();
payoffs.clear();
double deltat=1/365;
for ( int cycles=0; cycles< ITER; cycles++)
{ double lnSt=log(startS);
double lnvt=log(startv);
double curv=startv;
double curS=startS;
for ( int days=0; days<daynum; days++)
{
double box1=boxmuller();
double box2=boxmuller();
double ev= rho*box1+sqrt(1-rho*rho)*box2;
lnSt=lnSt+(r-curv/2)/365+sqrt(curv)*sqrt(deltat)*box2;
curS=exp(lnSt);
//next line is the problem: it's supposed to change from one days to an other, but it remains the same again and again, because the right part of the equation is equal to -zero
//with figures
// ln(0.2)=ln(0.2)+(2*(0.01-0.2)-0*0.2-0.1/2)/365+0.1*(1/sqrt(0.2))*sqrt(1/365)*ev
lnvt=lnvt+(kappa*(theta-curv)-lambda*curv-sigmav/2)*deltat+sigmav*(1/sqrt(curv))*sqrt(deltat)*ev;
curv=exp(lnvt);
Q.push_back(curS);
if (days==(daynum-1)) payoffs.push_back(std::max(Q[days]-strike,0.0));
}
sum=accumulate(payoffs.begin(), payoffs.end(),0.0);
}
return exp((-daynum / 365) * r)*sum/ITER;
}
|
the right part of the expression
sigmav*(1/sqrt(curv))*sqrt(deltat)*ev is equal to -0. of course it's not supposed to. Moreover, after cheking, the "ev" variable in the right part is NOT zero, so the problem doesn't come from here...
But cannot understand from where.. May be I am not looking in the right place?
Here is Box Muller for those who want to test, and the parameters for the MC function are heston(2,0.01,0,0,0.1,100,0.02,0.2,90,2,10);
1 2 3 4 5 6 7 8 9 10 11 12
|
double boxmuller ()
{
double x,y,xysquare, result;
do{
x = 2.0*rand() / static_cast<double>(RAND_MAX)-1;
y = 2.0*rand() / static_cast<double>(RAND_MAX)-1;
xysquare = x*x + y*y;
} while (xysquare >= 1.0);
result = x*sqrt(-2 * log(xysquare) / xysquare);
return result;
}
|