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)
from inf import * a = inf(1, lambda x: x % 3 == 0) print(a.take(10)) print(a.take(10)) print("\n") b = inf(2, 2) print(b.take(10)) print(b.take(10)) print("\n") c = inf(2, 3) print(c.take(10)) print(c.take(10))