oie-mines-paristech / lca_algebraic

Layer over brightway2 for algebraic definition of parametric models and super fast computation of LCA
BSD 2-Clause "Simplified" License
34 stars 18 forks source link

Suspected error in lognormal sampling #25

Open thomasgibon opened 1 year ago

thomasgibon commented 1 year ago

Hi, we have been struggling with @alvarojhahn about how the lognormal distribution is defined.

The lognormal sampling for a given parameter is done at the following line of code: https://github.com/oie-mines-paristech/lca_algebraic/blob/46144bff6096487fda2aacc57625fc94d8e047eb/lca_algebraic/params.py#L274

However, the scipy documentation indicates that a lognormal distribution can be configured with lognorm, using scale and shape s, with scale = exp(mu) and s = sigma with mu and sigma being the mean and standard deviation of the corresponding normal distribution. This means that mu = param.default and sigma = param.std for a lognormal-distributed parameter.

BUT apparently scale takes the median (exp(mu)) and not the mean, so you should not pass np.exp(param.default) but rather the following:

self._distrib = lognorm(scale=self.default * np.exp(-self.std**2/2), s=self.std)

It is confusing, because using the log-transformed mean and the non-transformed standard deviation is inconsistent, but it seems to yield the right results.