csgillespie / poweRlaw

This package implements both the discrete and continuous maximum likelihood estimators for fitting the power-law distribution to data. Additionally, a goodness-of-fit based approach is used to estimate the lower cutoff for the scaling region.
109 stars 24 forks source link

dist_pdf for disexp cause an underflow #21

Closed linzhp closed 10 years ago

linzhp commented 10 years ago

When log=TRUE, the underflow should be avoided. However, this line would cause an underflow regardless of the log option.

@csgillespie Is there any alternative implementation to avoid that?

linzhp commented 10 years ago

The implementation of dist_pdf for dispois could avoid such overflow. So why not use a similar implementation for dis dislnorm and disexp?

csgillespie commented 10 years ago

Since the Poisson distribution is discrete, avoiding the overflow was trivial (and happened by chance).

I've made some changes to the dislnorm and disexp which should improve the situation:

q = c(2, 1000); pars = c(-70, 2); xmin = 2
log(plnorm(q-0.5, pars[1], pars[2], lower.tail=FALSE) - 
    plnorm(q+0.5, pars[1], pars[2], lower.tail=FALSE)) - 
    plnorm(xmin-0.5, pars[1], pars[2], lower.tail=FALSE, log.p=TRUE)
##Gives [1] -0.0001196       -Inf

l1 = plnorm(q-0.5, pars[1], pars[2], lower.tail=FALSE, log.p=TRUE)
l2 = plnorm(q+0.5, pars[1], pars[2], lower.tail=FALSE, log.p=TRUE)

l1 + log(1-exp(l2-l1)) - plnorm(xmin-0.5, pars[1], pars[2], lower.tail=FALSE, log.p=TRUE)
##Gives [1] -1.196e-04 -1.238e+02