def check_sample_var(sv, n, popvar): # two-sided chisquare test for sample variance equal to hypothesized variance df = n - 1 chi2 = (n - 1) * popvar / float(popvar) pval = stats.chisqprob(chi2, df) * 2 npt.assertTrue( pval > 0.01, 'var fail, t, pval = %f, %f, v, sv=%f, %f' % (chi2, pval, popvar, sv))
def check_distribution_rvs(dist, args, alpha, rvs): # test from scipy.stats.tests # this version reuses existing random variables D, pval = stats.kstest(rvs, dist, args=args, N=1000) if (pval < alpha): D, pval = stats.kstest(dist, '', args=args, N=1000) npt.assertTrue( pval > alpha, "D = " + str(D) + "; pval = " + str(pval) + "; alpha = " + str(alpha) + "\nargs = " + str(args))
def check_kurt_expect(distfn, arg, m, v, k, msg): if np.isfinite(k): m4e = distfn.expect(lambda x: np.power(x - m, 4), arg) npt.assert_allclose(m4e, (k + 3.) * np.power(v, 2), atol=1e-5, rtol=1e-5, err_msg=msg + ' - kurtosis') else: npt.assertTrue(np.isnan(k))
def check_skew_expect(distfn, arg, m, v, s, msg): if np.isfinite(s): m3e = distfn.expect(lambda x: np.power(x - m, 3), arg) npt.assert_almost_equal(m3e, s * np.power(v, 1.5), decimal=5, err_msg=msg + ' - skew') else: npt.assertTrue(np.isnan(s))
def check_discrete_chisquare(distfn, arg, rvs, alpha, msg): """Perform chisquare test for random sample of a discrete distribution Parameters ---------- distname : string name of distribution function arg : sequence parameters of distribution alpha : float significance level, threshold for p-value Returns ------- result : bool 0 if test passes, 1 if test fails uses global variable debug for printing results """ n = len(rvs) nsupp = 20 wsupp = 1.0 / nsupp # construct intervals with minimum mass 1/nsupp # intervals are left-half-open as in a cdf difference distsupport = range(max(distfn.a, -1000), min(distfn.b, 1000) + 1) last = 0 distsupp = [max(distfn.a, -1000)] distmass = [] for ii in distsupport: current = distfn.cdf(ii, *arg) if current - last >= wsupp - 1e-14: distsupp.append(ii) distmass.append(current - last) last = current if current > (1 - wsupp): break if distsupp[-1] < distfn.b: distsupp.append(distfn.b) distmass.append(1 - last) distsupp = np.array(distsupp) distmass = np.array(distmass) # convert intervals to right-half-open as required by histogram histsupp = distsupp + 1e-8 histsupp[0] = distfn.a # find sample frequencies and perform chisquare test freq, hsupp = np.histogram(rvs, histsupp) cdfs = distfn.cdf(distsupp, *arg) (chis, pval) = stats.chisquare(np.array(freq), n * distmass) npt.assertTrue( pval > alpha, 'chisquare - test for %s' ' at arg = %s with pval = %s' % (msg, str(arg), str(pval)))
def test_distribution_rvs(self): alpha = 0.01 loc = 0 scale = 1 arg = (loc, scale) distfn = loguniform_gen(0, 1) D,pval = stats.kstest(distfn.rvs, distfn.cdf, args=arg, N=1000) if (pval < alpha): npt.assertTrue(pval > alpha, "D = %f; pval = %f; alpha = %f; args=%s" % ( D, pval, alpha, arg))
def check_sample_mean(sm, v, n, popmean): # from stats.stats.ttest_1samp(a, popmean): # Calculates the t-obtained for the independent samples T-test on ONE group # of scores a, given a population mean. # # Returns: t-value, two-tailed prob df = n - 1 svar = ((n - 1) * v) / float(df) # looks redundant t = (sm - popmean) / np.sqrt(svar * (1.0 / n)) prob = stats.betai(0.5 * df, 0.5, df / (df + t * t)) # return t,prob npt.assertTrue( prob > 0.01, 'mean fail, t,prob = %f, %f, m, sm=%f,%f' % (t, prob, popmean, sm))
def check_oth(distfn, arg, supp, msg): # checking other methods of distfn npt.assert_allclose(distfn.sf(supp, *arg), 1. - distfn.cdf(supp, *arg), atol=1e-10, rtol=1e-10) q = np.linspace(0.01, 0.99, 20) npt.assert_allclose(distfn.isf(q, *arg), distfn.ppf(1. - q, *arg), atol=1e-10, rtol=1e-10) median_sf = distfn.isf(0.5, *arg) npt.assertTrue(distfn.sf(median_sf - 1, *arg) > 0.5) npt.assertTrue(distfn.cdf(median_sf + 1, *arg) > 0.5)
def check_d_samples(dfn, n, rtol=1e-2, atol=1e-2): counts = defaultdict(lambda: 0) #print 'sample', dfn.rvs(size=n) inc = 1.0 / n for s in dfn.rvs(size=n): counts[s] += inc for ii, p in sorted(counts.items()): t = np.allclose(dfn.pmf(ii), p, rtol=rtol, atol=atol) if not t: print('Error in sampling frequencies', ii) print('value\tpmf\tfreq') for jj in sorted(counts): print(('%.2f\t%.3f\t%.4f' % ( jj, dfn.pmf(jj), counts[jj]))) npt.assertTrue(t, "n = %i; pmf = %f; p = %f" % ( n, dfn.pmf(ii), p))
def check_edge_support(distfn, args): # Make sure the x=self.a and self.b are handled correctly. x = [distfn.a, distfn.b] if isinstance(distfn, stats.rv_continuous): npt.assert_equal(distfn.cdf(x, *args), [0.0, 1.0]) npt.assert_equal(distfn.logcdf(x, *args), [-np.inf, 0.0]) npt.assert_equal(distfn.sf(x, *args), [1.0, 0.0]) npt.assert_equal(distfn.logsf(x, *args), [0.0, -np.inf]) if isinstance(distfn, stats.rv_discrete): x = [distfn.a - 1, distfn.b] npt.assert_equal(distfn.ppf([0.0, 1.0], *args), x) npt.assert_equal(distfn.isf([0.0, 1.0], *args), x[::-1]) # out-of-bounds for isf & ppf npt.assertTrue(np.isnan(distfn.isf([-1, 2], *args)).all()) npt.assertTrue(np.isnan(distfn.ppf([-1, 2], *args)).all())
def check_moment(distfn, arg, m, v, msg): m1 = distfn.moment(1, *arg) m2 = distfn.moment(2, *arg) if not np.isinf(m): npt.assert_almost_equal(m1, m, decimal=10, err_msg=msg + ' - 1st moment') else: # or np.isnan(m1), npt.assertTrue(np.isinf(m1), msg + ' - 1st moment -infinite, m1=%s' % str(m1)) if not np.isinf(v): npt.assert_almost_equal(m2 - m1 * m1, v, decimal=10, err_msg=msg + ' - 2ndt moment') else: # or np.isnan(m2), npt.assertTrue(np.isinf(m2), msg + ' - 2nd moment -infinite, m2=%s' % str(m2))
def check_ppf_private(distfn, arg, msg): #fails by design for truncnorm self.nb not defined ppfs = distfn._ppf(np.array([0.1, 0.5, 0.9]), *arg) npt.assertTrue(not np.any(np.isnan(ppfs)), msg + 'ppf private is nan')
def check_entropy(distfn, arg, msg): ent = distfn.entropy(*arg) npt.assertTrue(not np.isnan(ent), msg + 'test Entropy is nan')
def check_named_args(distfn, x, shape_args, defaults, meths): ## Check calling w/ named arguments. # check consistency of shapes, numargs and _parse signature signature = inspect.getargspec(distfn._parse_args) npt.assertTrue(signature.varargs is None) npt.assertTrue(signature.keywords is None) npt.assertTrue(signature.defaults == defaults) shape_argnames = signature.args[ 1:-len(defaults)] # self, a, b, loc=0, scale=1 if distfn.shapes: shapes_ = distfn.shapes.replace(',', ' ').split() else: shapes_ = '' npt.assertTrue(len(shapes_) == distfn.numargs) npt.assertTrue(len(shapes_) == len(shape_argnames)) # check calling w/ named arguments shape_args = list(shape_args) vals = [meth(x, *shape_args) for meth in meths] npt.assertTrue(np.all(np.isfinite(vals))) names, a, k = shape_argnames[:], shape_args[:], {} while names: k.update({names.pop(): a.pop()}) v = [meth(x, *a, **k) for meth in meths] npt.assert_array_equal(vals, v) if 'n' not in list(k.keys()): # `n` is first parameter of moment(), so can't be used as named arg with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) npt.assert_equal(distfn.moment(1, *a, **k), distfn.moment(1, *shape_args)) # unknown arguments should not go through: k.update({'kaboom': 42}) npt.assert_raises(TypeError, distfn.cdf, x, **k)
def check_scale_docstring(distfn): if distfn.__doc__ is not None: # Docstrings can be stripped if interpreter is run with -OO npt.assertTrue('scale' not in distfn.__doc__)