[SciPy-User] scipy.stats.uniform gives strange behavior (error?)
nicky van foreest
vanforeest at gmail.com
Tue Aug 3 16:07:00 EDT 2010
Hi Jason,
Thanks, this works. However, this solution leaves me with an uneasy
feeling. From the shape of f (see my former mail) it appears that the
fence posts of f are zero, since f starts at 0, and ends at 0. (Or do
I misunderstand the meaning of fence posts?) It makes me actually
wonder whether sum(rv.pdf(...) ) for any pdf in scipy might be allowed
to exceed 1 ever. Shouldn't a (naive) user be protected from this?
bye
Nicky
On 3 August 2010 21:53, Jason Rennie <jrennie at gmail.com> wrote:
> Sounds like you're accidentally counting both fence-posts when you should
> only be counting one. Try summing densities from the middle of the buckets:
> f = rv.pdf(arange(delta+delta/2., len(grid)*delta+delta/2.,delta))*delta
> Jason
>
> On Tue, Aug 3, 2010 at 3:35 PM, nicky van foreest <vanforeest at gmail.com>
> wrote:
>>
>> Hi,
>>
>> It seems that the sum over the density of the uniform distribution
>> does not always produces 1 (after proper division). Here is the code:
>>
>> ====
>> #!/usr/bin/env python
>>
>> from numpy import *
>> from scipy.stats import uniform
>>
>> start = 0.; end = 2.; delta = 0.005; grid = arange(start,end,delta)
>>
>> rv = uniform(loc=1., scale = 0.1)
>>
>> f = rv.pdf(arange(delta, len(grid)*delta,delta))*delta # 1
>> print sum(f)
>> print f
>> ======
>>
>> The result:
>>
>> 1.05
>> [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
>> 0.
>> 0. 0. 0. 0. 0. 0. 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> 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
>>
>> This is weird, since the sum must be 1. Moreover, if I change line #1
>> into:
>>
>> f = rv.pdf(arange(0., len(grid)*delta,delta))*delta # 1
>>
>> I get a sum of 1, as it should.
>>
>> If I change delta to 0.05 I get 1.0 in both cases.
>>
>> Does anybody have an idea about how this strange behavior may occur?
>>
>> Thanks
>>
>> Nicky
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
>
> --
> Jason Rennie
> Research Scientist, ITA Software
> 617-714-2645
> http://www.itasoftware.com/
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
More information about the SciPy-User
mailing list