Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate lognormal random numbers in MATLAB?

Tags:

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?

Thanks guys.

like image 923
Zack Avatar asked Jan 22 '13 19:01

Zack


People also ask

How do you simulate a lognormal distribution?

The method is simple: you use the RAND function to generate X ~ N(μ, σ), then compute Y = exp(X). The random variable Y is lognormally distributed with parameters μ and σ. This is the standard definition, but notice that the parameters are specified as the mean and standard deviation of X = log(Y).

How do you use the rand function in Matlab?

X = rand( sz ) returns an array of random numbers where size vector sz defines size(X) . For example, rand([3 4]) returns a 3-by-4 matrix. X = rand(___, typename ) returns an array of random numbers of data type typename . The typename input can be either "single" or "double" .


1 Answers

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.

like image 130
rburny Avatar answered Sep 28 '22 05:09

rburny