Fixed CCD now

This commit is contained in:
Michael T Smith 2016-11-10 18:16:24 +00:00
parent ed29f048f5
commit 2e1e4bef89
2 changed files with 50 additions and 5 deletions

View file

@ -199,23 +199,30 @@ class Model(ParamzModel, Priorizable):
import paramz
transformed_points = param_points.copy()
print transformed_points.shape
#alan's original code to transform those parameters, to the true space of parameters again
#mike's change: some parameters have no transform, and thus won't be called by any of the
#iterations through m2.constraints.items(). To handle these we keep track of those parameters
#not included, and then add them (untransformed) at the end.
f = np.ones(self.size).astype(bool)
f[self.constraints[paramz.transformations.__fixed__]] = paramz.transformations.FIXED
new_t_points = []
todo = range(0,self.size)
todo = range(0,sum(f))
new_t_points = np.zeros_like(transformed_points)
print new_t_points.shape
for c, ind in self.constraints.items():
print c,ind
if c != paramz.transformations.__fixed__:
new_t_points[:,ind] = (c.f(transformed_points[:, ind[f[ind]]]))
todo.remove(ind)
for i in ind[f[ind]]:
new_t_points[:,i] = (c.f(transformed_points[:, i]))
print i
todo.remove(i)
for param_ind in todo:
new_t_points[:,param_ind] = transformed_points[:, param_ind]
print todo
for i in todo:
print i
new_t_points[:,i] = transformed_points[:, i]
return new_t_points, point_densities

38
GPy/testing/ccd_test.py Normal file
View file

@ -0,0 +1,38 @@
import numpy as np
import matplotlib.pyplot as plt
import GPy
%matplotlib inline
np.set_printoptions(suppress=True, precision=10)
from paramz.transformations import Logexp
class SimpleModel(GPy.Model):
def __init__(self, name):
super(SimpleModel, self).__init__(name)
self.params = []
self.peak = np.array([1,2])
for i,pos in enumerate(self.peak):
p = GPy.Param('param%d' % i, 1.0)
self.params.append(p)
self.link_parameter(p)
def log_likelihood(self):
like = 0
for i,pos in enumerate(self.peak):
like -= ((self.params[i])-pos)**2
return like
def parameters_changed(self):
for i,pos in enumerate(self.peak):
self.params[i].gradient = -2*((self.params[i])-pos)
m2 = SimpleModel('simple')
m2.optimize()
assert np.all(np.isclose(m2.numerical_parameter_hessian(),np.array([[2,0],[0,2]]))), "Numerical approximation to Hessian doesn't match. Error in numerical_parameter_hessian()."
assert np.isclose(m2.param0,1,atol=0.01), "Failed to find likelihood maximum of test model's param0 while testing CCD"
assert np.isclose(m2.param1,2,atol=0.01), "Failed to find likelihood maximum of test model's param1 while testing CCD"
ccdpos,ccdres = m2.CCD()
m2.optimize()
assert np.all(np.isclose(np.sum((ccdpos-np.array([1,2]))**2,1)[1:],1.21,atol=0.01)), "CCD placement error - should be 1.21 from mode"
s