You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Paul Rivera <pa...@gmail.com> on 2012/03/29 04:11:52 UTC

Re: [math] Gamma Distribution Algorithm

Hi Guys,

I heard that there's lots of way to sample from a Gamma distribution.  What
is commons-math using when I call GammaDistribution.sample()?  From the
API, it says that sample() uses the "Inversion method" from:
  http://en.wikipedia.org/wiki/Inverse_transform_sampling

But this function is inherited from AbstractRealDistribution.  Doesn't the
sampling method differ between distributions (e.g. gaussian vs gamma vs
beta vs etc)?

Best Regards,
Paul

Re: [math] Gamma Distribution Algorithm

Posted by Paul Rivera <pa...@gmail.com>.
Thanks for all the info guys!

As Ted Dunning mentioned, the inverse cumulative function is specific to
GammaDistributionImpl (I'm actually using commons math 2.2).  What then is
the algorithm that GammaDistributionImpl.sample() uses?  The link
http://en.wikipedia.org/wiki/Inverse_transform_sampling from
AbstractRealDistribution is not specific to the GammaDistributionImpl
anymore.  Is there a paper that you guys are drawing from?  I've found the
following from RandomDataImpl.nextGamma():

For 0 < shape < 1:
Ahrens, J. H. and Dieter, U., *Computer methods for sampling from gamma,
beta, Poisson and binomial distributions.* Computing, 12, 223-246, 1974.

For shape >= 1:
Marsaglia and Tsang, *A Simple Method for Generating Gamma Variables.* ACM
Transactions on Mathematical Software, Volume 26 Issue 3, September, 2000.
Is it the same when I call sample() from GammaDistributionImpl?

Best Regards,
Paul

On Thu, Mar 29, 2012 at 10:41 PM, Sébastien Brisard <
sebastien.brisard@m4x.org> wrote:

> Hi Mikkel,
> >
> > I have created MATH-774 https://issues.apache.org/jira/browse/MATH-774
> > and assigned it to myself. I will check if other of the implemented
> > methods in oacm.random are not used either and open new issues if that
> > is the case.
> >
> > Thanks for making us aware of this, Paul.
> >
> > Cheers, Mikkel.
> >
> while you are at it, you might also want to check whether
> inverseCumulativeProbability(double) is appropriately overriden when
> closed-form expressions exist for the specific distribution at hand. I
> remember having to correct uniform and triangular distributions, which
> were invoking the default implementation of
> inverseCumulativeProbability(double), and therefore using iterative
> solvers to basically solve a linear (quadratic) equation...
>
> Best regards,
> Sébastien
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Re: [math] Gamma Distribution Algorithm

Posted by Sébastien Brisard <se...@m4x.org>.
Hi Mikkel,
>
> I have created MATH-774 https://issues.apache.org/jira/browse/MATH-774
> and assigned it to myself. I will check if other of the implemented
> methods in oacm.random are not used either and open new issues if that
> is the case.
>
> Thanks for making us aware of this, Paul.
>
> Cheers, Mikkel.
>
while you are at it, you might also want to check whether
inverseCumulativeProbability(double) is appropriately overriden when
closed-form expressions exist for the specific distribution at hand. I
remember having to correct uniform and triangular distributions, which
were invoking the default implementation of
inverseCumulativeProbability(double), and therefore using iterative
solvers to basically solve a linear (quadratic) equation...

Best regards,
Sébastien


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Gamma Distribution Algorithm

Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
Dear all,

I have created MATH-774 https://issues.apache.org/jira/browse/MATH-774
and assigned it to myself. I will check if other of the implemented
methods in oacm.random are not used either and open new issues if that
is the case.

Thanks for making us aware of this, Paul.

Cheers, Mikkel.

2012/3/29 Gilles Sadowski <gi...@harfang.homelinux.org>:
> Hi.
>
>>
>> Have you looked at the nextGamma in org.apache.commons.math.random? It
>> should be much faster than using the inversion method.
>
> Whenever there is a specific (thus faster) implementation (in
> "RandomDataImpl"), it should be called from the "sample" method of the
> corresponding distribution class.
>
> We should correct this performance bug.
> Could you open an issue on the bug tracking system?
>
>
> Best,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Gamma Distribution Algorithm

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

> 
> Have you looked at the nextGamma in org.apache.commons.math.random? It
> should be much faster than using the inversion method.

Whenever there is a specific (thus faster) implementation (in
"RandomDataImpl"), it should be called from the "sample" method of the
corresponding distribution class.

We should correct this performance bug.
Could you open an issue on the bug tracking system?


Best,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Gamma Distribution Algorithm

Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
Hi Paul,

Have you looked at the nextGamma in org.apache.commons.math.random? It
should be much faster than using the inversion method.

Cheers, Mikkel.

2012/3/29 Ted Dunning <te...@gmail.com>:
> Yes the sample method is inherited, but the inverse cumulative distribution
> function is not.
>
> On Wed, Mar 28, 2012 at 7:11 PM, Paul Rivera <pa...@gmail.com> wrote:
>
>> Hi Guys,
>>
>> I heard that there's lots of way to sample from a Gamma distribution.  What
>> is commons-math using when I call GammaDistribution.sample()?  From the
>> API, it says that sample() uses the "Inversion method" from:
>>  http://en.wikipedia.org/wiki/Inverse_transform_sampling
>>
>> But this function is inherited from AbstractRealDistribution.  Doesn't the
>> sampling method differ between distributions (e.g. gaussian vs gamma vs
>> beta vs etc)?
>>
>> Best Regards,
>> Paul
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] Gamma Distribution Algorithm

Posted by Ted Dunning <te...@gmail.com>.
Yes the sample method is inherited, but the inverse cumulative distribution
function is not.

On Wed, Mar 28, 2012 at 7:11 PM, Paul Rivera <pa...@gmail.com> wrote:

> Hi Guys,
>
> I heard that there's lots of way to sample from a Gamma distribution.  What
> is commons-math using when I call GammaDistribution.sample()?  From the
> API, it says that sample() uses the "Inversion method" from:
>  http://en.wikipedia.org/wiki/Inverse_transform_sampling
>
> But this function is inherited from AbstractRealDistribution.  Doesn't the
> sampling method differ between distributions (e.g. gaussian vs gamma vs
> beta vs etc)?
>
> Best Regards,
> Paul
>