コード例 #1
0
ファイル: ice.py プロジェクト: waternk/bugs
def nllf(p):
    tau, alpha, beta = p[0], p[1:Nage + 1], p[Nage + 1:Nage + 1 + K]
    alpha[0] = 0.  # alpha 0 is not a fitting parameter

    mu = exp(log(pyr) + alpha[age_index] + beta[year_index])

    betamean = np.empty(beta.shape)
    betamean[0] = 2 * beta[1] - beta[2]
    betamean[1] = (2 * beta[0] + 4 * beta[2] - beta[3]) / 5
    betamean[2:K - 2] = (4 * beta[1:K - 3] + 4 * beta[3:K - 1] -
                         beta[0:K - 4] - beta[4:K]) / 6
    betamean[-2] = (2 * beta[-1] + 4 * beta[-3] - beta[-4]) / 5
    betamean[-1] = 2 * beta[-2] - beta[-3]
    logRR = beta - beta[4]

    Nneighs = np.empty(beta.shape, 'i')
    Nneighs[0] = 1
    Nneighs[1] = 5
    Nneighs[2:K - 2] = 6
    Nneighs[-2] = 5
    Nneighs[-1] = 1

    betaprec = Nneighs * tau
    tau_like = Nneighs * beta * (beta - betamean)
    d = 0.0001 + np.sum(tau_like) / 2
    r = 0.0001 + K / 2

    cost = 0
    cost += np.sum(dpois_llf(cases, mu))
    cost += np.sum(dnorm_llf(beta, betamean, betaprec))
    cost += np.sum(dnorm_llf(alpha[1:], 0, 1e-6))
    cost += dgamma_llf(tau, r, d)

    return -cost
コード例 #2
0
ファイル: kidney.py プロジェクト: waternk/bugs
def nllf(p):
    beta_age, beta_sex = p[:2]
    beta_dis_ = np.empty(4)
    beta_dis_[0] = beta_dis[0]
    beta_dis_[1:] = p[2:5]
    alpha, r, tau = p[5:8]
    b = p[8:]

    mu = exp(alpha
             + beta_age*age
             + beta_sex*sex[:, None]
             + beta_dis_[disease[:, None]-1]  # numpy is 0-origin
             + b[:, None])

    cost = 0
    cost += np.sum(dweib_C_llf(t, r, mu, lower=t_cen))
    cost += np.sum(dnorm_llf(b, 0.0, tau))
    cost += dnorm_llf(alpha, 0, 0.0001)
    cost += dnorm_llf(beta_age, 0, 0.0001)
    cost += dnorm_llf(beta_sex, 0, 0.0001)
    cost += np.sum(dnorm_llf(beta_dis_[1:], 0, 0.0001))
    cost += dgamma_llf(tau, 1e-3, 1e-3)
    cost += dgamma_llf(r, 1, 1e-3)

    return -cost
コード例 #3
0
def nllf(pars):
    cost = 0
    alpha_star, beta = pars
    p = np.array([inv_p_model(alpha_star + beta * (xi - xbar)) for xi in x])
    cost += np.sum(dbin_llf(r, p, n))
    cost += dnorm_llf(beta, 0.0, 0.001)
    cost += dnorm_llf(alpha_star, 0.0, 0.001)
    return -cost
コード例 #4
0
ファイル: air.py プロジェクト: waternk/bugs
def nllf(pars):
    theta, X = pars[0:2], pars[2:]
    p = np.array([inverse[logit](theta[0] + theta[1] * Xj) for Xj in X])
    mu = data["alpha"] + data["beta"] * data["Z"]
    cost = 0
    cost += np.sum(dbin_llf(data["y"], p, data["n"]))
    cost += np.sum(dnorm_llf(X, mu, data["tau"]))
    cost += dnorm_llf(theta[0], 0.0, 0.001)
    cost += dnorm_llf(theta[1], 0.0, 0.001)
    return -cost
