def addmins(G,X,Y,S,D,xmin,mode=OFFHESSZERO, GRADNOISE=1e-9,EP_SOFTNESS=1e-9,EPROP_LOOPS=20): dim=X.shape[1] #grad elements are zero Xg = sp.vstack([xmin]*dim) Yg = sp.zeros([dim,1]) Sg = sp.ones([dim,1])*GRADNOISE Dg = [[i] for i in range(dim)] #offdiag hessian elements nh = ((dim-1)*dim)/2 Xh = sp.vstack([sp.empty([0,dim])]+[xmin]*nh) Dh=[] for i in xrange(dim): for j in xrange(i): Dh.append([i,j]) class MJMError(Exception): pass if mode==OFFHESSZERO: Yh = sp.zeros([nh,1]) Sh = sp.ones([nh,1])*GRADNOISE elif mode==OFFHESSINFER: raise MJMError("addmins with mode offhessinfer not implemented yet") else: raise MJMError("invalid mode in addmins") #diag hessian and min Xd = sp.vstack([xmin]*(dim+1)) Dd = [[sp.NaN]]+[[i,i] for i in xrange(dim)] [m,V] = G.infer_full_post(Xd,Dd) for i in xrange(dim): if V[i,i]<0: class MJMError(Exception): pass print [m,V] raise MJMError('negative on diagonal') yminarg = sp.argmin(Y) Y_ = sp.array([Y[yminarg,0]]+[0.]*dim) Z = sp.array([-1]+[1.]*dim) F = sp.array([S[yminarg,0]]+[EP_SOFTNESS]*dim) [Yd,Stmp] = eprop.expectation_prop(m,V,Y_,Z,F,EPROP_LOOPS) Sd = sp.diagonal(Stmp).flatten() Sd.resize([dim+1,1]) Yd.resize([dim+1,1]) #concat the obs Xo = sp.vstack([X,Xg,Xd,Xh]) Yo = sp.vstack([Y,Yg,Yd,Yh]) So = sp.vstack([S,Sg,Sd,Sh]) Do = D+Dg+Dd+Dh return [Xo,Yo,So,Do]
import eprop import scipy as sp from scipy import linalg as spl from scipy import stats as sps from matplotlib import pyplot as plt #basic ep test on three values m = sp.array([1.,2.,0.5]) v = sp.array([[1.,0.5,0.2],[0.5,2.,0.5],[0.2,0.5,1.]]) n=len(m) Y = sp.array([1.,0.,1.]) Z = sp.array([-1,1,-1]) F = sp.array([0.25**2,0.,0.]) mt,vt = eprop.expectation_prop(m,v,Y,Z,F,35) mu,vu = eprop.gaussian_fusion(m,mt,v,vt) f,ax = plt.subplots(n,sharex=True) ns=200 sup = sp.linspace(-6,6,ns) for i in xrange(n): ax[i].plot(sup,sps.norm.pdf(sup,loc=m[i],scale=sp.sqrt(v[i,i])),'b') ax[i].plot(sup,sps.norm.pdf(sup,loc=mu[i],scale=sp.sqrt(vu[i,i])),'r') ax[i].twinx().plot(sup,sps.norm.cdf(Z[i]*(sup-Y[i])/max(F[i],1e-20))*sps.norm.pdf(sup,loc=m[i],scale=sp.sqrt(v[i,i])),'g') #est on a gp import ESutils import GPdc nt=5
#!/usr/bin/env python2 # encoding: UTF-8 # To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import eprop import pickle [m0, V0, Y, Z, F, z] = pickle.load(open("epkill.p", "rb")) print m0 print V0 print Y print Z print F print z import eprop eprop.expectation_prop(m0, V0, Y, Z, F, z)
import eprop import scipy as sp from scipy import linalg as spl from scipy import stats as sps from matplotlib import pyplot as plt # basic ep test on three values m = sp.array([1.0, 2.0, 0.5]) v = sp.array([[1.0, 0.5, 0.2], [0.5, 2.0, 0.5], [0.2, 0.5, 1.0]]) n = len(m) Y = sp.array([1.0, 0.0, 1.0]) Z = sp.array([-1, 1, -1]) F = sp.array([0.25 ** 2, 0.0, 0.0]) mt, vt = eprop.expectation_prop(m, v, Y, Z, F, 35) mu, vu = eprop.gaussian_fusion(m, mt, v, vt) f, ax = plt.subplots(n, sharex=True) ns = 200 sup = sp.linspace(-6, 6, ns) for i in xrange(n): ax[i].plot(sup, sps.norm.pdf(sup, loc=m[i], scale=sp.sqrt(v[i, i])), "b") ax[i].plot(sup, sps.norm.pdf(sup, loc=mu[i], scale=sp.sqrt(vu[i, i])), "r") ax[i].twinx().plot( sup, sps.norm.cdf(Z[i] * (sup - Y[i]) / max(F[i], 1e-20)) * sps.norm.pdf(sup, loc=m[i], scale=sp.sqrt(v[i, i])), "g", ) # est on a gp
#!/usr/bin/env python2 #encoding: UTF-8 # To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. import OPTutils import scipy as sp from matplotlib import pyplot as plt import GPdc import eprop import pickle [m0,V0,Y,Z,F,z] = pickle.load(open('epkill.p','rb')) print m0 print V0 print Y print Z print F print z import eprop eprop.expectation_prop(m0,V0,Y,Z,F,z)