473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

program isn't running properly

hello, before i post my code for this, is there anyone out there at
this moment? no sense in posting if no one is out there to read and
answer it =P

Mar 7 '07 #1
15 1859
right, well, might as well i guess post up all the code for it. the
context of it is trying to calculate the price of a european put
option via monte carlo simulation methods. it compiles fine with no
errors, but when i go to run it, after the inputs, it just stops doing
anything (or is it that slow that it doesn't finish within a
minute!?!?!?) any help would be great! i suspect there is something
wrong with the two functions i have defined as uniform_deviate and
normal_deviate, but am not really sure.

here it is:

#include<iostream>
#include<math.h>
using namespace std;

double Wt, v, So, Si, r, T, Vi, Vt, V1, V2, U, W, LP, K, Am, Bm, Z,
Upper, Lower, sigma, mu;
int a, b, N, M, Vj, i;

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=Vj/(N-1);
return U;
}
double normal_deviate (double mu, double sigma)
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

double MAX(double x, double y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}

int main()
{
cout <<"What is the initial stock price? "<<endl;
cin >>So;
cout <<"What is the interest rate? "<<endl;
cin >>r;
cout <<"What is the volatility of the stock? "<<endl;
cin >>v;
cout <<"What is the time to maturity? "<<endl;
cin >>T;
cout <<"What is the number of simulations in the model? "<<endl;
cin >>M;
cout <<"What is the strike price of the option? "<<endl;
cin >>K;

Vj=1;
Vt=0.0;
LP=0.0;

double Values[10];//doesn't seem to want to let me assign an array of
size M here for some reason, so i made it small at 10 to see if i
could get it working.
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate(0,1));
Values[i] = exp(-r*T)*MAX(K-Si,0);
Vt = exp(-r*T)*MAX(K-Si,0);
Vt = Vt + Si;
}
Am = Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);
}
Bm = (1/(M-1))*LP;
Lower = Am-((1.96*Bm)/(pow(M,0.5)));
Upper = Am+((1.96*Bm)/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;
}

Mar 7 '07 #2
ia******@gmail.com wrote:
right, well, might as well i guess post up all the code for it. the
context of it is trying to calculate the price of a european put
option via monte carlo simulation methods. it compiles fine with no
errors, but when i go to run it, after the inputs, it just stops doing
anything (or is it that slow that it doesn't finish within a
minute!?!?!?) any help would be great! i suspect there is something
wrong with the two functions i have defined as uniform_deviate and
normal_deviate, but am not really sure.

here it is:

#include<iostream>
#include<math.h>
using namespace std;

double Wt, v, So, Si, r, T, Vi, Vt, V1, V2, U, W, LP, K, Am, Bm, Z,
Upper, Lower, sigma, mu;
int a, b, N, M, Vj, i;

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=Vj/(N-1);
return U;
}
double normal_deviate (double mu, double sigma)
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

double MAX(double x, double y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}

int main()
{
cout <<"What is the initial stock price? "<<endl;
cin >>So;
cout <<"What is the interest rate? "<<endl;
cin >>r;
cout <<"What is the volatility of the stock? "<<endl;
cin >>v;
cout <<"What is the time to maturity? "<<endl;
cin >>T;
cout <<"What is the number of simulations in the model? "<<endl;
cin >>M;
cout <<"What is the strike price of the option? "<<endl;
cin >>K;

Vj=1;
Vt=0.0;
LP=0.0;

double Values[10];//doesn't seem to want to let me assign an array of
size M here for some reason, so i made it small at 10 to see if i
could get it working.
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate(0,1));
Values[i] = exp(-r*T)*MAX(K-Si,0);
Vt = exp(-r*T)*MAX(K-Si,0);
Vt = Vt + Si;
}
Am = Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);
}
Bm = (1/(M-1))*LP;
Lower = Am-((1.96*Bm)/(pow(M,0.5)));
Upper = Am+((1.96*Bm)/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;
}
Wow, I suggest you try and wean yourself off using global variables, its
clearly a serious addiction.

Problem is here

U=Vj/(N-1);

You obviously intend this to be a floating point division, but because
Vj and N are integers it is an integral division and the result is
always 0. This means your loop never terminates. So change to this

U=(double)Vj/(N-1);

and at the very least you'll get a fractional value in U. Whether that's
enough to fix you program I'm not sure, there are a few strange things
about the code you've written, but I guess it's work in progress.

john
Mar 7 '07 #3
That seems to have fixed the problem of it not running. However now
it gives me a variance of 0 every time, which i think is a problem in
my function, because I'm told I should have parameters mu and sigma,
yet never really use them.

