Example #1
0
 def __init__(self,
              tau=None,
              init=Flat.dist(),
              sigma=None,
              mu=0.0,
              sd=None,
              *args,
              **kwargs):
     kwargs.setdefault("shape", 1)
     super().__init__(*args, **kwargs)
     if sum(self.shape) == 0:
         raise TypeError(
             "GaussianRandomWalk must be supplied a non-zero shape argument!"
         )
     if sd is not None:
         sigma = sd
         warnings.warn("sd is deprecated, use sigma instead",
                       DeprecationWarning)
     tau, sigma = get_tau_sigma(tau=tau, sigma=sigma)
     self.tau = tt.as_tensor_variable(tau)
     sigma = tt.as_tensor_variable(sigma)
     self.sigma = self.sd = sigma
     self.mu = tt.as_tensor_variable(mu)
     self.init = init
     self.mean = tt.as_tensor_variable(0.0)
def test_linear():
    lam = -0.78
    sig2 = 5e-3
    N = 300
    dt = 1e-1
    sde = lambda x, lam: (lam * x, sig2)
    x = floatX(_gen_sde_path(sde, (lam, ), dt, N, 5.0))
    z = x + np.random.randn(x.size) * sig2
    # build model
    with Model() as model:
        lamh = Flat("lamh")
        xh = EulerMaruyama("xh", dt, sde, (lamh, ), shape=N + 1, testval=x)
        Normal("zh", mu=xh, sigma=sig2, observed=z)
    # invert
    with model:
        trace = sample(init="advi+adapt_diag", chains=1)

    ppc = sample_posterior_predictive(trace, model=model)
    ppcf = fast_sample_posterior_predictive(trace, model=model)
    # test
    p95 = [2.5, 97.5]
    lo, hi = np.percentile(trace[lamh], p95, axis=0)
    assert (lo < lam) and (lam < hi)
    lo, hi = np.percentile(ppc["zh"], p95, axis=0)
    assert ((lo < z) * (z < hi)).mean() > 0.95
    lo, hi = np.percentile(ppcf["zh"], p95, axis=0)
    assert ((lo < z) * (z < hi)).mean() > 0.95
Example #3
0
    def __init__(
        self, rho, sigma=None, tau=None, constant=False, init=Flat.dist(), sd=None, *args, **kwargs
    ):
        super().__init__(*args, **kwargs)
        if sd is not None:
            sigma = sd

        tau, sigma = get_tau_sigma(tau=tau, sigma=sigma)
        self.sigma = self.sd = tt.as_tensor_variable(sigma)
        self.tau = tt.as_tensor_variable(tau)

        self.mean = tt.as_tensor_variable(0.0)

        if isinstance(rho, list):
            p = len(rho)
        else:
            try:
                shape_ = rho.shape.tag.test_value
            except AttributeError:
                shape_ = rho.shape

            if hasattr(shape_, "size") and shape_.size == 0:
                p = 1
            else:
                p = shape_[0]

        if constant:
            self.p = p - 1
        else:
            self.p = p

        self.constant = constant
        self.rho = rho = tt.as_tensor_variable(rho)
        self.init = init
Example #4
0
    def __init__(
        self, mu=0.0, cov=None, tau=None, chol=None, lower=True, init=Flat.dist(), *args, **kwargs
    ):
        super().__init__(*args, **kwargs)

        self.init = init
        self.innovArgs = (mu, cov, tau, chol, lower)
        self.innov = multivariate.MvNormal.dist(*self.innovArgs, shape=self.shape)
        self.mean = tt.as_tensor_variable(0.0)
Example #5
0
 def __init__(self,
              tau=None,
              init=Flat.dist(),
              sigma=None,
              mu=0.,
              sd=None,
              *args,
              **kwargs):
     kwargs.setdefault('shape', 1)
     super().__init__(*args, **kwargs)
     if sum(self.shape) == 0:
         raise TypeError("RW2 must be supplied a non-zero shape argument!")
     if sd is not None:
         sigma = sd
     tau, sigma = get_tau_sigma(tau=tau, sigma=sigma)
     self.tau = tt.as_tensor_variable(tau)
     sigma = tt.as_tensor_variable(sigma)
     self.sigma = self.sd = sigma
     self.mu = tt.as_tensor_variable(mu)
     self.init = init
     self.mean = tt.as_tensor_variable(0.)