def expected_statistics(h, b): w_prob = unlog(h[0] + b[1]) hits_expected = fast_average(hits_pre[0], w_prob) for k in range(1, params['M']): w_prob_new = np.zeros(n_w[k]) for w_prev in range(n_w[k-1]): w_weight = unlog(h[k][w_prev,:] + b[k+1]) w_prob_new += w_weight * w_prob[w_prev] hits_expected += (w_prob[w_prev] * fast_average(hits_pre[k][w_prev], w_weight)) w_prob = w_prob_new return hits_expected
def grad_neg_log_likelihood(theta_vec): theta = np.reshape(theta_vec, theta_dim) h, b = dp(theta) # Compute expected statistics w_prob = unlog(h[0] + b[1]) hits_expected = fast_average(hits[0], w_prob) for k in range(1, params['M']): w_prob_new = np.zeros(n_w[k]) for w_prev in range(n_w[k-1]): w_weight = unlog(h[k][w_prev,:] + b[k+1]) w_prob_new += w_weight * w_prob[w_prev] hits_expected += (w_prob[w_prev] * fast_average(hits[k][w_prev], w_weight)) w_prob = w_prob_new return np.reshape(hits_expected - hits_observed, theta_vec.shape)