You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Francesco Strino (Updated) (JIRA)" <ji...@apache.org> on 2012/02/24 22:41:49 UTC

[jira] [Updated] (MATH-753) Better implementation for the gamma distribution implementation

     [ https://issues.apache.org/jira/browse/MATH-753?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Francesco Strino updated MATH-753:
----------------------------------

    Description: 
The way the density of the gamma distribution function is estimated can be improved.

It's much more stable to calculate first the log of the density and then exponentiate, otherwise the function returns NaN for high values of the parameters alpha and beta. 

It would be sufficient to change the public double density(double x) function at line 204 in the file org.apache.commons.math.distribution.GammaDistributionImpl as follows:


return Math.exp(Math.log(x)*(alpha-1) - Math.log(beta)*alpha - x/beta - Gamma.logGamma(alpha)); 



  was:
The way the density of the gamma distribution function is estimated can be improved.

It's much more stable to calculate first the log of the density and then exponentiate, otherwise the function returns NaN for high values of the parameters alpha and beta. 

It would be sufficient to change line 204 in the file org.apache.commons.math.distribution.GammaDistributionImpl as follows (I write it down here because I don't know how to submit a patch with JIRA):

@Override
    public double density(double x) {
        if (x < 0) return 0;
        //return Math.pow(x / beta, alpha - 1) / beta * Math.exp(-x / beta) / Math.exp(Gamma.logGamma(alpha)); //unstable for large values
        return Math.exp(Math.log(x)*(alpha-1) - Math.log(beta)*alpha - x/beta - Gamma.logGamma(alpha)); 
    }



    
> Better implementation for the gamma distribution implementation
> ---------------------------------------------------------------
>
>                 Key: MATH-753
>                 URL: https://issues.apache.org/jira/browse/MATH-753
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 2.2
>            Reporter: Francesco Strino
>            Priority: Minor
>              Labels: improvement, stability
>             Fix For: 2.2
>
>
> The way the density of the gamma distribution function is estimated can be improved.
> It's much more stable to calculate first the log of the density and then exponentiate, otherwise the function returns NaN for high values of the parameters alpha and beta. 
> It would be sufficient to change the public double density(double x) function at line 204 in the file org.apache.commons.math.distribution.GammaDistributionImpl as follows:
> return Math.exp(Math.log(x)*(alpha-1) - Math.log(beta)*alpha - x/beta - Gamma.logGamma(alpha)); 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira