Skip to content

Instantly share code, notes, and snippets.

@Jsevillamol
Created September 8, 2021 16:11
Show Gist options
  • Save Jsevillamol/a07faa82ad9edd5a333b67d0f42ff998 to your computer and use it in GitHub Desktop.
Save Jsevillamol/a07faa82ad9edd5a333b67d0f42ff998 to your computer and use it in GitHub Desktop.
import numpy as np
mu_A = np.log(10)
sigma_A = 0.1
mu_B = np.log(20)
sigma_B = 0.1
def lognorm_pdf(x,mu,sigma):
return np.exp(-(np.log(x)-mu)**2/(2*(sigma**2))) / (x*sigma*np.sqrt(2*np.pi))
f_A = lambda x : lognorm_pdf(x, mu_A, sigma_A)
f_B = lambda x : lognorm_pdf(x, mu_B, sigma_B)
import scipy.integrate as integrate
f_aux = lambda x : np.sqrt(f_A(x)) * np.sqrt(f_B(x))
denominator, error = integrate.quad(f_aux, 0.1, np.inf)
assert np.abs(denominator/100) > error, f"The error is too big! {denominator, error}"
f = lambda x : f_aux(x) / denominator
expected_value, error = integrate.quad(lambda x : x*f(x), 0, np.inf)
assert np.abs(expected_value/100) > error, f"The error is too big! {denominator, error}"
print(f"The expected value of the aggregated density is {expected_value}")
mean_A = np.exp(mu_A + sigma_A**2 / 2)
mean_B = np.exp(mu_B + sigma_B**2 / 2)
print(f"The mean of the individual distributions is {mean_A, mean_B}")
geo_mean = np.sqrt(mean_A*mean_B)
print(f"The geometric mean of the means of the individual distributions is {geo_mean}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment