-1
我試圖爲股票價格的三叉樹模型使用三叉樹模型對股票價格進行定價,並在C++中設置期權。我設法擺脫了錯誤,但在運行命令後,它返回了選項價格的nan。代碼如下:對於選項歐洲期權,三叉樹,C++
#include <iostream>
#include <cmath>
using namespace std;
// contructor to initialise the data member by list
TriModel(double S0_, double q_, double sigma_, double R_):S0(S0_),q(q_),sigma(sigma_),R(R_)
{
}
/* member function to work out time interval dt
T maturity of the option, N number of time steps*/
void TriModel::Setdt(double T, int N)
{
T=1;
dt=T/N;
}
// risk-neutral probability of going up
double TriModel::RiskNeutProb_up()
{
double v=R-q-0.5*sigma*sigma;
double dx=sigma*sqrt(3*dt);
return 0.5*((sigma*sigma*dt+v*v*dt*dt)/(dx*dx)+v*dt/dx);
}
// calculate the risk neutual probability of going down
double TriModel::RiskNeutProb_down()
{
double v=R-q-0.5*sigma*sigma;
double dx=sigma*sqrt(3*dt);
return 0.5*((sigma*sigma*dt+v*v*dt*dt)/(dx*dx)-v*dt/dx);
}
// calculate the stock price at time step n and node i
double TriModel::S(int N, int i)
{
double dx=sigma*sqrt(3*dt);
return S0*exp(i*dx);
}
// return the risk free interest rate R
double TriModel::GetR()
{
return R;
}
// return time interval dt
double TriModel::Getdt()
{
return dt;
}
頭文件:對於選項
#ifndef Proj1_Options06_h
#define Proj1_Options06_h
#include "TriModel01.h"
class EurOption
{
private:
int N; //steps to expiry
public:
void SetN(int N_){N=N_;}
int getN(){return N;}
//Payoff defined to return 0.0
//for pedagogical purposes.
//To use a pure virtual function replace by
//virtual double Payoff(double z)=0;
virtual double Payoff(double z){return 0.0;}
//pricing European option by a Trinomial Tree model
// please implement this function in the accompany Options06.cpp file
double PriceByCRR(TriModel Model);
};
class Call: public EurOption
{
private:
double K; //strike price
public:
void SetK(double K_){K=K_;}
int GetInputData();
double Payoff(double z);
};
#endif
的源代碼:對於三叉樹
源代碼
#include "TriModel01.h"
#include "Proj1_Options06.h"
#include <iostream>
#include <cmath>
using namespace std;
double EurOption::PriceByCRR(TriModel Model)
{
double pu=Model.RiskNeutProb_up();
double pd=Model.RiskNeutProb_down();
double pm=1-pu-pd;
double Price[N+1];
for (int i=N; i>=-N;i--)
{
Price[i]=Payoff(Model.S(N,i));
}
for (int n=N-1; n>=0;n--)
{
for (int i=n; i>=-n; i--)
{
Price[i]=(pu*Price[i+1]+pm*Price[i]+pd*Price[i-1])*exp(-Model.GetR()*Model.Getdt());
}
}
return Price[0];
}
int Call::GetInputData()
{
cout << "Enter call option data:" << endl;
int N;
cout << "Enter steps to expiry N: "; cin >> N;
SetN(N);
cout << "Enter strike price K: "; cin >> K;
cout << endl;
return 0;
}
double Call::Payoff(double z)
{
if (z>K) return z-K;
return 0.0;
}
主要功能:
#include "TriModel01.h"
#include "Proj1_Options06.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double R=0.05;
double q=0.03;
double sigma=0.2;
double S0;
cout << "Enter S0: "; cin >> S0;
cout << endl;
TriModel Model(S0,q,sigma,R);
Call Option1;
Option1.GetInputData();
cout << "European call option price = "
<< Option1.PriceByCRR(Model)
<< endl << endl;
/*Put Option2;
Option2.GetInputData();
cout << "European put option price = "
<< Option2.PriceByCRR(Model)
<< endl << endl;*/
return 0;
}
任何幫助,將不勝感激! 謝謝!
所以,我不確定我是否理解這裏的問題。看起來你已經把一大堆代碼放在膝蓋上,並要求我爲你調試它。當然,那不是你在做什麼,對吧?因爲顯然StackOverflow不是一個神奇的調試機器。 –
1)這段代碼不完整,我們無法編譯它。 2)運行什麼命令? 3)你是什麼意思,「沒有任何反應」?如果你不給我們一個[最小完整的例子](http://sscce.org),我們將沒有太多的機會來幫助你,如果你不會嘗試,我們也不會。 – Beta
你爲什麼要做這個花哨的東西來定價歐式期權?只需使用black-scholes。 – thang