])
count = 0  # Pocitadlo akceptovasnych vzorku
nu_1 = nu_0 + n  # 5.23
log_A = h = np.array([0.0] * s)
# ===== 4. Metropolis within Gibbs =====
print("Metropolis within Gibbs ...")
for i in range(1, s):
    # a) podminena hustota p(h|gamma,y) ~ G(h_1,nu_1)
    f_x = gamma[0][i-1] * \
        ( gamma[1][i-1] * x[:,[1]]**gamma[3][i-1] + gamma[2][i-1] * x[:,[2]]**gamma[3][i-1] ) ** \
        ( 1 / gamma[3][i-1] )
    h_1 = (1 / nu_1 * ((y - f_x).T @ (y - f_x) + nu_0 / h_0))**(-1)
    h[i] = gamm_rnd_koop2(h_1, nu_1)
    # b) podminena hustota p(gamma|h,y) -> logaritmus jadrove podminene hustoty = externi funkce log_post_ces.py
    # Generovani kandidatu z kandidatske hustoty
    gamma_ast = gamma[:, [i - 1]] + norm_rnd(sigma)
    log_accept = min(
        log_post_ces(y, x, gamma_ast, h[i], gamma_0, v_0) - \
        log_post_ces(y, x, gamma[:,[i-1]], h[i], gamma_0, v_0),
        0
    )
    # Rozhodnuti o akceptaci
    if log_accept > log(uniform()):
        gamma[:, [i]] = gamma_ast
        count += 1
    else:
        gamma[:, [i]] = gamma[:, [i - 1]]

    progress_bar(i, s)

# ===== 5. Prezentace vysledku a posteriorni analyza =====
h[:, [0]] = h_0
v_beta[:, :, [0]] = v_beta_0
mu_beta[:, [0]] = mu_beta_0

nu_beta_1 = n + nu_beta_0
nu_1 = t * n + nu_0

for i in range(s):
    for j in range(n):
        v_1 = inv(h[:, [i - 1]] * x_i[:, :, [j]].T @ x_i[:, :, [j]] + inv(v_beta[:, :, [i - 1]]))
        beta_1 = v_1 @ (
            h[:, [i - 1]] * x_i[:, :, [j]].T @ y_i[:, [j]] + \
            inv(v_beta[:, :, [i - 1]]) @ mu_beta[:, [i - 1]]
        )
        # Podminena hustota pro beta
        beta[:, [i], [j]] = beta_1 + norm_rnd(v_1)  # Koop 7.25
    
    # Podminena hustota pro mu_beta
    sigma_beta_1 = inv(n * inv(v_beta[:, :, [i - 1]]) + inv(sigma_beta_0))
    pom = 0
    for j in range(n):
        pom += beta[:, [i], [j]]

    mu_beta_1 = sigma_beta_1 @ (inv(v_beta[:, :, [i - 1]]) @ pom + inv(sigma_beta_0) @ mu_beta_0)
    mu_beta[:, [i]] = mu_beta_1 + norm_rnd(sigma_beta_1)

    pom = 0
    for j in range(n):
        pom += (beta[:, [i], [j]] - mu_beta[:, [i]] @ (beta[:, [i], [j]] - mu_beta[:, [i]]).T)

    v_beta_1 = pom + v_beta_0
s_1 = 15_000
s = s_0 + s_1

beta = np.zeros((k, s))
h = np.zeros((1, s))
sd_nom = np.zeros((k, s))   # Citatel pro BF pomoci Savage-Dickey

# ===== 3. Gibbsuv vzorkovac =====
h[0] = h_0
beta[:, [0]] = beta_0
nu_1 = n + nu_0

for i in range(1, s):
    v_1 = inv(inv(v_0) + h[0][i - 1] * x.T @ x)
    beta_1 = v_1 @ (inv(v_0) @ beta_0 + h[0][i - 1] * x.T @ y)
    beta[:, [i]] = beta_1 + norm_rnd(v_1)
    h_1 = (1 / nu_1 * ((y - x @ beta[:,[i]]).T @ (y - x @ beta[:, [i]]) + nu_0 * h_0 ** (- 1))) ** (- 1)
    h[0][i] = gamm_rnd_koop2(h_1, nu_1)

    for j in range(k):
        # V Matlabu se jako skalovaci parametr vklada primo rozptyl, ktery je odmocnen ve funkci
        # V Pythonu se musi vlozit uz primo sm. odchylka
        sd_nom[j][i] = norm.pdf(0, beta_1[j][0], math.sqrt(v_1[j][j]))

    progress_bar(i, s)

