def verify_la_derivatives(self, lik, y, thr=1e-3): """ Laplace approximation derivatives verification. """ try: h = 1e-5 lp, dlp, d2lp, d3lp = lik.la(y, self.fmu) lp_h, dlp_h, d2lp_h, _ = lik.la(y, self.fmu + h) dlpn, d2lpn, d3lpn = (lp_h - lp) / h, (dlp_h - dlp) / h, (d2lp_h - d2lp) / h self.assertLessEqual(erelmax(dlp, dlpn), thr) self.assertLessEqual(erelmax(d2lp, d2lpn), thr) self.assertLessEqual(erelmax(d3lp, d3lpn), thr) hyp = lik.get_hyp() for i in range(len(hyp)): lp_dh, dlp_dh, d2lp_dh = lik.la(y, self.fmu, i=i) hyp[i] += h lik.set_hyp(hyp) lp_h, dlp_h, d2lp_h, _ = lik.la(y, self.fmu) hyp[i] -= h lik.set_hyp(hyp) lp_dhn, dlp_dhn = (lp_h - lp) / h, (dlp_h - dlp) / h d2lp_dhn = (d2lp_h - d2lp) / h self.assertLessEqual(erelmax(lp_dh, lp_dhn), thr) self.assertLessEqual(erelmax(dlp_dh, dlp_dhn), thr) self.assertLessEqual(erelmax(d2lp_dh, d2lp_dhn), thr) except NotImplementedError, err: print err
def verify_derivatives(self,inf,cov,mean,lik,thr): """ Compare numerical against analytical derivatives. """ n,D,X,y = self.n,self.D,self.X,self.y post,nlZ,dnlZ = inf(cov,mean,lik,X,y,deriv=True) h = 1e-5 hyp = cov.get_hyp(); dhyp = np.zeros_like(hyp) # covariance parameters for i in range(len(hyp)): hyp[i] += h; cov.set_hyp(hyp) _,nlZh = inf(cov,mean,lik,X,y) hyp[i] -= h; cov.set_hyp(hyp) dhyp[i] = (nlZh-nlZ)/h self.assertLessEqual(erel(dhyp,dnlZ.cov),thr) hyp = mean.get_hyp(); dhyp = np.zeros_like(hyp) # mean parameters for i in range(len(hyp)): hyp[i] += h; mean.set_hyp(hyp) _,nlZh = inf(cov,mean,lik,X,y) hyp[i] -= h; mean.set_hyp(hyp) dhyp[i] = (nlZh-nlZ)/h self.assertLessEqual(erel(dhyp,dnlZ.mean),thr) hyp = lik.get_hyp(); dhyp = np.zeros_like(hyp) # likelihood parameters for i in range(len(hyp)): hyp[i] += h; lik.set_hyp(hyp) _,nlZh = inf(cov,mean,lik,X,y) hyp[i] -= h; lik.set_hyp(hyp) dhyp[i] = (nlZh-nlZ)/h self.assertLessEqual(erel(dhyp,dnlZ.lik),thr)
def verify_ep_derivatives(self,lik,y,thr=1e-3): """ Expectation propagation derivatives verification. """ try: h = 1e-5 lZ,dlZ,d2lZ = lik.ep(y,self.fmu,self.fs2) lZ_h,dlZ_h,d2lZ_h = lik.ep(y,self.fmu+h,self.fs2) dlZn,d2lZn = (lZ_h-lZ)/h,(dlZ_h-dlZ)/h self.assertLessEqual(erelmax(dlZ,dlZn),thr) self.assertLessEqual(erelmax(d2lZ,d2lZn),thr) hyp = lik.get_hyp() for i in range(len(hyp)): lZ_dh = lik.ep(y,self.fmu,self.fs2,i=i) hyp[i] += h; lik.set_hyp(hyp) lZ_h,_,_ = lik.ep(y,self.fmu,self.fs2) hyp[i] -= h; lik.set_hyp(hyp) lZ_dhn = (lZ_h-lZ)/h self.assertLessEqual(erelmax(lZ_dh,lZ_dhn),thr) except NotImplementedError,err: print err except: raise
def verify_ep_derivatives(self, lik, y, thr=1e-3): """ Expectation propagation derivatives verification. """ try: h = 1e-5 lZ, dlZ, d2lZ = lik.ep(y, self.fmu, self.fs2) lZ_h, dlZ_h, d2lZ_h = lik.ep(y, self.fmu + h, self.fs2) dlZn, d2lZn = (lZ_h - lZ) / h, (dlZ_h - dlZ) / h self.assertLessEqual(erelmax(dlZ, dlZn), thr) self.assertLessEqual(erelmax(d2lZ, d2lZn), thr) hyp = lik.get_hyp() for i in range(len(hyp)): lZ_dh = lik.ep(y, self.fmu, self.fs2, i=i) hyp[i] += h lik.set_hyp(hyp) lZ_h, _, _ = lik.ep(y, self.fmu, self.fs2) hyp[i] -= h lik.set_hyp(hyp) lZ_dhn = (lZ_h - lZ) / h self.assertLessEqual(erelmax(lZ_dh, lZ_dhn), thr) except NotImplementedError, err: print err
def verify_la_derivatives(self,lik,y,thr=1e-3): """ Laplace approximation derivatives verification. """ try: h = 1e-5 lp,dlp,d2lp,d3lp = lik.la(y,self.fmu) lp_h,dlp_h,d2lp_h,_ = lik.la(y,self.fmu+h) dlpn,d2lpn,d3lpn = (lp_h-lp)/h,(dlp_h-dlp)/h,(d2lp_h-d2lp)/h self.assertLessEqual(erelmax(dlp,dlpn),thr) self.assertLessEqual(erelmax(d2lp,d2lpn),thr) self.assertLessEqual(erelmax(d3lp,d3lpn),thr) hyp = lik.get_hyp() for i in range(len(hyp)): lp_dh,dlp_dh,d2lp_dh = lik.la(y,self.fmu,i=i) hyp[i] += h; lik.set_hyp(hyp) lp_h,dlp_h,d2lp_h,_ = lik.la(y,self.fmu) hyp[i] -= h; lik.set_hyp(hyp) lp_dhn,dlp_dhn = (lp_h-lp)/h,(dlp_h-dlp)/h d2lp_dhn = (d2lp_h-d2lp)/h self.assertLessEqual(erelmax(lp_dh,lp_dhn),thr) self.assertLessEqual(erelmax(dlp_dh,dlp_dhn),thr) self.assertLessEqual(erelmax(d2lp_dh,d2lp_dhn),thr) except NotImplementedError,err: print err except: raise