0

Given a mean and a variance of a normal distribution, I want to generate random numbers from a any given distribution. for eg: Beta, Gamma or a Poisson distribution in Matlab.

If for eg: I am given a number, 0.1 and i want to generate random numbers around this. So i will take this number to be my mean with a predefined variance of say 0.75/1//2.

How can i then sample from any distribution given to me in matlab using this mean and variance?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
viggie
  • 183
  • 1
  • 3
  • 11
  • Are you saying you want to convert Gaussian samples to (say) Poisson samples, or that you want to create (say) Poisson samples with the same mean/variance? – Oliver Charlesworth Dec 04 '13 at 11:06
  • @Oli: I want to create Poisson/beta/gamma samples with the same mean and variance. – viggie Dec 04 '13 at 11:10

1 Answers1

3

If you have the Statistics toolbox:

  • To generate random samples with a Beta ditribution:

    samples = betarnd(a,b,m,n); %// parameters: a, b; sample size m x n
    
  • To generate random samples with a Gamma ditribution:

    samples = gamrnd(a,b,m,n); %// parameters: a, b; sample size m x n
    
  • To generate random samples with a Poisson ditribution:

    samples = poissrnd(l,m,n); %// parameter: l; sample size m x n
    

Note that the parameters of these distributions are not necessarily mean and variance. You will have to do a computation of the required parameters to achieve your desired mean and variance. In some cases, such as the Poisson distribution, there is only one parameter, so you can't specify mean and variance simultaneously.

For other distributions: type help stats. My version of the Statistics toolbox includes:

Random Number Generators.
 betarnd     - Beta random numbers.
 binornd     - Binomial random numbers.
 chi2rnd     - Chi square random numbers.
 evrnd       - Extreme value random numbers.
 exprnd      - Exponential random numbers.
 frnd        - F random numbers.
 gamrnd      - Gamma random numbers.
 geornd      - Geometric random numbers.
 gevrnd      - Generalized extreme value random numbers.
 gprnd       - Generalized Pareto inverse random numbers.
 hygernd     - Hypergeometric random numbers.
 iwishrnd    - Inverse Wishart random matrix.
 johnsrnd    - Random numbers from the Johnson system of distributions.
 lognrnd     - Lognormal random numbers.
 mhsample    - Metropolis-Hastings algorithm.
 mnrnd       - Multinomial random vectors.
 mvnrnd      - Multivariate normal random vectors.
 mvtrnd      - Multivariate t random vectors.
 nbinrnd     - Negative binomial random numbers.
 ncfrnd      - Noncentral F random numbers.
 nctrnd      - Noncentral t random numbers.
 ncx2rnd     - Noncentral Chi-square random numbers.
 normrnd     - Normal (Gaussian) random numbers.
 pearsrnd    - Random numbers from the Pearson system of distributions.
 poissrnd    - Poisson random numbers.
 randg       - Gamma random numbers (unit scale).
 random      - Random numbers from specified distribution.
 randsample  - Random sample from finite population.
 raylrnd     - Rayleigh random numbers.
 slicesample - Slice sampling method.
 trnd        - T random numbers.
 unidrnd     - Discrete uniform random numbers.
 unifrnd     - Uniform random numbers.
 wblrnd      - Weibull random numbers.
 wishrnd     - Wishart random matrix.
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • for beta and gamma distributions, i calculated the a & b using the mean and variance by applying the equations given in http://en.wikipedia.org/wiki/Beta_distribution & http://en.wikipedia.org/wiki/Gamma_distribution. But I faced a problem when the variance was less than 1. In this case, the random numbers generated were inconsistent with the given mean. Wat do i do in that case ? – viggie Dec 04 '13 at 11:17
  • and poissrnd(l,m,n) would generate only whole numbers and not decimals. In case i want to sample poissrnd(0.1), this would give me either a 0/1. But what i want is a random number from around that point 0.1. Please help! – viggie Dec 04 '13 at 11:20
  • @viggie A Poisson RV is __discrete__! It can only take __non-negative integers__! That's __basic__ statistics! – Luis Mendo Dec 04 '13 at 11:22
  • wat about the case, when the variance is less then 1? how do i resolve that issue to find appropriate parameters for the distributions ? – viggie Dec 04 '13 at 11:26
  • @viggie Compute the parameter values that will give you the desired variance. For the Poisson case, yes, a small variance implies most samples will be 0 and 1. So what? – Luis Mendo Dec 04 '13 at 11:30
  • @viggie Note that taking a small sample from a distribution with a certain mean may leave your sample mean far from the distribution mean. – Dennis Jaheruddin Dec 04 '13 at 12:42
  • I would also recommend looking into the [*Probability Distribution Objects*](https://www.mathworks.com/help/stats/working-with-probability-distributions.html#bum5reu) which allow you to use `random` and a host of other useful functions. – SecretAgentMan Oct 20 '18 at 16:57