RandomNumberGenerator

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

RandomNumberGenerator

John Kiff
Could someone please give me some pointers to where I can find out more about setting the key
parameters of the RandomNumberGenerator function: "dimension", "samples", "RNGType" and "seed"?


       
               
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

mike.parkerql
check out rngtraits.hpp and rngtypedefs.hpp

The default pseudo random generator is Mersenne Twister Uniform generator with
inverse cumulative normal to convert to gaussian. See typedef PseudoRandom.

e.g.
                Size dim1 = 20; //sequence length.
                long seed = 12345;

                GaussianRandomSequenceGenerator grsg =
PseudoRandom::make_sequence_generator(dim1, seed);
                Sample<Array> res = grsg.nextSequence();
                std::cout << res.value <<std::endl;
                std::cout << std::endl;



Examples of usage in e.g. mcvanillaengine.hpp

or could try,
                typedef GenericPseudoRandom<KnuthUniformRng,
InverseCumulativeNormal> fred;
                fred::rsg_type gen = fred::make_sequence_generator(dim1, seed);
                std::cout << gen.nextSequence().value <<std::endl;

Also, check out random numbers namespace. Example below uses Box-Mueller
method of transforming uniforms into gaussians.

                BoxMullerGaussianRng<MersenneTwisterUniformRng> bm_grsg(seed);
                for (Size sim=0; sim<10; sim++)
                        std::cout << bm_grsg.next().value << std::endl;

                BoxMullerGaussianRng<KnuthUniformRng> bm_grsg(seed); etc.

Hope this helps.

Mike

Quoting John Kiff <[hidden email]>:

> Could someone please give me some pointers to where I can find out more about
> setting the key
> parameters of the RandomNumberGenerator function: "dimension", "samples",
> "RNGType" and "seed"?
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Friends.  Fun.  Try the all-new Yahoo! Messenger.
> http://messenger.yahoo.com/ 
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the new InstallShield X.
> From Windows to Linux, servers to mobile, InstallShield X is the one
> installation-authoring solution that does it all. Learn more and
> evaluate today! http://www.installshield.com/Dev2Dev/0504
> _______________________________________________
> Quantlib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>


--


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

John Kiff
Mike, thanks for that, but I was also wondering if there is something more fundamental that I
should be looking at. I checked out my "Numerical Recipes" but so no obvious reference to
dimensions and samples. Maybe I'm using the wrong function, in that I just want to generate a
vector of random standard normals, and nothing much more than that. Anyways, perhaps the
Glasserman and Jackel books go deeper into these nuances, and I have neither. Any opinions as to
which will be the most helpful? John

--- [hidden email] wrote:

> check out rngtraits.hpp and rngtypedefs.hpp
>
> The default pseudo random generator is Mersenne Twister Uniform generator with
> inverse cumulative normal to convert to gaussian. See typedef PseudoRandom.
>
> e.g.
> Size dim1 = 20; //sequence length.
> long seed = 12345;
>
> GaussianRandomSequenceGenerator grsg =
> PseudoRandom::make_sequence_generator(dim1, seed);
> Sample<Array> res = grsg.nextSequence();
> std::cout << res.value <<std::endl;
> std::cout << std::endl;
>
>
>
> Examples of usage in e.g. mcvanillaengine.hpp
>
> or could try,
> typedef GenericPseudoRandom<KnuthUniformRng,
> InverseCumulativeNormal> fred;
> fred::rsg_type gen = fred::make_sequence_generator(dim1, seed);
> std::cout << gen.nextSequence().value <<std::endl;
>
> Also, check out random numbers namespace. Example below uses Box-Mueller
> method of transforming uniforms into gaussians.
>
> BoxMullerGaussianRng<MersenneTwisterUniformRng> bm_grsg(seed);
> for (Size sim=0; sim<10; sim++)
> std::cout << bm_grsg.next().value << std::endl;
>
> BoxMullerGaussianRng<KnuthUniformRng> bm_grsg(seed); etc.
>
> Hope this helps
>
> Quoting John Kiff <[hidden email]>:
>
> > Could someone please give me some pointers to where I can find out more about
> > setting the key
> > parameters of the RandomNumberGenerator function: "dimension", "samples",
> > "RNGType" and "seed"?


       
               
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

mike.parkerql
Hopefully I'm interpreting you correctly.

I have the Glasserman book, but haven't had time to go through it in detail.
It indeed contains some things that I think you're looking for.
To paraphrase:

A d-dimensional normal distribution is characterized by a d-vector of means,u,
and a dxd covariance matrix SIGMA.
So, you want to generate random samples from N(u, SIGMA).

