Exemplo n.º 1
0
def _noise_variance(hparams, tau_cmmn, verbose=0):
    dist_std_noise = hparams.dist_std_noise

    if dist_std_noise == 'tr_normal':
        h1 = HalfNormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = HalfNormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Truncated normal for prior scales')

    elif dist_std_noise == 'log_normal':
        h1 = Lognormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = Lognormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Log normal for prior scales')

    elif dist_std_noise == 'uniform':
        h1 = Uniform('h1', upper=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = Uniform('h2', upper=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Uniform for prior scales')

    else:
        raise ValueError("Invalid value of dist_std_noise: %s" %
                         dist_std_noise)

    return h1, h2
Exemplo n.º 2
0
def _noise_variance(
    hparams, tau_cmmn, HalfNormal, Lognormal, Uniform, floatX, verbose):
    if hparams['prior_scale'] == 'tr_normal':
        h1 = HalfNormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = HalfNormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Truncated normal for prior scales')

    elif hparams['prior_scale'] == 'log_normal':
        h1 = Lognormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = Lognormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Log normal for prior scales')

    elif hparams['prior_scale'] == 'uniform':
        h1 = Uniform('h1', upper=np.float32(1 / tau_cmmn[0]), dtype=floatX)
        h2 = Uniform('h2', upper=np.float32(1 / tau_cmmn[1]), dtype=floatX)

        if 10 <= verbose:
            print('Uniform for prior scales')

    else:
        raise ValueError("Invalid value of prior_scale: %s" % 
            hparams['prior_scale'])

    return h1, h2
Exemplo n.º 3
0
    def test_mixture_of_mixture(self):
        if theano.config.floatX == "float32":
            rtol = 1e-4
        else:
            rtol = 1e-7
        nbr = 4
        with Model() as model:
            # mixtures components
            g_comp = Normal.dist(mu=Exponential("mu_g",
                                                lam=1.0,
                                                shape=nbr,
                                                transform=None),
                                 sigma=1,
                                 shape=nbr)
            l_comp = Lognormal.dist(mu=Exponential("mu_l",
                                                   lam=1.0,
                                                   shape=nbr,
                                                   transform=None),
                                    sigma=1,
                                    shape=nbr)
            # weight vector for the mixtures
            g_w = Dirichlet("g_w",
                            a=floatX(np.ones(nbr) * 0.0000001),
                            transform=None,
                            shape=(nbr, ))
            l_w = Dirichlet("l_w",
                            a=floatX(np.ones(nbr) * 0.0000001),
                            transform=None,
                            shape=(nbr, ))
            # mixture components
            g_mix = Mixture.dist(w=g_w, comp_dists=g_comp)
            l_mix = Mixture.dist(w=l_w, comp_dists=l_comp)
            # mixture of mixtures
            mix_w = Dirichlet("mix_w",
                              a=floatX(np.ones(2)),
                              transform=None,
                              shape=(2, ))
            mix = Mixture("mix",
                          w=mix_w,
                          comp_dists=[g_mix, l_mix],
                          observed=np.exp(self.norm_x))

        test_point = model.test_point

        def mixmixlogp(value, point):
            floatX = theano.config.floatX
            priorlogp = (st.dirichlet.logpdf(
                x=point["g_w"],
                alpha=np.ones(nbr) * 0.0000001,
            ).astype(floatX) +
                         st.expon.logpdf(x=point["mu_g"]).sum(dtype=floatX) +
                         st.dirichlet.logpdf(
                             x=point["l_w"],
                             alpha=np.ones(nbr) * 0.0000001,
                         ).astype(floatX) +
                         st.expon.logpdf(x=point["mu_l"]).sum(dtype=floatX) +
                         st.dirichlet.logpdf(
                             x=point["mix_w"],
                             alpha=np.ones(2),
                         ).astype(floatX))
            complogp1 = st.norm.logpdf(x=value,
                                       loc=point["mu_g"]).astype(floatX)
            mixlogp1 = logsumexp(np.log(point["g_w"]).astype(floatX) +
                                 complogp1,
                                 axis=-1,
                                 keepdims=True)
            complogp2 = st.lognorm.logpdf(value, 1.0, 0.0,
                                          np.exp(point["mu_l"])).astype(floatX)
            mixlogp2 = logsumexp(np.log(point["l_w"]).astype(floatX) +
                                 complogp2,
                                 axis=-1,
                                 keepdims=True)
            complogp_mix = np.concatenate((mixlogp1, mixlogp2), axis=1)
            mixmixlogpg = logsumexp(np.log(point["mix_w"]).astype(floatX) +
                                    complogp_mix,
                                    axis=-1,
                                    keepdims=False)
            return priorlogp, mixmixlogpg

        value = np.exp(self.norm_x)[:, None]
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)

        # check logp of mixture
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point), rtol=rtol)

        # check model logp
        assert_allclose(priorlogp + mixmixlogpg.sum(),
                        model.logp(test_point),
                        rtol=rtol)

        # check input and check logp again
        test_point["g_w"] = np.asarray([0.1, 0.1, 0.2, 0.6])
        test_point["mu_g"] = np.exp(np.random.randn(nbr))
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point), rtol=rtol)
        assert_allclose(priorlogp + mixmixlogpg.sum(),
                        model.logp(test_point),
                        rtol=rtol)
Exemplo n.º 4
0
    def test_mixture_of_mixture(self):
        if theano.config.floatX == 'float32':
            rtol = 1e-4
        else:
            rtol = 1e-7
        nbr = 4
        with Model() as model:
            # mixtures components
            g_comp = Normal.dist(
                mu=Exponential('mu_g', lam=1.0, shape=nbr, transform=None),
                sigma=1,
                shape=nbr)
            l_comp = Lognormal.dist(
                mu=Exponential('mu_l', lam=1.0, shape=nbr, transform=None),
                sigma=1,
                shape=nbr)
            # weight vector for the mixtures
            g_w = Dirichlet('g_w', a=floatX(np.ones(nbr)*0.0000001), transform=None)
            l_w = Dirichlet('l_w', a=floatX(np.ones(nbr)*0.0000001), transform=None)
            # mixture components
            g_mix = Mixture.dist(w=g_w, comp_dists=g_comp)
            l_mix = Mixture.dist(w=l_w, comp_dists=l_comp)
            # mixture of mixtures
            mix_w = Dirichlet('mix_w', a=floatX(np.ones(2)), transform=None)
            mix = Mixture('mix', w=mix_w,
                          comp_dists=[g_mix, l_mix],
                          observed=np.exp(self.norm_x))

        test_point = model.test_point

        def mixmixlogp(value, point):
            floatX = theano.config.floatX
            priorlogp = st.dirichlet.logpdf(x=point['g_w'],
                                            alpha=np.ones(nbr)*0.0000001,
                                            ).astype(floatX) + \
                        st.expon.logpdf(x=point['mu_g']).sum(dtype=floatX) + \
                        st.dirichlet.logpdf(x=point['l_w'],
                                            alpha=np.ones(nbr)*0.0000001,
                                            ).astype(floatX) + \
                        st.expon.logpdf(x=point['mu_l']).sum(dtype=floatX) + \
                        st.dirichlet.logpdf(x=point['mix_w'],
                                            alpha=np.ones(2),
                                            ).astype(floatX)
            complogp1 = st.norm.logpdf(x=value,
                                       loc=point['mu_g']).astype(floatX)
            mixlogp1 = logsumexp(np.log(point['g_w']).astype(floatX) +
                                 complogp1,
                                 axis=-1, keepdims=True)
            complogp2 = st.lognorm.logpdf(value,
                                          1.,
                                          0.,
                                          np.exp(point['mu_l'])).astype(floatX)
            mixlogp2 = logsumexp(np.log(point['l_w']).astype(floatX) +
                                 complogp2,
                                 axis=-1, keepdims=True)
            complogp_mix = np.concatenate((mixlogp1, mixlogp2), axis=1)
            mixmixlogpg = logsumexp(np.log(point['mix_w']).astype(floatX) +
                                    complogp_mix,
                                    axis=-1, keepdims=True)
            return priorlogp, mixmixlogpg

        value = np.exp(self.norm_x)[:, None]
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)

        # check logp of mixture
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point),
                        rtol=rtol)

        # check model logp
        assert_allclose(priorlogp + mixmixlogpg.sum(),
                        model.logp(test_point),
                        rtol=rtol)

        # check input and check logp again
        test_point['g_w'] = np.asarray([.1, .1, .2, .6])
        test_point['mu_g'] = np.exp(np.random.randn(nbr))
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point),
                        rtol=rtol)
        assert_allclose(priorlogp + mixmixlogpg.sum(),
                        model.logp(test_point),
                        rtol=rtol)
