Exemple #1
0
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()
Exemple #2
0
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_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.
  
  This test passes in 50% of the cases, if everything is fine. It fails 90 % of the cases when inference is broken.
  '''
  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.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 20 ))')

  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 200)")

  xpost = 23.6 # were extrapolating quite far out.
  ypost = []
  for i in range(200):
      y = ripl.sample("(gp (array " + str(xpost) + " ))")
      ypost.append(y)
  
  (test_result, p_value) = stats.kstest(y-f_LIN_PER_WN(xpost),'norm')
  assert(p_value>0.05)
Exemple #4
0
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_gp(self, ripl):
        ripl.assume('make_lin',VentureFunction(makeLinear,[t.NumberType()], t.AnyType("VentureFunction")))
        ripl.assume('make_per',VentureFunction(makePeriodic,[t.NumberType(), t.NumberType(), t.NumberType()], t.AnyType("VentureFunction")))
        ripl.assume('a',"(tag (quote parameter) 0 (log  (uniform_continuous 0 8 ) ))")
        ripl.assume('sf',"(tag (quote parameter) 1 (log (uniform_continuous 0 8 ) ))")
        ripl.assume('p',"(tag (quote parameter) 2 (log (uniform_continuous 0.1 8) ))")
        ripl.assume('l',"(tag (quote parameter) 3 (log (uniform_continuous 0 8) ))")

        ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1+x2))

        ripl.assume('gp',"""(tag (quote model) 0
                            (make_gp_part_der zero
                                (apply_function func_plus
                                (apply_function make_per l p sf  )
                                (apply_function make_lin a ))))""")
Exemple #6
0
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()
Exemple #7
0
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()
Exemple #8
0
    def make_gp(self, ripl):
        ripl.assume('make_linear', VentureFunction(makeLinear, [t.NumberType(),t.IntegerType()], t.AnyType("VentureFunction")))
        ripl.assume('make_periodic', VentureFunction(makePeriodic, [t.NumberType(), t.NumberType(), t.NumberType(),t.IntegerType()], t.AnyType("VentureFunction")))
        ripl.assume('make_se',VentureFunction(makeSquaredExponential,[t.NumberType(), t.NumberType(),t.IntegerType()], t.AnyType("VentureFunction")))
        ripl.assume('make_rq', VentureFunction(makeRQ, [t.NumberType(), t.NumberType(), t.NumberType(),t.IntegerType()], t.AnyType("VentureFunction")))
 
     
        ripl.assume('a',' (tag (quote hyper ) 0 (uniform_continuous  0 8))')

        ripl.assume('l',' (tag (quote hyper) 1 (uniform_continuous  0 8))')
        ripl.assume('q',' (tag (quote  hyper) 2 (uniform_continuous  0.01 8))')
        ripl.assume('sf1','(tag (quote  hyper) 3 (uniform_continuous  0 8))')


        ripl.assume('theta_se_1',' (tag (quote hyper) 4 (uniform_continuous  0 8))')
        ripl.assume('theta_se_2',' (tag (quote hyper) 5 (uniform_continuous  0 8))')

        ripl.assume('theta_rq_1','(tag (quote hyper) 6 (uniform_continuous  0 8))')
        ripl.assume('theta_rq_2','(tag (quote hyper) 7 (uniform_continuous  0 8))')
        ripl.assume('theta_rq_3','(tag (quote hyper) 8 (uniform_continuous  0 8))')


        ripl.assume('lin', "(apply_function make_linear a  0 )")
        ripl.assume('per', "(apply_function make_periodic l q sf1 1 ) ")
        ripl.assume('se1', "(apply_function make_se theta_se_1 theta_se_2 2 )")
        ripl.assume('rq', "(apply_function make_rq theta_rq_1 theta_rq_2 theta_rq_3  7 )")
    
         #### GP Structure Prior

        ###### for simplicity, I start with the max amount of kernel per type given

        ripl.assume("func_times", makeLiftedMult(lambda x1, x2: np.multiply(x1,x2)))
        ripl.assume("func_plus", makeLiftedAdd(lambda x1, x2: x1 + x2))


        ripl.assume('cov_list','(list lin per se1 rq )')
        ripl.bind_foreign_sp("subset",typed_nr(Subset(), [t.ListType(),t.SimplexType()], t.ListType()))

        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+=" )"
        ripl.assume('s','(tag (quote grammar) 1 (subset cov_list '+simplex + ' ))')
        ripl.assume('cov_compo',"""
         (tag (quote grammar) 0
             (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)))
                    )
        )))
        """)


        ripl.assume('cov_structure','(cov_compo s)')
        ripl.assume('gp','(tag (quote model) 0 (make_gp_part_der zero cov_structure))')

        ripl.bind_foreign_sp("covariance_string",
                  deterministic_typed(lambda x:VentureSymbol(x.stuff['name']), [t.AnyType()], t.AnyType(),
                                      descr="returns the covariance type"))

        ripl.bind_foreign_sp("covariance_label",
                  deterministic_typed(lambda x:x.stuff['label_list'], [t.AnyType()], t.ArrayType(),
                                      descr="returns the covariance label"))
def __venture_start__(ripl, *_args):
    np.random.seed(3)
    random.seed(3)
    ripl.assume('make_linear',
                VentureFunctionDiff(makeLinear,
                                    [t.NumberType(),t.IntegerType()],
                                    t.AnyType("VentureFunction")))
    ripl.assume('make_periodic',
                VentureFunction(makePeriodic,
                                [t.NumberType(), t.NumberType(), t.NumberType(),t.IntegerType()],
                                t.AnyType("VentureFunction")))
    ripl.assume('make_squaredexp',
                VentureFunction(makeSquaredExponential,
                                [t.NumberType(), t.NumberType(),t.IntegerType()],
                                t.AnyType("VentureFunction")))
    ripl.assume('make_noise',
                VentureFunction(makeNoise,
                                [t.NumberType(),t.IntegerType()],
                                t.AnyType("VentureFunction")))
    ripl.assume('make_rq',
                VentureFunction(makeRQ,
                                [t.NumberType(), t.NumberType(), t.NumberType(),t.IntegerType()],
                                t.AnyType("VentureFunction")))

    ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType))



    ripl.assume("mult_funcs", makeLiftedMult(lambda x1, x2: np.multiply(x1,x2)))
    ripl.assume("add_funcs", makeLiftedAdd(lambda x1, x2: x1 + x2))

    ripl.bind_foreign_sp('allocate_gpmem', gpmem.allocateGPmemSP)

    ## pseude-uniform structure prior
    def uniform_structure_prior(number):
        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]
        return [float(perms[i])/total_perms for i in range(number)]
    uniform_structure = deterministic_typed(uniform_structure_prior, [t.IntegerType()],t.SimplexType())
    ripl.bind_foreign_sp('uniform_structure', uniform_structure)

    ripl.bind_foreign_sp("subset",typed_nr(Subset(), [t.ListType(),t.SimplexType()], t.ListType()))

    if ripl.evaluate("data")=="synthetic":
       from get_synthetic_data import make_data_function,data_xs
    elif ripl.evaluate("data")=="airline":
       from get_airline_data import make_data_function,data_xs
    elif ripl.evaluate("data")=="co2":
       from get_co2_data import make_data_function,data_xs
    else:
       raise ValueError('Data is not known, please specify synthetic, airline or co2')

    ripl.bind_foreign_sp('make_data_function', deterministic_typed(
    make_data_function, [t.StringType()], sp.SPType([t.NumberType()], t.NumberType())))
    # helper SP to get the input data
    get_data_xs_SP = deterministic_typed(
        lambda: data_xs, [], t.HomogeneousArrayType(t.NumberType()))
    ripl.bind_foreign_sp('get_data_xs', get_data_xs_SP)

    # SPs to interpret covariance structure
    ripl.bind_foreign_sp("covariance_string",
        deterministic_typed(lambda x:VentureSymbol(x.stuff['name']), [t.AnyType()], t.AnyType(),
                            descr="returns the covariance type"))

    # SP to output covariance label so that we only infer over the
    # hyper-parameters of base kernels that are actually in use
    ripl.bind_foreign_sp("covariance_label",
        deterministic_typed(lambda x:x.stuff['label_list'], [t.AnyType()], t.ArrayType(),
                            descr="returns the covariance label"))
def test_SEplusWN_gp():
    y = [2.0, 3.0, 4.0 ]
    x = np.matrix([1.3, -2.0,0])

    ripl = init_gp_ripl()

    ripl.assume('make_se',VentureFunctionDiff(covs.makeSquaredExponential,[t.NumberType(), t.NumberType()], 
    t.AnyType("VentureFunction")))

    ripl.assume('make_noise',VentureFunctionDiff(covs.makeNoise,[t.NumberType()], 
    t.AnyType("VentureFunction")))

    print("""CAVEAT - we are setting the randseed with np, not with ripl.seed,
    since the latter does not work at the moment. This could cause problems in
    future versions of Venture""")

    np.random.seed(1) 
    ripl.assume("s","(tag (quote hyper) 0 (uniform_continuous 0 2 ))")
    ripl.assume("l","(tag (quote hyper) 1 (uniform_continuous 0 2 ))")

    ripl.assume("n","(tag (quote hyper) 2 (uniform_continuous 1 5 ))")

    ripl.assume("add_funcs", covs.makeLiftedAdd(lambda x1, x2: x1 + x2))
    ripl.assume("cov","""(apply_diff_function add_funcs
				(apply_diff_function make_se s l)
				(apply_diff_function make_noise n)
			 )""")
    ripl.assume('gp',"(make_gp_part_der zero cov )")
    ripl.observe("(gp (array 1.3 -2.0 0))",array(y))


    old_state_s=ripl.sample("s")
    old_state_l=ripl.sample("l")
    old_state_n=ripl.sample("n")


    ripl.infer("(grad_ascent (quote hyper) all 1 1 1)")
    
    new_state_s=ripl.sample("s")
    new_state_l=ripl.sample("l")
    new_state_n=ripl.sample("n")

    k_se  = covs.makeSquaredExponential(old_state_s,old_state_l)
    k_wn  = covs.makeNoise(old_state_n)
    dfdsigma=k_se.stuff['derivatives'][0]
    dfdl=k_se.stuff['derivatives'][1]
    dfdn=k_wn.stuff['derivatives'][0]
    #import pdb;pdb.set_trace()
    sn = 0.01
    K = k_se(x.T,x.T) + k_wn(x.T,x.T) + (sn*np.identity(x.shape[1]))
    Kinv = np.linalg.inv(K)
    alpha = Kinv * np.matrix(y).T
    

    expected_step_s = 0.5 * np.trace((alpha*alpha.T - Kinv) * np.asmatrix(apply_cov(dfdsigma)))
    expected_step_l = 0.5 * np.trace((alpha*alpha.T - Kinv) * np.asmatrix(apply_cov(dfdl)))
    expected_step_n = 0.5 * np.trace((alpha*alpha.T - Kinv) * np.asmatrix(apply_cov(dfdn)))

    np.testing.assert_almost_equal((new_state_s-old_state_s),expected_step_s,
	err_msg="kernel addition: gradient with respect to scale factor in SE is not correct")
    np.testing.assert_almost_equal((new_state_l-old_state_l),expected_step_l,
	err_msg="kernel addition: gradient with respect to length scale in SE is not correct")
    np.testing.assert_almost_equal((new_state_n-old_state_n),expected_step_n,
	err_msg="kernel addition: gradient with respect to noise factor in WN  is not correct")
def __venture_start__(ripl, *args):

    # External SPs
    argmaxSP = deterministic_typed(np.argmax, [t.HomogeneousArrayType(t.NumberType())], t.NumberType())
    absSP = deterministic_typed(abs, [t.NumberType()], t.NumberType())
    
    ripl.assume('make_squaredexp',VentureFunctionDiff(makeSquaredExponential,
				 [t.NumberType(), t.NumberType()],
                                 t.AnyType("VentureFunctionDiff")))
    ripl.assume('make_noise', VentureFunctionDiff(makeNoise, 
				[t.NumberType()],
                                 t.AnyType("VentureFunctionDiff")))
 
    
    ripl.assume('make_const_func', VentureFunction(makeConstFunc, [t.NumberType()], constantType))
   
    ripl.bind_foreign_sp("apply_diff_function",applyDiffFunctionSP)

    ripl.assume("add_funcs", makeLiftedAdd(lambda x1, x2: x1 + x2))

    ripl.bind_foreign_sp('make_matrix_gp', gp.makeGPSP )
    
    #ripl.set_seed(2) 
    n = 55 
  
    @np.vectorize
    def regexmpl_f_noiseless(x):
      return 0.3 + 0.4*x + 0.5*np.sin(2.7*x) + (1.1/(1+x**2))
 
     # Regression example
    @np.vectorize
    def f_noisy(x):
        p_outlier = 0.3
        stdev = (1.0 if rand() < p_outlier else 0.1)
        return np.random.normal(regexmpl_f_noiseless(x), stdev)


    # generate and save a data set
    # print "generating regression example data set"
    n = 55
    global regexempl_data_xs,regexempl_data_ys # needs to be global for plotting plugin
    np.random.seed(2)
    regexempl_data_xs = np.random.normal(0,1,n)
    regexempl_data_ys = f_noisy(regexempl_data_xs)
    i = np.argsort(regexempl_data_xs)
    regexempl_data_xs= np.sort(regexempl_data_xs)
    regexempl_data_ys= regexempl_data_ys[i]
    minimum_index = np.argmin(regexempl_data_ys[1:])
    regexempl_data_ys[minimum_index+1]= -4

  
 
 
  
 # Gpmem example
    def make_data_function(name):
      def f_restr(x):
        matches = np.argwhere(np.abs(regexempl_data_xs - x) < 1e-6)
        if matches.size == 0:
            raise Exception('Illegal query')
        else:
            assert matches.size == 1
            i = matches[0,0]
            return regexempl_data_ys[i]
      f_restr.name = name
      audited_sp = deterministic_typed(f_restr, [t.NumberType()], t.NumberType())
      return sp.VentureSPRecord(audited_sp)

    ripl.bind_foreign_sp('make_data_function', deterministic_typed(
        make_data_function, [t.StringType()], sp.SPType([t.NumberType()], t.NumberType())))


    get_regexempl_data_xs_SP = deterministic_typed(lambda: regexempl_data_xs, [], t.HomogeneousArrayType(t.NumberType()))
    ripl.bind_foreign_sp('get_regexempl_data_xs', get_regexempl_data_xs_SP)

    get_regexempl_data_ys_SP = deterministic_typed(lambda: regexempl_data_ys, [], t.HomogeneousArrayType(t.NumberType()))
    ripl.bind_foreign_sp('get_regexempl_data_ys', get_regexempl_data_ys_SP)