If (d-dimension) Z distributed N(0, I) where I is identity matrix (i.e. just
generate d independent standard normals), then apply the transformation X = u
+ A*Z, where A is some dxd matrix. Then X is distributed N(u, AA^T) where A^T
is the transpose of A.

So, the problem boils down to finding A such that AA^T = SIGMA.
If A is in lower triangular form then you reduce the number of multiplications
you need to make. This is called Cholesky factorization, and there is a simple
algorithm to find A from SIGMA.

If you want to work with correlation matrix, rather than covariance matrix
then:
If i-th component has standard deviation sigma_i, can express SIGMA in terms
of the correlation matrix rho_i_j by
SIGMA_i_j = sigma_i * rho_i_j * sigma_j

Of course, for Brownian motion, you need to ensure that covariance wrt time
scales like t-s. This is like transforming standard normals by a lower
triangular matrix which contains the terms sqrt(t_j - t_i) for i<j, zero
otherwise, which is implicit in the usual construction: W(t_j)=W(t_i)+sqrt(t_j-
t_i)*Z(t_j). Z(t_j) distributed N(0, SIGMA).

To see these things in action, then look at e.g. multipathgenerator.hpp in the
montecarlo namespace.

Mike

Quoting John Kiff <[hidden email]>:

> Mike, thanks for that, but I was also wondering if there is something more
> fundamental that I
> should be looking at. I checked out my "Numerical Recipes" but so no obvious
> reference to
> dimensions and samples. Maybe I'm using the wrong function, in that I just
> want to generate a
> vector of random standard normals, and nothing much more than that. Anyways,
> perhaps the
> Glasserman and Jackel books go deeper into these nuances, and I have neither.
> Any opinions as to
> which will be the most helpful? John
>
> --- [hidden email] wrote:
> > check out rngtraits.hpp and rngtypedefs.hpp
> >
> > The default pseudo random generator is Mersenne Twister Uniform generator
> with
> > inverse cumulative normal to convert to gaussian. See typedef
> PseudoRandom.
> >
> > e.g.
> > Size dim1 = 20; //sequence length.
> > long seed = 12345;
> >
> > GaussianRandomSequenceGenerator grsg =
> > PseudoRandom::make_sequence_generator(dim1, seed);
> > Sample<Array> res = grsg.nextSequence();
> > std::cout << res.value <<std::endl;
> > std::cout << std::endl;
> >
> >
> >
> > Examples of usage in e.g. mcvanillaengine.hpp
> >
> > or could try,
> > typedef GenericPseudoRandom<KnuthUniformRng,
> > InverseCumulativeNormal> fred;
> > fred::rsg_type gen = fred::make_sequence_generator(dim1, seed);
> > std::cout << gen.nextSequence().value <<std::endl;
> >
> > Also, check out random numbers namespace. Example below uses Box-Mueller
> > method of transforming uniforms into gaussians.
> >
> > BoxMullerGaussianRng<MersenneTwisterUniformRng> bm_grsg(seed);
> > for (Size sim=0; sim<10; sim++)
> > std::cout << bm_grsg.next().value << std::endl;
> >
> > BoxMullerGaussianRng<KnuthUniformRng> bm_grsg(seed); etc.
> >
> > Hope this helps
> >
> > Quoting John Kiff <[hidden email]>:
> >
> > > Could someone please give me some pointers to where I can find out more
> about
> > > setting the key
> > > parameters of the RandomNumberGenerator function: "dimension",
> "samples",
> > > "RNGType" and "seed"?
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Friends.  Fun.  Try the all-new Yahoo! Messenger.
> http://messenger.yahoo.com/ 
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the new InstallShield X.
> From Windows to Linux, servers to mobile, InstallShield X is the one
> installation-authoring solution that does it all. Learn more and
> evaluate today! http://www.installshield.com/Dev2Dev/0504
> _______________________________________________
> Quantlib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>


--


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

John Kiff
Actually Mike, your answer goes beyond my question, albeit in a very interesting way. I am indeed
trying to simulate correlated Gaussian random numbers, but I'm doing it the long winded way. That
is, I have been generating uncorrelated simple random numbers [0,1] using the Excel RAND(0)
function, and multiplying this vector by the matrix created via the Cholesky factorization of the
correlation matrix, etc. From what you've told me QuantLib has a nice way of accomplishing most of
what takes me a number of steps in one step.

However, I'm quite satisfied to plod along for now, for pedagogic reasons, so I was just looking
for a better RAND(). I thought that RandomNumberGenerator() was just that, but I'm thrown by the
need for specifying not only a seed, but also an RNGType, "dimension" and "samples". I figure that
"RNGType", which appears to be numeric value, corresponds to some numbering scheme that specifies
a particular generator (e.g., Mersenne Twister) but I can't find the legend. However, if my need
is to produce a single random number [0,1] I can't figure out where "dimension" and "samples"
comes in.

