scipy binomial pmf

46 views
Skip to first unread message

Mikie

unread,
Jul 3, 2009, 11:27:12 AM7/3/09
to sage-support
What is the syntax to get the binomial function in Scipy to calculate?
I have tried stats.binom.pmf(1,10,.56,0) etc.
Thanks for the help.

William Stein

unread,
Jul 3, 2009, 11:32:00 AM7/3/09
to sage-s...@googlegroups.com

(1) I don't understand the question. What does your question mean?

(2) Possibly you should also ask on the scipy support list, where
there are likely to be more scipy experts...

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Mikie

unread,
Jul 3, 2009, 3:30:30 PM7/3/09
to sage-support
I am trying to use the binomial pmf function in Scipy. Forget the
question I will write my own.
Thanx

On Jul 3, 9:32 am, William Stein <wst...@gmail.com> wrote:

Ahmed Fasih

unread,
Jul 3, 2009, 8:17:44 PM7/3/09
to sage-support
Writing your own is a good way to understand the implementation issues
that we sometimes unthinkingly rely on, but for production code, it's
always a good idea to default to the pre-packaged implementation.

In this case, I think it's the standard issue with Scipy not
understanding Sage types. This problem is described in
http://wiki.sagemath.org/faq#Typeissuesusingscipy.2CcvxoptornumpyfromSage

