5

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?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Zack
  • 713
  • 2
  • 8
  • 21

2 Answers2

6

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.

rburny
  • 1,559
  • 1
  • 9
  • 28
  • Okay - what if I want to generate random number from a log normal distribution which has a mean = 0.3 and std. dev = 0.05? – Zack Jan 22 '13 at 20:02
  • 1
    I would like to add that if you were to increase your sigma (to, say, .5) and use a more refined histogram `hist(R,100)` you would see something more like the skewed lognormal curve you were expecting. – RussH Jan 22 '13 at 20:02
  • Thanks for accepting, but I did a mistake in calculations. See updated formulas (I've tested them and results seem correct). – rburny Jan 22 '13 at 20:24
0

tl;dr: You can also do this easily now with the Probability Distribution Objects which gives you access to a broader range of functions (see the list at link provided).

Example provided using makedist() and random().

N = 10000;                               % number of samples
pd = makedist('Lognormal',0.3,0.05)      % Probability Distribution Object
R = random(pd,N,1);                      % Call random()

As already pointed out, notice that

>> mean(pd)
ans =
    1.3515
>> std(pd)
ans =
    0.0676
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41