2013-01-10 209 views
0

我想C++類轉換爲C#和在這個過程中學習的C++的東西。我從來沒有遇到之前載體<>和我的理解是,這是像在C#中的列表<>功能。在類的轉換過程中,我使用List futures_price = New List(Convert.ToInt32(no_steps)+ 1);重寫了代碼。只要我運行代碼,我會得到一個「索引超出範圍」的錯誤。C# - 索引超出範圍

說完看了看周圍的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]; 
}; 
+0

爲什麼'no_steps'是雙重的? – SLaks

+0

..以及哪條線產生錯誤? –

+0

@ SLaks的答案是正確的,這只是一個建議你寫C#代碼而不是C++的評論。使用var,Integer.Parse(),CamelCase方法名稱和屬性等等。您可以通過查看其他源代碼來獲得一個好主意。 – rikkit

回答

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

哇!謝謝。這對於瞭解它是如何構建的非常有幫助。我在上面錯過了很多。 – JAS

3

一個List<double>開始是空的,直到你添加項目。 (通過構造參數只是設置容量,防止代價大小調整)

你不能訪問[0],直到你Add()它。

要使用它,你的方式,使用數組來代替。

+0

啊,所以我真的應該重新寫這個來正確運行。我看到我在這裏錯過了一些關鍵部分。 – JAS