Added wafo.stats + small fixes
parent
d0c62a08fa
commit
4d338e15ce
@ -0,0 +1,13 @@
|
||||
"""
|
||||
Statistics package in WAFO Toolbox.
|
||||
|
||||
Readme - Readme file for module STATS in WAFO Toolbox
|
||||
|
||||
"""
|
||||
from scipy.stats import *
|
||||
from wafo.stats.core import *
|
||||
#from wafo.stats.distributions import *
|
||||
#from wafo.spectrum.core import SpecData1D
|
||||
#import wafo.spectrum.models
|
||||
#import wafo.spectrum.dispersion_relation
|
||||
#from wafo.data_structures import SpecData1D
|
@ -0,0 +1,86 @@
|
||||
from __future__ import division
|
||||
from wafo.wafodata import WafoData
|
||||
import numpy as np
|
||||
from numpy import inf
|
||||
from numpy.lib.shape_base import atleast_1d
|
||||
from numpy.ma.core import arange, floor
|
||||
__all__ = ['edf']
|
||||
|
||||
def edf(x, method=2):
|
||||
'''
|
||||
Returns EDF Empirical Distribution Function.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array-like
|
||||
data vector
|
||||
method : integer scalar
|
||||
1. Interpolation so that F(X_(k)) == (k-0.5)/n.
|
||||
2. Interpolation so that F(X_(k)) == k/(n+1). (default)
|
||||
3. The empirical distribution. F(X_(k)) = k/n
|
||||
|
||||
Example
|
||||
-------
|
||||
>>> import wafo.stats as ws
|
||||
>>> x = np.linspace(0,6,200)
|
||||
>>> R = ws.rayleigh.rvs(scale=2,size=100)
|
||||
>>> F = ws.edf(R)
|
||||
>>> F.plot()
|
||||
|
||||
See also edf, pdfplot, cumtrapz
|
||||
'''
|
||||
|
||||
|
||||
z = atleast_1d(x)
|
||||
z.sort()
|
||||
|
||||
N = len(z)
|
||||
if method == 1:
|
||||
Fz1 = arange(0.5, N) / N
|
||||
elif method == 3:
|
||||
Fz1 = arange(1, N + 1) / N
|
||||
else:
|
||||
Fz1 = arange(1, N + 1) / (N + 1)
|
||||
|
||||
F = WafoData(Fz1, z, xlab='x', ylab='F(x)')
|
||||
F.setplotter('step')
|
||||
return F
|
||||
|
||||
def edfcnd(x, c=None, method=2):
|
||||
'''
|
||||
EDFCND Empirical Distribution Function CoNDitioned that X>=c.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array-like
|
||||
data vector
|
||||
method : integer scalar
|
||||
1. Interpolation so that F(X_(k)) == (k-0.5)/n.
|
||||
2. Interpolation so that F(X_(k)) == k/(n+1). (default)
|
||||
3. The empirical distribution. F(X_(k)) = k/n
|
||||
|
||||
Example
|
||||
-------
|
||||
>>> import wafo.stats as ws
|
||||
>>> x = np.linspace(0,6,200)
|
||||
>>> R = ws.rayleigh.rvs(scale=2,size=100)
|
||||
>>> Fc = ws.edfcd(R, 1)
|
||||
>>> Fc.plot()
|
||||
>>> F = ws.edf(R)
|
||||
>>> F.plot()
|
||||
|
||||
See also edf, pdfplot, cumtrapz
|
||||
'''
|
||||
z = atleast_1d(x)
|
||||
if c is None:
|
||||
c = floor(min(z.min(), 0))
|
||||
|
||||
try:
|
||||
F = edf(z[c <= z], method=method)
|
||||
except:
|
||||
ValueError('No data points above c=%d' % int(c))
|
||||
|
||||
if - inf < c:
|
||||
F.labels.ylab = 'F(x| X>=%g)' % c
|
||||
|
||||
return F
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,97 @@
|
||||
from numpy import asarray, ndarray, reshape, repeat, nan, product
|
||||
|
||||
def valarray(shape,value=nan,typecode=None):
|
||||
"""Return an array of all value.
|
||||
"""
|
||||
out = reshape(repeat([value],product(shape,axis=0),axis=0),shape)
|
||||
if typecode is not None:
|
||||
out = out.astype(typecode)
|
||||
if not isinstance(out, ndarray):
|
||||
out = asarray(out)
|
||||
return out
|
||||
|
||||
class rv_frozen(object):
|
||||
''' Frozen continous or discrete 1D Random Variable object (RV)
|
||||
|
||||
Methods
|
||||
-------
|
||||
RV.rvs(size=1)
|
||||
- random variates
|
||||
|
||||
RV.pdf(x)
|
||||
- probability density function (continous case)
|
||||
|
||||
RV.pmf(x)
|
||||
- probability mass function (discrete case)
|
||||
|
||||
RV.cdf(x)
|
||||
- cumulative density function
|
||||
|
||||
RV.sf(x)
|
||||
- survival function (1-cdf --- sometimes more accurate)
|
||||
|
||||
RV.ppf(q)
|
||||
- percent point function (inverse of cdf --- percentiles)
|
||||
|
||||
RV.isf(q)
|
||||
- inverse survival function (inverse of sf)
|
||||
|
||||
RV.stats(moments='mv')
|
||||
- mean('m'), variance('v'), skew('s'), and/or kurtosis('k')
|
||||
|
||||
RV.entropy()
|
||||
- (differential) entropy of the RV.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : array-like
|
||||
quantiles
|
||||
q : array-like
|
||||
lower or upper tail probability
|
||||
size : int or tuple of ints, optional, keyword
|
||||
shape of random variates
|
||||
moments : string, optional, keyword
|
||||
one or more of 'm' mean, 'v' variance, 's' skewness, 'k' kurtosis
|
||||
'''
|
||||
def __init__(self, dist, *args, **kwds):
|
||||
self.dist = dist
|
||||
loc0, scale0 = map(kwds.get, ['loc', 'scale'])
|
||||
if isinstance(dist,rv_continuous):
|
||||
args, loc0, scale0 = dist.fix_loc_scale(args, loc0, scale0)
|
||||
self.par = args + (loc0, scale0)
|
||||
else: # rv_discrete
|
||||
args, loc0 = dist.fix_loc(args, loc0)
|
||||
self.par = args + (loc0,)
|
||||
|
||||
|
||||
def pdf(self,x):
|
||||
''' Probability density function at x of the given RV.'''
|
||||
return self.dist.pdf(x,*self.par)
|
||||
def cdf(self,x):
|
||||
'''Cumulative distribution function at x of the given RV.'''
|
||||
return self.dist.cdf(x,*self.par)
|
||||
def ppf(self,q):
|
||||
'''Percent point function (inverse of cdf) at q of the given RV.'''
|
||||
return self.dist.ppf(q,*self.par)
|
||||
def isf(self,q):
|
||||
'''Inverse survival function at q of the given RV.'''
|
||||
return self.dist.isf(q,*self.par)
|
||||
def rvs(self, size=None):
|
||||
'''Random variates of given type.'''
|
||||
kwds = dict(size=size)
|
||||
return self.dist.rvs(*self.par,**kwds)
|
||||
def sf(self,x):
|
||||
'''Survival function (1-cdf) at x of the given RV.'''
|
||||
return self.dist.sf(x,*self.par)
|
||||
def stats(self,moments='mv'):
|
||||
''' Some statistics of the given RV'''
|
||||
kwds = dict(moments=moments)
|
||||
return self.dist.stats(*self.par,**kwds)
|
||||
def moment(self,n):
|
||||
par1 = self.par[:self.dist.numargs]
|
||||
return self.dist.moment(n,*par1)
|
||||
def entropy(self):
|
||||
return self.dist.entropy(*self.par)
|
||||
def pmf(self,k):
|
||||
'''Probability mass function at k of the given RV'''
|
||||
return self.dist.pmf(k,*self.par)
|
@ -0,0 +1,9 @@
|
||||
# Set this to eg. pylab to be able to plot
|
||||
import numpy
|
||||
try:
|
||||
from matplotlib import pyplot as plotbackend
|
||||
#from matplotlib import pyplot
|
||||
numpy.disp('Scipy.stats: plotbackend is set to matplotlib.pyplot')
|
||||
except:
|
||||
numpy.disp('Scipy.stats: Unable to load matplotlib.pyplot as plotbackend')
|
||||
plotbackend = None
|
Loading…
Reference in New Issue