[SciPy-User] Help with the truncnorm distribution

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Help with the truncnorm distribution

Paul Hobson-2
Hey everyone,

I'm having a little trouble fixing the shape parameters for the truncnorm distribution when fitting it to some data. For example, if I start with this:

import numpy
from scipy import stats

numpy.random.seed(0)

lower, upper = -0.5, 2.5
loc, scale = 1, 4

# shape params
a = (lower - loc) / scale  # -0.375
b = (upper - loc) / scale  # +0.375

stn = stats.truncnorm(a, b, loc, scale)
data = stn.rvs(size=37000)
print(data.min(), data.max())
# prints -0.499800757031 2.49990005171

But then when I do this:

stats.truncnorm.fit(data, a, b) # fix a & b?

I get this in return:

(-0.20306416258741233,  # not a
 1.0244302986808909,    # not b
 -0.0035940308456371621,
 2.443791525672637)


How can I fix the a & b parameters in the call to stats.truncnorm.fit?

Regards,
-Paul

(context: I'm adding truncnorm to paramnormal https://github.com/phobson/paramnormal/pull/36)

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Help with the truncnorm distribution

Evgeni Burovski
On Tue, Jul 26, 2016 at 7:39 PM, Paul Hobson <[hidden email]> wrote:

> Hey everyone,
>
> I'm having a little trouble fixing the shape parameters for the truncnorm
> distribution when fitting it to some data. For example, if I start with
> this:
>
> import numpy
> from scipy import stats
>
> numpy.random.seed(0)
>
> lower, upper = -0.5, 2.5
> loc, scale = 1, 4
>
> # shape params
> a = (lower - loc) / scale  # -0.375
> b = (upper - loc) / scale  # +0.375
>
> stn = stats.truncnorm(a, b, loc, scale)
> data = stn.rvs(size=37000)
> print(data.min(), data.max())
> # prints -0.499800757031 2.49990005171
>
> But then when I do this:
>
> stats.truncnorm.fit(data, a, b) # fix a & b?
>
> I get this in return:
>
> (-0.20306416258741233,  # not a
>  1.0244302986808909,    # not b
>  -0.0035940308456371621,
>  2.443791525672637)
>
>
>
> How can I fix the a & b parameters in the call to stats.truncnorm.fit?
>
> Regards,
> -Paul
>
> (context: I'm adding truncnorm to paramnormal
> https://github.com/phobson/paramnormal/pull/36)
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> https://mail.scipy.org/mailman/listinfo/scipy-user
>


truncnorm.fit(data, fa=a, fb=b)  ?


Indeed,

In [1]: from scipy.stats import truncnorm

In [2]: a, b = 1, 2

In [3]: import numpy as np

In [4]: data = truncnorm.rvs(a, b, size=100)

In [5]: truncnorm.fit(data, fa=a, fb=b)
/home/br/virtualenvs/scipy_py27/local/lib/python2.7/site-packages/scipy/stats/_continuous_distns.py:4417:
RuntimeWarning: divide by zero encountered in log
  self._logdelta = log(self._delta)
Out[5]: (1, 2, 0.011142481322181465, 0.99278381920540337)


Depending on how old scipy versions you want to support, you might
need to use f0 and f1 instead.
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Help with the truncnorm distribution

Paul Hobson-2

On Tue, Jul 26, 2016 at 7:39 PM, Paul Hobson <[hidden email]> wrote:
> Hey everyone,
>
> I'm having a little trouble fixing the shape parameters for the truncnorm
> distribution when fitting it to some data.

[snip]
 
>
> How can I fix the a & b parameters in the call to stats.truncnorm.fit?
>
> Regards,
> -Paul

On Tue, Jul 26, 2016 at 11:55 AM, Evgeni Burovski <[hidden email]> wrote:
 
truncnorm.fit(data, fa=a, fb=b)  ?


Indeed,

In [1]: from scipy.stats import truncnorm

In [2]: a, b = 1, 2

In [3]: import numpy as np

In [4]: data = truncnorm.rvs(a, b, size=100)

In [5]: truncnorm.fit(data, fa=a, fb=b)
/home/br/virtualenvs/scipy_py27/local/lib/python2.7/site-packages/scipy/stats/_continuous_distns.py:4417:
RuntimeWarning: divide by zero encountered in log
  self._logdelta = log(self._delta)
Out[5]: (1, 2, 0.011142481322181465, 0.99278381920540337)


Depending on how old scipy versions you want to support, you might
need to use f0 and f1 instead.

Ahhh -- thanks very much for such a quick reply! This answers my question entirely.
-p

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user