Exemple de code en python pour générer des nombres aléatoires depuis une loi log-normale em python
Loi log normal:
\begin{equation}
\frac{1}{x \sigma \sqrt{2\pi}}.exp(-\frac{(len(x)-\mu)^2}{2\sigma^2})
\end{equation}
2 -- Avec scipy lognorm
Exemple de comment générer des nombres aléatoires depuis une loi log-normale avec $\mu=0$ et $\sigma=0.5$ en passant par le fonction scipty lognorm:
from scipy.stats import lognormimport numpy as npimport matplotlib.pyplot as pltstd = 0.5print(lognorm.rvs(std))data = lognorm.rvs(std, size=100000)#print(data)hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")plt.ylim(0.0,max(hx)+0.05)plt.title('Generate random numbers \n from a log normal distribution with python')plt.grid()plt.savefig("generate_random_numbers_log_normal_distribution_01.png", bbox_inches='tight')#plt.show()plt.close()print(np.mean(data))print(np.exp(std**2 / 2.0))print(np.std(data))var = (np.exp(std**2)-1)*np.exp(std**2)print( np.sqrt(var) )

Note: Si $\mu \ne 0$ il suffit de multiplier par $exp(\mu)$, illustration:
mu = 3.0std = 0.5data = lognorm.rvs(std, size=100000) * np.exp(mu)hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")plt.ylim(0.0,max(hx)+0.05)plt.title('Generate random numbers \n from a log normal distribution with python')plt.grid()plt.savefig("generate_random_numbers_log_normal_distribution_02.png", bbox_inches='tight')plt.show()plt.close()print(np.mean(data))print(np.exp(mu + std**2 / 2.0))print(np.std(data))var = (np.exp(std**2)-1)*np.exp(2*mu+std**2)print( np.sqrt(var) )

2 -- En utilisant la fonction numpy: random.randn()
On peut aussi générer des nombres aléatoires depuis une loi log-normale en python en utilisant random.randn():
\begin{equation}
exp(\mu + \sigma Z)
\end{equation}
mu = 3.0sigma = 0.5data = np.random.randn(100000) * sigma + mudata = np.exp(data)hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")plt.ylim(0.0,max(hx)+0.05)plt.title('Generate random numbers \n from a log normal distribution with python')plt.grid()#plt.savefig("generate_random_numbers_log_normal_distribution_02.png", bbox_inches='tight')plt.show()

3 -- Références
- Loi log-normale
- Normal distribution | wikipedia
- numpy.random.randn | doc scipy
- numpy.random.normal | doc scipy
