Ejemplo n.º 1
0
def test_sample(concentration, rate, n_samples=int(1e6)):
    samples = InverseGamma(concentration, rate).sample((n_samples, ))
    mean, std = samples.mean().item(), samples.std().item()
    expected_mean = rate / (concentration - 1.0)
    expected_std = rate / (
        (concentration - 1.0) * math.sqrt(concentration - 2.0))
    assert_equal(mean, expected_mean, prec=1e-2)
    assert_equal(std, expected_std, prec=0.03)
Ejemplo n.º 2
0
    def mixed_simulator(theta):
        # Extract parameters
        beta, ps = theta[:, :1], theta[:, 1:]

        # Sample choices and rts independently.
        choices = Binomial(probs=ps).sample()
        rts = InverseGamma(concentration=2 * torch.ones_like(beta),
                           rate=beta).sample()

        return torch.cat((rts, choices), dim=1)
Ejemplo n.º 3
0
    def marginal(self, xts):

        sup = xts[1::] - self.phi * xts[0:-1]
        sup = np.power(sup, 2)
        sup = 0.5 * np.sum(sup)

        bup = np.exp(-xts[1::])
        bup = bup * (np.power(self.yts, 2))
        bup = 0.5 * np.sum(bup)

        T = xts.shape[0] - 1
        a = self.a + 0.5 * T

        b_sigma2 = self.b + sup
        b_beta2 = self.b + bup

        ig_sigma2 = IG(a, b_sigma2)
        ig_beta2 = IG(a, b_beta2)

        sigma2 = ig_sigma2.sample().numpy()
        beta2 = ig_beta2.sample().numpy()

        return (sigma2, beta2)
Ejemplo n.º 4
0
    def iid_likelihood(self, theta: torch.Tensor) -> torch.Tensor:
        """Returns the likelihood summed over a batch of i.i.d. data."""

        lp_choices = torch.stack(
            [
                Binomial(probs=th.reshape(1, -1)).log_prob(self.x_o[:, 1:])
                for th in theta[:, 1:]
            ],
            dim=1,
        )

        lp_rts = torch.stack(
            [
                InverseGamma(concentration=2 * torch.ones_like(beta_i),
                             rate=beta_i).log_prob(self.x_o[:, :1])
                for beta_i in theta[:, :1]
            ],
            dim=1,
        )

        joint_likelihood = (lp_choices + lp_rts).reshape(
            self.x_o.shape[0], theta.shape[0])

        return joint_likelihood.sum(0)
Ejemplo n.º 5
0
def test_log_prob(concentration, rate, value):
    value = torch.tensor(value)
    log_prob = InverseGamma(concentration, rate).log_prob(value)
    expected_log_prob = (Gamma(concentration, rate).log_prob(1.0 / value) -
                         2.0 * value.log())
    assert_equal(log_prob, expected_log_prob, prec=1e-6)