C# - 索引超出范围(C# - Index was out of range)

2019-11-01 09:36发布

我想一个C ++类转换为C#和在这个过程中学习的C ++的东西。 我从来没有碰到前一个vector <>和我的理解是,这是像在C#列表<>功能。 使用列表futures_price =新的列表(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];
};

Answer 1:

作为SLaks说,最好在这种情况下使用数组。 C#列表都充满了Add方法和值是通过删除方法删除......这将是更加复杂,因为你也替换值内存/性能昂贵。

public Double FuturesOptionPriceCallAmericanBinomial(Double spotPrice, Double optionStrike, Double r, Double sigma, Double time, Double steps)
{
    // Avoid calling Convert multiple times as it can be quite performance expensive.
    Int32 stepsInteger = Convert.ToInt32(steps);

    Double[] futurePrices = new Double[(stepsInteger + 1)];
    Double[] callValues = new Double[(stepsInteger + 1)];

    Double tDelta = time / steps;
    Double rInv = Math.Exp(-r * (tDelta));
    Double u = Math.Exp(sigma * Math.Sqrt(tDelta));
    Double d = 1.0 / u;
    Double uu = u * u;
    Double pUp = (1 - d) / (u - d);
    Double pDown = 1.0 - pUp;

    futurePrices[0] = spotPrice * Math.Pow(d, steps);

    for (Int32 i = 1; i <= steps; ++i)
        futurePrices[i] = uu * futurePrices[(i - 1)];

    for (Int32 i = 0; i <= steps; ++i)
        callValues[i] = Math.Max(0.0, (futurePrices[i] - optionStrike));

    for (Int32 step = stepsInteger - 1; step >= 0; --step)
    {
        for (Int32 i = 0; i <= step; ++i)
        {
            futurePrices[i] = d * futurePrices[(i + 1)];
            callValues[i] = ((pDown * callValues[i]) + (pUp * callValues[i + 1])) * rInv;
            callValues[i] = Math.Max(callValues[i], (futurePrices[i] - option_strike));
        }
    }

    return callValues[0];
}


Answer 2:

一个List<double>开始是空的,直到你添加项目。 (通过构造函数的参数只是设置的能力,避免了代价高昂调整大小)

您不能访问[0]直到你Add()它。

要使用它,你的方式,使用数组来代替。



文章来源: C# - Index was out of range