示例#1
0
def p_X_given_centers(model, data, state):
    lam = data.mask / state.sigma_sq_w
    h = lam * state.X
    temp = -0.5 * np.log(2*np.pi) + \
           -0.5 * np.log(state.sigma_sq_w) + \
           -0.5 * lam * state.X**2
    Z = (data.mask * temp).sum(1)
    return gaussians.Potential(h, psd_matrices.DiagonalMatrix(lam), Z)
示例#2
0
def evidence(model, data, state):
    K, D = state.Z.shape[1], state.X.shape[1]

    Lambda = np.dot(state.Z.T, state.Z) / state.sigma_sq_n + np.eye(K) / state.sigma_sq_f
    h = np.dot(state.Z.T, state.X) / state.sigma_sq_n

    # we can ignore the constant factors because they don't depend on Z
    pot = gaussians.Potential(h.T, psd_matrices.FullMatrix(Lambda[nax, :, :]), 0.)
    return pot.integral().sum()
示例#3
0
def sample_features(model, data, state):
    K, D = state.Z.shape[1], state.X.shape[1]

    Lambda = np.dot(state.Z.T, state.Z) / state.sigma_sq_n + np.eye(K) / state.sigma_sq_f
    h = np.dot(state.Z.T, state.X) / state.sigma_sq_n

    # we can ignore the constant factors because they don't depend on Z
    pot = gaussians.Potential(h.T, psd_matrices.FullMatrix(Lambda[nax, :, :]), 0.)
    return pot.to_distribution().sample().T
示例#4
0
def center_evidence(model, state, cache):
    lam = cache.obs_counts / state.sigma_sq_w
    mu = np.where(cache.obs_counts > 0, cache.sum_X / cache.obs_counts, 0.)
    h = mu * lam
    if model.isotropic_w:
        Z = -0.5 * cache.obs_counts.sum(1) * np.log(2*np.pi) + \
            -0.5 * cache.obs_counts.sum(1) * np.log(state.sigma_sq_w) + \
            -0.5 * cache.sum_X_sq.sum(1) / state.sigma_sq_w
    else:
        Z = -0.5 * cache.obs_counts.sum(1) * np.log(2 * np.pi) + \
            -0.5 * (cache.obs_counts * np.log(state.sigma_sq_w)).sum(1) + \
            -0.5 * (cache.sum_X_sq / state.sigma_sq_w).sum(1)
    return gaussians.Potential(h, psd_matrices.DiagonalMatrix(lam), Z)
def cond_U(X, obs, V, ssq_U, ssq_N):
    N, K, D = X.shape[0], V.shape[0], X.shape[1]
    if np.all(obs):
        Lambda = np.diag(1. / ssq_U) + np.dot(V, V.T) / ssq_N
        Lambda = Lambda[nax, :, :]
    else:
        Lambda = np.zeros((N, K, K))
        for i in range(N):
            idxs = np.where(obs[i, :])[0]
            V_curr = V[:, idxs]
            Lambda[i, :, :] = np.diag(1. / ssq_U) + np.dot(V_curr, V_curr.T) / ssq_N
    h = np.dot(X * obs, V.T) / ssq_N
    return gaussians.Potential(h, psd_matrices.FullMatrix(Lambda), 0.)