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 make_ripl(): ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp('make_gp_part_der', gp_der.makeGPSP) ripl.bind_foreign_sp('gpmem', gpmem.gpmemSP) ripl.bind_foreign_sp('argmax_of_array', argmaxSP) ripl.bind_foreign_sp('abs', absSP) return ripl
def test_gp_inference_known_example(): r''' Inference Quality Smoke Test -------------------------- We take a simple data generating process with a clear structure: :math:`f(x)= 2x + 5 \sin 3x + \eta` with :math:`\eta \sim \mathcal{N}(0,1)`. We learn the hyper-parameters for K = LIN + PER + WN and extrapolate. We perform ad-hoc tests (as statistical tests for an extrapolation seem to be too conservative). We use 100 data points for training data. ''' ripl = shortcuts.make_lite_church_prime_ripl() n = 100 x = np.random.uniform(0,20,n) y = f_LIN_PER_WN(x) + np.random.normal(0,1,n) ripl.bind_foreign_sp("make_gp_part_der",gp_w_der.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_per',VentureFunction(covs.makePeriodic,[t.NumberType(),t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_linear',VentureFunction(covs.makeLinear,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(covs.makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('sf','(tag (quote hyper) 0 (uniform_continuous 0 10))') ripl.assume('p','(tag (quote hyper) 1 (uniform_continuous 0 10))') ripl.assume('l','(tag (quote hyper) 2 (uniform_continuous 0 10))') ripl.assume('s','(tag (quote hyper) 3 (uniform_continuous 0 10))') ripl.assume('sigma','(tag (quote hyper) 4 (uniform_continuous 0 2 ))') ripl.assume('per', "(apply_function make_per sf p l )") ripl.assume('wn','(apply_function make_noise sigma )') ripl.assume('lin','(apply_function make_linear s )') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus lin (apply_function func_plus per wn ) )) )""") makeObservations(x,y,ripl) ripl.infer("(mh (quote hyper) one 100)") xpost = 25 # were extrapolating quite far out. ypost = [] for i in range(500): y = ripl.sample("(gp (array " + str(xpost) + " ))") ypost.append(y) # ad-hoc tests assert(np.std(ypost)<2.5) assert(np.std(ypost)>0.5) assert(abs(np.mean(ypost)-f_LIN_PER_WN(xpost))<=3)
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()
def make_ripl(): ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_der.makeGPSP) ripl.bind_foreign_sp('gpmem', gpmem.gpmemSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) return ripl
def init_gp_ripl(): ''' initialize the ripl for experiment intilaize zero mean functions. Other mean functions are not supported at the moment. All mean functions that I am aware of at the moment, can be absorbed into covariance functions anyway - so there's no real reason to support mean functions... ''' ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp.makeGPSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") return ripl
def init_gp_ripl(): ''' initialize the ripl for test intilaize zero mean functions. ''' ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp.makeGPSP) ripl.assume('make_const_func', VentureFunctionDiff(covs.makeConstFunc, [t.NumberType()], covs.constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.bind_foreign_sp("apply_diff_function",applyDiffFunctionSP) return ripl
def test_gp_inference_uniform(): observations_n = range(10,40) number_steps =6666660 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,1,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('sf','(tag (quote hyper) 0 (uniform_continuous 0 100 ))') ripl.assume('l','(tag (quote hyper) 1 (uniform_continuous 0 100 ))') 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): ripl.infer("(mh (quote hyper) one 1)") 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) sns.heatmap(kl_matrix,cmap="coolwarm",yticklabels=observations_n) sns.plt.show()
def doit(num_points, num_frames, show_pics): ripl = shortcuts.make_lite_church_prime_ripl() ripl.execute_program(model) makeObserves(ripl, num_points) if show_pics: plt.ion() x_l = list() y_l = list() for ni in range(num_points): dpoint = ripl.sample('(get_datapoint %d)' % ni) x_l.append(dpoint[0]) y_l.append(dpoint[1]) for ni in range(num_frames): do_infer(ripl, 1) clusters = collect_clusters(ripl, num_points) if show_pics: print "Cluster means:", clusters['mean'] do_plot(x_l, y_l, clusters, show_pics=show_pics)
def get_trained_proposal_program(proposal_program, target_ripl, c_list, t_list, num_training_samples): from venture.shortcuts import make_lite_church_prime_ripl u = make_lite_church_prime_ripl() src = "" for did in target_ripl.directive_id_to_stringable_instruction.keys(): directive = target_ripl.get_directive(did) # print directive if directive["instruction"] == "observe": directive.pop("value") directive["instruction"] = "predict" target_ripl.directive_id_to_stringable_instruction[did] = directive src = src + target_ripl.get_text(did)[1] training_samples = [] for i in range(num_training_samples): u.execute_program(src) trace = u.sivm.core_sivm.engine.getDistinguishedTrace() conditioned = [] for scope_block in c_list: scope, block = scope_block[0], scope_block[1] for node in trace.scopes[scope][block]: assert isinstance(node,OutputNode) conditioned.append(trace.groundValueAt(node).number) target = [] for scope_block in t_list: scope, block = scope_block[0], scope_block[1] for node in trace.scopes[scope][block]: assert isinstance(node,OutputNode) target.append(trace.groundValueAt(node).number) proposal_program.ripl.execute_program(proposal_program.gen_train_src(conditioned,target)) u.clear() # print '=====================' # print conditioned # print target # print [("weight %d %d"%(k,j), proposal_program.ripl.sample("(get_weight %d %d)"%(k,j))) for k in range(8) for j in range(3)] # print [("noise %d"%k, proposal_program.ripl.sample("(get_noise %d)"%k)) for k in range(8)] return proposal_program
def plot_hyper(n_iteration,dir_name): assert(os.path.exists("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name)) if not os.path.exists("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name): os.makedirs("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name) if not os.path.isfile("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name+"/after_parameters_"+str(n_iteration)): x = np.load("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name+"/data/x_s.npy") y = np.load("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name+"/data/y_s.npy") ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der", gp_der.makeGPSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") # ripl.infer('(resample '+ particles +' )') ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_se', VentureFunction(makeSquaredExponential, [t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise', VentureFunction(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 1 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('sf_l', '(log sf)') ripl.assume('l_l', '(log l)') ripl.assume('sigma', '(tag (quote hyper) 2 (uniform_continuous 0 5 ))') ripl.assume('l_sigma', '(log sigma)') ripl.assume('se', "(apply_function make_se sf_l l_l )") ripl.assume('wn', '(apply_function make_noise l_sigma )') ripl.assume('gp', """(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus se wn ) ) )""") makeObservations(x, y, ripl) rd = ripl.infer("(collect sf l sigma alpha_l beta_l alpha_sf beta_sf)") df_parameter_before = rd.asPandas() df_parameter_before.to_pickle("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name+"/before_parameters_"+str(n_iteration)) ripl.infer("(repeat 500 (do (mh (quote hyperhyper) one 2) (mh (quote hyper) one 1)))") # ripl.infer("(mh (quote hyper) one 100)") rd = ripl.infer("(collect sf l sigma alpha_l beta_l alpha_sf beta_sf)") df_parameter_after = rd.asPandas() df_parameter_after.to_pickle("/home/ulli/Dropbox/gpmemplots/syndata/"+dir_name+"/after_parameters_"+str(n_iteration))
def run_custommh(method,model,n_train,n_sample): v = make_lite_church_prime_ripl() target_src = """ [assume get_state (mem (lambda (t) (scope_include (quote state) t (truncated_normal -1 1 (if (= t 0) 0 (get_state (- t 1))) (if (= t 0) 1 0.1)))))] [assume get_datapoint (lambda (t) (scope_include (quote data) t (normal (get_state t) 0.1)))] """ v.execute_program(target_src) v.observe("(get_datapoint 0)", 0) v.observe("(get_datapoint 1)", 1) v.observe("(get_datapoint 2)", 0) v.observe("(get_datapoint 3)", 1) v.observe("(get_datapoint 4)", 0) v.observe("(get_datapoint 5)", 1) v.observe("(get_datapoint 6)", 0) v.observe("(get_datapoint 7)", 1) class ProposalProgram(object): def __init__(self, src, clist, tlist): self.src = src self.clist = clist self.tlist = tlist self.n_conditioned = len(clist) self.n_target = len(tlist) def gen_initialization_src(self): return "" def gen_train_src(self, cvals, tvals): return "" def gen_conditioned_src(self, cvals): return "" def gen_latent_src(self, cvals): return "" def gen_target_src(self, cvals): return "" class LinearRegressionProposalProgram(ProposalProgram): def __init__(self, clist, tlist): ''' A Linear Regression Model assuming weights independently have flat gaussian priors. The weights are trained using map operator in an online fashion ''' src = \ ''' [assume get_weight (mem (lambda (yid xid) (scope_include (quote weight) yid (normal 0 10))))] [assume make_weight_of_yid (lambda (yid) (lambda (xid) (get_weight yid xid)))] [assume dot (lambda (a get_b id) (if (= (size a) 0) 0 (+ (* (first a) (get_b id)) (dot (rest a) get_b (+ id 1)))))] [assume get_noise (mem (lambda (yid) (scope_include (quote hyper) yid (gamma 1.0 1.0))))] [assume get_y (lambda (yid x) (normal (dot x (make_weight_of_yid yid) 0) (get_noise yid)))] ''' for i in range(len(tlist)): for j in range(len(clist)+i+1): src = src + "[predict (get_weight %d %d)]"%(i,j) src = src + "[force (get_weight %d %d) 0]"%(i,j) src = src + "[predict (get_noise %d)]"%i src = src + "[force (get_noise %d) 0.8]"%i ProposalProgram.__init__(self,src,clist,tlist) self.datacount = 1 def gen_train_src(self, cvals, tvals): assert len(cvals) == self.n_conditioned assert len(tvals) == self.n_target x_str = "(list " + str(" ").join(map(lambda x: str(x), cvals)) train_src = "" for i in range(self.n_target): train_src = train_src + "train%d_%d:[observe (get_y %d %s) %f]"%(self.datacount,i,i,x_str + " 1)",tvals[i]) x_str = x_str + " " + str(tvals[i]) if self.datacount%10 == 0: train_src = train_src + """[infer {"kernel":"map", "scope":"weight", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.5/(self.datacount+10.0)) train_src = train_src + """[infer {"kernel":"map", "scope":"hyper", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.1/(self.datacount+10.0)) for k in range(10): train_src = train_src + "[forget train%d_%d]"%(self.datacount-k,i) self.datacount = self.datacount + 1 # print train_src return train_src def gen_target_src(self, cvals): x_str = "(list " + str(" ").join(map(lambda x: str(x), cvals)) target_src = "" for i in range(self.n_target): target_src = target_src + "target%d:[predict (get_y %d %s)]"%(i,i,x_str + " 1)") x_str = x_str + " target_value%d"%i return target_src class LinearRegressionForwardProposalProgram(ProposalProgram): def __init__(self, clist, tlist): ''' A Linear Regression Model specifically designed for HMM targets, using forward sampling, exploiting the conditional independency structure, assuming weights independently have flat gaussian priors. The weights are trained using map operator in an online fashion ''' src = \ ''' [assume get_weight (mem (lambda (yid xid) (scope_include (quote weight) yid (normal 0 10))))] [assume make_weight_of_yid (lambda (yid) (lambda (xid) (get_weight yid xid)))] [assume dot (lambda (a get_b id) (if (= (size a) 0) 0 (+ (* (first a) (get_b id)) (dot (rest a) get_b (+ id 1)))))] [assume get_noise (mem (lambda (yid) (scope_include (quote hyper) yid (gamma 1.0 1.0))))] [assume get_y (lambda (yid x) (normal (dot x (make_weight_of_yid yid) 0) (get_noise yid)))] ''' for i in range(len(tlist)): for j in range(3): src = src + "[predict (get_weight %d %d)]"%(i,j) src = src + "[force (get_weight %d %d) 0]"%(i,j) src = src + "[predict (get_noise %d)]"%i src = src + "[force (get_noise %d) 0.8]"%i ProposalProgram.__init__(self,src,clist,tlist) self.datacount = 1 def gen_train_src(self, cvals, tvals): assert len(cvals) == self.n_conditioned assert len(tvals) == self.n_target train_src = "" for i in range(self.n_target): x_str = "(list %f"%cvals[i] if i > 0: x_str = x_str + " %f"%tvals[i-1] train_src = train_src + "train%d_%d:[observe (get_y %d %s) %f]"%(self.datacount,i,i,x_str + " 1)",tvals[i]) if self.datacount%10 == 0: train_src = train_src + """[infer {"kernel":"map", "scope":"weight", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.5/(self.datacount+10.0)) train_src = train_src + """[infer {"kernel":"map", "scope":"hyper", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.1/(self.datacount+10.0)) for k in range(10): train_src = train_src + "[forget train%d_%d]"%(self.datacount-k,i) self.datacount = self.datacount + 1 # print train_src return train_src def gen_target_src(self, cvals): target_src = "" for i in range(self.n_target): x_str = "(list %f"%cvals[i] if i > 0: x_str = x_str + " target_value%d"%(i-1) target_src = target_src + "target%d:[predict (get_y %d %s)]"%(i,i,x_str + " 1)") return target_src class LinearRegressionForwardBackwardProposalProgram(ProposalProgram): def __init__(self, clist, tlist): ''' A Linear Regression Model specifically designed for HMM targets, using forward-backward algorithm, exploiting the conditional independency structure, assuming weights independently have flat gaussian priors. The weights are trained using map operator in an online fashion ''' src = \ ''' [assume get_weight (mem (lambda (yid xid) (scope_include (quote weight) yid (normal 0 10))))] [assume make_weight_of_yid (lambda (yid) (lambda (xid) (get_weight yid xid)))] [assume dot (lambda (a get_b id) (if (= (size a) 0) 0 (+ (* (first a) (get_b id)) (dot (rest a) get_b (+ id 1)))))] [assume get_noise (mem (lambda (yid) (scope_include (quote hyper) yid (gamma 1.0 1.0))))] [assume get_y (lambda (yid x) (normal (dot x (make_weight_of_yid yid) 0) (get_noise yid)))] ''' for i in range(len(tlist)): for j in range(3): src = src + "[predict (get_weight %d %d)]"%(i,j) src = src + "[force (get_weight %d %d) 0]"%(i,j) src = src + "[predict (get_noise %d)]"%i src = src + "[force (get_noise %d) 0.8]"%i ProposalProgram.__init__(self,src,clist,tlist) self.datacount = 1 def gen_train_src(self, cvals, tvals): assert len(cvals) == self.n_conditioned assert len(tvals) == self.n_target train_src = "" for i in range(self.n_target): x_str = "(list %f"%cvals[i] if i > 0: x_str = x_str + " %f"%tvals[i-1] train_src = train_src + "train%d_%d:[observe (get_y %d %s) %f]"%(self.datacount,i,i,x_str + " 1)",tvals[i]) if self.datacount%10 == 0: train_src = train_src + """[infer {"kernel":"map", "scope":"weight", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.5/(self.datacount+10.0)) train_src = train_src + """[infer {"kernel":"map", "scope":"hyper", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i,0.1/(self.datacount+10.0)) for k in range(10): train_src = train_src + "[forget train%d_%d]"%(self.datacount-k,i) for i in reversed(range(self.n_target-1)): x_str = "(list %f %f 1)"%(cvals[i],tvals[i+1]) train_src = train_src + "train%d_%d:[observe (get_y %d %s) %f]"%(self.datacount,i+self.n_target,i+self.n_target,x_str,tvals[i]) if self.datacount%10 == 0: train_src = train_src + """[infer {"kernel":"map", "scope":"weight", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i+self.n_target,0.5/(self.datacount+10.0)) train_src = train_src + """[infer {"kernel":"map", "scope":"hyper", "block":%d, "rate":%f, "steps":1, "transition":1}]"""%(i+self.n_target,0.1/(self.datacount+10.0)) for k in range(10): train_src = train_src + "[forget train%d_%d]"%(self.datacount-k,i+self.n_target) self.datacount = self.datacount + 1 # print train_src return train_src def gen_latent_src(self, cvals): latent_src = "" for i in range(self.n_target): x_str = "(list %f"%cvals[i] if i > 0: x_str = x_str + " latent_value%d"%(i-1) latent_src = latent_src + "latent%d:[predict (get_y %d %s)]"%(i,i,x_str + " 1)") return latent_src def gen_target_src(self, cvals): target_src = "" for i in reversed(range(self.n_target-1)): x_str = "(list %f %f 1)"%(cvals[i],tvals[i+1]) target_src = target_src + "target%d:[predict (get_y %d %s)]"%(i+self.,i,x_str + " 1)") return target_src class DPBernoulliMixtureProposalProgram(ProposalProgram): def __init__(self, clist, tlist): """ A Dirichlet Process Mixture of Bernoulli Model where each cluster has its own head probability per dimension, assuming dimensions are independent given cluster assignment. """ src = \ """ [assume alpha (scope_include (quote hyper) 0 (gamma 1.0 1.0))] [assume b (scope_include (quote hyper) 1 (gamma 1.0 1.0))] [assume crp (make_crp alpha)] [assume make_coin (mem (lambda (cluster dim) (scope_include (quote coinweight) cluster (make_beta_bernoulli b b))))] [assume get_cluster (mem (lambda (i) (scope_include (quote cluster) i (crp))))] [assume get_datapoint (mem (lambda (i dim) ((make_coin (get_cluster i) dim))))] [assume get_latent (lambda (dim) (make_coin (get_cluster -1) dim))] [assume get_target (lambda (dim) (get_datapoint -1 dim))] """ ProposalProgram.__init__(self,src,clist,tlist) self.datacount = 0 def gen_train_src(self, cvals, tvals): assert len(cvals) == self.n_conditioned assert len(tvals) == self.n_target dim_id = 0 train_src = "" for val in cvals: train_src = train_src + "[observe (get_datapoint %d %d) %f]"%(self.datacount,dim_id,val) dim_id = dim_id + 1 for val in tvals: train_src = train_src + "[observe (get_datapoint %d %d) %f]"%(self.datacount,dim_id,val) dim_id = dim_id + 1 train_src = train_src + """[infer {"kernel":"mh", "scope":"cluster", "block":"one", "transitions":10}]""" train_src = train_src + """[infer {"kernel":"mh", "scope":"hyper", "block":"one", "transitions":1}]""" self.datacount = self.datacount + 1 return train_src def gen_conditioned_src(self, cvals): conditioned_src = "" dim_id = 0 for val in cvals: conditioned_src = conditioned_src + "cond%d: [observe (get_datapoint -1 %d) %f]"%(dim_id,dim_id,val) dim_id = dim_id + 1 conditioned_src = conditioned_src + """[infer {"kernel":"mh", "scope":"cluster", "block":"one", "transition":"10"}]""" return conditioned_src def gen_latent_src(self, cvals): latent_src = "" for dim_id in range(self.n_conditioned, self.n_conditioned+self.n_target): latent_src = latent_src + "latent%d: [predict (make_coin (get_cluster -1) %d)]"%(dim_id,dim_id) return latent_src def gen_target_src(self, cvals): target_src = "" for dim_id in range(self.n_conditioned, self.n_conditioned+self.n_target): target_src = target_src + "target%d: [predict (get_datapoint -1 %d)]"%(dim_id,dim_id) return target_src if model[0] == 'linreg': v.register_proposal_program_class("LinearRegressionProposalProgram", LinearRegressionProposalProgram) proposal_src = """ [declare { "name":"mycustommh", "class":"LinearRegressionProposalProgram", "conditioned":[["data",0],["data",1],["data",2],["data",3],["data",4],["data",5],["data",6],["data",7]], "target":[["state",0],["state",1],["state",2],["state",3],["state",4],["state",5],["state",6],["state",7]], "num_samples":%d}] """%n_train elif model[0] == 'linreg-forward': v.register_proposal_program_class("LinearRegressionForwardProposalProgram", LinearRegressionForwardProposalProgram) proposal_src = """ [declare { "name":"mycustommh", "class":"LinearRegressionForwardProposalProgram", "conditioned":[["data",0],["data",1],["data",2],["data",3],["data",4],["data",5],["data",6],["data",7]], "target":[["state",0],["state",1],["state",2],["state",3],["state",4],["state",5],["state",6],["state",7]], "num_samples":%d}] """%n_train elif model[0] == 'linreg-forwardbackward': v.register_proposal_program_class("LinearRegressionForwardBackwardProposalProgram", LinearRegressionForwardBackwardProposalProgram) proposal_src = """ [declare { "name":"mycustommh", "class":"LinearRegressionForwardBackwardProposalProgram", "conditioned":[["data",0],["data",1],["data",2],["data",3],["data",4],["data",5],["data",6],["data",7]], "target":[["state",0],["state",1],["state",2],["state",3],["state",4],["state",5],["state",6],["state",7]], "num_samples":%d}] """%n_train elif model[0] == 'dp-bernoulli': v.register_proposal_program_class("DPBernoulliMixtureProposalProgram", DPBernoulliMixtureProposalProgram) proposal_src = """ [declare { "name":"mycustommh", "class":"DPBernoulliMixtureProposalProgram", "conditioned":[["data",0],["data",1],["data",2],["data",3],["data",4],["data",5],["data",6],["data",7]], "target":[["state",0],["state",1],["state",2],["state",3],["state",4],["state",5],["state",6],["state",7]], "num_samples":%d}] """%n_train else: raise(Exception("Model '%s' not recognized."%model[0])) if method[0] != 'default': v.execute_program(proposal_src) print proposal_src if method[0] == 'mc': infer_src = """ [infer {"kernel":"custommh", "program":"mycustommh", "method":"mc","mc_samples":50, "transitions":1}]""" elif method[0] == 'aux': infer_src = """ [infer {"kernel":"custommh", "program":"mycustommh", "method":"aux", "transitions":1}]""" elif method[0] == 'assumed_gibbs': infer_src = """[infer {"kernel":"custommh", "program":"mycustommh", "method":"assumed_gibbs", "transitions":1}]""" elif method[0] == 'default': infer_src = """ [infer {"kernel":"mh", "scope":"state", "block":"one", "transitions":1}]""" else: raise(Exception("Method '%s' not recognized."%method[0])) samples = [] for i in range(n_sample): v.execute_program(infer_src) samples.append([v.sample("(get_state 0)"), v.sample("(get_state 1)"), v.sample("(get_state 2)"), v.sample("(get_state 3)"), v.sample("(get_state 4)"), v.sample("(get_state 5)"), v.sample("(get_state 6)"), v.sample("(get_state 7)")]) # print [v.sample("(get_state 0)"), v.sample("(get_state 1)"), v.sample("(get_state 2)"), v.sample("(get_state 3)"), v.sample("(get_state 4)"), v.sample("(get_state 5)"), v.sample("(get_state 6)"), v.sample("(get_state 7)")] return samples
def test_gp_inference_smooth(): observations_n = range(10,100,10) 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.linspace(0,50,n) y = f_sqrt(x) + np.random.normal(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 1 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): ripl.infer("(do (mh (quote hyperhyper) one 2) (mh (quote hyper) one 1))") if (steps % every_n_step )==0: xpost = 51 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) sns.heatmap(kl_matrix,cmap="coolwarm",yticklabels=observations_n) sns.plt.show() cleaned_matrix = [] print(kl_matrix.max()) print(kl_matrix.min()) for n_i in range(len(observations_n)): if kl_matrix[n_i,:].max()<5000: cleaned_matrix.append( kl_matrix[n_i,:]) print(cleaned_matrix) sns.heatmap(cleaned_matrix,cmap="coolwarm") sns.plt.show()
# Venture is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Venture. If not, see <http://www.gnu.org/licenses/>. import numpy as np from venture import shortcuts as s from venture.server import RiplRestServer import venture.lite.gp as gp import venture.lite.value as v ripl = s.make_lite_church_prime_ripl() program = """ [assume mu (normal 0 5)] [assume mean (gp_mean_const mu)] ; [assume a (inv_gamma 2 5)] [assume a 1] ; [assume l (inv_gamma 5 50)] ; [assume l (uniform_continuous 10 100)] [assume l 10] ; [assume cov ((if (flip) gp_cov_sum gp_cov_product) (gp_cov_scale a (gp_cov_se (/ (* l l) 2.))) (gp_cov_linear (normal 0 10)))] ; [assume noise (inv_gamma 3 1)] [assume noise 0.1]
return xs[i] PlotData = collections.namedtuple('PlotData', ['sf1', 'l1', 'Xseen', 'Yseen']) def get_plot_data(ripl): sf1 = ripl.sample('sf1') l1 = ripl.sample('l1') stats = ripl.infer('(extract_stats gp)') assert len(stats[1]) > 0 (xbest, ybest) = stats[0] all_pairs = stats[1] (Xseen, Yseen) = zip(*all_pairs) return PlotData(sf1, l1, Xseen, Yseen) if __name__ == '__main__': ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_der.makeGPSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('sf1','(tag (quote hyper) 0 (log (uniform_continuous 0 10)))') ripl.assume('l1','(tag (quote hyper) 1 (log (uniform_continuous 0 10)))') ripl.assume('se', "(apply_function make_se sf1 l1 )") ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero se ))""")
def init_gp_ripl(): ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_with_der.makeGPSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") return ripl
#!/usr/bin/env python -i # Copyright (c) 2013, 2014 MIT Probabilistic Computing Project. # # This file is part of Venture. # # Venture is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Venture is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Venture. If not, see <http://www.gnu.org/licenses/>. from venture import shortcuts ripl = shortcuts.make_lite_church_prime_ripl() print "Venture RIPL handle in `ripl' variable"
import numpy as np def const(c): def f(x1, x2): return c return f def squared_exponential(a, l): def f(x1, x2): x = (x1-x2)/l return a * np.exp(- np.dot(x, x)) return f from venture import shortcuts as s ripl = s.make_lite_church_prime_ripl() from venture.lite.function import VentureFunction from venture.lite.sp import SPType import venture.lite.value as v import venture.lite.types as t fType = t.AnyType("VentureFunction") xType = t.NumberType() oType = t.NumberType() kernelType = SPType([xType, xType], oType)
def run_cgm_experiment(patient_number): mat_contents =scio.loadmat("../data/glucose"+patient_number) X = mat_contents['Glucose'] X = X[np.nonzero(X)] ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_der.makeGPSP) ripl.bind_foreign_sp("covfunc_interpreter",typed_nr(GrammarInterpreter(), [t.AnyType()], t.AnyType())) ripl.bind_foreign_sp("subset",typed_nr(Subset(), [t.ListType(),t.SimplexType()], t.ListType())) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume("func_times", makeLiftedMult(lambda x1, x2: np.multiply(x1,x2))) ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_linear', VentureFunction(makeLinear, [t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_periodic', VentureFunction(makePeriodic, [t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_rq',VentureFunction(makeRQ, [t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('a','(tag (quote parameter) 0 (log (uniform_continuous 0 5)))') ripl.assume('sf1','(tag (quote parameter) 1 (log (uniform_continuous 0 5 )))') ripl.assume('p',' (tag (quote parameter) 2 (log (uniform_continuous 0.01 5)))') ripl.assume('l',' (tag (quote parameter) 3 (log (uniform_continuous 0 5)))') ripl.assume('l1',' (tag (quote parameter) 4 (log (uniform_continuous 0 5)))') ripl.assume('sf_rq','(tag (quote parameter) 5 (log (uniform_continuous 0 5)))') ripl.assume('l_rq','(tag (quote parameter) 6 (log (uniform_continuous 0 5)))') ripl.assume('alpha','(tag (quote parameter)7 (log (uniform_continuous 0 5)))') ripl.assume('sf',' (tag (quote parameter) 8 (log (uniform_continuous 0 5)))') ripl.assume('lin1', "(apply_function make_linear a )") ripl.assume('per1', "(apply_function make_periodic l p sf ) ") ripl.assume('se1', "(apply_function make_se sf1 l1)") ripl.assume('rq', "(apply_function make_rq l_rq sf_rq alpha)") ripl.assume('interp','covfunc_interpreter') # Grammar ripl.assume('cov_compo',""" (lambda (l ) (if (lte ( size l) 1) (first l) (if (flip) (apply_function func_plus (first l) (cov_compo (rest l))) (apply_function func_times (first l) (cov_compo (rest l))) ) )) """) number = 4 total_perms =0 perms = [] for i in range(number): perms.append((len(list(itertools.permutations([j for j in range(i+1)]))))) total_perms+=perms[i] simplex = "( simplex " for i in range(number): simplex+=str(float(perms[i])/total_perms) + " " simplex+=" )" #print(' (tag (quote grammar) 0 (subset (list lin1 per1 se1 se2 rq) '+simplex + ' ))') ripl.assume('s',' (tag (quote hyper) 0 (subset (list lin1 per1 se1 rq) '+simplex + ' ))') ripl.assume('cov','(tag (quote hyper) 1 (cov_compo s))') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero cov))""") makeObservations([i for i in range(X.shape[0])],X,ripl) ripl.infer("(repeat "+steps+" (do (mh (quote hyper) one 1) (mh (quote parameter) one 5)))") # prediction fig = plt.figure(figsize=(figlength,figheigth), dpi=200) for i in range(number_curves): xpost= np.random.uniform(0,X.shape[0]+5,200) sampleString=genSamples(xpost,ripl) ypost = ripl.sample(sampleString) yp = [y_temp for (x_temp,y_temp) in sorted(zip(xpost,ypost))] pl.plot(sorted(xpost),yp,c="red",alpha=alpha_value,linewidth=2) #pl.locator_params(nbins=4) #plt.axis((-2,2,-1,3)) #pl.plot(X,color='blue') pl.scatter(range(X.shape[0]),X,color='black',marker='x',s=50,edgecolor='blue',linewidth='1.5') non_zero_index = np.nonzero(X) X = X[non_zero_index] time_stamps = get_time_stamps(patient) time_stamps=[time_stamps[i] for i in non_zero_index[0]] pl.xlabel('Time',fontsize=font_size) pl.ylabel('mg/dL',fontsize=font_size) pl.xticks(fontsize=font_size) pl.yticks(fontsize=font_size) ax = pl.gca() ax.set_xlim([0, X.shape[0]+5]) ax.set_ylim([np.min(X)-10,np.max(X)+10]) ticks = ax.get_xticks() time_stamps[0]='' ax.set_xticklabels([time_stamps[int(i)] for i in ticks[:-1]]) fig.savefig(path+'posterior_samples_patient_'+patient_number+'_'+str(number_predictive_samples)+'_'+str(number_curves)+'a_'+str(alpha_value)+'_'+exp_run+'.png', dpi=fig.dpi,bbox_inches='tight')
def plot_hyper(n_it): x = np.random.normal(0,1,n) y=np.zeros(x.shape) for i in range(n): if random.random()>0.3: y[i] = f(x[i]) + np.random.normal(0,0.1,1) else: y[i] = f(x[i]) + np.random.normal(0,outlier_sigma,1) ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_der.makeGPSP) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.infer('(resample '+ particles +' )') ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_noise',VentureFunction(makeNoise,[t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('sf','(tag (quote hyper) 0 (log (uniform_continuous 0 10 )))') ripl.assume('l','(tag (quote hyper) 1 (log (uniform_continuous 0 10 )))') ripl.assume('sigma','(tag (quote hyper) 2 (uniform_continuous 0 5 ))') ripl.assume('l_sigma','(log sigma)') ripl.assume('se', "(apply_function make_se sf l )") ripl.assume('wn','(apply_function make_noise sigma )') ds = ripl.infer('(collect sf l sigma)') df = ds.asPandas() df['Hyper-Parameter Learning']= pd.Series(['before' for _ in range(len(df.index))], index=df.index) df_before =df ripl.assume('t_dist','(lambda (i) (student_t 4))') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero (apply_function func_plus se wn ) ) )""") makeObservations(x,y,ripl) ripl.infer("(mh (quote hyper) one 200)") ds = ripl.infer('(collect sf l sigma)') df = ds.asPandas() df['Hyper-Parameter Learning']= pd.Series(['after' for _ in range(len(df.index))], index=df.index) plt.figure(n_it) sns.plt.yticks([]) sns.distplot(df_before['sigma']) sns.plt.xlabel(" ") plt.savefig('/home/ulli/Dropbox/gpmemplots/neal_unif_sigma_before_'+no+str(n_it)+'.png', dpi=200,bbox_inches='tight') plt.figure(n_it+100) sns.distplot(df['sigma']) sns.plt.yticks([]) sns.plt.xlabel(" ") plt.savefig('/home/ulli/Dropbox/gpmemplots/neal_unif_sigma_after_'+no+str(n_it)+'.png', dpi=200,bbox_inches='tight')
def run_cgm_experiment(patient_number,exp_index): if not glob.glob('/home/ulli/Dropbox/cgm/patient_structure_'+patient_number+'/*_'+str(exp_index)): mat_contents =scio.loadmat("../data/glucose"+patient_number) X = mat_contents['Glucose'] X = X[np.nonzero(X)] if not os.path.exists('/home/ulli/Dropbox/cgm/patient_structure_'+patient_number+'/'): os.makedirs('/home/ulli/Dropbox/cgm/patient_structure_'+patient_number+'/') ripl = shortcuts.make_lite_church_prime_ripl() ripl.bind_foreign_sp("make_gp_part_der",gp_der.makeGPSP) ripl.bind_foreign_sp("covfunc_interpreter",typed_nr(GrammarInterpreter(), [t.AnyType()], t.AnyType())) ripl.bind_foreign_sp("subset",typed_nr(Subset(), [t.ListType(),t.SimplexType()], t.ListType())) ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType)) ripl.assume('zero', "(apply_function make_const_func 0)") ripl.assume("func_times", makeLiftedMult(lambda x1, x2: np.multiply(x1,x2))) ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2)) ripl.assume('make_linear', VentureFunction(makeLinear, [t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_periodic', VentureFunction(makePeriodic, [t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('make_rq',VentureFunction(makeRQ, [t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction"))) ripl.assume('a','(tag (quote parameter) 0 (log (uniform_continuous 0 5)))') ripl.assume('sf1','(tag (quote parameter) 1 (log (uniform_continuous 0 5 )))') ripl.assume('p',' (tag (quote parameter) 2 (log (uniform_continuous 0.01 5)))') ripl.assume('l',' (tag (quote parameter) 3 (log (uniform_continuous 0 5)))') ripl.assume('l1',' (tag (quote parameter) 4 (log (uniform_continuous 0 5)))') ripl.assume('sf_rq','(tag (quote parameter) 5 (log (uniform_continuous 0 5)))') ripl.assume('l_rq','(tag (quote parameter) 6 (log (uniform_continuous 0 5)))') ripl.assume('alpha','(tag (quote parameter)7 (log (uniform_continuous 0 5)))') ripl.assume('sf',' (tag (quote parameter) 8 (log (uniform_continuous 0 5)))') ripl.assume('lin1', "(apply_function make_linear a )") ripl.assume('per1', "(apply_function make_periodic l p sf ) ") ripl.assume('se1', "(apply_function make_se sf1 l1)") ripl.assume('rq', "(apply_function make_rq l_rq sf_rq alpha)") ripl.assume('interp','covfunc_interpreter') # Grammar ripl.assume('cov_compo',""" (lambda (l ) (if (lte ( size l) 1) (first l) (if (flip) (apply_function func_plus (first l) (cov_compo (rest l))) (apply_function func_times (first l) (cov_compo (rest l))) ) )) """) number = 4 total_perms =0 perms = [] for i in range(number): perms.append((len(list(itertools.permutations([j for j in range(i+1)]))))) total_perms+=perms[i] simplex = "( simplex " for i in range(number): simplex+=str(float(perms[i])/total_perms) + " " simplex+=" )" #print(' (tag (quote grammar) 0 (subset (list lin1 per1 se1 se2 rq) '+simplex + ' ))') ripl.assume('s',' (tag (quote hyper) 0 (subset (list lin1 per1 se1 rq) '+simplex + ' ))') ripl.assume('cov','(tag (quote hyper) 1 (cov_compo s))') ripl.assume('gp',"""(tag (quote model) 0 (make_gp_part_der zero cov))""") makeObservations([i for i in range(X.shape[0])],X,ripl) ripl.infer("(repeat "+steps+" (do (mh (quote hyper) one 1) (mh (quote parameter) one 5)))") touch('/home/ulli/Dropbox/cgm/patient_structure_'+patient_number+'/',ripl.sample('(interp cov )')+'_'+str(exp_index))