I'm using the randn
function to generate a Gaussian random variable x1
with mean 0 and variance 1. After generating x1
, I generate another Gaussian random variable x
with mean m and variance σ2, that is: x = m + sqrt(σ)·x1
.
I want to create a log-normal random variable y = ex
and plot its PDF. I think I can use the histogram function to do this:
N = 10^5; %// sample
x1 = randn(N,1);
m = 0; %// mean of x1
sigma = 1; %// variance of x1
x = m + sigma.*x1; %// create x
y = exp(x);
figure;
hist(y);
and here is the diagram I get:
I think that my result is wrong but I don't know how to fix it.
Thank you so much! :)
The lognormal distribution can have a very long tail (i.e., even for large y
the prob(y) > 0
and not vanishing.
If you want to compare your pdf plot to those of e.g., the wiki web page, you will need to ignore the tail.
bins = linspace(0,5,500);
n = hist( y, bins );
bar( bins(1:end-1), n(1:end-1)/N ); axis([0 bins(end-1) 0 1]); % discard last bin that has the "tail"