コード例 #1
0
ファイル: hierregress.py プロジェクト: delgadom/open-estimate
def lincombo_hierregress_taubybeta(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_maxlintau(yy, stderrs, XX)
        print "Using maximum tau =", maxtau

    if maxtau[0] > 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([np.mean(yy)] * nummus, tau, yy, stdvars, XX, probability_prior_tau)

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

    print "Sampling mus..."

    return sample_posterior(yy, stderrs, XX, dist, draws)
コード例 #2
0
ファイル: pooling.py プロジェクト: delgadom/open-estimate
def lincombo_pooled(betas, stderrs, portions):
    betas, stdvars, portions = helpers.check_arguments(betas, stderrs, portions)

    numalphas = portions.shape[1]

    ## Fill in the inverse sigma matrix
    overvars = 1.0 / stdvars
    invsigma = np.zeros((numalphas, numalphas))
    for jj in range(numalphas):
        print "Row", jj
        for kk in range(numalphas):
            if helpers.issparse(portions):
                invsigma[jj, kk] = sum_multiply2(portions, jj, kk, overvars)
            else:
                invsigma[jj, kk] = sum(portions[:, jj] * portions[:, kk] * overvars)

    if helpers.issparse(portions):
        bb = [sum_multiply(portions[:, jj], betas * overvars) for jj in range(numalphas)]
    else:
        bb = [sum(betas * portions[:, jj] / stdvars) for jj in range(numalphas)]

    print "Begin inversion...", invsigma.shape
    sigma = np.linalg.inv(invsigma)
    alphas = np.dot(sigma, np.transpose(bb))

    return MultivariateNormal(alphas, sigma)
コード例 #3
0
ファイル: hiernorm.py プロジェクト: delgadom/open-estimate
def lincombo_hiernorm_taubybeta(betas, stderrs, portions, maxtaus=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 maxtaus is None:
        maxtaus = pooling.estimated_maxlintaus(betas, stderrs, portions)
        print "Using maximum tau =", maxtaus, "vs.", observed_tau

    if maxtaus[0] > 0:
        probability_prior_taus = MultivariateUniform([0, 0], maxtaus)

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

        # Create pdf for p(taus | betas)
        def pdf(*taus): # taus is [tau0s, tau1s]
            transtaus = np.transpose(taus) # [[tau0, tau1], ...]
            values = []
            for ii in range(len(taus[0])):
                ## Observation taus
                obstaus = transtaus[ii][0] + transtaus[ii][1] * np.abs(np.array(betas))

                # 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, 2)
        if guess_range:
            mins, maxs = dist.guess_ranges([0, 0], maxtaus, draws * 10)
        else:
            mins = [0, 0]
            maxs = maxtaus
        print mins, maxs
        dist.prepare_draws(mins, maxs, count=draws)
    else:
        # maxtau == 0
        dist = MultivariateDelta(np.zeros(2))

    print "Sampling alphas..."

    taus2obstaus = lambda taus: taus[0] + taus[1] * np.abs(np.array(betas))
    return sample_posterior(betas, stderrs, portions, dist, taus2obstaus, draws)
コード例 #4
0
ファイル: hierregress.py プロジェクト: delgadom/open-estimate
def sample_posterior(yy, stderrs, XX, taudist, draws=100):
    yy, stdvars, XX = helpers.check_arguments(yy, stderrs, XX)

    # Draw samples from posterior
    alltau = []
    allmus = []
    allbetahats = []
    for ii in range(draws):
        tau = taudist.rvs(size=1)
        alltau.append(tau)

        # Sample from p(mus | tau, yy)
        mus = mu_given_tau(yy, stdvars, XX, tau).rvs(size=1)
        allmus.append(mus)

        # Sample from p(betahat | tau, yy)
        betahats = betahat_given_tau(yy, stdvars, XX, tau).rvs(size=1)
        allbetahats.append(betahats)

    return alltau, allmus, allbetahats
コード例 #5
0
ファイル: hiernorm.py プロジェクト: delgadom/open-estimate
def sample_posterior(betas, stderrs, portions, taudist, taus2obstaus, draws=100):
    betas, stdvars, portions = helpers.check_arguments(betas, stderrs, portions)

    # Draw samples from posterior
    alltaus = []
    allalphas = []
    allbetahats = []
    for ii in range(draws):
        taus = np.ravel(taudist.rvs(size=1))
        alltaus.append(taus)

        obstaus = taus2obstaus(taus)

        # Sample from p(alphas | taus, betas)
        alphas = alpha_given_taus(betas, stdvars, portions, obstaus).rvs(size=1)
        allalphas.append(alphas)

        # Sample from p(betahat | taus, betas)
        betahats = betahat_given_taus(betas, stdvars, portions, obstaus).rvs(size=1)
        allbetahats.append(betahats)

    return alltaus, allalphas, allbetahats