gplib  1.0.0
C++ Gaussian Process Library
gp.hpp
Go to the documentation of this file.
1 
2 #ifndef GPLIB_GP
3 #define GPLIB_GP
4 
5 #include <armadillo>
6 #include <vector>
7 #include <memory>
8 #include <cassert>
9 
10 #include "mvgauss.hpp"
11 
12 namespace gplib {
13 
14  class kernel_class {
18  public:
26  virtual ~kernel_class() = default;
36  virtual arma::mat eval(const arma::mat &X, const arma::mat &Y,
37  bool diag = false) const = 0;
50  virtual arma::mat derivate(size_t param_id, const arma::mat &X,
51  const arma::mat &Y, bool diag = false) const = 0;
55  virtual size_t n_params() const = 0;
61  virtual void set_params(const std::vector<double> &params) = 0;
66  virtual void set_lower_bounds(const std::vector<double> &lower_bounds) = 0;
71  virtual void set_upper_bounds(const std::vector<double> &upper_bounds) = 0;
76  virtual std::vector<double> get_params() const = 0;
81  virtual std::vector<double> get_lower_bounds() const = 0;
86  virtual std::vector<double> get_upper_bounds() const = 0;
87  };
88 
89  class gp_reg {
93  private:
96  public:
100  gp_reg();
104  ~gp_reg();
109  void set_kernel(const std::shared_ptr<kernel_class> &k);
114  std::shared_ptr<kernel_class> get_kernel() const;
120  void set_training_set(const arma::mat &X, const arma::vec &y);
126  double train(int max_iter, double tol);
135  mv_gauss full_predict(const arma::mat &new_data) const;
144  arma::vec predict(const arma::mat &new_data) const;
145  };
146 
151  public:
164  const std::vector<std::shared_ptr<kernel_class>> &kernels,
165  const std::vector<arma::mat> &params) {}
169  virtual ~multioutput_kernel_class() = default;
179  virtual arma::mat eval(const std::vector<arma::mat> &X,
180  const std::vector<arma::mat> &Y, bool diag = false) const = 0;
193  virtual arma::mat derivate(size_t param_id, const std::vector<arma::mat> &X,
194  const std::vector<arma::mat> &Y, bool diag = false) const = 0;
199  virtual size_t n_params() const = 0;
207  virtual void set_params_k(const std::vector<arma::mat> &params) = 0;
217  virtual void set_params(const std::vector<double> &params,
218  size_t n_outputs = 0) = 0;
224  virtual void set_kernels(
225  const std::vector<std::shared_ptr<kernel_class>> &kernels) = 0;
231  virtual std::vector<arma::mat> get_params_k() const = 0;
236  virtual std::vector<double> get_params() const = 0;
241  virtual std::vector<std::shared_ptr<kernel_class>> get_kernels() const = 0;
248  virtual void set_lower_bounds(const double &lower_bounds) = 0;
255  virtual void set_upper_bounds(const double &upper_bounds) = 0;
261  virtual void set_lower_bounds(const std::vector<double> &lower_bounds) = 0;
267  virtual void set_upper_bounds(const std::vector<double> &params) = 0;
272  virtual std::vector<double> get_lower_bounds() const = 0;
277  virtual std::vector<double> get_upper_bounds() const = 0;
278  };
279 
280  class gp_reg_multi {
285  private:
288  public:
292  gp_reg_multi();
296  ~gp_reg_multi();
305  void set_kernel(const std::shared_ptr<multioutput_kernel_class> &k);
314  void set_training_set(const std::vector<arma::mat> &X,
315  const std::vector<arma::vec> &y);
322  double train(const int max_iter, const double tol);
331  double train(const int max_iter, const double tol, const size_t num_pi);
339  double train(const int max_iter, const double tol,
340  const std::vector<size_t> num_pi);
341  /* *
342  * @param num_pi : A vector containing the inducing points for each
343  * output class, each matrix should be smaller than
344  * the corresponding number of inputs for the
345  * output class, use of this parameter triggers the use
346  * of FITC instead of the full regression.
347  * */
348  double train(const int max_iter, const double tol,
349  const std::vector<arma::mat> num_pi);
359  mv_gauss full_predict(const std::vector<arma::mat> &new_data);
369  arma::vec predict(const std::vector<arma::mat> &new_data) const;
375  std::vector<double> get_params() const;
384  void set_params(const std::vector<double> &params);
385  enum {FULL, FITC};
386  };
387 };
388 
389 #endif
implementation * pimpl
Definition: gp.hpp:286
virtual arma::mat derivate(size_t param_id, const std::vector< arma::mat > &X, const std::vector< arma::mat > &Y, bool diag=false) const =0
Returns the value of the derivative wrt a certain parameter with a a particular pair of input matrice...
void set_kernel(const std::shared_ptr< multioutput_kernel_class > &k)
Sets the multioutput kernel to be used with the multioutput regression.
Definition: gpreg_multi.cc:270
mv_gauss full_predict(const std::vector< arma::mat > &new_data)
Uses the already trained model to predict output values for new inputs provided in the parameter...
Definition: gpreg_multi.cc:365
Definition: mvgauss.hpp:9
Definition: gp.hpp:14
void set_training_set(const arma::mat &X, const arma::vec &y)
Sets the training set to be used during the training process.
Definition: gpreg.cc:92
Definition: gpreg_multi.cc:10
multioutput_kernel_class(const std::vector< std::shared_ptr< kernel_class >> &kernels, const std::vector< arma::mat > &params)
Constructor, requires the inner kernels to be used and the parameter matrices.
Definition: gp.hpp:163
gp_reg()
Constructor.
Definition: gpreg.cc:76
virtual std::vector< double > get_params() const =0
Returns a std.
gp_reg_multi()
Constructor.
Definition: gpreg_multi.cc:262
virtual void set_lower_bounds(const double &lower_bounds)=0
Sets the lower bounds to be used by the kernel during training process including those of the inner k...
virtual ~kernel_class()=default
Destructor.
virtual void set_upper_bounds(const double &upper_bounds)=0
Sets the upper bounds to be used by the kernel during training process including those of the inner k...
~gp_reg()
Destructor.
Definition: gpreg.cc:80
Definition: gp.hpp:147
Definition: gp.hpp:280
virtual std::vector< std::shared_ptr< kernel_class > > get_kernels() const =0
Returns a Shared pointer to a vector containing the inner kernels currently set.
virtual std::vector< double > get_upper_bounds() const =0
Returns a vector with the upper bounds of all the parameters, including those of the inner kernels...
virtual void set_params_k(const std::vector< arma::mat > &params)=0
Sets the parameters of the multioutput kernel only, doesn't affect the parameters of the inner kernel...
virtual void set_lower_bounds(const std::vector< double > &lower_bounds)=0
Sets the lower bounds to be used by the kernel during training process.
multioutput_kernel_class()
Multioutput Kernel Class definition.
Definition: gp.hpp:155
implementation * pimpl
Definition: gp.hpp:94
arma::vec predict(const std::vector< arma::mat > &new_data) const
Uses the already trained model to predict output values for new inputs provided in the parameter...
Definition: gpreg_multi.cc:372
virtual void set_params(const std::vector< double > &params, size_t n_outputs=0)=0
Sets all the parameters of the multioutput kernel including those of the inner kernels using a std...
std::vector< double > get_params() const
Returns a vector with the complete set of parameters required by the multioutput regression (pseudo-i...
Definition: gpreg_multi.cc:381
virtual size_t n_params() const =0
Returns the total number of parameters needed bythe kernel (parameter matrices, plus the parameters o...
arma::vec predict(const arma::mat &new_data) const
Uses the already trained model to predict output values for new inputs provided in the parameter...
Definition: gpreg.cc:105
virtual void set_upper_bounds(const std::vector< double > &upper_bounds)=0
Sets the upper bounds to be used by the kernel during training process.
~gp_reg_multi()
Destructor.
Definition: gpreg_multi.cc:266
kernel_class()
Kernel Class definition.
Definition: gp.hpp:22
mv_gauss full_predict(const arma::mat &new_data) const
Uses the already trained model to predict output values for new inputs provided in the parameter...
Definition: gpreg.cc:101
void set_training_set(const std::vector< arma::mat > &X, const std::vector< arma::vec > &y)
Sets the pairs of known input and output data used to train the model.
Definition: gpreg_multi.cc:274
virtual std::vector< double > get_params() const =0
Returns a vector with the current values of the parameters of the kernel.
Definition: gpreg.cc:9
virtual void set_kernels(const std::vector< std::shared_ptr< kernel_class >> &kernels)=0
Sets the inner kernels.
virtual arma::mat eval(const std::vector< arma::mat > &X, const std::vector< arma::mat > &Y, bool diag=false) const =0
Evaluates the kernel function over the provided sets of matrices.
virtual arma::mat eval(const arma::mat &X, const arma::mat &Y, bool diag=false) const =0
Evaluates the kernel function over the provided matrices.
Definition: gp.hpp:385
virtual size_t n_params() const =0
Returns the number of params needed by the kernel.
Definition: gp.hpp:385
Definition: basic.cc:6
void set_kernel(const std::shared_ptr< kernel_class > &k)
Sets the kernel to be used during the regression process.
Definition: gpreg.cc:84
std::shared_ptr< kernel_class > get_kernel() const
Returns the current kernel set to be used during the regression process.
Definition: gpreg.cc:88
virtual ~multioutput_kernel_class()=default
Destructor.
virtual std::vector< arma::mat > get_params_k() const =0
Returns a vector of matrices containing the parameters of the multioutput kernel, but not those of th...
virtual arma::mat derivate(size_t param_id, const arma::mat &X, const arma::mat &Y, bool diag=false) const =0
Returns the value of the derivative wrt a certain parameter with a a particular pair of input matrice...
virtual void set_params(const std::vector< double > &params)=0
Sets the parameters of the kernel using the proided vector.
double train(const int max_iter, const double tol)
Trains the model using the standard procedure, in accordance to the provided training set...
Definition: gpreg_multi.cc:281
virtual std::vector< double > get_lower_bounds() const =0
Returns a vector with the lower bounds of all the parameters, including those of the inner kernels...
void set_params(const std::vector< double > &params)
Sets all the parameters of the multioutput regression using the Vector prvided.
Definition: gpreg_multi.cc:385
virtual std::vector< double > get_upper_bounds() const =0
Returns a vector with the current values of the upper_bounds for each of the parameters of the kernel...
virtual std::vector< double > get_lower_bounds() const =0
Returns a vector with the current values of the lower_bounds for each of the parameters of the kernel...
Definition: gp.hpp:89
double train(int max_iter, double tol)
Trains the model using the provided training set.
Definition: gpreg.cc:97