//MonteClass.cpp:implementation of the CMonteClass class
//
////////////////////////////////////////////////////////////
#include”stdafx.h”
#include”HLProc.h”
#include”MonteClass.h”
#include<math.h>
using namespace std;
#if _DEBUG
#undef THIS_FILE
static char THIS_FILE=_FILE_;
#define new DEBUG_NEW
#endif
////////////////////////////////////////////////////////////////
//Construction/Destruction
///////////////////////////////////////////////////////////////
CMonteClass::CMonteClass()
{
_tNum=10,_dt=1;
_rvalraw=1940952;
_lambda=0.062;
_gamavalaver=0.90,_gamavalraw=1.0453;
_pval=0,_pvalraw=80653;
_cr=0.0292,_dr=0.0277,_SumV=0,_rf=0.0381;
_sigmaraw=0.6482,_sigmaaver=0.78;
_muaver=0.31_mval=10,_muraw=0.3088;
_kai=2.27;
_xvalraw=169812;
_etaraw,_tao=0.15;
_fairaw=0.1034,_faiaver=0.0517;
m_simuTime=0;
}
CMonteClass::~CMonteClass()
{
}
void CMonteClass::SimulateOneTime(HWND hwnd,DWORD ID)
{
double rand1,rand2,rand3;
double rval=_rvalraw,muval=0,gamaval=0;
double cost=0,capx,dep,xval=_xvalraw,dx,Vval=0,yval=0;
double etapre;double sigmapre,faipre;
int flag=1;
rand1=RandomGuass();
rand2=RandomGuass();
rand3=RandomGuass();
for(int i=0;i<_tNum/_dt;i++)
{
sigmapre=Get_Sigema_Value(_sigmaraw,_sigmaaver,_kai,i-1);
etapre=Get_Eta_Value(_etaraw,_kai,i-1);
faipre=Get_Fai_Value(fairaw,_faiaver,_kai,i-1);
rval=Get_R_Value(rval,muval,_lambda,sigmapre,_dt,rand1);
muval=Get_U_Value(muval,_kai,_dt,_muaver,etapre,rand2);
gamaval=Get_Gama_Value(gamaval,_gamavalaver,_kai,_dt,faipre,rand3);
cost=gamaval*rval;
capx=_cr*rval;
_pval+=capx;
dep=_dr*rval;
yval=Get_Y_Value(flag,1,rval,cost,_dt);
dx=Get_DX_Value(_rf,xval,dep,capx,yval,_dt);
xval+=dx;
// t=t1/delta_t;
}
Vval=xval+_mval*(rval-cost);
_SumV+=Vval;
///////////////////////////////////////////////////////////////////
m_simuTime++;
CString str;
str.Format(“\n 仿真次数为:%d,\n\n 当前得到的结果是:%.2f”,m_simuTime,Vval);
LPCTSTR lpstrSystemError=(LPCTSTR)str”;
HWND hWnd=::GetDlgItem(hwnd,ID);
::SetWindowText(hWnd,lpstrSystemError);
}
double CMonteClass::Get_R_Value(double rvalpre,double mupre,double lambda,double sigmapre,double deltat,double random)
{
return(rvalpre*exp((mupre-lambda*sigmapre-sigmapre*sigmapre/2)*deltat+sigmapre*sqrt(deltat)*random));
}
double CMonteClass::Get_U_Value(double muvalpre,double kai,double deltat,double muaver,double etapre,double random));
{
return muvalpre*exp(-1*kai*deltat)+(1-exp(-1*kai*deltat))*muaver+sqrt((1-exp(-2*kai*deltat))/(2*kai))*etapre*random;
}
double CMonteClass::Get_Gama_Value(double gamavalpre,double gamavalaver,double kai,double deltat,double fai,double random)
{
return gamavalpre*exp(-1*kai*deltat)+(1-exp(-1*kai*deltat))*gamavalaver+sqrt((1-exp(-2*kai*deltat))/(2*kai)*fai*random;
}
double CMonteClass::RandomGuass()
{
static double V1,V2,S;
static int phase=0;
double X;
if(phase==0){
do{
double U1=(double)rand()/RAND_MAX;
double U2=(double)rand()/RAND_MAX;
V1=2*U1-1;
V2=2*U2-1;
S=V1*V1+V2*V2;
}while(S>=1//S==0);
X=V1*sqrt(-2*log(S)/S);
}else
X=V2*sqrt(-2*log(S)/S);
phase=1-phase;
return X;
}
double CMonteClass::Get_Sigema_Value(double sigmaraw,double sigmaaver,double kai,int time)
{
return(sigmaraw*exp(-kai*time)+sigmaaver*(1-exp(-kai*time)));
}
double CMonteClass::Get_Eta_Value(double etaraw,double kai,int time)
{
return etaraw*exp(-1*kai*time);
}
double CMonteClass::Get_Fai_Value(double etaraw,double kai,int time)
{
return(fairaw*exp(-1*kai*time)+faiaver*(1-exp(-1*kai*time)));
}
double CMonteClass::Get_Y_Value(double flag,double tao,double rval,double cost,double deltat )
{
double ytemp=rval-cost;
if(flag<=0)
{
flag=(-ytemp)>0?-ytemp:0;
}
else
flag=1-ytemp*deltat;
if(flag>0)
Tao=0;
else
Tao=0.15;
return(ytemp*(1-tao));
}
double CMonteClass::Get_DX_Value(double rf,double xpre,double dep,double capx,double yval,double deltat)
{
return(rf*xpre+dep-capx+yval)*deltat;
}
double CMonteClass::SimulateAll(int time,HWND hwnd,DWORD ID)
{
_SumV=0;
m_simuTime=0;
double v0=0;
for(int i=0;i<time;i++)
SimulateOneTime(hwnd,ID);
v0=_SumV/time*exp(-_rf*_tNum);
return v0;
}
void CMonteClass::Initialize(int tNum,int dt,double rvalraw,double l ambda,double gamavalaver,double gamavalraw,double pval,double pvalraw,double cr,double dr,double rf,double sigmaraw,double sigmaaver,double muaver,double mval,double muraw,double kai,double xvalraw,double etaraw,double tao,double fairaw,double faiaver)
{
_tNum=tNum,_dt=dt;
_rvalraw=rvalraw;
_lambda=sigmaraw;
_gamavalaver=sigmaraw;
_gamavalraw=gamavalraw;
_pval=pval;
_pvalraw=pvalraw;
_cr=cr,_dr=dr;
_rf=rf;
_sigmaraw=sigmaraw,_sigmaaver=sigmaaver;
_muaver=muaver,_mval=mval,_muraw=muraw;
_kai=kai;
_xvalraw=xvalraw;
_etaraw=etaraw,_tao=tao;
_fairaw=fairaw,_faiaver=faiaver;
}
评论0