7 Replies - 3527 Views - Last Post: 05 December 2011 - 09:21 AM Rate Topic: -----

#1 nickhun07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-December 11

error Index was outside the bounds of the array problem

Posted 04 December 2011 - 02:14 PM

Hi,

the error show after i entered the value

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

in line 87 is
callpayoffvec[i, 1] = (Math.Max(AverageSVec - K, 0));

I dont know that something wrong about my Array. I need your guy help me to solve
Thank you

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



Is This A Good Question/Topic? 0
  • +

Replies To: error Index was outside the bounds of the array problem

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 02:23 PM

well, you declared your array as
double[,] callpayoffvec = new double[i, 1];


and three lines above you set the value for i
int i = 0, j;



You should change to
double[,] callpayoffvec = new double[NSimulations, 1]


Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 02:28 PM

Understanding the error:
Out of range is pretty self explanatory. If your collection has 10 items in it they are elements 0-9. If you try to use element 15 that is beyond the range of your collection.

When this line was highlighted by Visual Studio, when the error occured, you can then hover your mouse over each variable to see its values. At that point it should become clear where the error is.
Attached Image

For more debugging tips and tutorials take a look at FAQ 5


FAQ (Frequently Asked Questions - Updated Nov 2011

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 nickhun07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-December 11

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 02:40 PM

View PostIonut, on 04 December 2011 - 02:23 PM, said:

well, you declared your array as
double[,] callpayoffvec = new double[i, 1];


and three lines above you set the value for i
int i = 0, j;



You should change to
double[,] callpayoffvec = new double[NSimulations, 1]



thank you for you reply.
i changed the code as you said but still be error at the same previous.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 02:49 PM

How many elements in each dimension of your array when the error occurs? What are the values of i and j when the error occurs?
Was This Post Helpful? 0
  • +
  • -

#6 nickhun07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-December 11

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 03:04 PM

View PosttlhIn`toq, on 04 December 2011 - 02:49 PM, said:

How many elements in each dimension of your array when the error occurs? What are the values of i and j when the error occurs?


How can i know the value when error occurs?
im a kind of newbie for c#.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,191
  • Joined: 02-June 10

Re: error Index was outside the bounds of the array problem

Posted 04 December 2011 - 03:12 PM

Take a look at post #3 again.

When Visual Studio stops on a line of code because of a problem you can look at each variable/object by just hovering the mouse over it.

You can also look in in the Autos and Locals pallet.

I will also repeat the recommendation to do the debugging tutorials I pointed out to you. An hour spent learning to debug will save you 100 hours wondering what the frak is going on with your program.
Was This Post Helpful? 0
  • +
  • -

#8 nickhun07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-December 11

Re: error Index was outside the bounds of the array problem

Posted 05 December 2011 - 09:21 AM

Thank you mate, the problem is in Array then i changed the value from 1 to 2
 double[,] callpayoffvec = new double[i, 1];


to

 double[,] callpayoffvec = new double[i, 2];


but new problem was come ....

value in
st=S;
are Infinity
it is not the value from
S


what should i do ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1