# lognormal distribution returns negative numbers #3418

Closed
opened this Issue Feb 28, 2014 · 5 comments

Projects
None yet
4 participants

### ysanders commented Feb 28, 2014

 If I try to generate random variables from the lognorm distribution, I seem to get the logarithm of the random variables rather than the random variables. For example, ```from scipy.stats import lognorm lognorm(.1,-1).rvs(10)``` returns an array of ten floats near zero, some of which are negative. Calculating the exponential of this array yields an array of ten floats near 1. Perhaps this is intentional behaviour, but I doubt it.

Contributor

### argriffing commented Feb 28, 2014

 I'm pretty sure the -1 is treated as a location parameter. See http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.lognorm.html `rv = lognorm(s, loc=0, scale=1)`. So `lognorm(.1, -1).rvs(10)` gives floats near 0, `lognorm(.1, 0).rvs(10)` gives floats near 1, and `lognorm(.1, -10).rvs(10)` gives floats near -9.
Member

### ev-br commented Feb 28, 2014

 Just what Alex said: ``````>>> import numpy as np >>> np.random.seed(123) >>> lognorm.rvs(0.1, -1) -0.10287767979642992 >>> >>> np.random.seed(123) >>> lognorm.rvs(0.1, loc=-1) -0.10287767979642992 `````` Not a bug, hence closing. Questions are most welcome in the scipy-user mailing list and/or stackoverflow.

### ysanders commented Mar 3, 2014

 Sorry, I'm afraid you don't understand what the issue is. Random variables sampled from the lognormal distribution are supposed to return positive real numbers -- see the Wikipedia page, for example. The behaviour of the current implementation of lognormal on scipy.stats is mathematically incorrect. As far as I can tell, lognorm.rvs is returning the logarithm of the values it is supposed to be returning. The fix I have implemented in my work has simply been to evaluate numpy.exp on the output array of the rvs method, and I think this should be implemented in the main code. Once again, I have to stress that the current behaviour of the function is mathematically incorrect. On 28 February 2014 18:22, Evgeni Burovski notifications@github.com wrote: Just what Alex said: import numpy as np np.random.seed(123) lognorm.rvs(0.1, -1) -0.10287767979642992 np.random.seed(123) lognorm.rvs(0.1, loc=-1) -0.10287767979642992 Not a bug, hence closing. Questions are most welcome in the scipy-user mailing list and/or stackoverflow. Reply to this email directly or view it on GitHubhttps://github.com/scipy/scipy/issues/3418#issuecomment-36405043 .
Contributor

### argriffing commented Mar 3, 2014

 @ysanders scipy.stats generalizes continuous distributions to translated and to spread-out variants of the original distribution. The -1 argument in your function is a translation. So this changes the support. Any distribution supported on the positive real numbers can be translated in this way so that the support of its translated distribution can include negative numbers. If you want to sample from lognormal distributions with the original support (positive real numbers) then don't pass -1 as the location parameter.

### ysanders commented Mar 3, 2014

 Alright, thanks very much. Apologies for the confusion. On 2 March 2014 22:21, argriffing notifications@github.com wrote: @ysanders https://github.com/ysanders scipy.stats generalizes continuous distributions to translated and to spread-out variants of the original distribution. The -1 argument in your function is a translation. So this changes the support. Any distribution supported on the positive real numbers can be translated in this way so that the support of its translated distribution can include negative numbers. If you want to sample from lognormal distributions with the original support (positive real numbers) then don't pass -1 as the location parameter. Reply to this email directly or view it on GitHubhttps://github.com/scipy/scipy/issues/3418#issuecomment-36478868 .