Per.Andreas.Brodtkorb 15 years ago
parent 4d338e15ce
commit 652bf4981d

@ -0,0 +1,69 @@
<map version="0.8.1">
<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->
<node CREATED="1268907205249" ID="Freemind_Link_1834025809" MODIFIED="1268920224244" STYLE="fork" TEXT="WAFO Objects">
<font NAME="SansSerif" SIZE="11"/>
<node CREATED="1268919537155" ID="Freemind_Link_823244205" MODIFIED="1268920190865" POSITION="right" STYLE="bubble" TEXT="SpecData1D" VSHIFT="36">
<node CREATED="1268908695254" ID="Freemind_Link_834391422" MODIFIED="1268908707582" TEXT="bandwidth"/>
<node CREATED="1268908733082" ID="Freemind_Link_1433599166" MODIFIED="1268908736348" TEXT="characteristic"/>
<node CREATED="1268908690441" ID="Freemind_Link_1284342306" MODIFIED="1268908694941" TEXT="normalize"/>
<node CREATED="1268908669144" ID="Freemind_Link_1815459961" MODIFIED="1268908677316" TEXT="nyquist_freq"/>
<node CREATED="1268908664160" ID="Freemind_Link_703739784" MODIFIED="1268919800253" TEXT="moment" VSHIFT="4"/>
<node CREATED="1268908678394" ID="Freemind_Link_1387269779" MODIFIED="1268919203464" TEXT="sampling_period" VSHIFT="3"/>
<node CREATED="1268908643159" ID="Freemind_Link_895051601" MODIFIED="1268908655410" TEXT="stats_nl"/>
<node CREATED="1268907362986" ID="Freemind_Link_1959199999" MODIFIED="1268920763548" TEXT="sim">
<arrowlink DESTINATION="Freemind_Link_1110096845" ENDARROW="Default" ENDINCLINATION="-26;-470;" ID="Freemind_Arrow_Link_658989110" STARTARROW="None" STARTINCLINATION="134;20;"/>
</node>
<node CREATED="1268908639894" ID="Freemind_Link_881848843" MODIFIED="1268920768236" TEXT="sim_nl" VSHIFT="-116">
<arrowlink DESTINATION="Freemind_Link_1110096845" ENDARROW="Default" ENDINCLINATION="-12;-203;" ID="Freemind_Arrow_Link_194892354" STARTARROW="None" STARTINCLINATION="286;65;"/>
</node>
<node CREATED="1268908656097" ID="Freemind_Link_932055041" MODIFIED="1268920049331" TEXT="testgaussian" VSHIFT="-11"/>
<node CREATED="1268908524408" ID="Freemind_Link_451836141" MODIFIED="1268920637967" TEXT="tocovdata" VSHIFT="122">
<arrowlink DESTINATION="Freemind_Link_1298097150" ENDARROW="Default" ENDINCLINATION="22;-61;" ID="Freemind_Arrow_Link_459039584" STARTARROW="None" STARTINCLINATION="155;-17;"/>
</node>
<node CREATED="1268907881846" ID="Freemind_Link_961675105" MODIFIED="1268908523861" TEXT="tocov_matrix"/>
<node CREATED="1268908531877" ID="Freemind_Link_1361566099" MODIFIED="1268920224244" TEXT="to_linspec"/>
<node CREATED="1268908548674" ID="Freemind_Link_28901814" MODIFIED="1268920222025" TEXT="to_specnorm"/>
<node CREATED="1268908684613" ID="Freemind_Link_180569473" MODIFIED="1268917666022" TEXT="resample"/>
</node>
<node CREATED="1268918310233" HGAP="471" ID="Freemind_Link_1110096845" MODIFIED="1268920768236" POSITION="right" STYLE="bubble" TEXT="TimeSeries" VSHIFT="-65">
<node CREATED="1268918758255" ID="Freemind_Link_1306080198" MODIFIED="1268920381237" STYLE="fork" TEXT="tospecdata"/>
<node CREATED="1268918767833" ID="Freemind_Link_1037382917" MODIFIED="1268920384363" STYLE="fork" TEXT="tocovdata"/>
<node CREATED="1268918809021" ID="Freemind_Link_767625747" MODIFIED="1268918816990" STYLE="fork" TEXT="wave_periods"/>
<node CREATED="1268918817396" ID="Freemind_Link_294440483" MODIFIED="1268918823943" STYLE="fork" TEXT="reconstruct"/>
<node CREATED="1268918824365" ID="Freemind_Link_1893872699" MODIFIED="1268920714281" STYLE="fork" TEXT="plot_wave" VSHIFT="29"/>
<node CREATED="1268918835318" ID="Freemind_Link_1625232632" MODIFIED="1268918848897" STYLE="fork" TEXT="plot_sp_wave"/>
<node CREATED="1268918669644" ID="Freemind_Link_525365373" MODIFIED="1268918757817" STYLE="fork" TEXT="sampling_period"/>
<node CREATED="1268918800427" ID="Freemind_Link_540262488" MODIFIED="1268920657436" STYLE="fork" TEXT="through_crest" VSHIFT="11">
<arrowlink DESTINATION="Freemind_Link_1866699423" ENDARROW="Default" ENDINCLINATION="120;-87;" ID="Freemind_Arrow_Link_724369012" STARTARROW="None" STARTINCLINATION="298;0;"/>
</node>
<node CREATED="1268918781115" ID="Freemind_Link_191369931" MODIFIED="1268920652670" STYLE="fork" TEXT="turning_points">
<arrowlink DESTINATION="Freemind_Link_1866699423" ENDARROW="Default" ENDINCLINATION="354;-263;" ID="Freemind_Arrow_Link_301423490" STARTARROW="None" STARTINCLINATION="-57;21;"/>
</node>
</node>
<node CREATED="1268917687085" ID="Freemind_Link_1298097150" MODIFIED="1268920673171" POSITION="right" STYLE="bubble" TEXT="CovData1D" VSHIFT="-212">
<node CREATED="1268917901666" ID="Freemind_Link_1791940512" MODIFIED="1268919887910" STYLE="fork" TEXT="tospecdata">
<arrowlink DESTINATION="Freemind_Link_823244205" ENDARROW="Default" ENDINCLINATION="14;87;" ID="Freemind_Arrow_Link_728651707" STARTARROW="None" STARTINCLINATION="-21;-38;"/>
</node>
<node CREATED="1268918164841" ID="Freemind_Link_1718380955" MODIFIED="1268918173622" STYLE="fork" TEXT="sampling_period"/>
<node CREATED="1268918174044" ID="Freemind_Link_1888500587" MODIFIED="1268920740704" STYLE="fork" TEXT="sim">
<arrowlink DESTINATION="Freemind_Link_1110096845" ENDARROW="Default" ENDINCLINATION="-2;21;" ID="Freemind_Arrow_Link_174025984" STARTARROW="None" STARTINCLINATION="271;12;"/>
</node>
<node CREATED="1268918178013" ID="Freemind_Link_757742844" MODIFIED="1268920750954" STYLE="fork" TEXT="simcond">
<arrowlink DESTINATION="Freemind_Link_1110096845" ENDARROW="Default" ENDINCLINATION="-46;173;" ID="Freemind_Arrow_Link_1889053469" STARTARROW="None" STARTINCLINATION="171;0;"/>
</node>
</node>
<node CREATED="1268918904132" ID="Freemind_Link_1866699423" MODIFIED="1268920673171" POSITION="right" STYLE="bubble" TEXT="TurningPoints">
<node CREATED="1268918978226" ID="Freemind_Link_1033172969" MODIFIED="1268918989023" STYLE="fork" TEXT="cycle_pairs"/>
</node>
<node CREATED="1268918988977" ID="Freemind_Link_280025207" MODIFIED="1268920787861" POSITION="right" STYLE="bubble" TEXT="CyclePairs" VSHIFT="91">
<node CREATED="1268919011477" ID="Freemind_Link_867889153" MODIFIED="1268919019180" STYLE="fork" TEXT="amplitudes"/>
<node CREATED="1268919019586" ID="Freemind_Link_40128767" MODIFIED="1268919024414" STYLE="fork" TEXT="damage"/>
<node CREATED="1268919025024" ID="Freemind_Link_1194799087" MODIFIED="1268919033727" STYLE="fork" TEXT="level_crossings"/>
</node>
<node CREATED="1268919295668" ID="Freemind_Link_106637151" MODIFIED="1268919305606" POSITION="right" STYLE="bubble" TEXT="LevelCrossings">
<node CREATED="1268919349809" ID="Freemind_Link_1941466065" MODIFIED="1268920453960" STYLE="fork" TEXT="sim"/>
<node CREATED="1268919305574" ID="Freemind_Link_1378561112" MODIFIED="1268919349122" STYLE="fork" TEXT="trdata"/>
</node>
<node CREATED="1268920796033" ID="_" MODIFIED="1268920800424" POSITION="right" STYLE="bubble" TEXT="TrData"/>
</node>
</map>

