You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.2 KiB
Python
112 lines
3.2 KiB
Python
11 years ago
|
from __future__ import division, print_function, absolute_import
|
||
|
|
||
11 years ago
|
import os
|
||
|
|
||
11 years ago
|
import numpy as np
|
||
11 years ago
|
from numpy.testing import dec
|
||
11 years ago
|
|
||
|
from wafo import stats
|
||
|
|
||
11 years ago
|
from wafo.stats.tests.test_continuous_basic import distcont
|
||
11 years ago
|
|
||
|
# this is not a proper statistical test for convergence, but only
|
||
|
# verifies that the estimate and true values don't differ by too much
|
||
11 years ago
|
|
||
|
fit_sizes = [1000, 5000] # sample sizes to try
|
||
|
thresh_percent = 0.25 # percent of true parameters for fail cut-off
|
||
11 years ago
|
thresh_min = 0.75 # minimum difference estimate - true to fail test
|
||
|
|
||
11 years ago
|
failing_fits = [
|
||
|
'burr',
|
||
|
'chi',
|
||
|
'chi2',
|
||
|
'gausshyper',
|
||
|
'genexpon',
|
||
|
'gengamma',
|
||
|
'ksone',
|
||
|
'mielke',
|
||
|
'ncf',
|
||
|
'ncx2',
|
||
|
'pearson3',
|
||
|
'powerlognorm',
|
||
|
'truncexpon',
|
||
|
'tukeylambda',
|
||
|
'vonmises',
|
||
|
'wrapcauchy',
|
||
10 years ago
|
'levy_stable'
|
||
11 years ago
|
]
|
||
11 years ago
|
|
||
11 years ago
|
# Don't run the fit test on these:
|
||
|
skip_fit = [
|
||
|
'erlang', # Subclass of gamma, generates a warning.
|
||
|
]
|
||
11 years ago
|
|
||
|
|
||
11 years ago
|
@dec.slow
|
||
|
def test_cont_fit():
|
||
11 years ago
|
# this tests the closeness of the estimated parameters to the true
|
||
|
# parameters with fit method of continuous distributions
|
||
10 years ago
|
# Note: is slow, some distributions don't converge with sample size <= 10000
|
||
11 years ago
|
|
||
11 years ago
|
for distname, arg in distcont:
|
||
11 years ago
|
if distname not in skip_fit:
|
||
10 years ago
|
yield check_cont_fit, distname,arg
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
def check_cont_fit(distname,arg):
|
||
|
options = dict(method='mps', floc=0.)
|
||
11 years ago
|
if distname in failing_fits:
|
||
|
# Skip failing fits unless overridden
|
||
|
xfail = True
|
||
|
try:
|
||
|
xfail = not int(os.environ['SCIPY_XFAIL'])
|
||
|
except:
|
||
|
pass
|
||
|
if xfail:
|
||
|
msg = "Fitting %s doesn't work reliably yet" % distname
|
||
10 years ago
|
msg += " [Set environment variable SCIPY_XFAIL=1 to run this test nevertheless.]"
|
||
|
#dec.knownfailureif(True, msg)(lambda: None)()
|
||
|
options['floc']=0.
|
||
|
options['fscale']=1.
|
||
11 years ago
|
|
||
10 years ago
|
|
||
|
# print('Testing %s' % distname)
|
||
11 years ago
|
distfn = getattr(stats, distname)
|
||
10 years ago
|
|
||
|
truearg = np.hstack([arg,[0.0,1.0]])
|
||
|
diffthreshold = np.max(np.vstack([truearg*thresh_percent,
|
||
|
np.ones(distfn.numargs+2)*thresh_min]),0)
|
||
11 years ago
|
|
||
|
for fit_size in fit_sizes:
|
||
|
# Note that if a fit succeeds, the other fit_sizes are skipped
|
||
|
np.random.seed(1234)
|
||
|
|
||
|
with np.errstate(all='ignore'):
|
||
|
rvs = distfn.rvs(size=fit_size, *arg)
|
||
10 years ago
|
# phat = distfn.fit2(rvs)
|
||
|
|
||
|
phat = distfn.fit2(rvs, **options)
|
||
|
|
||
11 years ago
|
est = phat.par
|
||
|
#est = distfn.fit(rvs) # start with default values
|
||
11 years ago
|
|
||
|
diff = est - truearg
|
||
|
|
||
|
# threshold for location
|
||
10 years ago
|
diffthreshold[-2] = np.max([np.abs(rvs.mean())*thresh_percent,thresh_min])
|
||
11 years ago
|
|
||
|
if np.any(np.isnan(est)):
|
||
|
raise AssertionError('nan returned in fit')
|
||
|
else:
|
||
|
if np.all(np.abs(diff) <= diffthreshold):
|
||
|
break
|
||
11 years ago
|
else:
|
||
11 years ago
|
txt = 'parameter: %s\n' % str(truearg)
|
||
|
txt += 'estimated: %s\n' % str(est)
|
||
|
txt += 'diff : %s\n' % str(diff)
|
||
|
raise AssertionError('fit not very good in %s\n' % distfn.name + txt)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
np.testing.run_module_suite()
|