def test_1dmog(mus=[-2., 2.], sigs=[1., np.sqrt(3.)], pis=[0.5, 0.5], deg = 3, obsdeg = 6): print 'testing 1d mixture of Gaussians' # constraints on parameters here K = len(mus) mu,sig = sp.symbols('mu,sigma') M = mm.MomentMatrix(deg, [mu, sig], morder='grevlex') num_constrs = obsdeg + 1; # so observe num_constrs-1 moments H = abs(hermite_coeffs(num_constrs)) constrs = [0]*num_constrs for order in range(num_constrs): for i in range(order+1): constrs[order] = constrs[order] + H[order,i]* mu**(i) * sig**(order-i) constrsval = 0 for k in range(K): constrsval += pis[k]*constrs[order].evalf(subs={mu:mus[k], sig:sigs[k]}) constrs[order] -= constrsval print constrs cin = M.get_cvxopt_inputs(constrs[1:]) gs = [sig-0.2, 5-sig, sig+5, 10-mu, mu+10] #gs = [sig-0.2] locmatrices = [mm.LocalizingMatrix(M, g) for g in gs] Ghs = [lm.get_cvxopt_Gh() for lm in locmatrices] Gs=cin['G']# + [Gh['G'] for Gh in Ghs] hs=cin['h']# + [Gh['h'] for Gh in Ghs] sol = solvers.sdp(cin['c'],Gs=Gs, \ hs=hs, A=cin['A'], b=cin['b']) ## Q = cin['A'].T*cin['A'] ## weight = 1; ## dims = {} ## dims['l'] = 0; dims['q'] = []; dims['s'] = [len(M.row_monos)] ## ipdb.set_trace() ## sol = solvers.coneqp(Q*weight, cin['A'].T*cin['b']*weight + cin['c'],G=Gs[0], \ ## h=hs[0][:], dims = dims, A=cin['A'], b=cin['b']) for i,mono in enumerate(M.matrix_monos): trueval = 0; if i>0: for k in range(K): trueval += pis[k]*mono.evalf(subs={mu:mus[k], sig:sigs[k]}) else: trueval = 1 print '%s:\t%f\t%f' % (str(mono), sol['x'][i], trueval) print M.extract_solutions_lasserre(sol['x'], Kmax=len(mus)) #import MomentMatrixSolver as mmsolver #print mmsolver.alternating_solver(M, constrs, 2, maxiter=30000) return M,sol
def polymom_univariate(xi, c, order): constr = 0 H = abs(hermite_coeffs(order+1)) for i in range(order+1): constr = constr + H[order,i]* xi**(i) * c**((order-i)/2) return constr
def test_1dmog(mus=[-2., 2.], sigs=[1., np.sqrt(3.)], pis=[0.5, 0.5], deg=3, obsdeg=6): print 'testing 1d mixture of Gaussians' # constraints on parameters here K = len(mus) mu, sig = sp.symbols('mu,sigma') M = mm.MomentMatrix(deg, [mu, sig], morder='grevlex') num_constrs = obsdeg + 1 # so observe num_constrs-1 moments H = abs(hermite_coeffs(num_constrs)) constrs = [0] * num_constrs for order in range(num_constrs): for i in range(order + 1): constrs[order] = constrs[order] + H[order, i] * mu**(i) * sig**(order - i) constrsval = 0 for k in range(K): constrsval += pis[k] * constrs[order].evalf(subs={ mu: mus[k], sig: sigs[k] }) constrs[order] -= constrsval print constrs cin = M.get_cvxopt_inputs(constrs[1:]) gs = [sig - 0.2, 5 - sig, sig + 5, 10 - mu, mu + 10] #gs = [sig-0.2] locmatrices = [mm.LocalizingMatrix(M, g) for g in gs] Ghs = [lm.get_cvxopt_Gh() for lm in locmatrices] Gs = cin['G'] # + [Gh['G'] for Gh in Ghs] hs = cin['h'] # + [Gh['h'] for Gh in Ghs] sol = solvers.sdp(cin['c'],Gs=Gs, \ hs=hs, A=cin['A'], b=cin['b']) ## Q = cin['A'].T*cin['A'] ## weight = 1; ## dims = {} ## dims['l'] = 0; dims['q'] = []; dims['s'] = [len(M.row_monos)] ## ipdb.set_trace() ## sol = solvers.coneqp(Q*weight, cin['A'].T*cin['b']*weight + cin['c'],G=Gs[0], \ ## h=hs[0][:], dims = dims, A=cin['A'], b=cin['b']) for i, mono in enumerate(M.matrix_monos): trueval = 0 if i > 0: for k in range(K): trueval += pis[k] * mono.evalf(subs={mu: mus[k], sig: sigs[k]}) else: trueval = 1 print '%s:\t%f\t%f' % (str(mono), sol['x'][i], trueval) print M.extract_solutions_lasserre(sol['x'], Kmax=len(mus)) #import MomentMatrixSolver as mmsolver #print mmsolver.alternating_solver(M, constrs, 2, maxiter=30000) return M, sol