Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
欧式期权,三项式树,C++; 我试图用C++的三叉树模型来定价股票价格。我设法消除了错误,但在运行命令后,它返回了nan作为选项价格。代码如下:_C++_Options - Fatal编程技术网

欧式期权,三项式树,C++; 我试图用C++的三叉树模型来定价股票价格。我设法消除了错误,但在运行命令后,它返回了nan作为选项价格。代码如下:

欧式期权,三项式树,C++; 我试图用C++的三叉树模型来定价股票价格。我设法消除了错误,但在运行命令后,它返回了nan作为选项价格。代码如下:,c++,options,C++,Options,三项式树的源代码: #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 t

三项式树的源代码:

#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;
} 
#包括“TriModel01.h”
#包括“项目选项06.h”
#包括
#包括
使用名称空间std;
双重欧洲::PriceByCRR(三模型)
{
double pu=Model.risknutprob_up();
double pd=Model.risknutprob_down();
双pm=1-pu-pd;
双倍价格[N+1];
对于(int i=N;i>=-N;i--)
{
价格[i]=收益(模型S(N,i));
}
对于(int n=n-1;n>=0;n--)
{
对于(int i=n;i>=-n;i--)
{
价格[i]=(pu*价格[i+1]+pm*价格[i]+pd*价格[i-1])*exp(-Model.GetR()*Model.Getdt());
}
}
退货价格[0];
}
int调用::GetInputData()
{
库特K;
cout K)返回z-K;
返回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;
}
#包括“TriModel01.h”
#包括“项目选项06.h”
#包括
#包括
使用名称空间std;
int main()
{
双R=0.05;
双q=0.03;
双西格玛=0.2;
双S0;
cout>S0;

cout在PriceByCRR中,您有几个错误,您正在将负索引写入Price数组,这将导致内存损坏

for (int i=-N; i<=N;i++)
{
    Price[i]=Payoff(Model.S(N,i));
}

这些只是我看到的前两个bug…我还发现了另外两个bug,只是看了一眼,但我会让你去弄清楚。

所以,我不确定我是否理解这里的问题。看起来你刚刚在我的膝上扔了一大堆代码,让我帮你调试。你肯定不是这么做的,对吗?因为StackOverflow显然不是一台神奇的调试机器。1)这段代码不完整,我们无法编译。2)运行什么命令?3)什么意思,“什么都没发生”?如果你不给我们一个机会,我们就没有多少机会帮助你,如果你不尝试,我们也不会。你为什么要做这种花哨的事情来为欧式期权定价?就用布莱克-斯科尔斯吧。
for (int n=N-1; n>=0;n++)