def test_gp_inference_per(): # few to many observations. Less than 4 here normally crashes to due to bad covaricance matrices observations_n = range(20,50,2) number_steps =100 ripl = shortcuts.make_lite_church_prime_ripl() every_n_step=1 kl_matrix=np.zeros((len(observations_n),number_steps/every_n_step)) for n_i in range(len(observations_n)): n = observations_n[n_i] x = np.random.uniform(0,30,n) y = f_periodic(x)# + np.random.normal(0,0.1,n) ripl.clear() ripl.bind_foreign_sp("make_gp_part_der",gp.makeGPSP) ripl.assume('make_const_func', VentureFunction(covs.makeConstFunc, [t.NumberType()], covs.constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume('make_per',VentureFunction(covs.makePeriodic,[t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(covs.makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume("func_plus", covs.makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('sf','(tag (quote hyper) 0 (uniform_continuous 0 100 ))') ripl.assume('l','(tag (quote hyper) 1 (uniform_continuous 0 100 ))') ripl.assume('p','(tag (quote hyper) 2 (uniform_continuous 0.01 100 ))') ripl.assume('sigma','0.1') ripl.assume('per', "(apply_function make_per sf p l )") ripl.assume('wn', "(apply_function make_noise sigma )") ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus per wn ) ) )""") makeObservations(x,y,ripl) for steps in range(number_steps): if (steps % every_n_step )==0: xpost = np.random.uniform(33,36,1)[0] ypost = [] for i in range(100): y = ripl.sample("(gp (array " + str(xpost) + " ))") ypost.append(y) kl_matrix[n_i][steps/every_n_step]= KL_normal(np.mean(ypost),np.std(ypost),f(xpost),0.1) ripl.infer("(mh (quote hyper) one 1)") orig_cmap = matplotlib.cm.coolwarm shifted_cmap = shiftedColorMap(orig_cmap, midpoint=0.3, name='shifted') sns.heatmap(kl_matrix,cmap=shifted_cmap,yticklabels=observations_n) sns.plt.show() max_kl = kl_matrix.max() shift = 1./max_kl heavily_shifted_cmap = shiftedColorMap(orig_cmap, midpoint=shift, name='shifted') sns.heatmap(kl_matrix,cmap= heavily_shifted_cmap ,yticklabels=observations_n) sns.plt.show()
def test_gp_inference(): observations_n = range(10,100,4) number_steps =80 ripl = shortcuts.make_lite_church_prime_ripl() every_n_step=1 kl_matrix=np.zeros((len(observations_n),number_steps/every_n_step)) for n_i in range(len(observations_n)): n = observations_n[n_i] x = np.random.uniform(-3,3,n) y = f(x) + np.random.normal(0,0.1,n) ripl.clear() ripl.bind_foreign_sp("make_gp_part_der",gp.makeGPSP) ripl.assume('make_const_func', VentureFunction(covs.makeConstFunc, [t.NumberType()], covs.constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume("func_plus", covs.makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_se',VentureFunction(covs.makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(covs.makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('alpha_sf','(tag (quote hyperhyper) 0 (gamma 7 1))') ripl.assume('beta_sf','(tag (quote hyperhyper) 2 (gamma 2 0.5))') ripl.assume('alpha_l','(tag (quote hyperhyper) 1 (gamma 7 1))') ripl.assume('beta_l','(tag (quote hyperhyper) 3 (gamma 1 0.5))') ripl.assume('sf','(tag (quote hyper) 0 (gamma alpha_sf beta_sf ))') ripl.assume('l','(tag (quote hyper) 1 (gamma alpha_l beta_l ))') ripl.assume('sigma','0.1') ripl.assume('l_sigma','sigma') ripl.assume('se', "(apply_function make_se sf l )") ripl.assume('wn','(apply_function make_noise sigma )') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus se wn ) ) )""") makeObservations(x,y,ripl) for steps in range(number_steps): if (steps % every_n_step )==0: xpost = 0.5 ypost = [] for i in range(100): y = ripl.sample("(gp (array " + str(xpost) + " ))") ypost.append(y) kl_matrix[n_i][steps/every_n_step]= KL_normal(np.mean(ypost),np.std(ypost),f(xpost),0.1) ripl.infer("(do (mh (quote hyperhyper) one 2) (mh (quote hyper) one 1))") orig_cmap = matplotlib.cm.coolwarm shifted_cmap = shiftedColorMap(orig_cmap, midpoint=0.3, name='shifted') sns.heatmap(kl_matrix,cmap=shifted_cmap,yticklabels=observations_n) sns.plt.show() max_kl = kl_matrix.max() shift = 1./max_kl heavily_shifted_cmap = shiftedColorMap(orig_cmap, midpoint=shift, name='shifted') sns.heatmap(kl_matrix,cmap= heavily_shifted_cmap ,yticklabels=observations_n) sns.plt.show()
def test_gp_inference_lin(): observations_n = range(10,50,1) number_steps =50 ripl = shortcuts.make_lite_church_prime_ripl() every_n_step=1 kl_matrix=np.zeros((len(observations_n),number_steps/every_n_step)) for n_i in range(len(observations_n)): n = observations_n[n_i] x = np.random.uniform(-30,30,n) slope = 2 intercept = 5 y = slope * x + intercept + np.random.normal(0,0.1,n) ripl.clear() ripl.bind_foreign_sp("make_gp_part_der",gp.makeGPSP) ripl.assume('make_const_func', VentureFunction(covs.makeConstFunc, [t.NumberType()], covs.constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume("func_plus", covs.makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_lin',VentureFunction(covs.makeLinear,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_constant',VentureFunction(covs.makeConst,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(covs.makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('slope','(tag (quote hyper) 0 (uniform_continuous 0 100 ))') ripl.assume('intercept','(tag (quote hyper) 1 (uniform_continuous 0 100 ))') ripl.assume('sigma','0.1') ripl.assume('lin', "(apply_function make_lin slope )") ripl.assume('wn','(apply_function make_noise sigma )') ripl.assume('c','(apply_function make_constant intercept )') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus (apply_function func_plus lin c) wn) ) )""") makeObservations(x,y,ripl) for steps in range(number_steps): if (steps % every_n_step )==0: xpost = 31 ypost = [] for i in range(100): y = ripl.sample("(gp (array " + str(xpost) + " ))") ypost.append(y) kl_matrix[n_i][steps/every_n_step]= KL_normal(np.mean(ypost),np.std(ypost),slope*xpost+intercept,0.1) ripl.infer("(mh (quote hyper) one 1)") orig_cmap = matplotlib.cm.coolwarm shifted_cmap = shiftedColorMap(orig_cmap, midpoint=0.3, name='shifted') sns.heatmap(kl_matrix,cmap=shifted_cmap,yticklabels=observations_n) sns.plt.show() max_kl = kl_matrix.max() shift = 1./max_kl heavily_shifted_cmap = shiftedColorMap(orig_cmap, midpoint=shift, name='shifted') sns.heatmap(kl_matrix,cmap= heavily_shifted_cmap ,yticklabels=observations_n) sns.plt.show()