# [SciPy-user] Sparse Random Variables

Tom Johnson tjhnson@gmail....
Sat Oct 20 23:43:22 CDT 2007

```Hi, I'm still looking for some help here....

On 10/17/07, Tom Johnson <tjhnson@gmail.com> wrote:
>
> 4) Also, for some reason entropy() doesn't always work on the first try...
>
> >>> from scipy import *
> >>> x = 1e3
> >>> v = rand(x)
> >>> v = v/sum(x)
> >>> a = stats.rv_discrete(name='test', values=(range(x), v))
> >>> a.entropy()
> >>> a.entropy()
>
> The first entropy raises an error.  The second works.  The problem
> seems to be with:
>
> /home/me/lib/python/scipy/stats/distributions.py in entropy(self, *args, **kwds)
> -> 3794         place(output,cond0,self.vecentropy(*goodargs))
>
> /home/me/lib/python/numpy/lib/function_base.py in __call__(self, *args)
>     940
>     941         if self.nout == 1:
> --> 942             _res =
> array(self.ufunc(*args),copy=False).astype(self.otypes[0])
>     943         else:
>     944             _res = tuple([array(x,copy=False).astype(c) \
>
> <type 'exceptions.TypeError'>: function not supported for these types,
> and can't coerce safely to supported types
>
>

Should I submit a bug?

>
> 5) I really need to have random variables where the xk are tuples of
> the same type (integers xor floats xor strings ...)
>
> p( (0,0) ) = .25
> p( (0,1) ) = .25
> p( (1,0) ) = .25
> p( (1,1) ) = .25
>
> but
>
> a = stats.rv_discrete(name='test', values=(((0,0),(0,1),(1,0),(1,1)), [.25]*4))
>
> yields
>
> /home/me/lib/python/numpy/core/fromnumeric.py in take(a, indices,
> axis, out, mode)
>      79     except AttributeError:
>      80         return _wrapit(a, 'take', indices, axis, out, mode)
> ---> 81     return take(indices, axis, out, mode)
>      82
>      83
>
> <type 'exceptions.IndexError'>: index out of range for array
>
> My initial thought would be that the xk could be anything that is
> hashable.  For dictionary-based discrete distributions, I do use
> tuples...but I would like to start using scipy.stats.  I am fishing
> for too much or in the wrong lake?
>

Any hints on this?
```