Пример #1
0
def lincombo_hierregress_taubymu(yy, stderrs, XX, maxtau=None, guess_range=False, draws=100):
    yy, stdvars, XX = helpers.check_arguments(yy, stderrs, XX)
    nummus = XX.shape[1]

    print "Sampling tau..."

    if maxtau is None:
        maxtau = pooling.estimated_maxtau(yy, stderrs, XX)
        print "Using maximum tau =", maxtau

    if maxtau > 0:
        probability_prior_tau = uniform(0, maxtau)

        # Prepare to sample from from p(tau | yy)

        # Create pdf for p(tau | yy)
        def pdf(tau):
            # Requires mus, but is invarient to them
            return probability_tau([1] * nummus, tau, yy, stdvars, XX, probability_prior_tau)

        dist = ContinuousSampled(pdf)
        if guess_range:
            mini, maxi = dist.guess_ranges(0, maxtau, draws * 10)
        else:
            mini, maxi = 0, maxtau
        dist.prepare_draws(mini, maxi, count=draws)
    else:
        # maxtau == 0
        dist = MultivariateDelta(np.zeros(nummus))

    print "Sampling mus..."

    return sample_posterior(yy, stderrs, XX, dist, draws)
Пример #2
0
def lincombo_hiernorm_taubyalpha(betas, stderrs, portions, maxtau=None, guess_range=False, draws=100):
    betas, stdvars, portions = helpers.check_arguments(betas, stderrs, portions)
    numalphas = portions.shape[1]

    print "Sampling taus..."

    observed_tau = 2 * np.sqrt(np.var(betas) + max(stderrs)**2)
    if observed_tau == 0:
        return None, None, None

    if maxtau is None:
        maxtau = pooling.estimated_maxtau(betas, stderrs, portions)
        print "Using maximum tau =", maxtau, "vs.", observed_tau

    if maxtau > 0:
        probability_prior_taus = MultivariateUniform([0] * numalphas, [maxtau] * numalphas)

        # Prepare to sample from from p(taus | betas)

        # Create pdf for p(taus | betas)
        def pdf(*taus): # taus is [tau1s, tau2s, ...]
            transtaus = np.transpose(taus) # [[tau1, tau2, ...], ...]
            values = []
            for ii in range(len(taus[0])):
                ## Observation taus
                obstaus = np.array([sum(portions[ii, :] * transtaus[ii]) for ii in range(portions.shape[0])])

                # Requires alphas, but is invarient to them
                values.append(probability_tau([np.mean(betas)] * numalphas, transtaus[ii], obstaus, betas, stdvars, portions, probability_prior_taus))

            return values

        dist = MultivariateSampled(pdf, numalphas)
        if guess_range:
            mins, maxs = dist.guess_ranges([0] * numalphas, [maxtau] * numalphas, draws * 10)
        else:
            mins = [0] * numalphas
            maxs = [maxtau] * numalphas
        dist.prepare_draws(mins, maxs, count=draws)
    else:
        # maxtau == 0
        dist = MultivariateDelta(np.zeros(numalphas))

    print "Sampling alphas..."

    taus2obstaus = lambda taus: np.array([sum(portions[ii, :] * taus) for ii in range(portions.shape[0])])
    return sample_posterior(betas, stderrs, portions, dist, taus2obstaus, draws)