Not a comment on any bugs as much as on style.
<(EMail Removed)> wrote in message
news:(EMail Removed) 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/(N1);
> 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((r0.5*v*v)*T+v*pow(T,0.5)*normal_deviate());
> Values[i] = exp(r*T)*MAX(KSi,0);
>
> Vt = exp(r*T)*MAX(KSi,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/(M1);
> 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;
>
