Re: TRandom PoissonD returns int

From: Vassili Maroussov <Vassili.Maroussov_at_cern.ch>
Date: Fri, 16 Mar 2012 14:14:37 +0100


Alternatively, you can use the UNURAN package:

Double_t PoissonReal(const Double_t *k, const Double_t *lambda) {

    return TMath::Exp(k[0]*TMath::Log(lambda[0])-lambda[0]) / TMath::Gamma(k[0]+1.);
}

TF1 MyPoisson = new TF1("MyPoisson", PoissonReal,0.,MaxPoissArg,1)

TUnuran u;

u.Init(TUnuranDistrCont(MyPoisson));

X = u.Sample();

On 03/16/2012 01:56 PM, Vassili Maroussov wrote:
> Dear Michele,
>
> You can generalize the Poisson in the real domain as:
>
> f(k,\lambda)=\frac{\lambda^{k} e^{-\lambda}}{k!}=\frac{e^{k
> \ln{\lambda}-\lambda}}{\Gamma{(k+1)}}
>
> One possible way is to use TF1:GetRandom() :
>
> Double_t PoissonReal(const Double_t *k, const Double_t *lambda) {
> return TMath::Exp(k[0]*TMath::Log(lambda[0])-lambda[0]) /
> TMath::Gamma(k[0]+1.);
> }
>
> //Max. argument expected, depends on your application. Cannot be too
> big to avoid overflow in TMath::Gamma
> Double_t MaxPoissArg = 99.;
>
> TF1 MyPoisson("MyPoisson", PoissonReal,0.,MaxPoissArg,1)
>
> //Then you should set \lambda :
> MyPoisson.SetParameters(SomeLambdaValue);
>
> //To assign a random number to variable X
> X = MyPoisson.GetRandom();
>
> Cheers,
>
> Vassili
>
> On 03/16/2012 12:27 PM, Michele Selvaggi wrote:
>> Dear ROOT experts,
>>
>> I am trying to generate random numbers according to a poisson
>> distribution. I need non integer values. I thought the PoissonD
>> method does the job, but i seem do get only int as an output.
>>
>> TRandom *rd = new TRandom();
>> cout<<rd->Poisson(5)<<endl;
>>
>> I get as expected an integer, but if I do
>>
>> cout<<rd->PoissonD(5)<<endl;
>>
>> then I also get an integer.. Maybe I am using the wrong method? Isn't
>> the continuous version of a poissonian still called a poissonian?
>>
>> Thanks
>>
>> Michele
>
Received on Fri Mar 16 2012 - 14:14:43 CET

This archive was generated by hypermail 2.2.0 : Sat Mar 17 2012 - 23:50:01 CET