For loops into for loops
Author Message
For loops into for loops

I've got big problems with a for loop into another for loop.
I report below the piece of code.

---------------------
sgenrand(rand() %(RAND_MAX+1)); // Random seed of the pseudorandom sequence
for (int i=0;  i< no_steps; ++i)
{
double y = genrand1(); // random numbers
S_t = S_t * exp(R + SD * cndev(y));
prices[i]=S_t;   // The array

};

//..... some manipulations
double price = sum_c_max* (exp (-r*time));  // The result
---------------------

I'd like to calculate "double price" n times with different random numbers
in order to obtain the average of such values.
I thought that i needed to put this process into another for loop.
But i don't know how to do that.
I did as follows, but that gives wrong results:

for (int j=0;  j< no_paths; ++j)
{
sgenrand(rand() %(RAND_MAX+1));

for (int i=0;  i< no_steps; ++i)
{

double y = genrand1();
S_t = S_t * exp(R + SD * cndev(y));
prices[i]=S_t;

};

m = *max_element(prices.begin(),prices.end()); // the max value of the
array
tx = prices.back(); // the last value
if (m > bar)
sum_c_max  += max(0.0, tx - X) ;

Quote:
}

double price = sum_c_max * exp (-r*time) / no_paths;

Could anyone help me?

MAX

Mon, 16 Dec 2002 03:00:00 GMT
For loops into for loops

Quote:

> I've got big problems with a for loop into another for loop.
> I report below the piece of code.

> ---------------------
> sgenrand(rand() %(RAND_MAX+1)); // Random seed of the pseudorandom sequence

What *exactly* are you doing trying to reduce the output of rand()
modulo RAND_MAX + 1?  Are you perhaps thinking that it might return
greater than RAND_MAX?  What do you think RAND_MAX means?

More intriguingly, what happens when RAND_MAX == INT_MAX? ;-)

Quote:
> for (int i=0;  i< no_steps; ++i)
>         {
>           double y = genrand1(); // random numbers
>           S_t = S_t * exp(R + SD * cndev(y));
>           prices[i]=S_t;   // The array

I've no idea where prices is declared, or where S_t, R and SD are
initialized.

Quote:
>         };

>     //..... some manipulations
>     double price = sum_c_max* (exp (-r*time));  // The result

Is that `r' meant to be the same as the previous `R'?  Are you sure you
want to use the name `time'?

Quote:
> I'd like to calculate "double price" n times with different random numbers
> in order to obtain the average of such values.
> I thought that i needed to put this process into another for loop.
> But i don't know how to do that.
> I did as follows, but that gives wrong results:

> for (int j=0;  j< no_paths; ++j)
> {
>     sgenrand(rand() %(RAND_MAX+1));

Maybe you want to reinitialize S_t here.

Quote:
>    for (int i=0;  i< no_steps; ++i)
>           {
>             double y = genrand1();
>             S_t = S_t * exp(R + SD * cndev(y));
>             prices[i]=S_t;
>           };

>     m = *max_element(prices.begin(),prices.end()); // the max value of the
> array

Nope.  That's not C.  Definitely not.

Quote:
>     tx = prices.back(); // the last value

Nor's that.

Quote:
>     if (m > bar)
>     sum_c_max  += max(0.0, tx - X) ;
> }
>     double price = sum_c_max * exp (-r*time) / no_paths;

> Could anyone help me?

I doubt it.

-- [mdw]

Mon, 23 Dec 2002 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages