support for truncated normal distribution

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

support for truncated normal distribution

pfeldman
I've noticed that there is no truncated normal distribution in NumPy, at least according to the following source:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.mtrand.RandomState.html,

I've written code to generate random deviates from a truncated normal distribution via acceptance-rejection, but this is inefficient when the acceptance probability is low. I assume that NumPy is generating standard normal deviates via the Ziggurat algorithm. That algorithm can be modified to produce random deviates from a truncated normal without the use of acceptance-rejection.  I'd be very grateful if someone can implement this.
Reply | Threaded
Open this post in threaded view
|

Re: [SciPy-user] support for truncated normal distribution

Robert Kern-2
On Tue, Mar 15, 2011 at 13:45, Dr. Phillip M. Feldman
<[hidden email]> wrote:

>
> I've noticed that there is no truncated normal distribution in NumPy, at
> least according to the following source:
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.mtrand.RandomState.html,
>
> I've written code to generate random deviates from a truncated normal
> distribution via acceptance-rejection, but this is inefficient when the
> acceptance probability is low. I assume that NumPy is generating standard
> normal deviates via the Ziggurat algorithm. That algorithm can be modified
> to produce random deviates from a truncated normal without the use of
> acceptance-rejection.  I'd be very grateful if someone can implement this.

No, we use the Box-Mueller transform, which is not easily truncated.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: [SciPy-user] support for truncated normal distribution

Wes McKinney
On Tue, Mar 15, 2011 at 2:58 PM, Robert Kern <[hidden email]> wrote:

> On Tue, Mar 15, 2011 at 13:45, Dr. Phillip M. Feldman
> <[hidden email]> wrote:
>>
>> I've noticed that there is no truncated normal distribution in NumPy, at
>> least according to the following source:
>>
>> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.mtrand.RandomState.html,
>>
>> I've written code to generate random deviates from a truncated normal
>> distribution via acceptance-rejection, but this is inefficient when the
>> acceptance probability is low. I assume that NumPy is generating standard
>> normal deviates via the Ziggurat algorithm. That algorithm can be modified
>> to produce random deviates from a truncated normal without the use of
>> acceptance-rejection.  I'd be very grateful if someone can implement this.
>
> No, we use the Box-Mueller transform, which is not easily truncated.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>   -- Umberto Eco
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

I have an implementation here (using the inverse CDF method):

https://github.com/wesm/statlib/blob/master/statlib/distributions.py#L12

There is also scipy.stats.truncnorm (which I have not tested but assume works):

    Notes
    -----
    Truncated Normal distribution.

      The standard form of this distribution is a standard normal
truncated to the
      range [a,b] --- notice that a and b are defined over the domain
      of the standard normal.  To convert clip values for a specific mean and
      standard deviation use a,b = (myclip_a-my_mean)/my_std,
(myclip_b-my_mean)/my_std
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: [SciPy-user] support for truncated normal distribution

josef.pktd
On Tue, Mar 15, 2011 at 3:03 PM, Wes McKinney <[hidden email]> wrote:

> On Tue, Mar 15, 2011 at 2:58 PM, Robert Kern <[hidden email]> wrote:
>> On Tue, Mar 15, 2011 at 13:45, Dr. Phillip M. Feldman
>> <[hidden email]> wrote:
>>>
>>> I've noticed that there is no truncated normal distribution in NumPy, at
>>> least according to the following source:
>>>
>>> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.mtrand.RandomState.html,
>>>
>>> I've written code to generate random deviates from a truncated normal
>>> distribution via acceptance-rejection, but this is inefficient when the
>>> acceptance probability is low. I assume that NumPy is generating standard
>>> normal deviates via the Ziggurat algorithm. That algorithm can be modified
>>> to produce random deviates from a truncated normal without the use of
>>> acceptance-rejection.  I'd be very grateful if someone can implement this.
>>
>> No, we use the Box-Mueller transform, which is not easily truncated.
>>
>> --
>> Robert Kern
>>
>> "I have come to believe that the whole world is an enigma, a harmless
>> enigma that is made terrible by our own mad attempt to interpret it as
>> though it had an underlying truth."
>>   -- Umberto Eco
>> _______________________________________________
>> SciPy-User mailing list
>> [hidden email]
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
> I have an implementation here (using the inverse CDF method):
>
> https://github.com/wesm/statlib/blob/master/statlib/distributions.py#L12
>
> There is also scipy.stats.truncnorm (which I have not tested but assume works):

It`s using the generic rvs which is also inverse cdf method.

Josef

>
>    Notes
>    -----
>    Truncated Normal distribution.
>
>      The standard form of this distribution is a standard normal
> truncated to the
>      range [a,b] --- notice that a and b are defined over the domain
>      of the standard normal.  To convert clip values for a specific mean and
>      standard deviation use a,b = (myclip_a-my_mean)/my_std,
> (myclip_b-my_mean)/my_std
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user