Flavio Coelho
unread,Apr 9, 2009, 6:34:15 AM4/9/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sage-support
Hi I having trouble with running code within sage which runs perfectly
in straight Python:
import scipy.stats as stats
import numpy
def lhsFromSample(sample,siz=100):
"""
Latin Hypercube Sample from a set of values
:Parameters:
- `sample`: list, tuple of array
- `siz`: Number or shape tuple for the output sample
"""
if not isinstance(sample, (list,tuple,numpy.ndarray)):
raise TypeError('sample is not a list, tuple or numpy vector')
n = siz
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)
perc = numpy.arange(0,100.,100./n)
numpy.random.shuffle(perc)
smp = [stats.uniform(i,100./n).rvs() for i in perc]
v = numpy.array([stats.scoreatpercentile(sample,p) for p in smp])
if isinstance(siz,(tuple,list)):
v.shape = siz
return v
def lhsFromDensity(kde,siz=100):
'''
LHS sampling from a variables Kernel density estimate.
:Parameters:
- `kde`: scipy.stats.kde.gaussian_kde object
- `siz`: Number or shape tuple for the output sample
'''
if not isinstance(kde,scipy.stats.kde.gaussian_kde):
raise TypeError("kde is not a density object")
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)
s = kde.resample(n)
v = lhsFromSample(s,n)
if isinstance(siz,(tuple,list)):
v.shape = siz
return v
def lhs(dist, parms, siz=100):
'''
Latin Hypercube sampling of any distrbution.
dist is is a scipy.stats random number generator
such as stats.norm, stats.beta, etc
parms is a tuple with the parameters needed for
the specified distribution.
:Parameters:
- `dist`: random number generator from scipy.stats module.
- `parms`: tuple of parameters as required for dist.
- `siz` :number or shape tuple for the output sample
'''
if not isinstance(dist, (stats.rv_discrete,stats.rv_continuous)):
raise TypeError('dist is not a scipy.stats distribution
object')
n=siz
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)
perc = numpy.arange(0,1.,1./n)
numpy.random.shuffle(perc)
smp = [stats.uniform(i,1./n).rvs() for i in perc]
v = dist(*parms).ppf(smp)
if isinstance(siz,(tuple,list)):
v.shape = siz
return v
lhs(stats.uniform,(1,30),10)
If I run this code in Sage 3.4 it gives the following error and
traceback:
Traceback (click to the left for traceback)
...
TypeError: array cannot be safely cast to required type
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/fccoelho/.sage/sage_notebook/worksheets/admin/2/code/
4.py", line 79, in <module>
lhs(stats.uniform,
(_sage_const_1 ,_sage_const_30 ),_sage_const_10 )
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/SQLAlchemy-0.4.6-py2.5.egg/", line
1, in <module>
File "/home/fccoelho/.sage/sage_notebook/worksheets/admin/2/code/
4.py", line 73, in lhs
v = dist(*parms).ppf(smp)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/scipy/stats/distributions.py", line
112, in ppf
return self.dist.ppf(q,*self.args,**self.kwds)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/scipy/stats/distributions.py", line
563, in ppf
place(output,cond,self._ppf(*goodargs)*scale + loc)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/numpy/lib/function_base.py", line
1357, in place
return _insert(arr, mask, vals)
TypeError: array cannot be safely cast to required type
can anyone tell me if this is a Sage "bug" or "feature" ;-)
Flávio