我正在尝试将 C++ 类转换为 C#,并在此过程中学习 C++。我以前从未遇到过 vector<>,我的理解是这就像 C# 中的 List<> 函数。在类转换期间,我使用 List futures_price = New List(Convert.ToInt32(no_steps) + 1); 重写了代码。一旦我运行代码,我就会收到“索引超出范围”错误。
环顾 SOF 后,我认为问题在于参数超出了与此相关的索引范围,但我没有看到使用以下代码解决此问题的简单解决方案。
特别是,这是触发错误的行:futures_prices[0] = spot_price * Math.Pow(d, no_steps);
以下是完整代码:
public double futures_option_price_call_american_binomial(double spot_price, double option_strike, double r, double sigma, double time, double no_steps)
        {
           //double spot_price, // price futures contract
           //double option_strike, // exercise price
           //double r, // interest rate
           //double sigma, // volatility
           //double time, // time to maturity
           //int no_steps
            List<double> futures_prices = new List<double>(Convert.ToInt32(no_steps) + 1);
               //(no_steps+1);
           //double call_values = (no_steps+1);
            List<double> call_values = new List<double>(Convert.ToInt32(no_steps) + 1);
           double t_delta = time/no_steps;
           double Rinv = Math.Exp(-r*(t_delta));
           double u = Math.Exp(sigma * Math.Sqrt(t_delta));
           double d = 1.0/u;
           double uu= u*u;
           double pUp   = (1-d)/(u-d);   // note how probability is calculated
           double pDown = 1.0 - pUp;
           futures_prices[0] = spot_price * Math.Pow(d, no_steps);
            int i;
            for (i=1; i<=no_steps; ++i) futures_prices[i] = uu*futures_prices[i-1]; // terminal tree nodes
            for (i=0; i<=no_steps; ++i) call_values[i] = Math.Max(0.0, (futures_prices[i]-option_strike));
            for (int step = Convert.ToInt32(no_steps) - 1; step >= 0; --step)
            {
                for (i = 0; i <= step; ++i)
                {
                    futures_prices[i] = d * futures_prices[i + 1];
                    call_values[i] = (pDown * call_values[i] + pUp * call_values[i + 1]) * Rinv;
                    call_values[i] = Math.Max(call_values[i], futures_prices[i] - option_strike); // check for exercise
                };
            };
            return call_values[0];
        }
以下是 C++ 的原始源代码:
double futures_option_price_call_american_binomial(const double& F, // price futures contract
                           const double& K, // exercise price
                           const double& r, // interest rate
                           const double& sigma, // volatility
                           const double& time, // time to maturity
                           const int& no_steps) { // number of steps
   vector<double> futures_prices(no_steps+1);
   vector<double> call_values (no_steps+1);
   double t_delta= time/no_steps;
   double Rinv = exp(-r*(t_delta));
   double u = exp(sigma*sqrt(t_delta));
   double d = 1.0/u;
   double uu= u*u;
   double pUp   = (1-d)/(u-d);   // note how probability is calculated
   double pDown = 1.0 - pUp;
   futures_prices[0] = F*pow(d, no_steps);
   int i;
   for (i=1; i<=no_steps; ++i) futures_prices[i] = uu*futures_prices[i-1]; // terminal tree nodes
   for (i=0; i<=no_steps; ++i) call_values[i] = max(0.0, (futures_prices[i]-K));
   for (int step=no_steps-1; step>=0; --step) {
      for (i=0; i<=step; ++i)   {
     futures_prices[i] = d*futures_prices[i+1];
     call_values[i] = (pDown*call_values[i]+pUp*call_values[i+1])*Rinv;
     call_values[i] = max(call_values[i], futures_prices[i]-K); // check for exercise
      };
   };
   return call_values[0];
};