4

Lets say I have a known probability distribution. It could be power-law distribution or a Gaussian distribution for example. If I know the necessary parameters of the distribution, then how do I explicitly sample data points from this known distribution?

I realized that I have so far fitted data into probability distributions but I have never sampled data from other probability distributions other than the uniform distribution which is trivial to do.

How do I explicitly code this e.g. in Matlab? Could I have an easy example?

Tim
  • 108,699
  • 20
  • 212
  • 390
jjepsuomi
  • 5,207
  • 11
  • 34
  • 47
  • I edited your tags, `[sampling]` is about sampling cases from population, `[random-generation]` is tag that describes your question. – Tim Dec 10 '15 at 12:46
  • Nothing to be sorry about. Notice that if you click or hover on some tag than it leads you to tags main page and on the top of it in many cases there is a short description what is the tag about. – Tim Dec 10 '15 at 12:51

2 Answers2

3

There is a number of methods for pseudo-random numbers generation (check other threads tagged as ). One of the most simple and popular ones is inverse transform method. You could also use other algorithms as rejection sampling, slice sampling, or ziggurat algorithm (see also this recent thread). Luc Devroye (1986) wrote a whole book on this topic.

As about software, most statistical software includes functions for sampling for most common distributions. For example, in R you use r* functions: rnorm for normal distribution, rt for $t$-distribution, rpois for Poisson distribution etc. Google search suggests that Matlab also has such functions, e.g random, or normrnd.


Devroye, L. (1986). Non-Uniform Random Variate Generation. New York: Springer-Verlag.

Tim
  • 108,699
  • 20
  • 212
  • 390
1

For Gaussian distributions, you can just use the built-in randn function. It will give you a sample from the normal distribution. Then you just have to multiply it by the standard deviation and add the mean to get a sample from any Gaussian distribution.

For a general distribution, you can use inverse transform sampling. The idea is that you first sample from a uniform distribution, then use the cdf of your target distribution to find the value that matches the same quantile in your target distribution.

  • Thank you for your help =) I don't like using packages or built-in functions so much, because then I can't understand how the actual implementation is done x) +1 – jjepsuomi Dec 10 '15 at 13:41