as for weaning off global variables, if only i knew what they
were! ;-)

what are the other strange things you find?

This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++

but thanks for the help so far!

ian

Mar 7 '07 #4
ia******@gmail.com wrote:
That seems to have fixed the problem of it not running. However now
it gives me a variance of 0 every time, which i think is a problem in
my function, because I'm told I should have parameters mu and sigma,
yet never really use them.

as for weaning off global variables, if only i knew what they
were! ;-)
All those variables defined outside of any function. It rightly
considered bad style to rely too heavily on global variables. Declare
variables as locally as possible and use parameters to pass values from
one function to another.

Also note you have mu and sigma as globals but you also have them
declared as parameters to normal_deviate. That's *really* bad style, and
a recipe for confusion.
>
what are the other strange things you find?
Well you mentioned one above, unused parameters.

Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?
>
This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++
Well knowing there is an issue is a start.

For programs the size of the one you are writing here good style isn't
such a big deal, main thing is to get it working. These issues really
start to bite when you work on large programs, or programs with more
than one author.
>
but thanks for the help so far!

ian
john
Mar 7 '07 #5
>
Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?
this is my current code on that section:

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=(double)Vj/(N-1);
return U;
}
double normal_deviate ()
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

important i guess to also note that in int main () Vj starts at a
value of 1.

i dont think that it calls the same number every time? i did a cout
<<U << in the uniform deviate function, and it was a different value
each time (because Vj keeps changing, its a very basic random number
generator, not a good one though). And the values in normal deviate
also appear to be changing, because i ran a cout <<Z << in there as
well, and it was numbers from the normal curve. Now each time i run
the program, it will give me the exact same "random" numbers i think,
but i think thats what we are supposed to do for this one. its a bad
random number generator mainly =P

What I am currently struggling with is the calculations within the int
main ().

I was miscalculating Am, because i was simply takign the last Vt value
and adding the last Si value from the loop.

here is the relevant code:

double Values[10];
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values[i] = exp(-r*T)*MAX(K-Si,0);