# ===== 4. Vyhozeni prvnich s_0 vzorku =====
beta = beta[:, s_0:]
h = h[:, s_0:]
sd_nom = sd_nom[:, s_0:]
nu_1 = n
# Promenne S-D density ratio pro rho_i = 0
sd_prior = np.zeros((p, 1))
sd_post = np.zeros((p, s))

for i in range(1, s):
    y_star = y
    x_star = x

    for j in range(p):
        y_star = y_star - rho[j][s - 1] * y_lag[:, j]
        x_star = x_star - rho[j][s - 1] * x_lag[:, :, [j]]

    v_1 = inv(h[0][i - 1] * x_star.T @ x_star)
    beta_1 = v_1 @ (h[0][i - 1] * x_star.T @ x_star.T)
    beta[:, [i]] = beta_1 + norm_rnd(v_1)

    h_1 = (1 / nu_1 * (
        (y_star - x_star @ beta[:, [i]]).T @ (y_star - x_star @ beta[:, [i]]))
           )**(-1)
    h[0][i] = gamm_rnd_koop2(h_1, nu_1)

    # Vypocet a sestrojeni matice E (nahodnych slozek a jejich zpozdeni)
    eps_full = y_full - x_full @ beta[:, [i]]
    eps = eps_full[p:n, :]
    e = np.zeros((n - p, p))
    for j in range(p):
        e[:, [j]] = eps_full[(p + 1 - j):(n - j), [0]]

    vrho_1 = inv(inv(vrho_0) + h[0][i] * e.T @ e)
    rho_1 = vrho_1 @ (inv(vrho_0) @ rho_0 + h[0][i] * e.T @ eps)
Exemple #5
0
beta[:, [0]] = beta_0
alpha_rw[:, [0]] = alpha_0
nu_1 = n + nu_0
error_check = 0
count_rw = 0

for i in range(1, s):

    omega = np.diag(((z @ alpha_rw[:, i - 1] + 1)**2).ravel())
    inv_omega = inv(omega)
    b_omega = inv(x.T @ inv_omega @ x) @ x.T @ inv_omega @ y
    v_1 = inv(inv(v_0) + h[0][i - 1] * x.T @ inv_omega @ x)
    beta_1 = v_1 @ (inv(v_0) @ beta_0 +
                    h[0][i - 1] * x.T @ inv_omega @ x @ b_omega)
    try:
        beta[:, [i]] = beta_1 + norm_rnd(v_1)
    except:
        beta[:, [i]] = beta_1 + norm_rnd(v_1 + 0.000000001)
        error_check += 1

    h_1 = (1 / nu_1 * \
            ((y - x @ beta[:, [i]]).T @ inv_omega @ (y - x @ beta[:, [i]]) + nu_0 * h_0 ** (- 1)) \
        ) ** (- 1)
    h[:, [i]] = gamm_rnd_koop2(h_1, nu_1)

    a_can_rw = alpha_rw[:, [i - 1]] + norm_rnd(vscale_rw)
    log_accept_rw = min(
        a_post(a_can_rw, beta[:, [i]], h[0][i], y, x, z) -
        a_post(alpha_rw[:, [i - 1]], beta[:, [i]], h[0][i], y, x, z), 0)

    if log_accept_rw > math.log(np.random.uniform()):
nu_lam_rw[:, [0]] = nu_lam_0

nu_1 = n + nu_0
error_check = 0
count_rw = 0

