对数正态分布lognormal distribution

为了方便后面的描述,我们先定义正态分布的两个参数为: 均值mean表示为 \(\mu_N\), 标准差standard deviation 表示为 \(\sigma_N\) (对应方差Variance表示为 \(\sigma^2_N\))。
为了区分,我们用\(m\)\(v\) 分别表示对数正态分布的均值和方差, 他们与其对应的正太分布的关系如下:
lognormal均值: \(m_{LogN} = e^{\mu_N+\sigma_N^2/2}\)
lognormal方差: \(v_{LogN} = (e^{\sigma_N^2}-1)e^{2\mu_N+\sigma_N^2}\).
另外:
lognormal 众数(mode) = \(e^{\mu_N- \sigma_N^2}\);
lognormal 中位数(median) = \(e^{\mu_N}\).

生成符合lognromal distribution 的随机数(\(n\)个数),无论是Python还是Matlab, 都利用\(\mu_N\)\(\sigma_N\)来生成对数正态分布随机数:

  1. Python (numpy)
import numpy as np
y0 = np.random.lognormal(mu_N, sigma_N, n)

示例:我们取\(\mu_N = 0.5\)\(\sigma_N= 0.5\), \(n = 10000\), 执行并画出Python生成的随机数histogram (bin数量取50)如下:

  1. Matlab
%% method 1: build-in matlab makedist function
pd = makedist('Lognormal', 'mu' ,mu_N,'sigma',sigma_N);
rng('default');  % For reproducibility
y1 = random(pd,n,1);
% logx = log(y1); %logx distributed as normal distribution with mu and sigma
% mean(logx); % 可以验证为 mu_N

%% method 2: build-in matlab lognrnd function
rng('default');  % For reproducibility
y2 = lognrnd(mu_N, sigma_N, [n,1]);

%% method 3: from normal distriution
rng('default');  % For reproducibility
z = randn([n,1]); %standard normal
x = mu_N + sigma_N.*z;% x follows normal distribution N(mu_N, sigma_N)
y3 = exp(x); % y follows lognormal distribution

示例:我们取\(\mu_N = 0.5\)\(\sigma_N= 0.5\), \(n = 10000\), 执行并画出Matlab生成的随机数histogram (bin数量取50)如下:

posted @ 2021-12-16 13:42  pas_a_pas  阅读(4135)  评论(0编辑  收藏  举报