sage: import scipy.stats as stats
sage: stats.binom.pmf(1,10,.56,0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
<snip>
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and
'numpy.bool_'

There are numerous fixes to this problem, one of which is:

sage: stats.binom.pmf(1r,10r,.56r,0r)
0.0034614823012532187

As an alternative, if you plan on doing everything in Scipy/Numpy and
don't mind a fat-fisted approach, you may want to use IPython in Pylab
mode: start Sage with "sage -ipython -pylab" and you don't have to
worry about the preprocessor.

Ahmed Fasih

unread,
Jul 3, 2009, 8:19:12 PM7/3/09
to sage-support
This is also an excellent resource: "How To Ask Questions The Smart
Way," by the peerless Eric Steven Raymond:
http://catb.org/esr/faqs/smart-questions.html

On Jul 3, 8:17 pm, Ahmed Fasih <wuzzyv...@gmail.com> wrote:
> Writing your own is a good way to understand the implementation issues
> that we sometimes unthinkingly rely on, but for production code, it's
> always a good idea to default to the pre-packaged implementation.
>
> In this case, I think it's the standard issue with Scipy not
> understanding Sage types. This problem is described inhttp://wiki.sagemath.org/faq#Typeissuesusingscipy.2Ccvxoptornumpyfrom...

Mikie

unread,
Jul 6, 2009, 3:57:58 PM7/6/09
to sage-support
Ahmed,

Looks good, but I am creating a function in python that is called.
And 1r, .56r ,etc. does not pass. I am taking these parameters from a
form and then using the function.
Is there a work-a-round?
Thanks
> > > > University of Washingtonhttp://wstein.org- Hide quoted text -
>
> - Show quoted text -

Ahmed Fasih

unread,
Jul 6, 2009, 5:16:41 PM7/6/09
to sage-support
If you're calling Scipy functions from a standard Python environment,
you won't have the pre-processor issues, so I may be misunderstanding
what your underlying problem is.

Assuming you are in Sage (since this is a Sage mailing list) and
calling Scipy, other options that achieve the same effect as the raw
suffix were detailed on the FAQ: cast the types to float() explicitly,
turn off the pre-processor temporarily, redefine RealNumber() and
Integer() to float() and int(), etc. etc.

If none of these answer your question, please describe what you're
trying to do and what errors you are getting.
> > > > > University of Washingtonhttp://wstein.org-Hide quoted text -

Mikie

unread,
Jul 6, 2009, 5:21:05 PM7/6/09
to sage-support
Yes, I am calling Scipy functions from a standard Python, thus no
preparser. It does not recognize 1r, 10r, etc.
Is there anyway to get it to work? I have also tried R, but r.binom
does not work.
> > > > > > University of Washingtonhttp://wstein.org-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -

Kevin Horton

unread,
Jul 6, 2009, 5:33:21 PM7/6/09
to sage-s...@googlegroups.com
Maybe you can use:

sage: RealNumber=float; Integer=int

or, explicitly define the number type when calling the scipy function.

Good luck,

Kevin Horton

Mikie

unread,
Jul 6, 2009, 5:51:10 PM7/6/09
to sage-support
Kevin,
It worked.
Thanks, a lot.
> >>>>> University of Washingtonhttp://wstein.org-Hide quoted text -- Hide quoted text -

Robert Bradshaw

unread,
Jul 7, 2009, 5:58:48 AM7/7/09
to sage-s...@googlegroups.com
On Jul 6, 2009, at 2:21 PM, Mikie wrote:

> Yes, I am calling Scipy functions from a standard Python, thus no
> preparser. It does not recognize 1r, 10r, etc.
> Is there anyway to get it to work? I have also tried R, but r.binom
> does not work.

An example, including the error message, would be really helpful
here. If you're having issues calling Scipy functions form standard
Python, the Scipy lists may be a good place to ask as well.

- Robert

Jason Grout

unread,
Jul 9, 2009, 5:53:40 AM7/9/09
to sage-s...@googlegroups.com
Ahmed Fasih wrote:
> Writing your own is a good way to understand the implementation issues
> that we sometimes unthinkingly rely on, but for production code, it's
> always a good idea to default to the pre-packaged implementation.
>
> In this case, I think it's the standard issue with Scipy not
> understanding Sage types. This problem is described in
> http://wiki.sagemath.org/faq#Typeissuesusingscipy.2CcvxoptornumpyfromSage
>
> sage: import scipy.stats as stats
> sage: stats.binom.pmf(1,10,.56,0)
> ---------------------------------------------------------------------------
> TypeError Traceback (most recent call
> last)
> <snip>
> TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and
> 'numpy.bool_'
>
> There are numerous fixes to this problem, one of which is:
>
> sage: stats.binom.pmf(1r,10r,.56r,0r)
> 0.0034614823012532187


Robert Bradshaw just posted a patch to #5081 that makes this work:

sage: from scipy import stats
sage: stats.binom.pmf(1,10,.56,0)
0.0034614823012532187

(no preparser magic or anything; it just works).

And there was much rejoicing in the land!

Jason

Ahmed Fasih

unread,
Jul 9, 2009, 7:37:20 AM7/9/09
to sage-support
Jason, Robert, I'm trying to understand the patch, but it looks like
this is a fix for *all* Numpy/Scipy-Sage type issues? If so---oh happy
day!

On Jul 9, 5:53 am, Jason Grout <jason-s...@creativetrax.com> wrote:
> Ahmed Fasih wrote:
> > Writing your own is a good way to understand the implementation issues
> > that we sometimes unthinkingly rely on, but for production code, it's
> > always a good idea to default to the pre-packaged implementation.
>
> > In this case, I think it's the standard issue with Scipy not
> > understanding Sage types. This problem is described in
> >http://wiki.sagemath.org/faq#Typeissuesusingscipy.2Ccvxoptornumpyfrom...

Mikie

unread,
Jul 9, 2009, 2:31:52 PM7/9/09
to sage-support
Robert, how do I update my sage installation when you create a
patch. Do I have to reinstall Sage?
Thanx
> > Jason- Hide quoted text -

William Stein

unread,
Jul 9, 2009, 3:02:25 PM7/9/09
to sage-s...@googlegroups.com
On Thu, Jul 9, 2009 at 11:31 AM, Mikie<thephan...@hotmail.com> wrote:
>
> Robert,  how do I update my sage installation when you create a
> patch.  Do I have to reinstall Sage?
> Thanx

I just did

sage: hg_sage.apply('http://trac.sagemath.org/sage_trac/attachment/ticket/5081/5081-numpy-types.patch')

sage: quit

sage -br

...

sage: import numpy
sage: numpy.array([1,2,3]).dtype
dtype('int64')

w00t!

William

William Stein

unread,
Jul 9, 2009, 3:12:53 PM7/9/09
to sage-s...@googlegroups.com

And the patch works, in that not only does numpy now work, but the
original problem that started this thread is resolved:

sage: scipy.stats.binom.pmf(1,10,.56,0)
0.0034614823012532187

However, this works

sage: scipy.stats.binom.pmf(1,10,.560000000000000000000,0)
0.0034614823012532187

but this doesn't:

sage: scipy.stats.binom.pmf(1,10,.56000000000000000000000000000000000000,0)


---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/wstein/.sage/temp/teragon_2.local/93783/_Users_wstein__sage_init_sage_0.py
in <module>()

/Users/wstein/build/64bit/sage-4.1/local/lib/python2.6/site-packages/scipy/stats/distributions.py
in pmf(self, k, *args, **kwds)
3883 output = zeros(shape(cond),'d')
3884 place(output,(1-cond0)*(cond1==cond1),self.badvalue)
-> 3885 goodargs = argsreduce(cond, *((k,)+args))
3886 place(output,cond,self._pmf(*goodargs))
3887 if output.ndim == 0:

/Users/wstein/build/64bit/sage-4.1/local/lib/python2.6/site-packages/scipy/stats/distributions.py
in argsreduce(cond, *args)
246 # make sure newarr is not a scalar
247 newarr = atleast_1d(args[k])
--> 248 newargs[k] = extract(cond,newarr*expand_arr)
249 return newargs
250

TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.bool_'


-- William

William Stein

unread,
Jul 9, 2009, 3:15:46 PM7/9/09
to sage-s...@googlegroups.com

I can see that the above failing is by design (in the source code),
and seems sensible to me.

-- William

Kevin Horton

unread,
Jul 9, 2009, 4:57:39 PM7/9/09
to sage-s...@googlegroups.com

Wow! I tried it, and it solves all the problems I was having with
sage <-> numpy. I am very impressed. Kudos and thanks to Robert
Bradshaw. If I ever meet you, I'd be happy to buy you a beer, or
other beverage of your liking.

Will this change make it in sage 4.1, or will it slip to 4.1.1?

--
Kevin Horton
Ottawa, Canada

Robert Bradshaw

unread,
Jul 10, 2009, 3:10:56 AM7/10/09
to sage-s...@googlegroups.com
On Jul 9, 2009, at 1:57 PM, Kevin Horton wrote:

>
> On 9 Jul 2009, at 15:02, William Stein wrote:
>
>> On Thu, Jul 9, 2009 at 11:31 AM, Mikie<thephan...@hotmail.com>
>> wrote:
>>>
>>> Robert, how do I update my sage installation when you create a
>>> patch. Do I have to reinstall Sage?
>>> Thanx
>>
>> I just did
>>
>> sage: hg_sage.apply('http://trac.sagemath.org/sage_trac/attachment/
>> ticket/5081/5081-numpy-types.patch')
>>
>> sage: quit
>>
>> sage -br
>>
>> ...
>>
>> sage: import numpy
>> sage: numpy.array([1,2,3]).dtype
>> dtype('int64')
>>
>> w00t!
>
> Wow! I tried it, and it solves all the problems I was having with
> sage <-> numpy. I am very impressed.

Well, I figured it's about time someone did it :). I thought I was
going to have to patch the numpy c sources myself, but shortly after
diving into them I saw the much simpler solution.

> Kudos and thanks to Robert
> Bradshaw. If I ever meet you, I'd be happy to buy you a beer, or
> other beverage of your liking.
>
> Will this change make it in sage 4.1, or will it slip to 4.1.1?

Looks like 4.1 is already "out," so 4.1.1. Also, http://
trac.sagemath.org/sage_trac/ticket/6506 is up.

- Robert

Mikie

unread,
Jul 10, 2009, 12:13:28 PM7/10/09
to sage-support
I am using 3.2.2 sage. Will this fix work? The work around is
working for me and I would like not to damage the 40 stat functions I
have created.
Thanks

On Jul 10, 3:10 am, Robert Bradshaw <rober...@math.washington.edu>
wrote:
> On Jul 9, 2009, at 1:57 PM, Kevin Horton wrote:
>
>
>
>
>
> > On 9 Jul 2009, at 15:02, William Stein wrote:
>
> >> On Thu, Jul 9, 2009 at 11:31 AM, Mikie<thephantom6...@hotmail.com>

Robert Bradshaw

unread,
Jul 10, 2009, 12:19:37 PM7/10/09
to sage-s...@googlegroups.com
On Jul 10, 2009, at 9:13 AM, Mikie wrote:

> I am using 3.2.2 sage. Will this fix work?

No idea, but it might. You can try it out.

> The work around is
> working for me and I would like not to damage the 40 stat functions I
> have created.

Are you talking about modifications to the library itself? I would
recommend creating a patch and trying to get it into Sage proper
(there has been discussion about the lack of easy-to-use stats
support). A lot has changed since 3.2.2.

Mikie

unread,
Jul 10, 2009, 4:25:54 PM7/10/09
to sage-support
If it works don't fix. I like 3.2.2 because of the output in a
textarea is nicer than 4.0. I will leave it along.

On Jul 10, 10:19 am, Robert Bradshaw <rober...@math.washington.edu>
> >> - Robert- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages