Subscribe to DSC Newsletter

Simulating Distributions with One-Line Formulas, even in Excel

If you don't like using black-box R functions, or you don't have access to these functions, here are simple options to simulate deviates from various distributions. They can even be implemented in Excel! You first need to simulate uniform deviates on [0, 1]. If you don't trust the function available in your programming language, here is a good alternative:


rnd = 1000
for (n=0; n<20000; n++) {
     rnd=(10232193 * rnd + 3701101) % 54198451371
     Rand= rnd / 54198451371

}

This code produces 20,000 deviates of a uniform distribution on [0, 1].  The deviates are stored in the variable named Rand. The symbol % stands for the modulo operator.

Now, assuming Rand, Rand1 and Rand2 are uniform deviates on [0, 1], here is how to sample deviates from various other distributions:

Normal(0, 1) and log-normal deviates:

  • Normal: x = sqrt(-2* log(Rand1)) * cos(2* Pi *Rand2)   
  • Log-normal: y = exp(x)

Exponential deviates of parameter Lambda:

  • x = - log(1 - Rand) / Lambda

Geometric deviates of parameter P:

  • if (Rand < P) { x = 0 } else { x = int(log(1 - Rand) / log(1 - P)) }

Power law deviates with exponent B, on [0, A]:

  • x = A * Rand^(1 / B)

Do you know any simple formula to generate other types of deviates?

Views: 726

Reply to This

Replies to This Discussion

Can you recommend any references you can recommend that list parameters for Linear Congruent Generators and document their statistical performance? Most sources (like Wikipedia) list parameters with little discussion of statistical properties.

Hi Dennis, the choice of the parameters in my linear congruent generator is a little arbitrary. I played with various values until I found ones that give a long enough period. So my solution is not spectacular by any means, especially since I was just trying to get a far better generator than the one available in the Perl programming language (that one can only produce 32,767 different values.)

I'm researching the topic for a project, and will be happy to share my notes.
Basically, I need enough documentation to satisfy clients (including convincing them that we don't need a cryptographic grade random number generator).
I thought this was a good paper and references some pretty robust methods.
https://link.springer.com/content/pdf/10.1007%2Fs12095-017-0225-x.pdf

Thanks Dennis, and if you find a better one than mine, please share. Should be easy. Likewise, I don't need weapon-grade cryptography.

RSS

Videos

  • Add Videos
  • View All

© 2019   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service