def test_wald(self): # Cannot do anything too exciting as scipy wald is a # location-scale model of the *standard* wald with mu=1 and lam=1 pymc3_random(Wald, { 'mu': Domain([1., 1., 1.]), 'lam': Domain([1., 1., 1.]), 'alpha': Rplus }, ref_rand=lambda size, mu=None, lam=None, alpha=None: st. wald.rvs(size=size, loc=alpha))
def pymc3_random_discrete(dist, paramdomains, valuedomain=Domain([0]), ref_rand=None, size=100000, alpha=0.05, fails=20): model = build_model(dist, valuedomain, paramdomains) domains = paramdomains.copy() for pt in product(domains): pt = Point(pt, model=model) p = alpha # Allow Chisq test to fail (i.e., the samples be different) # a certain number of times. f = fails while p <= alpha and f > 0: o = model.named_vars['value'].random(size=size, point=pt) e = ref_rand(size=size, **pt) o = np.atleast_1d(o).flatten() e = np.atleast_1d(e).flatten() observed = dict(zip(*np.unique(o, return_counts=True))) expected = dict(zip(*np.unique(e, return_counts=True))) for e in expected.keys(): expected[e] = (observed.get(e, 0), expected[e]) k = np.array([v for v in expected.values()]) if np.all(k[:, 0] == k[:, 1]): p = 1. else: _chi, p = st.chisquare(k[:, 0], k[:, 1]) f -= 1 assert p > alpha, str(pt)
def test_mv_t(self): for n in [2, 3]: pymc3_random(MvStudentT, {'nu': Domain([5, 10, 25, 50]), 'Sigma': PdMatrix(n), 'mu':Vector(R,n)}, size=100, valuedomain=Vector(R,n), ref_rand=(lambda nu=None, Sigma=None, mu=None, size=None: mu + np.sqrt(nu) * (st.multivariate_normal.rvs(cov=Sigma, size=size).T / st.chi2.rvs(df=nu, size=size)).T))
def pymc3_random(dist, paramdomains, ref_rand=None, valuedomain=Domain([0]), size=10000, alpha=0.05, fails=10): model = build_model(dist, valuedomain, paramdomains) domains = paramdomains.copy() for pt in product(domains): pt = Point(pt, model=model) p = alpha # Allow KS test to fail (i.e., the samples be different) # a certain number of times. Crude, but necessary. f = fails while p <= alpha and f > 0: s0 = model.named_vars['value'].random(size=size, point=pt) s1 = ref_rand(size=size, **pt) _, p = st.ks_2samp(np.atleast_1d(s0).flatten(), np.atleast_1d(s1).flatten()) f -= 1 assert p > alpha, str(pt)
from pymc3.tests.test_transforms import * import ContinuousTimeMarkovModel.transforms as ctrans from pymc3.tests.test_distributions import Domain min_rate_matrix = np.zeros(shape=(5, 5)) max_rate_matrix = np.zeros(shape=(5, 5)) + np.inf max_rate_matrix = np.fill_diagonal(max_rate_matrix, -np.inf) rate_matrix_test = np.array([[-6, 2, 2, 1, 1], [1, -4, 0, 1, 2], [1, 0, -4, 2, 1], [2, 1, 0, -3, 0], [1, 1, 1, 1, -4]]) RateMatrixDomain = Domain([min_rate_matrix, rate_matrix_test, max_rate_matrix]) def check_matrix_transform_identity(transform, domain): return check_transform_identity(transform, domain, t.dmatrix, test=rate_matrix_test) def test_rate_matrix(): check_matrix_transform_identity(ctrans.rate_matrix(0, 100), RateMatrixDomain) min_rate_matrix = np.zeros(shape=(6, 6)) max_rate_matrix = np.zeros(shape=(6, 6)) + np.inf max_rate_matrix = np.fill_diagonal(max_rate_matrix, -np.inf) rate_matrix_test = np.array( [[-0.631921, 0.631921, 0.0000000, 0.0000000, 0.0000000, 0.0000000],