|
4 | 4 |
|
5 | 5 | from .helpers import SeededTest, select_by_precision
|
6 | 6 | from ..vartypes import continuous_types
|
7 |
| -from ..model import Model, Point, Potential |
| 7 | +from ..model import Model, Point, Potential, Deterministic |
8 | 8 | from ..blocking import DictToVarBijection, DictToArrayBijection, ArrayOrdering
|
9 | 9 | from ..distributions import (DensityDist, Categorical, Multinomial, VonMises, Dirichlet,
|
10 |
| - MvStudentT, MvNormal, ZeroInflatedPoisson, GaussianRandomWalk, |
| 10 | + MvStudentT, MvNormal, ZeroInflatedPoisson, |
11 | 11 | ZeroInflatedNegativeBinomial, Constant, Poisson, Bernoulli, Beta,
|
12 |
| - BetaBinomial, HalfStudentT, StudentT, Weibull, Pareto, NormalMixture, |
| 12 | + BetaBinomial, HalfStudentT, StudentT, Weibull, Pareto, |
13 | 13 | InverseGamma, Gamma, Cauchy, HalfCauchy, Lognormal, Laplace,
|
14 | 14 | NegativeBinomial, Geometric, Exponential, ExGaussian, Normal,
|
15 | 15 | Flat, LKJCorr, Wald, ChiSquared, HalfNormal, DiscreteUniform,
|
16 |
| - Bound, Uniform, Triangular, Binomial, SkewNormal, DiscreteWeibull, Gumbel, |
17 |
| - Interpolated, ZeroInflatedBinomial, HalfFlat) |
| 16 | + Bound, Uniform, Triangular, Binomial, SkewNormal, DiscreteWeibull, |
| 17 | + Gumbel, Interpolated, ZeroInflatedBinomial, HalfFlat) |
18 | 18 | from ..distributions import continuous
|
19 | 19 | from pymc3.theanof import floatX
|
20 | 20 | from numpy import array, inf, log, exp
|
@@ -929,27 +929,54 @@ def test_bound():
|
929 | 929 | assert rand >= 5 and rand <= 8
|
930 | 930 |
|
931 | 931 |
|
932 |
| -def test_repr_latex_(): |
933 |
| - with Model(): |
934 |
| - x0 = Binomial('Discrete', p=.5, n=10) |
935 |
| - x1 = Normal('Continuous', mu=0., sd=1.) |
936 |
| - x2 = GaussianRandomWalk('Timeseries', mu=x1, sd=1., shape=2) |
937 |
| - x3 = MvStudentT('Multivariate', nu=5, mu=x2, Sigma=np.diag(np.ones(2)), shape=2) |
938 |
| - x4 = NormalMixture('Mixture', w=np.array([.5, .5]), mu=x3, sd=x0) |
939 |
| - |
940 |
| - assert x0._repr_latex_() == '$Discrete \\sim \\text{Binomial}' \ |
941 |
| - '(\\mathit{n}=10, \\mathit{p}=0.5)$' |
942 |
| - assert x1._repr_latex_() == '$Continuous \\sim \\text{Normal}' \ |
943 |
| - '(\\mathit{mu}=0.0, \\mathit{sd}=1.0)$' |
944 |
| - assert x2._repr_latex_() == '$Timeseries \\sim \\text' \ |
945 |
| - '{GaussianRandomWalk}(\\mathit{mu}=Continuous, ' \ |
946 |
| - '\\mathit{sd}=1.0)$' |
947 |
| - assert x3._repr_latex_() == '$Multivariate \\sim \\text{MvStudentT}' \ |
948 |
| - '(\\mathit{nu}=5, \\mathit{mu}=Timeseries, ' \ |
949 |
| - '\\mathit{cov}=array)$' |
950 |
| - assert x4._repr_latex_() == '$Mixture \\sim \\text{NormalMixture}' \ |
951 |
| - '(\\mathit{w}=array, \\mathit{mu}=Multivariate, ' \ |
952 |
| - '\\mathit{sigma}=f(Discrete))$' |
| 932 | +class TestLatex(object): |
| 933 | + |
| 934 | + def setup_class(self): |
| 935 | + # True parameter values |
| 936 | + alpha, sigma = 1, 1 |
| 937 | + beta = [1, 2.5] |
| 938 | + |
| 939 | + # Size of dataset |
| 940 | + size = 100 |
| 941 | + |
| 942 | + # Predictor variable |
| 943 | + X = np.random.normal(size=(size, 2)).dot(np.array([[1, 0], [0, 0.2]])) |
| 944 | + |
| 945 | + # Simulate outcome variable |
| 946 | + Y = alpha + X.dot(beta) + np.random.randn(size)*sigma |
| 947 | + with Model() as self.model: |
| 948 | + # Priors for unknown model parameters |
| 949 | + alpha = Normal('alpha', mu=0, sd=10) |
| 950 | + b = Normal('beta', mu=0, sd=10, shape=(2,), observed=beta) |
| 951 | + sigma = HalfNormal('sigma', sd=1) |
| 952 | + |
| 953 | + # Expected value of outcome |
| 954 | + mu = Deterministic('mu', alpha + tt.dot(X, b)) |
| 955 | + |
| 956 | + # Likelihood (sampling distribution) of observations |
| 957 | + Y_obs = Normal('Y_obs', mu=mu, sd=sigma, observed=Y) |
| 958 | + self.distributions = [alpha, sigma, mu, b, Y_obs] |
| 959 | + self.expected = ( |
| 960 | + '$alpha \\sim \\text{Normal}(\\mathit{mu}=0, \\mathit{sd}=10.0)$', |
| 961 | + '$sigma \\sim \\text{HalfNormal}(\\mathit{sd}=1.0)$', |
| 962 | + '$mu \\sim \\text{Deterministic}(alpha, \\text{Constant}, beta)$', |
| 963 | + '$beta \\sim \\text{Normal}(\\mathit{mu}=0, \\mathit{sd}=10.0)$', |
| 964 | + '$Y_obs \\sim \\text{Normal}(\\mathit{mu}=mu, \\mathit{sd}=f(sigma))$' |
| 965 | + ) |
| 966 | + |
| 967 | + def test__repr_latex_(self): |
| 968 | + for distribution, tex in zip(self.distributions, self.expected): |
| 969 | + assert distribution._repr_latex_() == tex |
| 970 | + |
| 971 | + model_tex = self.model._repr_latex_() |
| 972 | + |
| 973 | + for tex in self.expected: # make sure each variable is in the model |
| 974 | + assert tex.strip('$') in model_tex |
| 975 | + |
| 976 | + def test___latex__(self): |
| 977 | + for distribution, tex in zip(self.distributions, self.expected): |
| 978 | + assert distribution._repr_latex_() == distribution.__latex__() |
| 979 | + assert self.model._repr_latex_() == self.model.__latex__() |
953 | 980 |
|
954 | 981 |
|
955 | 982 | def test_discrete_trafo():
|
|
0 commit comments