BTW, I think that QuantLib, and particularly QuantLibXL, looks like a very promising structure. I
work in the risk management area at a G-10 central bank, and I'm particularly interested in using
open source software to develop central-bank-specific trading and risk management software. Other
central banks are interested, although they are tending to lean in favour of using MatLab as the
foundation. I'm hoping I can drum up some support to set out in a very different direction, and I
think that QuantLib could be part of that "road".

John

--- [hidden email] wrote:

> Hopefully I'm interpreting you correctly.
>
> I have the Glasserman book, but haven't had time to go through it in detail.
> It indeed contains some things that I think you're looking for.
> To paraphrase:
>
> A d-dimensional normal distribution is characterized by a d-vector of means,u,
> and a dxd covariance matrix SIGMA.
> So, you want to generate random samples from N(u, SIGMA).
>
> If (d-dimension) Z distributed N(0, I) where I is identity matrix (i.e. just
> generate d independent standard normals), then apply the transformation X = u
> + A*Z, where A is some dxd matrix. Then X is distributed N(u, AA^T) where A^T
> is the transpose of A.
>
> So, the problem boils down to finding A such that AA^T = SIGMA.
> If A is in lower triangular form then you reduce the number of multiplications
> you need to make. This is called Cholesky factorization, and there is a simple
> algorithm to find A from SIGMA.
>
> If you want to work with correlation matrix, rather than covariance matrix
> then:
> If i-th component has standard deviation sigma_i, can express SIGMA in terms
> of the correlation matrix rho_i_j by
> SIGMA_i_j = sigma_i * rho_i_j * sigma_j
>
> Of course, for Brownian motion, you need to ensure that covariance wrt time
> scales like t-s. This is like transforming standard normals by a lower
> triangular matrix which contains the terms sqrt(t_j - t_i) for i<j, zero
> otherwise, which is implicit in the usual construction: W(t_j)=W(t_i)+sqrt(t_j-
> t_i)*Z(t_j). Z(t_j) distributed N(0, SIGMA).
>
> To see these things in action, then look at e.g. multipathgenerator.hpp in the
> montecarlo namespace.



       
               
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

Luigi Ballabio-2
On 2004.06.08 03:36, John Kiff wrote:
> I was just looking for a better RAND(). I thought that  
> RandomNumberGenerator() was just that, but I'm thrown by the
> need for specifying not only a seed, but also an RNGType, "dimension"
> and "samples". I figure that "RNGType", which appears to be numeric  
> value, corresponds to some numbering scheme that specifies
> a particular generator (e.g., Mersenne Twister) but I can't find the
> legend. However, if my need is to produce a single random number  
> [0,1] I can't figure out where "dimension" and "samples" comes in.

John,
        I'm not familiar with QuantLibXL at all, but as Obi-Wan used to  
say, "use the source, Luke." It appears that in true spreadsheet  
tradition, RandomNumberGenerator and its Gaussian counterpart were  
meant to be jacks of all trades. They can use either a pseudo-random  
(RNGType = 1) or a low-discrepancy (RNGType = 2) generator. The  
"dimension" parameter is used to return either a single number or a  
random vector of a given dimension (because for low-discrepancy  
generators, it does matter whether you want a single multi-dimensional  
sample in R^N or N consecutive samples in R.) Finally, the function  
kindly offers to return M samples in one single call--be they numbers  
or vectors. To summarize, if you want a single number at every call,  
you can put dimension = samples = 1.

I'll purposely refrain from commenting on the idea of "generality"  
meant as "cram as much parameters as you can" :)

Hope this helps,
                Luigi


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

John Kiff
Luigi, thanks very much. This will definitely get me off the ground. I see the merits in going to
the "source" but I live in a galaxy of spreadsheets. John

--- Luigi Ballabio <[hidden email]> wrote:

> I'm not familiar with QuantLibXL at all, but as Obi-Wan used to  
> say, "use the source, Luke." It appears that in true spreadsheet  
> tradition, RandomNumberGenerator and its Gaussian counterpart were  
> meant to be jacks of all trades. They can use either a pseudo-random  
> (RNGType = 1) or a low-discrepancy (RNGType = 2) generator. The  
> "dimension" parameter is used to return either a single number or a  
> random vector of a given dimension (because for low-discrepancy  
> generators, it does matter whether you want a single multi-dimensional  
> sample in R^N or N consecutive samples in R.) Finally, the function  
> kindly offers to return M samples in one single call--be they numbers  
> or vectors. To summarize, if you want a single number at every call,  
> you can put dimension = samples = 1.
>
> I'll purposely refrain from commenting on the idea of "generality"  
> meant as "cram as much parameters as you can" :)


       
               
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 


