繁体   English   中英

C#-索引超出范围

[英]C# - Index was out of range

我试图将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];
};

List<double>开始为空,直到您向其中添加项目。 (传递构造函数参数只是设置容量,以防止昂贵的调整大小)

在您Add()之前,您无法访问[0]

要按原样使用它,请改用数组。

正如SLaks所说,在这种情况下最好使用数组。 C#列表填充有Add方法,而值则通过Remove方法删除...这将更加复杂,内存/性能昂贵,因为您还要替换值。

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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM