def CA_iteration(z1, z2, prev_theta, max_iter, fix_mu=False, fix_sigma=False, eps=1e-12): """Fit the gaussian model params via coordinate ascent. """ init_lhd = calc_gaussian_mix_log_lhd(prev_theta, z1, z2) prev_lhd = init_lhd min_vals = [MIN_MU, MIN_SIGMA, MIN_RHO, MIN_MIX_PARAM] max_vals = [MAX_MU, MAX_SIGMA, MAX_RHO, MAX_MIX_PARAM] theta = numpy.array(prev_theta).copy() for i in range(max_iter): for index, (min_val, max_val) in enumerate(zip(min_vals, max_vals)): if index == 0 and fix_mu: continue if index == 1 and fix_sigma: continue theta, new_lhd = CA_step(z1, z2, theta, index, min_val, max_val) theta, changed_params = clip_model_params(theta) assert changed_params == False if not changed_params: assert new_lhd + 1e-6 >= prev_lhd if new_lhd - prev_lhd < eps: return theta, new_lhd prev_theta = theta prev_lhd = new_lhd return theta, new_lhd
def EM_iteration(z1, z2, prev_theta, max_iter, fix_mu=False, fix_sigma=False, eps=1e-12): """Fit the gaussian model params via EM. """ init_lhd = calc_gaussian_mix_log_lhd(prev_theta, z1, z2) prev_lhd = init_lhd for i in range(max_iter): theta = EM_step(z1, z2, prev_theta) theta, changed_params = clip_model_params(theta) new_lhd = calc_gaussian_mix_log_lhd(theta, z1, z2) # if the model is at the boundary, abort if changed_params: return theta, new_lhd, True assert new_lhd + 1e-6 >= prev_lhd if new_lhd - prev_lhd < eps: return theta, new_lhd, False prev_theta = theta prev_lhd = new_lhd return theta, new_lhd, False
def grid_search(r1, r2 ): res = [] best_theta = None max_log_lhd = -1e100 for mu in numpy.linspace(0.1, 5, num=10): for sigma in numpy.linspace(0.5, 3, num=10): for rho in numpy.linspace(0.1, 0.9, num=10): for pi in numpy.linspace(0.1, 0.9, num=10): z1 = compute_pseudo_values(r1, mu, sigma, pi) z2 = compute_pseudo_values(r2, mu, sigma, pi) log_lhd = calc_gaussian_mix_log_lhd((mu, sigma, rho, pi), z1, z2) if log_lhd > max_log_lhd: best_theta = ((mu,mu), (sigma,sigma), rho, pi) max_log_lhd = log_lhd return best_theta
def f(alpha): inner_theta[index] = theta[index] + alpha return -calc_gaussian_mix_log_lhd(inner_theta, z1, z2)
def log_lhd_loss(r1, r2, theta): mu, sigma, rho, p = theta z1 = compute_pseudo_values(r1, mu, sigma, p) z2 = compute_pseudo_values(r2, mu, sigma, p) return -calc_gaussian_mix_log_lhd(theta, z1, z2)