<s> import GPy <EOL> import numpy as np <EOL> from GPy . util import datasets <EOL> try : <EOL> import matplotlib . pyplot as plt <EOL> except : <EOL> pass <EOL> def student_t_approx ( optimize = True , plot = True ) : <EOL> """<STR_LIT>""" <EOL> real_std = <NUM_LIT:0.1> <EOL> X = np . linspace ( <NUM_LIT:0.0> , np . pi * <NUM_LIT:2> , <NUM_LIT:100> ) [ : , None ] <EOL> Y = np . sin ( X ) + np . random . randn ( * X . shape ) * real_std <EOL> Y = Y / Y . max ( ) <EOL> Yc = Y . copy ( ) <EOL> X_full = np . linspace ( <NUM_LIT:0.0> , np . pi * <NUM_LIT:2> , <NUM_LIT> ) [ : , None ] <EOL> Y_full = np . sin ( X_full ) <EOL> Y_full = Y_full / Y_full . max ( ) <EOL> Yc [ <NUM_LIT> : <NUM_LIT> ] += <NUM_LIT:1> <EOL> deg_free = <NUM_LIT:1> <EOL> print ( "<STR_LIT>" , real_std ) <EOL> initial_var_guess = <NUM_LIT:0.5> <EOL> edited_real_sd = initial_var_guess <EOL> kernel1 = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . White ( X . shape [ <NUM_LIT:1> ] ) <EOL> kernel2 = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . White ( X . shape [ <NUM_LIT:1> ] ) <EOL> kernel3 = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . White ( X . shape [ <NUM_LIT:1> ] ) <EOL> kernel4 = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . White ( X . shape [ <NUM_LIT:1> ] ) <EOL> m1 = GPy . models . GPRegression ( X , Y . copy ( ) , kernel = kernel1 ) <EOL> m1 [ '<STR_LIT>' ] . constrain_fixed ( <NUM_LIT> ) <EOL> m1 . randomize ( ) <EOL> m2 = GPy . models . GPRegression ( X , Yc . copy ( ) , kernel = kernel2 ) <EOL> m2 [ '<STR_LIT>' ] . constrain_fixed ( <NUM_LIT> ) <EOL> m2 . randomize ( ) <EOL> t_distribution = GPy . likelihoods . StudentT ( deg_free = deg_free , sigma2 = edited_real_sd ) <EOL> laplace_inf = GPy . inference . latent_function_inference . Laplace ( ) <EOL> m3 = GPy . core . GP ( X , Y . copy ( ) , kernel3 , likelihood = t_distribution , inference_method = laplace_inf ) <EOL> m3 [ '<STR_LIT>' ] . constrain_bounded ( <NUM_LIT> , <NUM_LIT> ) <EOL> m3 [ '<STR_LIT>' ] . constrain_fixed ( <NUM_LIT> ) <EOL> m3 . randomize ( ) <EOL> t_distribution = GPy . likelihoods . StudentT ( deg_free = deg_free , sigma2 = edited_real_sd ) <EOL> laplace_inf = GPy . inference . latent_function_inference . Laplace ( ) <EOL> m4 = GPy . core . GP ( X , Yc . copy ( ) , kernel4 , likelihood = t_distribution , inference_method = laplace_inf ) <EOL> m4 [ '<STR_LIT>' ] . constrain_bounded ( <NUM_LIT> , <NUM_LIT> ) <EOL> m4 [ '<STR_LIT>' ] . constrain_fixed ( <NUM_LIT> ) <EOL> m4 . randomize ( ) <EOL> print ( m4 ) <EOL> debug = True <EOL> if debug : <EOL> m4 . optimize ( messages = <NUM_LIT:1> ) <EOL> from matplotlib import pyplot as pb <EOL> pb . plot ( m4 . X , m4 . inference_method . f_hat ) <EOL> pb . plot ( m4 . X , m4 . Y , '<STR_LIT>' ) <EOL> m4 . plot ( ) <EOL> print ( m4 ) <EOL> return m4 <EOL> if optimize : <EOL> optimizer = '<STR_LIT>' <EOL> print ( "<STR_LIT>" ) <EOL> m1 . optimize ( optimizer , messages = <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" ) <EOL> m2 . optimize ( optimizer , messages = <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" ) <EOL> m3 . optimize ( optimizer , messages = <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" ) <EOL> m4 . optimize ( optimizer , messages = <NUM_LIT:1> ) <EOL> if plot : <EOL> plt . figure ( <NUM_LIT:1> ) <EOL> plt . suptitle ( '<STR_LIT>' ) <EOL> ax = plt . subplot ( <NUM_LIT> ) <EOL> m1 . plot ( ax = ax ) <EOL> plt . plot ( X_full , Y_full ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> ax = plt . subplot ( <NUM_LIT> ) <EOL> m2 . plot ( ax = ax ) <EOL> plt . plot ( X_full , Y_full ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . figure ( <NUM_LIT:2> ) <EOL> plt . suptitle ( '<STR_LIT>' ) <EOL> ax = plt . subplot ( <NUM_LIT> ) <EOL> m3 . plot ( ax = ax ) <EOL> plt . plot ( X_full , Y_full ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> ax = plt . subplot ( <NUM_LIT> ) <EOL> m4 . plot ( ax = ax ) <EOL> plt . plot ( X_full , Y_full ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> return m1 , m2 , m3 , m4 <EOL> def boston_example ( optimize = True , plot = True ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> import sklearn <EOL> from sklearn . cross_validation import KFold <EOL> optimizer = '<STR_LIT>' <EOL> messages = <NUM_LIT:0> <EOL> data = datasets . boston_housing ( ) <EOL> degrees_freedoms = [ <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:10> ] <EOL> X = data [ '<STR_LIT:X>' ] . copy ( ) <EOL> Y = data [ '<STR_LIT:Y>' ] . copy ( ) <EOL> X = X - X . mean ( axis = <NUM_LIT:0> ) <EOL> X = X / X . std ( axis = <NUM_LIT:0> ) <EOL> Y = Y - Y . mean ( ) <EOL> Y = Y / Y . std ( ) <EOL> num_folds = <NUM_LIT:10> <EOL> kf = KFold ( len ( Y ) , n_folds = num_folds , indices = True ) <EOL> num_models = len ( degrees_freedoms ) + <NUM_LIT:3> <EOL> score_folds = np . zeros ( ( num_models , num_folds ) ) <EOL> pred_density = score_folds . copy ( ) <EOL> def rmse ( Y , Ystar ) : <EOL> return np . sqrt ( np . mean ( ( Y - Ystar ) ** <NUM_LIT:2> ) ) <EOL> for n , ( train , test ) in enumerate ( kf ) : <EOL> X_train , X_test , Y_train , Y_test = X [ train ] , X [ test ] , Y [ train ] , Y [ test ] <EOL> print ( "<STR_LIT>" . format ( n ) ) <EOL> noise = <NUM_LIT> <EOL> rbf_len = <NUM_LIT:0.5> <EOL> data_axis_plot = <NUM_LIT:4> <EOL> kernelstu = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . white ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . bias ( X . shape [ <NUM_LIT:1> ] ) <EOL> kernelgp = GPy . kern . RBF ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . white ( X . shape [ <NUM_LIT:1> ] ) + GPy . kern . bias ( X . shape [ <NUM_LIT:1> ] ) <EOL> score_folds [ <NUM_LIT:0> , n ] = rmse ( Y_test , np . mean ( Y_train ) ) <EOL> print ( "<STR_LIT>" ) <EOL> mgp = GPy . models . GPRegression ( X_train . copy ( ) , Y_train . copy ( ) , kernel = kernelgp . copy ( ) ) <EOL> mgp . constrain_fixed ( '<STR_LIT>' , <NUM_LIT> ) <EOL> mgp [ '<STR_LIT>' ] = rbf_len <EOL> mgp [ '<STR_LIT>' ] = noise <EOL> print ( mgp ) <EOL> if optimize : <EOL> mgp . optimize ( optimizer = optimizer , messages = messages ) <EOL> Y_test_pred = mgp . predict ( X_test ) <EOL> score_folds [ <NUM_LIT:1> , n ] = rmse ( Y_test , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> pred_density [ <NUM_LIT:1> , n ] = np . mean ( mgp . log_predictive_density ( X_test , Y_test ) ) <EOL> print ( mgp ) <EOL> print ( pred_density ) <EOL> print ( "<STR_LIT>" ) <EOL> N , D = Y_train . shape <EOL> g_distribution = GPy . likelihoods . noise_model_constructors . gaussian ( variance = noise , N = N , D = D ) <EOL> g_likelihood = GPy . likelihoods . Laplace ( Y_train . copy ( ) , g_distribution ) <EOL> mg = GPy . models . GPRegression ( X_train . copy ( ) , Y_train . copy ( ) , kernel = kernelstu . copy ( ) , likelihood = g_likelihood ) <EOL> mg . constrain_positive ( '<STR_LIT>' ) <EOL> mg . constrain_fixed ( '<STR_LIT>' , <NUM_LIT> ) <EOL> mg [ '<STR_LIT>' ] = rbf_len <EOL> mg [ '<STR_LIT>' ] = noise <EOL> print ( mg ) <EOL> if optimize : <EOL> mg . optimize ( optimizer = optimizer , messages = messages ) <EOL> Y_test_pred = mg . predict ( X_test ) <EOL> score_folds [ <NUM_LIT:2> , n ] = rmse ( Y_test , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> pred_density [ <NUM_LIT:2> , n ] = np . mean ( mg . log_predictive_density ( X_test , Y_test ) ) <EOL> print ( pred_density ) <EOL> print ( mg ) <EOL> for stu_num , df in enumerate ( degrees_freedoms ) : <EOL> print ( "<STR_LIT>" . format ( df ) ) <EOL> t_distribution = GPy . likelihoods . noise_model_constructors . student_t ( deg_free = df , sigma2 = noise ) <EOL> stu_t_likelihood = GPy . likelihoods . Laplace ( Y_train . copy ( ) , t_distribution ) <EOL> mstu_t = GPy . models . GPRegression ( X_train . copy ( ) , Y_train . copy ( ) , kernel = kernelstu . copy ( ) , likelihood = stu_t_likelihood ) <EOL> mstu_t . constrain_fixed ( '<STR_LIT>' , <NUM_LIT> ) <EOL> mstu_t . constrain_bounded ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT:1000> ) <EOL> mstu_t [ '<STR_LIT>' ] = rbf_len <EOL> mstu_t [ '<STR_LIT>' ] = noise <EOL> print ( mstu_t ) <EOL> if optimize : <EOL> mstu_t . optimize ( optimizer = optimizer , messages = messages ) <EOL> Y_test_pred = mstu_t . predict ( X_test ) <EOL> score_folds [ <NUM_LIT:3> + stu_num , n ] = rmse ( Y_test , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> pred_density [ <NUM_LIT:3> + stu_num , n ] = np . mean ( mstu_t . log_predictive_density ( X_test , Y_test ) ) <EOL> print ( pred_density ) <EOL> print ( mstu_t ) <EOL> if plot : <EOL> plt . figure ( ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test , c = '<STR_LIT:r>' , marker = '<STR_LIT:x>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . figure ( ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test , c = '<STR_LIT:r>' , marker = '<STR_LIT:x>' ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> plt . figure ( ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test_pred [ <NUM_LIT:0> ] ) <EOL> plt . scatter ( X_test [ : , data_axis_plot ] , Y_test , c = '<STR_LIT:r>' , marker = '<STR_LIT:x>' ) <EOL> plt . title ( '<STR_LIT>' . format ( df ) ) <EOL> print ( "<STR_LIT>" . format ( np . mean ( score_folds , <NUM_LIT:1> ) ) ) <EOL> print ( "<STR_LIT>" . format ( np . mean ( pred_density , <NUM_LIT:1> ) ) ) <EOL> if plot : <EOL> stu_t_legends = [ '<STR_LIT>' . format ( df ) for df in degrees_freedoms ] <EOL> legends = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + stu_t_legends <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> bp = ax . boxplot ( score_folds . T , notch = <NUM_LIT:0> , sym = '<STR_LIT:+>' , vert = <NUM_LIT:1> , whis = <NUM_LIT> ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' , marker = '<STR_LIT:+>' ) <EOL> xtickNames = plt . setp ( ax , xticklabels = legends ) <EOL> plt . setp ( xtickNames , rotation = <NUM_LIT> , fontsize = <NUM_LIT:8> ) <EOL> ax . set_ylabel ( '<STR_LIT>' ) <EOL> ax . set_xlabel ( '<STR_LIT>' ) <EOL> ax . yaxis . grid ( True , linestyle = '<STR_LIT:->' , which = '<STR_LIT>' , color = '<STR_LIT>' , <EOL> alpha = <NUM_LIT:0.5> ) <EOL> ax . set_axisbelow ( True ) <EOL> fig = plt . figure ( ) <EOL> ax = fig . add_subplot ( <NUM_LIT> ) <EOL> plt . title ( '<STR_LIT>' ) <EOL> bp = ax . boxplot ( pred_density [ <NUM_LIT:1> : , : ] . T , notch = <NUM_LIT:0> , sym = '<STR_LIT:+>' , vert = <NUM_LIT:1> , whis = <NUM_LIT> ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' ) <EOL> plt . setp ( bp [ '<STR_LIT>' ] , color = '<STR_LIT>' , marker = '<STR_LIT:+>' ) <EOL> xtickNames = plt . setp ( ax , xticklabels = legends [ <NUM_LIT:1> : ] ) <EOL> plt . setp ( xtickNames , rotation = <NUM_LIT> , fontsize = <NUM_LIT:8> ) <EOL> ax . set_ylabel ( '<STR_LIT>' ) <EOL> ax . set_xlabel ( '<STR_LIT>' ) <EOL> ax . yaxis . grid ( True , linestyle = '<STR_LIT:->' , which = '<STR_LIT>' , color = '<STR_LIT>' , <EOL> alpha = <NUM_LIT:0.5> ) <EOL> ax . set_axisbelow ( True ) <EOL> return mstu_t </s>
