 |
|
มีปัญหาเรื่อง error Index was outside the bounds of the array. ครับ |
|
 |
|
|
 |
 |
|
เนื่องจากว่าผมบังเอิญต้องทำ web service ของ Asian Option Price using Monte Carlo Simulation ครับ
คราวนี้ systax ไม่ error สักอย่าง แต่เวลาผมลองใส่ค่ามันจะแจ้ง erro ดังนี้ครับ
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Service.asianprice(Double S, Double K, Double T, Double r, Double sigma, Int32 NSimulations, Int32 NStep) in c:\Users\nickhun07\Documents\Visual Studio 2010\WebSites\MonteCarlo\App_Code\Service.cs:line 87
ซื่ง Line 87 ที่ค่านั้นคือ
callpayoffvec[i, 1] = (Math.Max(AverageSVec - K, 0));
putpayoffvec[i, 1] = (Math.Max(K - AverageSVec, 0));
ยังไงรบกวนช่วยตรวจดูให้หน่อยนะครับ ผมบอกตรงๆว่าผมไม่มีพื้นฐาน c# ที่ดีเลย ผมทำด้าน Network แต่ว่าจำเป็นต้องทำครับ
ขอบคุณครับ
Code (C#)
public void asianprice(double S,double K,double T,double r,double sigma,int NSimulations,int NStep)
{
double dt, vsqrdt, drift;
double str_S = S; //spot price
double str_K = K; //strike
double str_T = T; //maturity
double str_r = r; //interest rate
double str_sigma = sigma; //volatility
double str_NSim = NSimulations; //no of monte carlo simulations
double str_NSt = NStep; //no of time steps
int i = 0, j;
dt = T / (NStep - 1);
vsqrdt = sigma * (Math.Pow(0.5, dt));
drift = (r - (Math.Pow(2, sigma)) / 2) * dt;
double[,] callpayoffvec = new double[i, 1];
double[,] putpayoffvec = new double[i, 1];
int simtost = NSimulations * NStep;
Random nsmlnsi = new Random();
int randvec = nsmlnsi.Next(simtost);
// find the result of sizeof of (S) and float
/* for (i = 0; i < sizeof(int); i++)
{
Smat[i, 0] = S; // find the number of each S */
for (i = 0; i <= NSimulations-1; i++)
{
double st = S;
double curtime = 0;
double tmpsum = 0;
for (j = 0; j <= NStep-1; j++)
{
curtime = curtime + dt;
st = st * Math.Exp(drift + vsqrdt + randvec);
tmpsum = tmpsum + st;
}
double AverageSVec = tmpsum / NStep;
callpayoffvec[i, 1] = (Math.Max(AverageSVec - K, 0));
putpayoffvec[i, 1] = (Math.Max(K - AverageSVec, 0));
}
double MC_callprice = Math.Exp(-r * T) * Mean(callpayoffvec);
double MC_putprice = Math.Exp(-r * T) * Mean(putpayoffvec);
}
double Mean(double[,] dblArray)
{
double dblResult = 0;
foreach (double dblValue in dblArray)
dblResult += dblValue;
return dblResult / dblArray.Length;
}
Tag : .NET, C#
|
|
 |
 |
 |
 |
Date :
2011-12-05 04:06:53 |
By :
nickhun07 |
View :
2599 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอนนี้แก้ไปแล้วครับโดยการแก้
double[,] callpayoffvec = new double[i, 1]; เป็น 2
double[,] putpayoffvec = new double[i, 1]; เป็น 2
มีอีกปัญหานึงครับคือ ค่า st ไม่ยอมเป็นค่า S กลับเป็นค่า Infinity แทน
แก้ไขอย่างไรครับ
|
 |
 |
 |
 |
Date :
2011-12-05 23:12:44 |
By :
nickhun07 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|