C++ 클래스를 C#으로 변환하려고하는데이 과정에서 C++에 대해 배웁니다. 나는 전에 벡터 <에 뛰어 들어 본 적이 없다. 그리고 내 이해는 C#의 List <> 함수와 같다. 클래스를 변환하는 동안 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];
};
왜 'no_steps'가 두 번입니까? – SLaks
.. 그리고 어떤 줄이 오류를 생성합니까? –
@ SLaks의 대답은 정확합니다. 이것은 C#이 아닌 C#과 같은 C# 코드를 작성하도록 제안하는 것입니다. var, Integer.Parse(), CamelCase 메소드 이름 및 속성 등을 사용하십시오. 다른 소스 코드를 보면 좋은 생각을 얻을 수 있습니다. – rikkit