【问题标题】:European Options, Trinomial Tree, C++ [closed]欧式期权,三叉树,C++ [关闭]
【发布时间】:2014-01-15 17:39:07
【问题描述】:

我正在尝试使用股票价格的三叉树模型为 C++ 中的欧洲看涨期权和看跌期权定价。我设法摆脱了错误,但在运行命令后,它返回 nan 作为期权价格。代码如下:

三叉树源码:

#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)你是什么意思,“什么都没有发生”?如果您不给我们minimal complete example,我们将没有多少机会帮助您,如果您不尝试,我们也不会。
  • 你为什么要为欧洲期权定价?只需使用 black-scholes。

标签: c++ options


【解决方案1】:

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

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

在下一个 for 循环中,您还有另一个错误,在这里您将 n 设置为 N-1,然后在 for 循环的迭代步骤中将其加 1。

for (int n=N-1; n>=0;n++)

这只是我看到的前两个错误...还有两个我只是看了一眼就发现了,但我会留给你弄清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多