# ===== 3. Gibbsuv vzorkovac =====
for i in range(1, s):
    omega = np.diag((lam[:, [i - 1]]**(-1)).ravel())
    inv_omega = inv(omega)
    b_omega = inv(x.T @ inv_omega @ x) @ x.T @ inv_omega @ y
    v_1 = inv(inv(v_0) + h[0][i - 1] * x.T @ inv_omega @ x)
    beta_1 = v_1 @ (inv(v_0) @ beta_0 +
                    h[0][i - 1] * x.T @ inv_omega @ x @ b_omega)
    try:
        beta[:, [i]] = beta_1 + norm_rnd(v_1)
    except:
        beta[:, [i]] = beta_1 + norm_rnd(v_1 + 0.000000001)
        error_check += 1

    h_1 = (1 / nu_1 * \
            ((y - x @ beta[:, [i]]).T @ inv_omega @ (y - x @ beta[:, [i]]) + nu_0 * h_0 ** (- 1)) \
        ) ** (- 1)
    h[:, [i]] = gamm_rnd_koop2(h_1, nu_1)

    epsilon = y - x @ beta[:, [i]]
    nu_lam_1 = nu_lam_rw[:][i - 1] + 1
    for j in range(n):
        lam_1 = nu_lam_1 / (h[0][i] * epsilon[j][0]**2 + nu_lam_1 - 1)
        # pdb.set_trace()
        # Pouziva jiny typ funkce pro generovani cisel z gamma rozdeleni nez v Matlab scriptu
# ===== 2. Gibbsuv vzorkovac =====
for i in range(1, s):
    sum_v_1 = np.zeros((k, k))
    sum_b_1 = np.zeros((k, k))
    for j in range(n):
        sum_v_1 += x[(j * 2):(j * 2 + 2), :].T @ h[:, :, [j - 1]] @ x[
            (j * 2):(j * 2 + 2), :]
        sum_b_1 += x[(j * 2):(j * 2 + 2), :].T @ h[:, :, [j - 1]] @ y[
            (j * 2):(j * 2 + 2), [0]]

    v_1 = inv(v_0) + sum_v_1  # Koop 6.50
    beta_1 = inv(v_1) @ (inv(v_0) @ beta_0 + sum_b_1)  # Koop 6.51

    # Podminena hustota pro beta
    beta[:, [i]] = beta_1 + norm_rnd(inv(v_1))  # Koop 6.49
    # Podminena hustota pro h
    sum_h = np.zeros((m, m))
    for j in range(n):
        # cast sumy v Koop 6.54
        sum_h += (
                y[(j * 2):(j * 2 + 2), [0]] - \
                x[(j * 2):(j * 2 + 2), :] @ beta[:, [i]]
            ) @ (
                y[(j * 2):(j * 2 + 2), [0]] - \
                x[(j * 2):(j * 2 + 2), :] @ beta[:, [i]]
            ).T

    # Koop 6.52
    h_1 = inv(inv_h_0 + sum_h)
    # Podminena hustota pro H
Exemple #8
0
            y_i[:, [j]] - alpha[j, [i - 1]] @ np.ones((t, 1)) - x_i_til[:, :, [j]] @ beta[:, i] 
        )
    # Podminena hustota pro h
    h_1 = (1 / nu_1 * (pom + nu_0 / h_0)) ** (- 1)
    h[:, [i]] = gamm_rnd_koop(h_1, nu_1)

    # Podminene hustoty pro kazde alpha_j
    for j in range(n):
        v_1_j = (v_alpha[:, [i - 1]] * h[:, [i]] ** (- 1)) / (t * v_alpha[:, [i - 1]] + h[:, [i]] ** (- 1))
        alpha_1 = (
            v_alpha[:, [i - 1]] @ (y_i[:, [j]] - x_i_til[:, :, [j]] @ beta[:, [j]]).T @ \
            np.ones((t, 1)) + h[:, [i]] ** (- 1) * mu_alpha[:, [i - 1]]
        ) / (
            t * v_alpha[:, [i - 1]] + h[:, [i]] ** (- 1)
        )
        alpha[[j],[s]] = alpha_1 + norm_rnd(v_1_j)      # Koop 7.16

    # Podminena hustota pro mu_alpha
    sigma_2_alpha_1 = v_alpha[:, [i - 1]] / h_alpha_0 / (v_alpha[:, [i - 1]] + n / h_alpha_0)
    mu_alpha_1 = (
        v_alpha[:, [i - 1]] * mu_alpha_0 / h_alpha_0 * np.sum(alpha[:, [i]])
    ) / (
        v_alpha[:, [i - 1]] + n / h_alpha_0
    ) 
    mu_alpha[:, [i]] = mu_alpha_1 + norm_rnd(sigma_2_alpha_1)   # Koop 7.17

    # Podminena hustota pro inv(v_alpha)
    pom = 0
    for j in range(n):
        pom += (alpha[[j], [i]] - mu_alpha[:, [i]]) ** 2