(self, X, y, do_optimize=True, **kwargs)
Performs MCMC sampling to sample hyperparameter configurations from the
        likelihood and trains for each sample a GP on X and y

        Parameters
        ----------
        X: np.ndarray (N, D)
            Input data points. The dimensionality of X is (N, D),
            with N as the number of points and D is the number of features.
        y: np.ndarray (N,)
            The corresponding target values.
        do_optimize: boolean
            If set to true we perform MCMC sampling otherwise we just use the
            hyperparameter specified in the kernel.
Performs MCMC sampling to sample hyperparameter configurations from the
        likelihood and trains for each sample a GP on X and y
Performs MCMC sampling to sample hyperparameter configurations from the likelihood and trains for each sample a GP on X and y
def train(self, X, y, do_optimize=True, **kwargs):
        """
        Performs MCMC sampling to sample hyperparameter configurations from the
        likelihood and trains for each sample a GP on X and y

        Parameters
        ----------
        X: np.ndarray (N, D)
            Input data points. The dimensionality of X is (N, D),
            with N as the number of points and D is the number of features.
        y: np.ndarray (N,)
            The corresponding target values.
        do_optimize: boolean
            If set to true we perform MCMC sampling otherwise we just use the
            hyperparameter specified in the kernel.
        """

        if self.normalize_input:
            # Normalize input to be in [0, 1]
            self.X, self.lower, self.upper = normalization.zero_one_normalization(X, self.lower, self.upper)

        else:
            self.X = X

        if self.normalize_output:
            # Normalize output to have zero mean and unit standard deviation
            self.y, self.y_mean, self.y_std = normalization.zero_mean_unit_var_normalization(y)
            if self.y_std == 0:
                raise ValueError("Cannot normalize output. All targets have the same value")
        else:
            self.y = y

        # Use the mean of the data as mean for the GP
        self.mean = np.mean(self.y, axis=0)
        self.gp = george.GP(self.kernel, mean=self.mean)

        if do_optimize:
            # We have one walker for each hyperparameter configuration
            sampler = emcee.EnsembleSampler(self.n_hypers,
                                            len(self.kernel) + 1,
                                            self.loglikelihood)
            sampler.random_state = self.rng.get_state()
            # Do a burn-in in the first iteration
            if not self.burned:
                # Initialize the walkers by sampling from the prior
                if self.prior is None:
                    self.p0 = self.rng.rand(self.n_hypers, len(self.kernel) + 1)
                else:
                    self.p0 = self.prior.sample_from_prior(self.n_hypers)
                # Run MCMC sampling
                self.p0, _, _ = sampler.run_mcmc(self.p0,
                                                 self.burnin_steps,
                                                 rstate0=self.rng)

                self.burned = True

            # Start sampling
            pos, _, _ = sampler.run_mcmc(self.p0,
                                         self.chain_length,
                                         rstate0=self.rng)

            # Save the current position, it will be the start point in
            # the next iteration
            self.p0 = pos

            # Take the last samples from each walker
            self.hypers = sampler.chain[:, -1]

        else:
            self.hypers = self.gp.kernel[:].tolist()
            self.hypers.append(self.noise)
            self.hypers = [self.hypers]

        self.models = []
        for sample in self.hypers:

            # Instantiate a GP for each hyperparameter configuration
            kernel = deepcopy(self.kernel)
            kernel.set_parameter_vector(sample[:-1])
            noise = np.exp(sample[-1])
            model = GaussianProcess(kernel,
                                    normalize_output=self.normalize_output,
                                    normalize_input=self.normalize_input,
                                    noise=noise,
                                    lower=self.lower,
                                    upper=self.upper,
                                    rng=self.rng)
            model.train(X, y, do_optimize=False)
            self.models.append(model)

        self.is_trained = True
def train ( self , X , y , do_optimize = True , * * kwargs ) : if self . normalize_input : # Normalize input to be in [0, 1] self . X , self . lower , self . upper = normalization . zero_one_normalization ( X , self . lower , self . upper ) else : self . X = X if self . normalize_output : # Normalize output to have zero mean and unit standard deviation self . y , self . y_mean , self . y_std = normalization . zero_mean_unit_var_normalization ( y ) if self . y_std == 0 : raise ValueError ( "Cannot normalize output. All targets have the same value" ) else : self . y = y # Use the mean of the data as mean for the GP self . mean = np . mean ( self . y , axis = 0 ) self . gp = george . GP ( self . kernel , mean = self . mean ) if do_optimize : # We have one walker for each hyperparameter configuration sampler = emcee . EnsembleSampler ( self . n_hypers , len ( self . kernel ) + 1 , self . loglikelihood ) sampler . random_state = self . rng . get_state ( ) # Do a burn-in in the first iteration if not self . burned : # Initialize the walkers by sampling from the prior if self . prior is None : self . p0 = self . rng . rand ( self . n_hypers , len ( self . kernel ) + 1 ) else : self . p0 = self . prior . sample_from_prior ( self . n_hypers ) # Run MCMC sampling self . p0 , _ , _ = sampler . run_mcmc ( self . p0 , self . burnin_steps , rstate0 = self . rng ) self . burned = True # Start sampling pos , _ , _ = sampler . run_mcmc ( self . p0 , self . chain_length , rstate0 = self . rng ) # Save the current position, it will be the start point in # the next iteration self . p0 = pos # Take the last samples from each walker self . hypers = sampler . chain [ : , - 1 ] else : self . hypers = self . gp . kernel [ : ] . tolist ( ) self . hypers . append ( self . noise ) self . hypers = [ self . hypers ] self . models = [ ] for sample in self . hypers : # Instantiate a GP for each hyperparameter configuration kernel = deepcopy ( self . kernel ) kernel . set_parameter_vector ( sample [ : - 1 ] ) noise = np . exp ( sample [ - 1 ] ) model = GaussianProcess ( kernel , normalize_output = self . normalize_output , normalize_input = self . normalize_input , noise = noise , lower = self . lower , upper = self . upper , rng = self . rng ) model . train ( X , y , do_optimize = False ) self . models . append ( model ) self . is_trained = True