Vt = exp(-r*T)*MAX(K-Si,0);
//in this space here i need to calculate a sum of all the Si's. or
rather, need to use it in the line below. is there a sum function
where i could say sum Values[] and have it add all the numbers stored
in my array? because i need that sum divided by M, as seen below.

}
Am = ***need sum of numbers from array, not Vt*** Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);

}
Bm = LP/(M-1);
Lower = Am-(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
Upper = Am+(1.96*(pow(Bm,0.5))/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;

Mar 7 '07 #6
ia******@gmail.com wrote:
>>Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?


this is my current code on that section:

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=(double)Vj/(N-1);
return U;
}
double normal_deviate ()
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

important i guess to also note that in int main () Vj starts at a
value of 1.

i dont think that it calls the same number every time? i did a cout
<<U << in the uniform deviate function, and it was a different value
each time (because Vj keeps changing, its a very basic random number
generator, not a good one though).
Yes, you are right. I was reading uniform_deviate as if the variable Vj
was local to that function. That's the thing about global variables,
they make code so hard to understand. At least that's my excuse.

john
Mar 7 '07 #7
OK i think i solved my own question. Code in that section looks like
this:

double Values[10000];
double sum;
sum=0.0;
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values[i] = exp(-r*T)*MAX(K-Si,0);
sum = sum + Values[i];
}
Am = sum/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);

}
Bm = LP/(M-1);
Lower = Am-(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
Upper = Am+(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
The only issue i am now having is that it won't let me make my array
called Values be of size M, if i put in Values[M] it gives me the
following errors:

M:\Homework 3\Homework 3.cpp(64) : error C2057: expected constant
expression
M:\Homework 3\Homework 3.cpp(64) : error C2466: cannot allocate an
array of constant size 0
M:\Homework 3\Homework 3.cpp(64) : error C2133: 'Values' : unknown
size

Which i dont understand, because in the code above it, there is a cin
>>M >>endl; so it should know that it will get a value for M. any
thoughts on that? And I think the other section is correct in that
sum i put in?

Mar 7 '07 #8
ia******@gmail.com wrote:
OK i think i solved my own question. Code in that section looks like
this:

double Values[10000];
double sum;
sum=0.0;
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values[i] = exp(-r*T)*MAX(K-Si,0);
sum = sum + Values[i];
}
Am = sum/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);

}
Bm = LP/(M-1);
Lower = Am-(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
Upper = Am+(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
The only issue i am now having is that it won't let me make my array
called Values be of size M, if i put in Values[M] it gives me the
following errors:

M:\Homework 3\Homework 3.cpp(64) : error C2057: expected constant
expression
M:\Homework 3\Homework 3.cpp(64) : error C2466: cannot allocate an
array of constant size 0
M:\Homework 3\Homework 3.cpp(64) : error C2133: 'Values' : unknown
size

Which i dont understand, because in the code above it, there is a cin
>>>M >>endl; so it should know that it will get a value for M. any

thoughts on that? And I think the other section is correct in that
sum i put in?
That's because arrays must be a fixed size. If you want to have a size
that varies the best way is to use a vector

#include <vector>
using namespace std;

....

vector<doubleValues(M);
....

all other code is unchanged.

john
Mar 7 '07 #9
ia******@gmail.com wrote:
This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++
The thing that bugs me is why you chose C++ as development tool? Having
worked about 10 years with C++ (the last 3 of them on a daily basis), I
would consider myself as experienced with a tendency to expert, but
there are still a lot of things that I don't know. If you ask me I'd say
that C++ is definitely not the best choice for beginners, as it offers
too many features that are designed for real large scale projects but
are only confusing to beginners. I doubt that you would have had this
trouble if you had used other tools like Matlab or Maple (though they
are a lot more expensive than a C++ compiler that you can get everywhere
for free).

Having a lot of global variables is not bad in general, but makes the
code you have written next to impossible to re-use. For real small scale
programs (say less than 500 lines of code) global variables are
acceptable. If you want to spend more time learning C++ I'd advice you
to re-write your code with proper functions and without any global
variables. If you want to spend more time on the problem you have in
hand, you should better use another tool.

Regards,
Stuart
Mar 7 '07 #10
On Mar 7, 8:42 am, Stuart Redmann <DerTop...@web.dewrote:
ianwe...@gmail.com wrote:
This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++

The thing that bugs me is why you chose C++ as development tool? Having
worked about 10 years with C++ (the last 3 of them on a daily basis), I
would consider myself as experienced with a tendency to expert, but
there are still a lot of things that I don't know. If you ask me I'd say
that C++ is definitely not the best choice for beginners, as it offers
too many features that are designed for real large scale projects but
are only confusing to beginners. I doubt that you would have had this
trouble if you had used other tools like Matlab or Maple (though they
are a lot more expensive than a C++ compiler that you can get everywhere
for free).

Having a lot of global variables is not bad in general, but makes the
code you have written next to impossible to re-use. For real small scale
programs (say less than 500 lines of code) global variables are
acceptable. If you want to spend more time learning C++ I'd advice you
to re-write your code with proper functions and without any global
variables. If you want to spend more time on the problem you have in
hand, you should better use another tool.

Regards,
Stuart
Sure, other programs are easier to use, and more user friendly, and
nicer for beginners; i agree whole heartedly. However, its not like
using C++ is an option. We are told by our proffessor to write this
program in C++ to figure out these financial things. Monte Carlo
simulation is something I have done in the past, and used other
software that was amazingly simple, such as crystal ball, where you
just input some stuff and away you go. However, we have to sit here
and derive how to do very basic simple things that excel, matlab, etc.
can do (or even using a statistics text book!); for example, we had to
write a program that calculated the normal_cdf function. I don't know
why, its not up to me, but we have to do what our proffessors tell us
to do on the software they assign us.

ian

Mar 7 '07 #11
On 7 Mar, 10:15, ianwe...@gmail.com wrote:
On Mar 7, 8:42 am, Stuart Redmann <DerTop...@web.dewrote:
ianwe...@gmail.com wrote:
This is what happens when you take people from a Mathematics and
Financial background, and try and make them develop C++ programs with
no real instruction on how to properly build them. We are definately
computer programming "noobs" =( and find it very frustrating. So I
think everything I do is gonna seem basic, strange, and chunky to
anyone with an extensive knowledge of C++
The thing that bugs me is why you chose C++ as development tool? Having
worked about 10 years with C++ (the last 3 of them on a daily basis), I
would consider myself as experienced with a tendency to expert, but
there are still a lot of things that I don't know. If you ask me I'd say
that C++ is definitely not the best choice for beginners, as it offers
too many features that are designed for real large scale projects but
are only confusing to beginners. I doubt that you would have had this
trouble if you had used other tools like Matlab or Maple (though they
are a lot more expensive than a C++ compiler that you can get everywhere
for free).
Having a lot of global variables is not bad in general, but makes the
code you have written next to impossible to re-use. For real small scale
programs (say less than 500 lines of code) global variables are
acceptable. If you want to spend more time learning C++ I'd advice you
to re-write your code with proper functions and without any global
variables. If you want to spend more time on the problem you have in
hand, you should better use another tool.
Regards,
Stuart

Sure, other programs are easier to use, and more user friendly, and
nicer for beginners; i agree whole heartedly. However, its not like
using C++ is an option. We are told by our proffessor to write this
program in C++ to figure out these financial things. Monte Carlo
simulation is something I have done in the past, and used other
software that was amazingly simple, such as crystal ball, where you
just input some stuff and away you go. However, we have to sit here
and derive how to do very basic simple things that excel, matlab, etc.
can do (or even using a statistics text book!); for example, we had to
write a program that calculated the normal_cdf function. I don't know
why, its not up to me, but we have to do what our proffessors tell us
to do on the software they assign us.
Since you have a background in math you should have no problem with
the idea of functions, so try to organize your code as a number of
functions that you call. Try to make each function depend only on its
given parameters, this should reduce the number of global variables.
Start with the basic functionalities and then build more "high-level"
functions that perform their job by calling other functions. This way
you will get code that can more easily be reused in other assignments.

--
Erik Wikström

Mar 7 '07 #12
Not a comment on any bugs as much as on style.

<ia******@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
>
>>
Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?

this is my current code on that section:

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=(double)Vj/(N-1);
return U;
}
Right, you're using a lot of global variables here. Now, if a, b and N are
being set by this function and not used elsewhere, why not just make them
local? (but then without looking at the rest of the program I don't know if
you're using a, b or N anywhere else, that's part of the evils of global
variables). I would first attempt to change this one function to this
(which may or may not work depending on the rest of your code)

double uniform_deviate( double& Vj )
{
int a = 1366;
int b = 150889;
int N = 714025;
Vj = ( a * Vj + b ) % N;
double U = Vj / ( N - 1 );
return U;
}

This function now does not depend on any variables that weren't passed to it
or are local to it. The only variable it changes that can be seen from
outside it are Vj, which is passed as a reference (meaning any changes in
this function will be seen by whatever calls it). Also the value that is
returned. But again, without looking at the rest of your code (which I
really didn't) I can't tell if you need a, b and N anywhere else. Or even
U. But U is being reutrned so that shouldn't matter.

double normal_deviate ()
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

important i guess to also note that in int main () Vj starts at a
value of 1.

i dont think that it calls the same number every time? i did a cout
<<U << in the uniform deviate function, and it was a different value
each time (because Vj keeps changing, its a very basic random number
generator, not a good one though). And the values in normal deviate
also appear to be changing, because i ran a cout <<Z << in there as
well, and it was numbers from the normal curve. Now each time i run
the program, it will give me the exact same "random" numbers i think,
but i think thats what we are supposed to do for this one. its a bad
random number generator mainly =P

What I am currently struggling with is the calculations within the int
main ().

I was miscalculating Am, because i was simply takign the last Vt value
and adding the last Si value from the loop.

here is the relevant code:

double Values[10];
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values[i] = exp(-r*T)*MAX(K-Si,0);

Vt = exp(-r*T)*MAX(K-Si,0);
//in this space here i need to calculate a sum of all the Si's. or
rather, need to use it in the line below. is there a sum function
where i could say sum Values[] and have it add all the numbers stored
in my array? because i need that sum divided by M, as seen below.

}
Am = ***need sum of numbers from array, not Vt*** Vt/M;
for(i=0; i<M; i++)
{
LP=LP+pow((Values[i]-Am),2.0);

}
Bm = LP/(M-1);
Lower = Am-(1.96*(pow(Bm,0.5))/(pow(M,0.5)));
Upper = Am+(1.96*(pow(Bm,0.5))/(pow(M,0.5)));

cout << "The mean is = " <<Am <<endl;
cout << "The variance is = " <<Bm <<endl;
cout << "The confidence interval is (" <<Lower << ", " <<Upper <<")
"<<endl;
return 0;

Mar 7 '07 #13

<ia******@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
>
>>
Also I was struck by how you were calling the function uniform_deviate
in a loop, but the way it is written it will always return the same
value, so what is the point of calling it repeatedly?

this is my current code on that section:

double uniform_deviate ()
{
a=1366;
b=150889;
N=714025;
Vj = (a*Vj + b) % N;
U=(double)Vj/(N-1);
return U;
}
double normal_deviate ()
{
V1=1.0;
V2=1.0;
while(V1*V1+V2*V2 1)
{
V1=2*uniform_deviate()-1;
V2=2*uniform_deviate()-1;
}
W=(V1*V1+V2*V2);
Z=V1*pow((-2*log(W))/W,0.5);
return Z;
}

important i guess to also note that in int main () Vj starts at a
value of 1.

i dont think that it calls the same number every time? i did a cout
<<U << in the uniform deviate function, and it was a different value
each time (because Vj keeps changing, its a very basic random number
generator, not a good one though). And the values in normal deviate
also appear to be changing, because i ran a cout <<Z << in there as
well, and it was numbers from the normal curve. Now each time i run
the program, it will give me the exact same "random" numbers i think,
but i think thats what we are supposed to do for this one. its a bad
random number generator mainly =P

What I am currently struggling with is the calculations within the int
main ().

I was miscalculating Am, because i was simply takign the last Vt value
and adding the last Si value from the loop.

here is the relevant code:

double Values[10];
for(i=0; i<M; i++)
{
Si = So*exp((r-0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
Values[i] = exp(-r*T)*MAX(K-Si,0);

Vt = exp(-r*T)*MAX(K-Si,0);
//in this space here i need to calculate a sum of all the Si's. or
rather, need to use it in the line below. is there a sum function
where i could say sum Values[] and have it add all the numbers stored
in my array? because i need that sum divided by M, as seen below.
John has already given good advice about your other issues, but I
don't think he answered this. Yes, there's a standard library function
you can use to compute a sum. It's called 'std::accumulate', and it's
declared by standard header <numeric>.

Here's an example of how to use it:

#include <iostream>
#include <numeric>

int main(void)
{
double values[] = {1, 2, 3, 4, 5};
size_t elems(sizeof values / sizeof *values);
std::cout << std::accumulate(values, values + elems, 0.0) << '\n';
return 0;
}

I also second John's advice to use a 'std::vector' instead of an array.

Here's an example of how to compute the sum using a vector:

#include <iostream>
#include <numeric>
#include <vector>

int main(void)
{
std::vector<double>::size_type m(5);
std::vector<doublevalues(m);

for(double d = 0; d < m; ++d)
values[d] = d + 1;

std::cout << std::accumulate(values.begin(), values.end(), 0.0) << '\n';
return 0;
}

(The last argument to 'accumulate()' is a starting value to which the
array or vector elements will be added. That's why it's called
'accumulate'
and not 'sum').

-Mike
Mar 7 '07 #14
On Mar 7, 1:51 am, ianwe...@gmail.com wrote:
[snip]
We are definately
computer programming "noobs"
General hint then: There is a news group "down the web"
that has "learning" in the title. Possibly they will have
much more help for you there.

Also, you want to get yourself a good textbook.
I quite like _Accelerated C++_ by Koenig and Moo.
You probably want that first, then Stroustrup's
latest on C++. Then after that, depending on how
much development you are going to do you may want
some good texts on how to program well. Say, along
the lines of _Code Complete_.
Socks

Mar 7 '07 #15
<ia******@gmail.comwrote:
Sure, other programs are easier to use, and more user friendly, and
nicer for beginners; i agree whole heartedly. However, its not like
using C++ is an option. We are told by our proffessor to write this
program in C++ to figure out these financial things. Monte Carlo
simulation is something I have done in the past, and used other
software that was amazingly simple, such as crystal ball, where you
just input some stuff and away you go. However, we have to sit here
and derive how to do very basic simple things that excel, matlab, etc.
can do (or even using a statistics text book!); for example, we had to
write a program that calculated the normal_cdf function. I don't know
why, its not up to me, but we have to do what our proffessors tell us
to do on the software they assign us.
Were you told not to use rand() and srand() which are an intrinsic part of
C++? That seems to be what your uniform_deviate() is doing. A few minutes
of patient searching might find code for normal_deviate() as well. Adding
the word 'snippets' to a search is sometimes helpful in looking for source
code.
Mar 7 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
Does python support named semaphores for Linux? I saw that ActivePython for Win32 does. Can I get a list of currently running processes? I already posted in the GTK forum about looking for the...
0
by: Todd Krein | last post by:
I'm running activestate Python 2.4 for windows, and the latest BLT, under XP. I'm using pythonWin as my environment. When I run my plotting program the first time, it works just fine. If I exit...
17
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
8
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
1
by: KeyboardSurfer | last post by:
I am still having problems with switching thru tasks in such a way that it will work properly for me ... here is the basic information and a code snippet follows below .. My program is running...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
0
by: Steve Ingram | last post by:
Found out what I'd done, and it wasn't py2exe causug the problem. I wasn't closing the main dialog properly, I was calling Close() instead of Destroy(), so the dialog stayed in memory, basically it...
4
waynetheengineer
by: waynetheengineer | last post by:
Hi everyone, I have a program that opens an Excel file, reads data from it, then closes the open workbook. But when I exit my program and go to edit that Excel file, it won't let me open it. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.