コード例 #5
0
ファイル: dyes.py プロジェクト: waternk/bugs
def nllf(p):
    theta, tau_with, tau_btw = p[:3]
    mu = p[3:]

    cost = 0
    cost += np.sum(dnorm_llf(mu, theta, tau_btw))
    cost += np.sum(dnorm_llf(y, mu[:, None], tau_with))
    cost += dgamma_llf(tau_with, 0.001, 0.001)
    cost += dgamma_llf(tau_btw, 0.001, 0.001)
    cost += dnorm_llf(theta, 0, 1e-6)

    return -cost
コード例 #6
0
ファイル: surgicalrand.py プロジェクト: waternk/bugs
def nllf(p):
    b, tau, mu = p[:-2], p[-2], p[-1]

    p = ilogit(b)

    cost = 0
    cost += np.sum(dnorm_llf(b, mu, tau))
    cost += np.sum(dbin_llf(r, p, n))
    cost += dnorm_llf(mu, 0.0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #7
0
def nllf(p):
    alpha, beta, theta = p[:T], p[T], p[T + 1:]

    p = ilogit(beta * theta[:, None] - alpha[None, :])

    cost = 0
    cost += np.sum(dbern_llf(r, p))
    cost += np.sum(dnorm_llf(theta, 0.0, 1.0))
    cost += np.sum(dnorm_llf(alpha, 0, 0.0001))
    cost += dflat_llf(beta)

    return -cost
コード例 #8
0
ファイル: cervix.py プロジェクト: waternk/bugs
def nllf(p):
    beta0C, beta, phi, q, x[x_index] = p[0], p[1], p[2:6].reshape(2, 2), p[6], p[7:]

    p = ilogit(beta0C + beta*np.floor(x))
    x_int, d_int = [np.asarray(np.floor(v), 'i') for v in (x, d)]
    cost = 0
    cost += np.sum(dbern_llf(d, q))
    cost += np.sum(dbern_llf(d, p))
    cost += np.sum(dbern_llf(w, phi[x_int, d_int]))
    cost += dnorm_llf(beta0C, 0, 0.00001)
    cost += dnorm_llf(beta, 0, 0.00001)

    return -cost
コード例 #9
0
ファイル: epil.py プロジェクト: waternk/bugs
def nllf(p):
    a0, alpha_Base, alpha_Trt, alpha_BT, alpha_Age, alpha_V4, tau_b1, tau_b = p[:8]
    b = p[8:8+N*T].reshape(N, T)
    b1 = p[8+N*T:8+N*T+N]

    mu = exp(
        a0
        + alpha_Base * (log_Base4[:, None] - log_Base4_bar)
        + alpha_BT * (BT[:, None] - BT_bar)
        + alpha_Age * (log_Age[:, None] - log_Age_bar)
        + alpha_V4 * (V4[None, :] - V4_bar)
        + b1[:, None]
        + b
    )

    cost = 0
    cost += np.sum(dpois_llf(y, mu))
    cost += np.sum(dnorm_llf(b, 0.0, tau_b))
    cost += np.sum(dnorm_llf(b1, 0.0, tau_b1))
    cost += dnorm_llf(a0, 0, 1e-4)
    cost += dnorm_llf(alpha_Base, 0, 1e-4)
    cost += dnorm_llf(alpha_Trt, 0, 1e-4)
    cost += dnorm_llf(alpha_BT, 0, 1e-4)
    cost += dnorm_llf(alpha_Age, 0, 1e-4)
    cost += dnorm_llf(alpha_V4, 0, 1e-4)
    cost += dgamma_llf(tau_b1, 1e-3, 1e-3)
    cost += dgamma_llf(tau_b, 1e-3, 1e-3)

    return -cost
コード例 #10
0
def nllf(p):
    beta, tau, dL0, b = p[0], p[1], p[2:2 + T], p[2 + T:]
    Y = step(obs_t[0:N, None] - t[None, 0:T] + eps)
    dN = Y * step(t[None, 1:T + 1] - obs_t[0:N, None] - eps) * fail[0:N, None]
    Idt = Y * exp(beta * Z[0:N, None] + b[pair[0:N, None] - 1]) * dL0[None,
                                                                      0:T]

    cost = 0
    cost += np.sum(dpois_llf(dN, Idt))
    cost += np.sum(dgamma_llf(dL0, mu, c))
    cost += np.sum(dnorm_llf(b, 0.0, tau))
    cost += dgamma_llf(tau, 0.001, 0.001)
    cost += dnorm_llf(beta, 0.0, 0.000001)

    return -cost
コード例 #11
0
def nllf(p):
    alpha, beta = p[0:N], p[N:2 * N]
    alpha_c, alpha_tau, beta_c, beta_tau, tau_c = p[2 * N:2 * N + 5]
    mu = alpha[:, None] + beta[:, None] * (x[None, :] - xbar)

    cost = 0.
    cost += np.sum(dnorm_llf(Y, mu, tau_c))
    cost += np.sum(dnorm_llf(alpha, alpha_c, alpha_tau))
    cost += np.sum(dnorm_llf(beta, beta_c, beta_tau))
    cost += dgamma_llf(tau_c, 0.001, 0.001)
    cost += dnorm_llf(alpha_c, 0.0, 1e-6)
    cost += dgamma_llf(alpha_tau, 0.001, 0.001)
    cost += dnorm_llf(beta_c, 0.0, 1e-6)
    cost += dgamma_llf(beta_tau, 0.001, 0.001)
    return -cost
コード例 #12
0
def nllf(p):
    mu, phi, pi, tau1, tau2 = p[:5]
    delta = p[5:]
    m = mu + sign_T * phi / 2 + sign_k[None, :] * pi / 2 + delta[:, None]

    cost = 0
    cost += np.sum(dnorm_llf(Y, m, tau1))
    cost += np.sum(dnorm_llf(delta, 0.0, tau2))
    cost += dgamma_llf(tau1, 0.001, 0.001)
    cost += dgamma_llf(tau2, 0.001, 0.001)
    cost += dnorm_llf(mu, 0, 1e-6)
    cost += dnorm_llf(phi, 0, 1e-6)
    cost += dnorm_llf(pi, 0, 1e-6)

    return -cost
コード例 #13
0
def nllf(p):
    alpha0, alpha1, alpha2, alpha12, tau = p[:5]
    b = p[5:]

    p = ilogit(alpha0 + alpha1 * x1 + alpha2 * x2 + alpha12 * x1 * x2 + b)

    cost = 0
    cost += np.sum(dbin_llf(r, p, n))
    cost += np.sum(dnorm_llf(b, 0., tau))
    cost += dnorm_llf(alpha0, 0.0, 1e-6)
    cost += dnorm_llf(alpha1, 0.0, 1e-6)
    cost += dnorm_llf(alpha2, 0.0, 1e-6)
    cost += dnorm_llf(alpha12, 0.0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #14
0
def nllf(p):
    alpha, beta, gamma, tau = p[:4]
    lambda_ = p[4:].reshape(doses, plates)

    mu = exp(alpha + beta * log(x[:, None] + 10) + gamma * x[:, None] +
             lambda_)

    cost = 0
    cost += np.sum(dpois_llf(y, mu))
    cost += np.sum(dnorm_llf(lambda_, 0.0, tau))
    cost += dnorm_llf(alpha, 0.0, 1e-6)
    cost += dnorm_llf(beta, 0.0, 1e-6)
    cost += dnorm_llf(gamma, 0.0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #15
0
def nllf(p):
    theta = p[:K*3].reshape(K, 3)
    mu, tau = p[K*3:K*3+3], p[K*3+3:K*3+3+3]
    tauC = p[K*3+3+3]

    phi0, phi1, phi2 = exp(theta[:, 0]), expm1(theta[:, 1]), -exp(theta[:, 2])
    eta = phi0[:, None] / (1 + phi1[:, None] * exp(phi2[:, None] * x[None, :]))

    cost = 0
    cost += np.sum(dnorm_llf(Y, eta, tauC))
    cost += np.sum(dnorm_llf(theta, mu[None, :], tau[None, :]))
    cost += dgamma_llf(tauC, 0.001, 0.001)
    cost += np.sum(dnorm_llf(mu, 0, 1e-4))
    cost += np.sum(dgamma_llf(tau, 1e-3, 1e-3))

    return -cost
コード例 #16
0
ファイル: blockers.py プロジェクト: waternk/bugs
def nllf(pars):
    d, delta_new, tau = pars[0:3]
    mu, delta = pars[3:3+Num], pars[3+Num:3+2*Num]

    pc = ilogit(mu)
    pt = ilogit(mu + delta)

    cost = 0
    cost += np.sum(dbin_llf(rc, pc, nc))
    cost += np.sum(dbin_llf(rt, pt, nt))
    cost += np.sum(dnorm_llf(mu, 0, 1e-5))
    cost += np.sum(dt_llf(delta, d, tau, 4))
    cost += dnorm_llf(d, 0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)
    cost += dt_llf(delta_new, d, tau, 4)

    return -cost
コード例 #17
0
def nllf(p):
    lambda1, P1, theta, tau = p[:4]
    T[1:-1] = p[4:4 + N - 2]
    lambda2 = lambda1 + theta
    P2 = 1 - P1  # P must sum to 1 for dirichlet
    P = np.array([P1, P2])
    T_int = np.asarray(np.floor(T), 'i')
    mu = np.array([lambda1, lambda2])[T_int - 1]

    cost = 0
    cost += np.sum(dnorm_llf(y, mu, tau))
    cost += np.sum(dcat_llf(T_int, P))
    cost += ddirich_llf(P, alpha)
    cost += dunif_llf(theta, 0, 1000)
    cost += dnorm_llf(lambda1, 0.001, 0.001)
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #18
0
def nllf(p):
    delta, alpha, state = p[0], p[1], p[2:]
    beta = exp(alpha)
    theta = ilogit(delta)

    P = np.array([ilogit(alpha), 0])
    state = np.asarray(np.floor(state), 'i')

    #state1 = state + 1  # zero-indexing in numpy
    #prop = P[state] # unused

    cost = 0
    cost += np.sum(dbin_llf(y, P[state], t))
    cost += np.sum(dbern_llf(state, theta))
    cost += dnorm_llf(alpha, 0, 1e-4)
    cost += dnorm_llf(delta, 0, 1e-4)

    return -cost
コード例 #19
0
ファイル: magnesium.py プロジェクト: waternk/bugs
def nllf(p):
    if PRIOR > 0:
        sigma = np.array([sigma_prior[PRIOR-1](p[0])])
        mu = np.array([p[1]])
    else:
        #inv_tau_sqrd_1, tau_sqrd_2, tau_3, B0, D0, tau_sqrd_6 = p[:6]
        sigma = np.array([f(v) for f, v in zip(sigma_prior, p[:6])])
        mu = p[6:12]

    tau = 1/np.sqrt(sigma)

    cost = 0
    for _ in range(MARGINALIZATION_COUNT):
        theta = np.random.normal(mu[:, None], sigma[:, None], size=(Nprior, Nstudy))
        pc = np.random.rand(Nprior, Nstudy)
        pt = ilogit(theta + logit(pc))
        cost += np.sum(dnorm_llf(theta, mu[:, None], tau[:, None]))
        cost += np.sum(dbin_llf(rt[None, :], pt, nt[None, :]))
        cost += np.sum(dbin_llf(rc[None, :], pc, nc[None, :]))
    cost /= MARGINALIZATION_COUNT

    #cost += np.sum(dunif_llf(mu, -10, 10))

    # Prior 1: Gamma(0.001, 0.001) on inv.tau.sqrd
    if PRIOR == 1: cost += dgamma_llf(p[0], 0.001, 0.001)
    elif PRIOR == 0: cost += dgamma_llf(p[0], 0.001, 0.001)

    # Prior 2: Uniform(0, 50) on tau.sqrd
    #cost += dunif_llf(tau_sqrd_2, 0, 50)

    # Prior 3: Uniform(0, 50) on tau
    #cost += dunif_llf(tau_3, 0, 50)

    # Prior 4: Uniform shrinkage on tau.sqrd
    #cost += dunif_llf(B0, 0, 1)

    # Prior 5: Dumouchel on tau
    #cost += dunif_llf(D0, 0, 1)

    # Prior 6: Half-Normal on tau.sqrd
    if PRIOR == 6: cost += dnorm_llf(p[0], 0, prior_6_p0)
    elif PRIOR == 0: cost += dnorm_llf(p[5], 0, prior_6_p0)

    return -cost
コード例 #20
0
ファイル: mice.py プロジェクト: waternk/bugs
def nllf(p):
    beta, r = p[:M], p[M]
    mu = exp(beta)

    cost = 0
    cost += np.sum(dweib_C_llf(t, r, mu[:, None], lower=t_cen))
    cost += np.sum(dnorm_llf(beta, 0.0, 0.001))
    cost += dexp_llf(r, 0.001)

    return -cost
コード例 #21
0
def nllf(p):
    alpha, beta, tau, gamma = p

    mu = alpha - beta * gamma**x

    cost = 0
    cost += np.sum(dnorm_llf(Y, mu, tau))
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #22
0
def nllf(p):
    beta, pi, kappa, tau = p[:4]
    a = p[4:7]
    b = p[7:]

    ## quick rejection of unordered a points
    if not(-1000 <= a[0] and a[0] <= a[1] and a[1] <= a[2] and a[2] <= 1000):
        return inf
    if tau <= 0:
        return inf

    sigma = 1 / sqrt(tau)
    mu = beta * treat/2 + pi * period/2 + kappa * carry
    prob = np.empty((N, T, Ncut+1))

    ## Marginalize over random effects (b[N] ~ N(0, tau))
    cost = 0
    for _ in range(MARGINALIZATION_COUNT):
        b = np.random.normal(0.0, sigma, size=N)
        #cost += np.sum(dnorm_llf(b, 0, tau))

        Q = ilogit(-(a[None, None, :] + mu[group, :, None] + b[:, None, None]))
        prob[:, :, 0] = 1 - Q[:, :, 0]
        for j in range(1, Ncut):
            prob[:, :, j] = Q[:, :, j-1] - Q[:, :, j]
        prob[:, :, -1] = Q[:, :, -1]

        cost += np.sum(dcat_llf(response, prob))
    cost /= MARGINALIZATION_COUNT

    cost += dnorm_llf(beta, 0, 1e-6)
    cost += dnorm_llf(pi, 0, 1e-6)
    cost += dnorm_llf(kappa, 0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)
    ## ordered cut points for underlying continuous latent variable
    #cost += dflat_llf(a[0]) if -1000 <= a[0] <= a[1] else -inf
    #cost += dflat_llf(a[1]) if a[0] <= a[1] <= a[2] else -inf
    #cost += dflat_llf(a[2]) if a[1] <= a[2] <= 1000 else -inf

    ## PAK: model looks over-parameterized: anchor a[1]
    #cost += dnorm_llf(a[0], 0.707, 1/0.1364**2)

    return -cost
コード例 #23
0
ファイル: leuk.py プロジェクト: waternk/bugs
def nllf(p):
    beta, dL0 = p[0], p[1:]
    Y = step(obs_t[:N, None] - t[None, :T] + eps)
    dN = Y * step(t[None, 1:] - obs_t[:, None] - eps) * fail[:, None]
    Idt = Y*exp(beta * Z[:, None]) * dL0[None, :]

    cost = 0
    cost += np.sum(dpois_llf(dN, Idt))
    cost += np.sum(dgamma_llf(dL0, mu, c))
    cost += dnorm_llf(beta, 0.0, 0.000001)

    return -cost
コード例 #24
0
def nllf(p):
    alpha, beta1, beta2, tau = p[:4]
    mu, b = p[4:4 + K], p[4 + K:]

    # median values from fit
    #alpha, beta1, beta2, tau = 0.5793, -0.0457, 0.007004, 1/0.08059**2

    logPsi = alpha + beta1 * year + beta2 * (year * year - 22) + b
    p0 = ilogit(mu)
    p1 = ilogit(mu + logPsi)

    cost = 0
    cost += np.sum(dbin_llf(r0, p0, n0))
    cost += np.sum(dbin_llf(r1, p1, n1))
    cost += np.sum(dnorm_llf(b, 0, tau))
    cost += np.sum(dnorm_llf(mu, 0, 1e-6))
    cost += dnorm_llf(alpha, 0, 1e-6)
    cost += dnorm_llf(beta1, 0, 1e-6)
    cost += dnorm_llf(beta2, 0, 1e-6)
    cost += dgamma_llf(tau, 0.001, 0.001)

    return -cost
コード例 #25
0
ファイル: aligators.py プロジェクト: waternk/bugs
def nllf(active_pars):
    pars = p0.copy()
    pars[active_index] = active_pars
    alpha = pars[0:K]
    beta = pars[K:K + I * K].reshape(I, K)
    gamma = pars[K + I * K:K + I * K + J * K].reshape(J, K)
    Lambda = pars[K + I * K + J * K:K + I * K + J * K + I * J].reshape(I, J)

    cost = 0
    alpha[0] = 0  # zero contrast for baseline food
    cost += np.sum(dnorm_llf(alpha[1:], 0, 0.00001))  # vague priors

    # Loop around lakes:
    beta[0, :] = 0  # corner-point contrast with first lake
    beta[:, 0] = 0  # zero contrast for baseline food
    cost += np.sum(dnorm_llf(beta[1:, 1:], 0, 0.00001))  # vague priors

    # Loop around sizes:
    gamma[0, :] = 0  # corner-point contrast with first size
    gamma[:, 0] = 0  # zero contrast for baseline food
    cost += np.sum(dnorm_llf(gamma[1:, 1:], 0, 0.00001))  # vague priors

    if 0:
        # Multinomial response
        n = np.sum(X, axis=2)
        phi = np.exp(alpha[None, None, :] + beta[:, None, :] +
                     gamma[None, :, :])
        p = phi / np.sum(phi, axis=2)
        cost += np.sum(dmulti_llf(X, p, n))
    else:
        # Fit standard Poisson regressions relative to baseline
        cost += np.sum(dnorm_llf(Lambda, 0, 0.00001))  # vague priors
        mu = np.exp(Lambda[:, :, None] + alpha[None, None, :] +
                    beta[:, None, :] + gamma[None, :, :])
        cost += np.sum(dpois_llf(X, mu))

    return -cost
コード例 #26
0
ファイル: otreesmvn.py プロジェクト: waternk/bugs
def nllf(p):
    theta = p[:K * 3].reshape(K, 3)
    mu, tau_L = p[K * 3:K * 3 + 3], p[K * 3 + 3:K * 3 + 3 + n_tau_L]
    tauC = p[K * 3 + 3 + n_tau_L]

    tau = expand_pd(tau_L, 3)

    phi0, phi1, phi2 = exp(theta[:, 0]), expm1(theta[:, 1]), -exp(theta[:, 2])
    eta = phi0[:, None] / (1 + phi1[:, None] * exp(phi2[:, None] * x[None, :]))

    cost = 0
    cost += np.sum(dnorm_llf(Y, eta, tauC))
    cost += sum(dmnorm_llf(theta_k, mu, tau) for theta_k in theta)
    cost += dgamma_llf(tauC, 0.001, 0.001)
    cost += dmnorm_llf(mu, mean, prec)
    cost += dwish_llf(tau, R, 3)

    return -cost
コード例 #27
0
def bones(pars):
    theta = pars[:13]
    grade[missing_grade_index] = np.floor(pars[13:] + 0.5)

    p = np.empty((nChild, nInd, 5))  # max(ncat) is 5
    Q = np.empty((nChild, nInd, 4))
    Q = ilogit(delta[None, :, None] *
               (theta[:, None, None] - gamma[None, :, :]))
    p[:, :, 1:-1] = Q[:, :, :-1] - Q[:, :, 1:]
    p[:, :, 0] = 1 - Q[:, :, 0]
    p[:, :, ncat - 1] = Q[:, :, ncat - 2]

    cost = 0
    cost += np.sum(dnorm_llf(theta, 0.0, 0.001))
    for i in range(nChild):
        for j in range(nInd):
            if 1 <= grade[i, j] <= ncat[j]:
                cost += dcat_llf(grade[i, j], p[i, j, :ncat[j]])
            else:
                cost = -inf

    return -cost