scipy.stats.poisson.pmf doesn't work

97 views
Skip to first unread message

Alden

unread,
Apr 28, 2009, 3:29:41 PM4/28/09
to sage-support
On two different computers running Ubuntu 9.04, I downloaded and built
from source sage 3.4.1. I also downloaded scipy using the synaptic
package manager. I am under the impression that python and scipy in
sage lead completely separate lives from python and scipy outside
sage. In normal python (i.e. running python from the command line),
the following works fine:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy.stats
>>> scipy.stats.poisson.pmf(5,1)
0.00306566200976202

But when I run sage, I get the following:
----------------------------------------------------------------------
| Sage Version 3.4.1, Release Date: 2009-04-21 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import scipy.stats
sage: scipy.stats.poisson.pmf(5,1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)

/home/awalker/sage-3.4.1/<ipython console> in <module>()

/home/awalker/sage-3.4.1/local/lib/python2.5/site-packages/scipy/stats/
distributions.pyc in pmf(self, k, *args, **kwds)
3517 output = zeros(shape(cond),'d')
3518 place(output,(1-cond0)*(cond1==cond1),self.badvalue)
-> 3519 goodargs = argsreduce(cond, *((k,)+args))
3520 place(output,cond,self._pmf(*goodargs))
3521 return output

/home/awalker/sage-3.4.1/local/lib/python2.5/site-packages/scipy/stats/
distributions.pyc in argsreduce(cond, *args)
237 # make sure newarr is not a scalar
238 newarr = atleast_1d(args[k])
--> 239 newargs[k] = extract(cond,newarr*expand_arr)
240 return newargs
241

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

Mike Hansen

unread,
Apr 28, 2009, 3:35:24 PM4/28/09
to sage-s...@googlegroups.com
Hello,

On Tue, Apr 28, 2009 at 12:29 PM, Alden <alden....@gmail.com> wrote:
>
> On two different computers running Ubuntu 9.04, I downloaded and built
> from source sage 3.4.1.  I also downloaded scipy using the synaptic
> package manager.  I am under the impression that python and scipy in
> sage lead completely separate lives from python and scipy outside
> sage.  In normal python (i.e. running python from the command line),
> the following works fine:

This is correct.

> sage: import scipy.stats
> sage: scipy.stats.poisson.pmf(5,1)

When you type this, what get sent to Sage is the output of

sage: preparse('scipy.stats.poisson.pmf(5,1)')
'scipy.stats.poisson.pmf(Integer(5),Integer(1))'

Numpy/Scipy don't know how to deal with Sage's Integer class. You can
get around this in a number of ways:

1) Use the "r" notation for "raw" Python ints:

sage: preparse('scipy.stats.poisson.pmf(5r,1r)')
'scipy.stats.poisson.pmf(5,1)'
sage: scipy.stats.poisson.pmf(5r,1r)
array(0.00306566200976202)

2) Explicitly make ints:

sage: scipy.stats.poisson.pmf(int(5),int(1))
array(0.00306566200976202)

3) Turn off the preparser:

sage: preparser(False)
sage: scipy.stats.poisson.pmf(5,1)
array(0.00306566200976202)
sage: preparser(True)

4) Set Integer to be int (and RealNumber to be Float):

sage: Integer = int
sage: RealNumber = float
sage: scipy.stats.poisson.pmf(5,1)
array(0.00306566200976202)

--Mike

William Stein

unread,
Apr 28, 2009, 3:35:51 PM4/28/09
to sage-s...@googlegroups.com
On Tue, Apr 28, 2009 at 12:29 PM, Alden <alden....@gmail.com> wrote:
>
> On two different computers running Ubuntu 9.04, I downloaded and built
> from source sage 3.4.1.  I also downloaded scipy using the synaptic
> package manager.  I am under the impression that python and scipy in
> sage lead completely separate lives from python and scipy outside
> sage.

True.

>  In normal python (i.e. running python from the command line),
> the following works fine:

This will evidently get fixed when we upgrade the version of scipy
included in Sage, which we
hope to do soon.
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

William Stein

unread,
Apr 28, 2009, 3:40:08 PM4/28/09
to sage-s...@googlegroups.com
On Tue, Apr 28, 2009 at 12:35 PM, William Stein <wst...@gmail.com> wrote:
> On Tue, Apr 28, 2009 at 12:29 PM, Alden <alden....@gmail.com> wrote:
>>
>> On two different computers running Ubuntu 9.04, I downloaded and built
>> from source sage 3.4.1.  I also downloaded scipy using the synaptic
>> package manager.  I am under the impression that python and scipy in
>> sage lead completely separate lives from python and scipy outside
>> sage.
>
> True.
>
>>  In normal python (i.e. running python from the command line),
>> the following works fine:
>
> This will evidently get fixed when we upgrade the version of scipy
> included in Sage, which we
> hope to do soon.
>

Mike's solution is right and my remark isn't. I had tested resetting
Integer by doing the following
in a notebook cell:

{{{
RealNumber=float; Integer=int
import scipy.stats
scipy.stats.poisson.pmf(5,1)
}}}

However, stupidly Integer=int gets evaluated after the constant 5 gets
factored out. This would have worked

{{{
RealNumber=float; Integer=int
}}}

{{{
import scipy.stats
scipy.stats.poisson.pmf(5,1)
}}}

We *really* need to patch scipy.stats so that this fixed...

-- William

Alden

unread,
Apr 28, 2009, 3:50:48 PM4/28/09
to sage-support
Thanks!

Jason Grout

unread,
Apr 28, 2009, 4:15:03 PM4/28/09
to sage-s...@googlegroups.com
William Stein wrote:

>
>> In normal python (i.e. running python from the command line),
>> the following works fine:
>
> This will evidently get fixed when we upgrade the version of scipy
> included in Sage, which we
> hope to do soon.


Are you saying this is already fixed in scipy (i.e., a new scipy will
recognize sage numbers)?

Thanks,

Jason


--
Jason Grout

William Stein

unread,
Apr 28, 2009, 4:45:18 PM4/28/09
to sage-s...@googlegroups.com
No.
Reply all
Reply to author
Forward
0 new messages