@ -12,6 +12,7 @@ if False:
else:
try:
from matplotlib import pyplot as plotbackend
plotbackend.interactive(True)
print('wafo.wafodata: plotbackend is set to matplotlib.pyplot')
except:
print('wafo: Unable to load matplotlib.pyplot as plotbackend')

@ -236,107 +236,6 @@ class rv_frozen_old(object):
return self.dist.pmf(k, *self.args, **self.kwds)
def ecross(t,f,ind,v):
''' Extracts exact level v crossings
Parameters
-----------
t,f : vectors
arguments and functions values, respectively.
ind : scalar or vector of integers
indices to level v crossings as found by findcross.
v : scalar or vector (of size(ind))
defining the level(s) to cross.
Returns
--------
t0 : vector
exact level v crossings.
Description
-----------
ECROSS interpolates t and f linearly to find the exact level v
crossings, i.e., the points where f(t0) = v
Example
-------
>>> from matplotlib import pylab as plb
>>> t = plb.linspace(0,7*pi,250)
>>> x = sin(t);
>>> ind = findcross(x,0.75)
>>> t0 = ecross(t,x,ind,0.75);
>>> plb.plot(t,x,'.',t[ind],x[ind],'r.',t, ones(t.shape)*.75, t0,ones(t0.shape)*0.75,'g.')
See also
--------
findcross
'''
# Tested on: Python 2.5
# revised pab Feb2004
# By pab 18.06.2001
return t[ind]+(v-f[ind])*(t[ind+1]-t[ind])/(f[ind+1]-f[ind])
def findcross(x,v=0.0):
'''
Return indices to level v up and downcrossings of a vector
Parameters
----------
x : vector
sampled values.
v : scalar
crossing level. (Default 0).
Returns
-------
ind : vector of integers
indices to the crossings in the original sequence x.
Example
-------
>>> from matplotlib import pylab as plb
>>> v = 0.75
>>> t = plb.linspace(0,7*pi,250); x = sin(t);
>>> ind = findcross(x,v)
>>> plb.plot(t,x,'.',t[ind],x[ind],'r.', t, ones(t.shape)*v)
See also
---------
crossdef
'''
xn = numpy.atleast_1d(x)
xn = numpy.int8(numpy.sign(xn.ravel()-v))
ind = None
n = len(xn)
if n>1:
iz, = numpy.nonzero(xn==0)
if any(iz):
# Trick to avoid turning points on the crossinglevel.
if iz[0]==0:
if len(iz)==n:
#warning('All values are equal to crossing level!')
return ind
diz = numpy.diff(iz)
ix = iz((diz>1).argmax())
if not any(ix):
ix = iz[-1]
#x(ix) is a up crossing if x(1:ix) = v and x(ix+1) > v.
#x(ix) is a downcrossing if x(1:ix) = v and x(ix+1) < v.
xn[0:ix] = -xn[ix+1]
iz = iz[ix::]
for ix in iz.tolist():
xn[ix] = xn[ix-1]
#% indices to local level crossings ( without turningpoints)
#ind, = numpy.nonzero(xn[:n-1]*xn[1:] < 0)
ind, = (xn[:n-1]*xn[1:] < 0).nonzero()
return ind
def stirlerr(n):
"""
Return error of Stirling approximation,
@ -400,6 +299,7 @@ def stirlerr(n):
y[n15] = (S0 - (S1 - (S2 - (S3 - S4 / nn15) / nn15) / nn15) / nn15) / n1[n15]
return y
def bd0(x, npr):
"""
Return deviance term x*log(x/npr) + npr - x
@ -5734,12 +5634,15 @@ Skellam distribution
"""
)
if __name__=='__main__':
def main():
import matplotlib
matplotlib.interactive(True)
#nbinom(10, 0.75).rvs(3)
bernoulli(0.75).rvs(3)
t = bernoulli(0.75).rvs(3)
x = np.r_[5, 10]
npr = np.r_[9, 9]
bd0(x,npr)
t2 = bd0(x, npr)
#Examples MLE and better CI for phat.par[0]
R = weibull_min.rvs(1, size=100);
phat = weibull_min.fit(R, 1, 1, par_fix=[nan, 0, nan])
@ -5777,3 +5680,5 @@ if __name__=='__main__':
r = genpareto.rvs(0, size=100)
pht = genpareto.fit(r, 1, par_fix=[0, 0, nan])
lp = pht.profile()
if __name__ == '__main__':
main()

@ -8,6 +8,8 @@ from __future__ import division
from wafo.plotbackend import plotbackend
from wafo.misc import ecross, findcross, JITImport
from scipy.misc.ppimport import ppimport
import numdifftools
from scipy.linalg import pinv2
@ -16,12 +18,13 @@ from scipy.linalg import pinv2
from scipy import optimize
# Trick to avoid error due to circular import
#_WAFOCOV = ppimport('wafo.covariance')
_WAFODIST = JITImport('wafo.stats.distributions')
_WAFODIST = ppimport('wafo.stats.distributions')
#_WAFODIST = JITImport('wafo.stats.distributions')
from numpy import alltrue, arange, \
ravel, ones, sum, \
zeros, log, sqrt, exp
from numpy import atleast_1d, any, vectorize, asarray, nan, inf, pi
from numpy import atleast_1d, any, asarray, nan, inf, pi, reshape, repeat, product, ndarray
import numpy
import numpy as np
@ -38,13 +41,17 @@ floatinfo = np.finfo(float)
#arr = atleast_1d
arr = asarray
all = alltrue
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
# Frozen RV class
class rv_frozen(object):
@ -240,7 +247,7 @@ class Profile(object):
self.i_free = nonzero(isfree)
self.Lmax = Lmax
self.alpha_Lrange = 0.5*chi2.isf(self.alpha,1)
self.alpha_Lrange = 0.5 * _WAFODIST.chi2.isf(self.alpha, 1)
self.alpha_cross_level = Lmax - self.alpha_Lrange
lowLevel = self.alpha_cross_level - self.alpha_Lrange / 7.0
@ -337,7 +344,7 @@ class Profile(object):
pcov = self.fit_dist.par_cov[i_notfixed, :][:, i_notfixed]
pvar = sum(numpy.dot(drl, pcov) * drl)
p_crit = norm.isf(self.alpha/2.0)*sqrt(numpy.ravel(pvar))*1.5
p_crit = _WAFODIST.norm.isf(self.alpha / 2.0) * sqrt(numpy.ravel(pvar)) * 1.5
if self.pmin == None:
self.pmin = p_opt - 5.0 * p_crit
if self.pmax == None:
@ -359,10 +366,10 @@ class Profile(object):
prb = exp(self.logSF)
return self.fit_dist.dist.isf(prb, *mphat);
def _myprbfun(phatnotfixed):
def _myprbfun(self, phatnotfixed):
mphat = self._par.copy()
mphat[self.i_notfixed] = phatnotfixed;
return self.fit_dist.dist.sf(self.x,*mphat);
return self.fit_dist.dist.sf(self.x, *mphat)
def _nlogfun(self, free_par, fix_par):
@ -385,7 +392,7 @@ class Profile(object):
if alpha < self.alpha:
raise ValueError('Unable to return CI with alpha less than %g' % self.alpha)
cross_level = self.Lmax - 0.5*chi2.isf(alpha,1)
cross_level = self.Lmax - 0.5 * _WAFODIST.chi2.isf(alpha, 1)
ind = findcross(self.data, cross_level)
N = len(ind)
if N == 0:
@ -529,7 +536,7 @@ class FitDistribution(rv_frozen):
self.par_cov[:, :] = nan
pvar = numpy.diag(self.par_cov)
zcrit = -norm.ppf(self.alpha/2.0)
zcrit = -_WAFODIST.norm.ppf(self.alpha / 2.0)
self.par_lower = self.par - zcrit * sqrt(pvar)
self.par_upper = self.par + zcrit * sqrt(pvar)
@ -614,8 +621,6 @@ class FitDistribution(rv_frozen):
return Profile(self, **kwds)
def plotfitsumry(self):
''' Plot various diagnostic plots to asses the quality of the fit.
@ -771,7 +776,7 @@ class FitDistribution(rv_frozen):
dx = numpy.diff(x, axis=0)
tie = (dx == 0)
if any(tie):
disp('P-value is on the conservative side (i.e. too large) due to ties in the data!')
print('P-value is on the conservative side (i.e. too large) due to ties in the data!')
T = self.nlogps(theta, x)
@ -788,7 +793,7 @@ class FitDistribution(rv_frozen):
C1 = m - sqrt(0.5 * n * v)
C2 = sqrt(v / (2.0 * n))
Tn = (T + 0.5 * k * isParUnKnown - C1) / C2 # chi2 with n degrees of freedom
pvalue = chi2.sf(Tn,n)
pvalue = _WAFODIST.chi2.sf(Tn, n)
return pvalue
def nlogps(self, theta, x):
@ -830,7 +835,7 @@ class FitDistribution(rv_frozen):
if (any(cond0)):
return inf
else:
linfo = numpy.finfo(float)
#linfo = numpy.finfo(float)
realmax = floatinfo.machar.xmax
lowertail = True
@ -857,7 +862,7 @@ class FitDistribution(rv_frozen):
i_tie = nonzero(tie)
tiedata = x[i_tie]
logD[(I_tie[0]+1,)]= log(self.dist._pdf(tiedata,*args)) + log(scale)
logD[(i_tie[0] + 1,)] = log(self.dist._pdf(tiedata, *args)) + log(scale)
finiteD = numpy.isfinite(logD)
nonfiniteD = 1 - finiteD
@ -884,15 +889,16 @@ class FitDistribution(rv_frozen):
return inf
x = arr((x - loc) / scale)
cond0 = (x <= self.a) | (self.b <= x)
newCall = False
if newCall:
goodargs = argsreduce(1-cond0, *((x,)))
goodargs = tuple(goodargs + list(args))
N = len(x)
Nbad = sum(cond0)
xmin = floatinfo.machar.xmin
return self._nnlf(*goodargs) + N*log(scale) + Nbad*100.0*log(xmin)
elif (any(cond0)):
# newCall = False
# if newCall:
# goodargs = argsreduce(1-cond0, *((x,)))
# goodargs = tuple(goodargs + list(args))
# N = len(x)
# Nbad = sum(cond0)
# xmin = floatinfo.machar.xmin
# return self._nnlf(*goodargs) + N*log(scale) + Nbad*100.0*log(xmin)
# el
if (any(cond0)):
return inf
else:
N = len(x)
@ -957,16 +963,17 @@ class FitDistribution(rv_frozen):
#pcov = -pinv(H);
return - H
if __name__=='__main__':
def main():
#nbinom(10, 0.75).rvs(3)
bernoulli(0.75).rvs(3)
import matplotlib
matplotlib.interactive(True)
t = _WAFODIST.bernoulli(0.75).rvs(3)
x = np.r_[5, 10]
npr = np.r_[9, 9]
bd0(x,npr)
t2 = _WAFODIST.bd0(x, npr)
#Examples MLE and better CI for phat.par[0]
R = weibull_min.rvs(1, size=100);
phat = weibull_min.fit(R,1,1,par_fix=[nan,0,nan])
R = _WAFODIST.weibull_min.rvs(1, size=100);
phat = _WAFODIST.weibull_min.fit(R, 1, 1, par_fix=[nan, 0, nan])
Lp = phat.profile(i=0)
Lp.plot()
Lp.get_CI(alpha=0.1)
@ -990,14 +997,17 @@ if __name__=='__main__':
t = zipf(2)
t.ppf(0.5)
import pylab as plb
rice.rvs(1)
_WAFODIST.rice.rvs(1)
x = plb.linspace(-5, 5)
y = genpareto.cdf(x,0)
y = _WAFODIST.genpareto.cdf(x, 0)
#plb.plot(x,y)
#plb.show()
on = ones((2, 3))
r = genpareto.rvs(0,size=100)
pht = genpareto.fit(r,1,par_fix=[0,0,nan])
r = _WAFODIST.genpareto.rvs(0, size=100)
pht = _WAFODIST.genpareto.fit(r, 1, par_fix=[0, 0, nan])
lp = pht.profile()
if __name__ == '__main__':
main()

@ -0,0 +1,9 @@
from scipy.misc.ppimport import ppimport
st = ppimport('scitools')
def main():
t = st.numpytools.linspace(0,1)
print(t)
if __name__ == '__main__':
main()
Loading…
Cancel
Save