Exemple #1
0
 def __init__( self ):
     self.gp = CppGaussianProcess()
Exemple #2
0
class GaussianProcess( object ):
    def __init__( self ):
        self.gp = CppGaussianProcess()

    def function_domain( self, func_domain ):
        self.func_domain = func_domain
        self.gp.domain( self.func_domain.ranges.flatten() )

       
    def build( self, build_points, build_values ):
        self.gp.build( build_points, build_values )

    def evaluate_set( self, test_points ):
        print test_points.shape
        print  self.gp.evaluate_set( test_points ).shape
        return self.gp.evaluate_set( test_points )[:,0]
    
    def evaluate_surface_variance( self, test_points ):
        return self.gp.evaluate_surface_variance( test_points )[:,0]

    def leave_one_out_cross_validation( self ):
        return self.gp.leave_one_out_cross_validation()

    def set_correlation_lengths( self, corr_lengths ):
        self.gp.set_correlation_lengths( corr_lengths )

    def get_correlation_lengths( self ):
        return self.gp.get_correlation_lengths()

    def get_correlation_thetas( self ):
        return self.gp.get_correlation_thetas()

    def set_optimization_method( self, method_enum ):
        self.gp.set_optimization_method( method_enum )

    def set_verbosity( self, verbosity ):
        self.gp.set_verbosity( verbosity )

    def get_correlation_matrix( self ):
        return self.gp.get_correlation_matrix()

    def clear( self ):
        # delete the internal memory of the c++ object
        self.gp.clear()

    def parse_cross_validation_data( self, pts, vals, cv_params ):
         return  pts, vals

    def initialise_cross_validation( self, pts, vals, cv_params,
                                     use_fast_cross_validation ):
        self.use_fast_cross_validation = use_fast_cross_validation

        # build an initial approximation on the entire data set to get
        # correlation lengths which can then be used as a good guess 
        # for local optimization of the maximum likelihood. The
        # use of the local optimizer should speed up convergence
        #self.set_optimization_method( 3 ) # global_local
        self.set_optimization_method( 4 ) # sampling
        self.build( pts, vals )
        self.corr_lengths = self.get_correlation_lengths()

        self.set_optimization_method( 1 ) # local
 
    def compute_residuals( self, pts, vals, train_indices,
                           validation_indices, cv_params = None ):
        pts_train = pts[:,train_indices]
        vals_train = vals[train_indices]
        pts_validation = pts[:,validation_indices]
        vals_validation = vals[validation_indices]
        
        self.set_correlation_lengths( self.corr_lengths )
        self.build( pts_train, vals_train )
        fn_vals_pred = self.evaluate_set( pts_validation )

        # must clear predictor if predictor is a c++ wrapped class
        # otherwise c++ internally created memory will not be deleted
        self.clear()

        print 'In gaussian_process.compute_residuals'

        # cv_params has not be changed, e.g. new params added so return None
        return vals_validation - fn_vals_pred, None