Reply | Threaded
Open this post in threaded view
|

Re: RandomNumberGenerator

mike.parkerql
In reply to this post by John Kiff
Yeah, never trust an implementation of a random number generator unless you
can verify its properties. I don't know how good or bad Excel Rand() is, but I
was told that its cycle length/period is something around 13 million, compared
to 2^Huge for Mersenne + 623 equi-distributed.

I've used QuantLibXL and XLW, and it's easy to implement - just follow the
examples ....
I did have a problem with it going to calculation mode as soon as the final
piece of data is put into the wizard (i.e. before clicking OK on the wizard),
but that's about it.
I'm not sure, but maybe the QuantLibXL has an rng already implemented?

You may also like to compare values with NTRand (search the net).

Have fun! And if you can contribute to the development of QuantLib, then come
on and join the party!

BTW, you always mentioned normal random variates rather than Brownian random
variates. Don't forget (forgive me if I'm stating the obvious) the sqrt(t-s)
bits in the latter.

Mike

Quoting John Kiff <[hidden email]>:

> Actually Mike, your answer goes beyond my question, albeit in a very
> interesting way. I am indeed
> trying to simulate correlated Gaussian random numbers, but I'm doing it the
> long winded way. That
> is, I have been generating uncorrelated simple random numbers [0,1] using the
> Excel RAND(0)
> function, and multiplying this vector by the matrix created via the Cholesky
> factorization of the
> correlation matrix, etc. From what you've told me QuantLib has a nice way of
> accomplishing most of
> what takes me a number of steps in one step.
>
> However, I'm quite satisfied to plod along for now, for pedagogic reasons, so
> I was just looking
> for a better RAND(). I thought that RandomNumberGenerator() was just that,
> but I'm thrown by the
> need for specifying not only a seed, but also an RNGType, "dimension" and
> "samples". I figure that
> "RNGType", which appears to be numeric value, corresponds to some numbering
> scheme that specifies
> a particular generator (e.g., Mersenne Twister) but I can't find the legend.
> However, if my need
> is to produce a single random number [0,1] I can't figure out where
> "dimension" and "samples"
> comes in.
>
> BTW, I think that QuantLib, and particularly QuantLibXL, looks like a very
> promising structure. I
> work in the risk management area at a G-10 central bank, and I'm particularly
> interested in using
> open source software to develop central-bank-specific trading and risk
> management software. Other
> central banks are interested, although they are tending to lean in favour of
> using MatLab as the
> foundation. I'm hoping I can drum up some support to set out in a very
> different direction, and I
> think that QuantLib could be part of that "road".
>
> John
>
> --- [hidden email] wrote:
> > Hopefully I'm interpreting you correctly.
> >
> > I have the Glasserman book, but haven't had time to go through it in
> detail.
> > It indeed contains some things that I think you're looking for.
> > To paraphrase:
> >
> > A d-dimensional normal distribution is characterized by a d-vector of
> means,u,
> > and a dxd covariance matrix SIGMA.
> > So, you want to generate random samples from N(u, SIGMA).
> >
> > If (d-dimension) Z distributed N(0, I) where I is identity matrix (i.e.
> just
> > generate d independent standard normals), then apply the transformation X =
> u
> > + A*Z, where A is some dxd matrix. Then X is distributed N(u, AA^T) where
> A^T
> > is the transpose of A.
> >
> > So, the problem boils down to finding A such that AA^T = SIGMA.
> > If A is in lower triangular form then you reduce the number of
> multiplications
> > you need to make. This is called Cholesky factorization, and there is a
> simple
> > algorithm to find A from SIGMA.
> >
> > If you want to work with correlation matrix, rather than covariance matrix
>
> > then:
> > If i-th component has standard deviation sigma_i, can express SIGMA in
> terms
> > of the correlation matrix rho_i_j by
> > SIGMA_i_j = sigma_i * rho_i_j * sigma_j
> >
> > Of course, for Brownian motion, you need to ensure that covariance wrt time
>
> > scales like t-s. This is like transforming standard normals by a lower
> > triangular matrix which contains the terms sqrt(t_j - t_i) for i<j, zero
> > otherwise, which is implicit in the usual construction:
> W(t_j)=W(t_i)+sqrt(t_j-
> > t_i)*Z(t_j). Z(t_j) distributed N(0, SIGMA).
> >
> > To see these things in action, then look at e.g. multipathgenerator.hpp in
> the
> > montecarlo namespace.
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Friends.  Fun.  Try the all-new Yahoo! Messenger.
> http://messenger.yahoo.com/ 
>


--