0

Happy Holidays, everyone !

I'm trying to generate paths of a square root process using the QuantLib/Boost C++ libraries and have encountered what I believe to be an annoying little problem with a fast and simple solution! I'm pretty new to programming so please don't be too harsh on me :) Here's what I know: 1. The constructor looks like this:

SquareRootProcess( Real b, Real a, Volatility sigma, Real x0 = 0.0, const boost::shared_ptr<discretization>& d = boost::shared_ptr<discretization>(new EulerDiscretization))

  1. The crucial function to be used when simulating a stochastic process with QuantLib is evolve(t,x,dt,dw).

  2. Here's what my code looks like:

    #include "stdafx.h" 
    #include <ql/quantlib.hpp>
    #include <ql/stochasticprocess.hpp>
    #include <ql/processes/squarerootprocess.hpp>
    #include <ql/Processes/eulerdiscretization.hpp>
    using namespace QuantLib;
    void SquareRootProcessSimulation()
    {
    
    Real miu0=0.0;
    Real miu;
    Real b=0.3;
    Real a=5.5;
    Volatility sigma=2.02;
    
    BigInteger seed=12324;
    MersenneTwisterUniformRng unifMt(seed);
    BoxMullerGaussianRng<MersenneTwisterUniformRng> bmGauss(unifMt);
    
    const boost::shared_ptr<StochasticProcess1D::discretization> &d = 
                       boost::shared_ptr<StochasticProcess1D::discretization>(
                        EndEulerDiscretization);
    boost::shared_ptr<SquareRootProcess> squareRootProcess(new 
                                      SquareRootProcess(b, a, sigma, miu0, d&));
    
    Time dt=0.1,t=0.0;
    Real dw;
    Size numVals=10;
    
    for (Size j=1;j<=numVals;++j)
    {
          dw=bmGauss.next().value;
      miu=squareRootProcess->evolve(t,miu0,dt,dw);
          std::cout << "Time: " << t+dt << ", miu_t: " << miu << std::endl;
      t+=dt;
    }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        SquareRootProcessSimulation();
        std::cin.get();
        return 0;
    }
    

    `


I get no errors when compiling/running the code, but what comes out is a constant value, i.e. something is obviously wrong. I think the problem is in the way I've defined the stochastic process, I cannot quite figure out how the interpret the last part of the constructor with the boost::shared_ptr.

I'm happy to hear any suggestions and hints and thanks for taking the time to read my question!

best regards :)

2 Answers2

1

I'm not quite sure if this will solve the problem, but at least I want to try to help:

First of all let's have a look the constructor of SquareRootProcess:

SquareRootProcess( Real b, 
                Real a, 
                Volatility sigma, 
                Real x0 = 0.0, 
                const boost::shared_ptr<discretization>& d = boost::shared_ptr<discretization>(new EulerDiscretization))

As you can see the last two parameters have default values. This means you can call the function like this

SquareRoot(b,a,sigma);

This would mean that the function is called with the values of b, a and sigma. x0 and d ( the last two parameters ) would get their default values as written in the constructor. In this case that would be 0.0 for x0 and a new shared pointer object of the type discretization. But since you want the value from the last paramter the default value is not the right choice for you.

As far as I can tell the function SquareRootProcess will calculate some stuff and then store the data at the pointers address. Here we come to the second part of the Constructor, the &.

The & in the parameter list means that you pass the function a reference to a shared pointer. This means if you call the function your pointer will ( most likely ) be changed and point to the desired value. If the function has a call by reference you actually don't need to add any signs in the function call. Just to make things clear, the same process with some integers:

void add(int a,int b,int& sum)
{
    sum = a + b;
}
int main()
{
     int sum;
     add(5,12,sum);
     // Now sum has the value 17 
     return 0;
}

So long story short : If a function expects a reference to an object you just pass the object itself in the function call.

So now back to your case:

You just need to create an shared pointer with the type discretization and then pass it on in the function call.

const boost::shared_ptr<StochasticProcess1D::discretization> d(new StochasticProcess1D::discretizitation(/*Whatever constructor is needed for this type*/));
boost::shared_ptr<SquareRootProcess> squareRootProcess(new SquareRootProcess(b, a, sigma, miu0, d));

This should actually do the deal. Just let me know if it worked or if you have any further questions. Best Regards

Toby
  • 3,815
  • 14
  • 51
  • 67
  • Thank you so much for the quick response! I tried the following : `const boost::shared_ptr d(new EulerDiscretization); boost::shared_ptr squareRootProcess(new SquareRootProcess(b, a, sigma, miu0, d));` but unfortunately I still get constant results, something still doesn't work! – sunshine Dec 27 '11 at 14:24
  • Let me just ask some questions, to make things more clear: 1. How do you read the value of d? ( How do you know it didn't change ) . You actually can't just read the value out of d ( it's the address ). E.g. with the boost shared pointers you can access the value with `d.get()` I actually don't get exactly which value you expect to change and how you check it. 2. Is there any documentation to this function? Or do you have the implementation? – Toby Dec 27 '11 at 14:37
  • When I run the entire code, it should cout 10 different values of miu (i.e. the values in 10 different moments in time - from 0.1 to 1). But I get the same values in each and every time step, which implies that the process is constant. Hence I believe I didn't defined it correctly. The value of d can be either - EulerDiscretization or EndEulerDiscretization - as far as I know. I wouldn't expect it to change, it's just a condition for the simulation of the process that I set out in the beginning and that should remain the same throughout the whole path generation. Or am I getting it wrong? – sunshine Dec 27 '11 at 14:49
  • Here is all the information I have at my disposal : http://quantlib.sourcearchive.com/documentation/1.1/classQuantLib_1_1SquareRootProcess.html – sunshine Dec 27 '11 at 14:51
  • Ah okay, well now I see more where your problem lies. Unfortunately it doesn't bring up any great ideas here. Isn't there any example code from quantlib, where someone already tried this? Sorry that I can't help you any further :( – Toby Dec 27 '11 at 15:04
1

As already pointed out, you don't need to pass the discretization object if you don't want to customize it, so

boost::shared_ptr<SquareRootProcess> squareRootProcess(new 
                              SquareRootProcess(b, a, sigma, miu0));

will do for you. The problem I'm seeing is with the repeated calls to evolve; you wrote

miu=squareRootProcess->evolve(t,miu0,dt,dw);

but that will cause each step to always start from miu0. The idea here is that you start from t=0 and x=miu0 and evolve() gives you the new miu at t=0.1. Then you start from t=0.1 and x=miu (not miu0) and generate another step. So you'll have to write something like:

miu = miu0;
for (Size j=1;j<=numVals;++j)
{
    ...
    miu=squareRootProcess->evolve(t,miu,dt,dw);  // not miu0
    ...
}

to get the desired behavior.

As for documentation, you might want to take a look at chapter 6 of Implementing QuantLib, which describes the Monte Carlo framework. It will also get you started with path generators; you can use those to generate paths without having to drive the process yourself.

Oh, by the way: is there any particular reason you're using the docs at sourcearchive.com, instead of the "official" ones on quantlib.org? Is there any way you think we should improve them?

Luigi Ballabio
  • 4,128
  • 21
  • 29
  • thank you so so much ! I knew it had to be something minor :) quite frankly, there is no particular reason for me to use the docs at sourcearchive.com, they were simply the first set of documentation I came across and I kept on using it. Thank you for the hint though, I'll check out quantlib.org as well. Have a wonderful New Year's Eve! – sunshine Dec 28 '11 at 09:20