Uniform Random Numbers generation using class template RandomSequenceGenerator<>

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Uniform Random Numbers generation using class template RandomSequenceGenerator<>

Amine Ifri
Hi all,

This is my first posting on the Quantlib Users forum.

I am currently working on a small random numbers generation framework for a simulation model, and using the class template RandomSequenceGenerator<> with the Mersenne Twister generator provided with QL.

I generate 30 scenarios per time point, along 80 time points or so. I noticed that the format of the numbers outputted on my std::out is set to some weird floating representations (e-294, etc…) and after a while the generator sets the numbers to 0.

Any help on this will be very much appreciated,

Thanks,
Amine


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

signature.asc (859 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Uniform Random Numbers generation using class template RandomSequenceGenerator<>

Luigi Ballabio

Hello Amine,
   may you post your code?

Luigi


On Fri, Jan 27, 2017, 14:01 Amine Ifri <[hidden email]> wrote:
Hi all,

This is my first posting on the Quantlib Users forum.

I am currently working on a small random numbers generation framework for a simulation model, and using the class template RandomSequenceGenerator<> with the Mersenne Twister generator provided with QL.

I generate 30 scenarios per time point, along 80 time points or so. I noticed that the format of the numbers outputted on my std::out is set to some weird floating representations (e-294, etc…) and after a while the generator sets the numbers to 0.

Any help on this will be very much appreciated,

Thanks,
Amine

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Uniform Random Numbers generation using class template RandomSequenceGenerator<>

Luigi Ballabio
Hi Amine,
    I'm adding the mailing list back in cc so that others can help.

About your code: it's difficult to say what goes wrong without seeing, for instance, how your generator_ instance is created.  Adding that part of your code would help.  This said, a few suggestions:

- you might try printing out the values of v and w inside CreateCorrelatedUniformNumbers, in order to check what comes out of the generator and what it becomes after it's multiplied by the correlation;

- you might want to print out values_ after the generation, to make sure that you're copying it correctly out of the getValues() method;

- also, you probably got your call to the generator at the wrong point in the loop.  Right now it's inside both the (for i...) and the (for j...) loop, which means that you're sampling a whole vector, then you copy only w[i], then you throw the rest away and sample another whole vector.  I'm guessing you wanted something like the following instead:

for (int j=0; j<timesteps; ++j) {
    v = (generator_.nextSequence()).value;
    ...
    for (int i=0; i<w.size(); ++i) {
        (values_[i])(k,j) = w[i];
    }
}

or some other loop structure.

Later,
    Luigi


On Fri, Jan 27, 2017 at 2:13 PM Amine Ifri <[hidden email]> wrote:
Luigi,

Its a bit long and spread out in various .hpp, but here is the main part of it: 

template<typename Gen>
void SimulationFrameWork::AssetSimulation::UniformRandoms<Gen>::CreateCorrelatedUniformNumbers(Size nb_scenarios, Size timepoints)
{
Disposable<Matrix> sqrt_correl = pseudoSqrt(correlation_matrix_,SalvagingAlgorithm::None);
vector<Real> v;


Array w(correlation_matrix_.rows(), 0);

for (auto i = 0; i< w.size(); ++i) values_[i] = Matrix(nb_scenarios, timepoints, 0);

for (auto k = 0; k< nb_scenarios; ++k)
{ for (auto i =0; i<w.size(); ++i)
{ for (auto j = 0; j< timepoints ;++j)
{ v = (generator_.nextSequence()).value;  this is where something probably strange happens, or maybe an issue of output formatting ?
Array a(v.begin(), v.end());
w = sqrt_correl * a;
(values_[i])(k,j) = w[i];
//std::cout<<"scenario "<< k <<" time point "<< j<<" for asset: " << i << " : " << (values_[i])(k,j) <<"\n";
}}
}
}

inline std::ostream& operator<<(std::ostream& out, const UniformRandoms& rands)
{
vector<Matrix> values = rands.getValues();
for (int i = 0; i<values.size(); ++i)
{
for (int k = 0; values[i].columns(); ++k)
{
for (int j = 0; j< values[i].rows(); ++j)
{
out << "|" << values[i](j,k)<<"\n";
}
out << "\n";
}
out << "=====================" << "\n";
}

return out;
}

The weird output is something like (forget the “| character):  

|1.31481e-259
|5.40126e-62
|7.97701e-72
|5.39455e-62
|7.58067e-96
|4.755e-38
|1.91825e-76
|4.58223e-72
|1.58822e-47
|3.45301e-86
|9.96356e-43
|1.08646e-71
|1.65502e-47
|7.59532e-96
|4.755e-38
|6.38272e-67
|1.30372e-76
|2.17733e-76
|3.45301e-86
|1.79454e-52
|7.97702e-72
|1.65502e-47
|7.57884e-96
|1.55795e-76
|2.17598e-76
|1.30372e-76
|2.00393e-76
|1.31481e-259
|9.51515e-43
|7.977e-72




Thanks for the help!


On 27 Jan 2017, at 13:03, Luigi Ballabio <[hidden email]> wrote:

Hello Amine,
   may you post your code?

Luigi


On Fri, Jan 27, 2017, 14:01 Amine Ifri <[hidden email]> wrote:
Hi all,

This is my first posting on the Quantlib Users forum.

I am currently working on a small random numbers generation framework for a simulation model, and using the class template RandomSequenceGenerator<> with the Mersenne Twister generator provided with QL.

I generate 30 scenarios per time point, along 80 time points or so. I noticed that the format of the numbers outputted on my std::out is set to some weird floating representations (e-294, etc…) and after a while the generator sets the numbers to 0.

Any help on this will be very much appreciated,

Thanks,
Amine

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Uniform Random Numbers generation using class template RandomSequenceGenerator<>

Amine Ifri
Luigi, all,

Just to give you the heads up that the below matter is closed. It was more an error of printing the values rather than a design problem. The small bit I have written creates a simulation framework where one can leverage the already developed Random sequence generators from QL to build a set of correlated gaussians (via Square factorisation, e.g Cholesky), per asset, time point/scenario. This comes handy for evolving stochastic processes through time and scenario for application in credit exposure calculations.

Happy to share my code…

cheers,
Amine

On 27 Jan 2017, at 14:22, Luigi Ballabio <[hidden email]> wrote:

Hi Amine,
    I'm adding the mailing list back in cc so that others can help.

About your code: it's difficult to say what goes wrong without seeing, for instance, how your generator_ instance is created.  Adding that part of your code would help.  This said, a few suggestions:

- you might try printing out the values of v and w inside CreateCorrelatedUniformNumbers, in order to check what comes out of the generator and what it becomes after it's multiplied by the correlation;

- you might want to print out values_ after the generation, to make sure that you're copying it correctly out of the getValues() method;

- also, you probably got your call to the generator at the wrong point in the loop.  Right now it's inside both the (for i...) and the (for j...) loop, which means that you're sampling a whole vector, then you copy only w[i], then you throw the rest away and sample another whole vector.  I'm guessing you wanted something like the following instead:

for (int j=0; j<timesteps; ++j) {
    v = (generator_.nextSequence()).value;
    ...
    for (int i=0; i<w.size(); ++i) {
        (values_[i])(k,j) = w[i];
    }
}

or some other loop structure.

Later,
    Luigi


On Fri, Jan 27, 2017 at 2:13 PM Amine Ifri <[hidden email]> wrote:
Luigi,

Its a bit long and spread out in various .hpp, but here is the main part of it: 

template<typename Gen>
void SimulationFrameWork::AssetSimulation::UniformRandoms<Gen>::CreateCorrelatedUniformNumbers(Size nb_scenarios, Size timepoints)
{
Disposable<Matrix> sqrt_correl = pseudoSqrt(correlation_matrix_,SalvagingAlgorithm::None);
vector<Real> v;

Array w(correlation_matrix_.rows(), 0);

for (auto i = 0; i< w.size(); ++i) values_[i] = Matrix(nb_scenarios, timepoints, 0);

for (auto k = 0; k< nb_scenarios; ++k)
{ for (auto i =0; i<w.size(); ++i)
{ for (auto j = 0; j< timepoints ;++j)
{ v = (generator_.nextSequence()).value;  this is where something probably strange happens, or maybe an issue of output formatting ?
Array a(v.begin(), v.end());
w = sqrt_correl * a;
(values_[i])(k,j) = w[i];
//std::cout<<"scenario "<< k <<" time point "<< j<<" for asset: " << i << " : " << (values_[i])(k,j) <<"\n";
}}
}
}

inline std::ostream& operator<<(std::ostream& out, const UniformRandoms& rands)
{
vector<Matrix> values = rands.getValues();
for (int i = 0; i<values.size(); ++i)
{
for (int k = 0; values[i].columns(); ++k)
{
for (int j = 0; j< values[i].rows(); ++j)
{
out << "|" << values[i](j,k)<<"\n";
}
out << "\n";
}
out << "=====================" << "\n";
}

return out;
}

The weird output is something like (forget the “| character):  

|1.31481e-259
|5.40126e-62
|7.97701e-72
|5.39455e-62
|7.58067e-96
|4.755e-38
|1.91825e-76
|4.58223e-72
|1.58822e-47
|3.45301e-86
|9.96356e-43
|1.08646e-71
|1.65502e-47
|7.59532e-96
|4.755e-38
|6.38272e-67
|1.30372e-76
|2.17733e-76
|3.45301e-86
|1.79454e-52
|7.97702e-72
|1.65502e-47
|7.57884e-96
|1.55795e-76
|2.17598e-76
|1.30372e-76
|2.00393e-76
|1.31481e-259
|9.51515e-43
|7.977e-72




Thanks for the help!


On 27 Jan 2017, at 13:03, Luigi Ballabio <[hidden email]> wrote:

Hello Amine,
   may you post your code?

Luigi


On Fri, Jan 27, 2017, 14:01 Amine Ifri <[hidden email]> wrote:
Hi all,

This is my first posting on the Quantlib Users forum.

I am currently working on a small random numbers generation framework for a simulation model, and using the class template RandomSequenceGenerator<> with the Mersenne Twister generator provided with QL.

I generate 30 scenarios per time point, along 80 time points or so. I noticed that the format of the numbers outputted on my std::out is set to some weird floating representations (e-294, etc…) and after a while the generator sets the numbers to 0.

Any help on this will be very much appreciated,

Thanks,
Amine

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

signature.asc (859 bytes) Download Attachment