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

Implement dist_rand for dislnorm and disexp #18

Closed linzhp closed 10 years ago

linzhp commented 10 years ago

@csgillespie

Please review. Specifically, please confirm my use of m$pars in disexp$dis_rand is correct.

Thanks!

csgillespie commented 10 years ago

Thanks for the request.

A couple of suggestions/comments before merging:

u = runif(n, 0, exp(-m$pars*(m$xmin-0.5)))
-log(u)/m*pars

will work - I just rearranged the standard exponential random number algorithm.

What do you think?

linzhp commented 10 years ago

Thanks for the comments. I will address them and update the pull request.

Question: in the lognormal RNG, wouldn't min <- round(log(m$xmin)) be better than min <- log(m$xmin -0.5)?

csgillespie commented 10 years ago

I think you're correct about rounding xmin. A couple of other points I just thought of:

  1. Check your RNGs by simulating a large number of times and comparing to the exact pdf - dist_pdf
  2. For the lognormal example, if we simulate n values on average we will discard plnorm(xmin-0.5, m$pars[1],m$pars[2], lower.tail=TRUE) So simulate (something like)
ceiling(n/plnorm(m$xmin-0.5,  m$pars[1],m$pars[2], lower.tail=FALSE))
linzhp commented 10 years ago

I've updated the pull request according to your suggestions. However, I don't understand what

ceiling(n/plnorm(m$xmin-0.5,  m$pars[1],m$pars[2], lower.tail=FALSE))

does.

I used

r<-dist_rand(exponential, 1000)
plot(density(r, from=min(r)))
curve(dexp(x, exponential$pars), from=min(r), to=max(r))

to visually compare the two pdfs