df = pd.read_csv("data/data-conc-2.txt")
df = df.drop("PersonID", axis=1)
Y_nt = df.values
n_patient = Y_nt.shape[0]
times = np.reshape(np.array([1, 2, 4, 8, 12, 24]), (1, -1))

basic_model = Model()

with basic_model:
    #全体平均
    a_0 = Normal('a_0', mu=0, sd=10)
    b_0 = Normal('b_0', mu=0, sd=10)
    #全体分散
    s_ag = HalfNormal('sigma_a', sd=10)
    s_bg = HalfNormal('sigma_b', sd=10)

    #個人パラメータ
    a = Lognormal('a', mu=a_0, sd=s_ag, shape=[n_patient, 1])
    b = Lognormal('b', mu=b_0, sd=s_bg, shape=[n_patient, 1])

    #個人分散
    s_Y = HalfNormal('sigma_Y', sd=10)

    #likelihood
    mu = 1 - tt.exp(-tt.dot(b, times))
    mu = a * mu
    Y_obs = Normal('Y_obs', mu=mu, sd=s_Y, observed=Y_nt)

    #サンプリング
    trace = sample(100)
    summary(trace)
Exemplo n.º 6
0
    def test_mixture_of_mixture(self):
        nbr = 4
        with Model() as model:
            # mixtures components
            g_comp = Normal.dist(mu=Exponential('mu_g',
                                                lam=1.0,
                                                shape=nbr,
                                                transform=None),
                                 sigma=1,
                                 shape=nbr)
            l_comp = Lognormal.dist(mu=Exponential('mu_l',
                                                   lam=1.0,
                                                   shape=nbr,
                                                   transform=None),
                                    sigma=1,
                                    shape=nbr)
            # weight vector for the mixtures
            g_w = Dirichlet('g_w',
                            a=floatX(np.ones(nbr) * 0.0000001),
                            transform=None)
            l_w = Dirichlet('l_w',
                            a=floatX(np.ones(nbr) * 0.0000001),
                            transform=None)
            # mixture components
            g_mix = Mixture.dist(w=g_w, comp_dists=g_comp)
            l_mix = Mixture.dist(w=l_w, comp_dists=l_comp)
            # mixture of mixtures
            mix_w = Dirichlet('mix_w', a=floatX(np.ones(2)), transform=None)
            mix = Mixture('mix',
                          w=mix_w,
                          comp_dists=[g_mix, l_mix],
                          observed=np.exp(self.norm_x))

        test_point = model.test_point

        def mixmixlogp(value, point):
            priorlogp = st.dirichlet.logpdf(x=point['g_w'],
                                            alpha=np.ones(nbr)*0.0000001,
                                            ) + \
                        st.expon.logpdf(x=point['mu_g']).sum() + \
                        st.dirichlet.logpdf(x=point['l_w'],
                                            alpha=np.ones(nbr)*0.0000001,
                                            ) + \
                        st.expon.logpdf(x=point['mu_l']).sum() + \
                        st.dirichlet.logpdf(x=point['mix_w'],
                                            alpha=np.ones(2),
                                            )
            complogp1 = st.norm.logpdf(x=value, loc=point['mu_g'])
            mixlogp1 = logsumexp(np.log(point['g_w']) + complogp1,
                                 axis=-1,
                                 keepdims=True)
            complogp2 = st.lognorm.logpdf(value, 1., 0., np.exp(point['mu_l']))
            mixlogp2 = logsumexp(np.log(point['l_w']) + complogp2,
                                 axis=-1,
                                 keepdims=True)
            complogp_mix = np.concatenate((mixlogp1, mixlogp2), axis=1)
            mixmixlogpg = logsumexp(np.log(point['mix_w']) + complogp_mix,
                                    axis=-1,
                                    keepdims=True)
            return priorlogp, mixmixlogpg

        value = np.exp(self.norm_x)[:, None]
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)

        # check logp of mixture
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point))

        # check model logp
        assert_allclose(priorlogp + mixmixlogpg.sum(), model.logp(test_point))

        # check input and check logp again
        test_point['g_w'] = np.asarray([.1, .1, .2, .6])
        test_point['mu_g'] = np.exp(np.random.randn(nbr))
        priorlogp, mixmixlogpg = mixmixlogp(value, test_point)
        assert_allclose(mixmixlogpg, mix.logp_elemwise(test_point))
        assert_allclose(priorlogp + mixmixlogpg.sum(), model.logp(test_point))