예제 #1
0
def test_PoissonZeroProcess_point():
    test_states = np.r_[0, 0, 1, 1, 0, 1]

    with pm.Model():
        test_mean = pm.Constant("c", 1000.0)
        test_point = {"c": 100.0}
        test_sample = PoissonZeroProcess.dist(
            test_mean, test_states).random(point=test_point)

    assert np.all(0 < test_sample[..., test_states > 0])
    assert np.all(test_sample[..., test_states > 0] < 200)
예제 #2
0
파일: tests.py 프로젝트: t8ch/pm-prophet
def test_multiplicative_seasonality():
    z = np.sin(np.linspace(0, 200, 200)) * np.linspace(0, 200, 200)
    df = pd.DataFrame()
    df["ds"] = pd.date_range(start="2018-01-01", periods=200)
    df["y"] = z
    m = PMProphet(df, auto_changepoints=False, growth=True, intercept=False, name="model")
    with m.model:
        m.priors['growth'] = pm.Constant('growth_model', 1)
    m.add_seasonality(seasonality=3.14 * 2, fourier_order=3, mode=Seasonality.MULTIPLICATIVE)
    m.fit()
    m.predict(60, alpha=0.2, include_history=True, plot=True)
    m.plot_components(intercept=False)
def test_pymc_pydantic():
    """
    Test that a simple PyMC3 model is successfully serialized/deserialized
    when part of a Pydantic Model.
    """

    import mackelab_toolbox.typing as mtbtyping
    import mackelab_toolbox.theano
    mackelab_toolbox.theano.freeze_theano_types()

    ## Simplistic scalar model – Test basic serialize/deserialize concept
    with PyMC_Model() as model:
        x = pm.Normal('x', mu=0, sigma=2)
        y = pm.Deterministic('y', x**2)

    class Foo(BaseModel):
        model: PyMC_Model

        class Config:
            json_encoders = mtbtyping.json_encoders

    foo = Foo(model=model)

    ## Following is useful to inspect the serialized data
    # import json
    # json.loads(foo.json())

    # Smoke test
    foo2 = Foo.parse_raw(foo.json())

    # Confirm that the deserialized y is bound to the deserialized x
    symbolic_inputs = [
        v for v in theano.graph.basic.graph_inputs([foo2.model.y])
        if isinstance(v, tt.Variable) and not isinstance(v, tt.Constant)
    ]
    assert len(symbolic_inputs) == 1
    assert foo2.model.x is symbolic_inputs[0]
    assert foo2.model.y.eval({foo2.model.x: 4}) == 16

    ## Test serialization of more ops: Reduce (Sum), DimShuffle
    with PyMC_Model() as model_b:
        x = pm.Normal('x', mu=0, sigma=2, shape=(3, 3))
        y = pm.Deterministic('y', x**2)
        z = pm.Deterministic('z', y.sum(axis=1))

    # Smoke test
    foo_b = Foo(model=model_b)
    foo2_b = Foo.parse_raw(foo_b.json())

    ## Test with bigger model; incl. sqrt, abs
    with PyMC_Model() as prior:
        M = 2
        Mtilde = 2
        pm.Constant('M', M)
        pm.Constant('Mtilde', Mtilde)
        μ = pm.Normal('μtilde', mu=0, sigma=2, shape=(M, ))
        logτ = pm.Normal('τtilde', mu=0, sigma=1, shape=(M, ))
        W = pm.Normal('Wtilde', mu=0, sigma=1, shape=(Mtilde, M))
        σ = pm.Deterministic('σtilde', tt.sqrt(abs(W.sum(axis=1))))

    foo_prior = Foo(model=prior)
    foo2_prior = Foo.parse_raw(foo_prior.json())

    assert isinstance(foo2_prior.model.M.distribution, pm.Constant)