Search code examples
matlabrandomprobability-distribution

Generate lognormal random numbers in MATLAB?


I'm trying to generate 10000 random numbers taken from a log normal distribution who's associated normal distribution has mean = 0.3 and std. dev. = 0.05 in MATLAB.

I'm using the built in lognrnd function.

My attempt is to do:

R = lognrnd(0.3,0.05,10000,1)

However, when I plot the histogram of R using hist(R), the associated plot is normal, not log normal.

Where am I messing up? If the mean = 0.3 and std. dev. = 0.05 of the normal distribution, shouldn't the generated log normal numbers have a mean = 0.3 and std. dev = 0.05?


Solution

  • The numbers you generate are actually from log-normal distribution. Plot just looks similar for your parameters. Compare hist(R) with hist(log(R)) - the shape is pretty much the same.

    As for mean and deviation, take a look at lognrnd documentation:

    mu and sigma are the mean and standard deviation, respectively,
    of the associated normal distribution.
    

    hence generated numbers are expected to have different mean and deviation.

    EDIT: I'm not sure if Matlab lets you specify lognormal distribution parameters directly, but you can derive one set of the parameters from the other. Assuming M and V are desired parameters of lognormal variable, you can calculate mu and sigma using following formulas:

    x = 1 + V / M^2
    sigma = sqrt(log(x))
    mi    = log(M / sqrt(x))
    

    See wikipedia for opposite conversion.