Ejemplo n.º 1
0
def test_max_likelihood():
    import pdb
    pdb.set_trace()
    x = [1., 2.]
    obs = gaussian_generator(mu=x[0], sigma=x[1])
    pdf = norm(x[0], x[1]).pdf

    func_to_minimize = lambda x: -get_log_likelihood( norm(x[0], x[1]).pdf, obs )

    solver = Solver(func_to_minimize, minimize)

    x0 = [0., 0.5]
    res = solver.solve(x0)
    print res
Ejemplo n.º 2
0
def test_max_likelihood(params=None, params0=None, method="L-BFGS-B"):
    # simple test with a two gaussian mixture
    if not params:
        params = {"mu": [0.0, 5.0], "sigma": [0.01, 0.01], "p": [0.5]}

    obs = gaussian_mix_generator(params["mu"], params["sigma"], params["p"])

    """
    def penalize(params):
        penalization = 0
        if True in (np.array(params['p'])<0.01):
            penalization += 10000000.
        if True in (np.array(params['sigma'])<0.01):
            penalization += 10000000.
        return penalization
    """

    func_to_minimize = lambda params: -get_log_likelihood(GaussianMix(params).pdf, obs)
    solver = Solver(
        func_to_minimize, minimize, preprocess=GaussianMix.preprocess_func, postprocess=GaussianMix.postprocess_func
    )

    bounds = ((None, None), (None, None), (0.0, None), (0.0, None), (0.0, 1.0))

    # the initial point is set by kmeans
    k = len(params["mu"])
    kmeans_res = kmeans2(obs, k)
    mu0 = kmeans_res[0]
    sigma0 = np.zeros(k)
    p0 = np.zeros(k)

    for id in range(k):
        sigma0[id] = obs[np.where(kmeans_res[1] == id)].std()
        p0[id] = np.where(kmeans_res[1] == id)[0].size * 1.0 / obs.size

    params0 = {"mu": mu0.tolist(), "sigma": sigma0.tolist(), "p": p0[:-1].tolist()}
    print "initial point", params0

    res = solver.solve(params0, method=method, bounds=bounds)
    print "final point", res

    print "function value for the correct point", func_to_minimize(params)
    print "function value for the initial point", func_to_minimize(params0)
    print "function value for the final point", func_to_minimize(res)