program isn't running properly

Discussion in 'C++' started by ianweise@gmail.com, Mar 7, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    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 = 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-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, 2007
    #2
    1. Advertising

  3. 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 = 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-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
     
    John Harrison, Mar 7, 2007
    #3
  4. Guest

    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, 2007
    #4
  5. 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
     
    John Harrison, Mar 7, 2007
    #5
  6. Guest


    >
    > 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 = 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-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, 2007
    #6
  7. 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
     
    John Harrison, Mar 7, 2007
    #7
  8. Guest

    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 = exp(-r*T)*MAX(K-Si,0);
    sum = sum + Values;
    }
    Am = sum/M;
    for(i=0; i<M; i++)
    {
    LP=LP+pow((Values-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, 2007
    #8
  9. 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 = exp(-r*T)*MAX(K-Si,0);
    > sum = sum + Values;
    > }
    > Am = sum/M;
    > for(i=0; i<M; i++)
    > {
    > LP=LP+pow((Values-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<double> Values(M);
    ....

    all other code is unchanged.

    john
     
    John Harrison, Mar 7, 2007
    #9
  10. 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
     
    Stuart Redmann, Mar 7, 2007
    #10
  11. Guest

    On Mar 7, 8:42 am, Stuart Redmann <> wrote:
    > 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, 2007
    #11
  12. On 7 Mar, 10:15, wrote:
    > On Mar 7, 8:42 am, Stuart Redmann <> wrote:
    >
    >
    >
    > > 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 7, 2007
    #12
  13. Jim Langston Guest

    Not a comment on any bugs as much as on style.

    <> wrote in message
    news:...
    >
    >>
    >> 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 = 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-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;
    >
     
    Jim Langston, Mar 7, 2007
    #13
  14. Mike Wahler Guest

    <> wrote in message
    news:...
    >
    >>
    >> 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 = 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<double> values(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
     
    Mike Wahler, Mar 7, 2007
    #14
  15. Puppet_Sock Guest

    On Mar 7, 1:51 am, 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
     
    Puppet_Sock, Mar 7, 2007
    #15
  16. osmium Guest

    <> wrote:

    > 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.
     
    osmium, Mar 7, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    0
    Views:
    492
    TN Bella
    Jul 2, 2004
  2. Bart Nessux
    Replies:
    3
    Views:
    280
    Bart Nessux
    Jan 23, 2004
  3. judith
    Replies:
    5
    Views:
    362
    Mark Space
    Oct 17, 2006
  4. Katherine

    web service isn't called properly

    Katherine, Mar 9, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    132
    Dan Rogers
    Mar 9, 2005
  5. G G
    Replies:
    6
    Views:
    209
Loading...

Share This Page