SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GaussianProcessRegression.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Roman Votyakov
8  * Copyright (C) 2012 Jacob Walker
9  * Copyright (C) 2013 Roman Votyakov
10  *
11  * Code adapted from Gaussian Process Machine Learning Toolbox
12  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
13  */
14 
16 
17 #ifdef HAVE_EIGEN3
18 
19 #include <shogun/io/SGIO.h>
21 
22 using namespace shogun;
23 
26 {
27 }
28 
30  : CGaussianProcessMachine(method)
31 {
32  // set labels
33  m_labels=method->get_labels();
34 }
35 
37 {
38 }
39 
41 {
42  // check whether given combination of inference method and likelihood
43  // function supports regression
44  REQUIRE(m_method, "Inference method should not be NULL\n")
46  REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
47  "regression\n", m_method->get_name(), lik->get_name())
48  SG_UNREF(lik);
49 
50  CRegressionLabels* result;
51 
52  // if regression data equals to NULL, then apply regression on training
53  // features
54  if (!data)
55  {
56  CFeatures* feat;
57 
58  // use latent features for FITC inference method
60  {
61  CFITCInferenceMethod* fitc_method=
63  feat=fitc_method->get_latent_features();
64  SG_UNREF(fitc_method);
65  }
66  else
67  feat=m_method->get_features();
68 
69  result=new CRegressionLabels(get_mean_vector(feat));
70 
71  SG_UNREF(feat);
72  }
73  else
74  {
75  result=new CRegressionLabels(get_mean_vector(data));
76  }
77 
78  return result;
79 }
80 
82 {
83  // check whether given combination of inference method and likelihood
84  // function supports regression
85  REQUIRE(m_method, "Inference method should not be NULL\n")
87  REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
88  "regression\n", m_method->get_name(), lik->get_name())
89  SG_UNREF(lik);
90 
91  if (data)
92  {
93  // set latent features for FITC inference method
95  {
96  CFITCInferenceMethod* fitc_method=
98  fitc_method->set_latent_features(data);
99  SG_UNREF(fitc_method);
100  }
101  else
102  m_method->set_features(data);
103  }
104 
105  // perform inference
107  m_method->update();
108 
109  return true;
110 }
111 
113 {
114  // check whether given combination of inference method and likelihood
115  // function supports regression
116  REQUIRE(m_method, "Inference method should not be NULL\n")
118  REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
119  "regression\n", m_method->get_name(), lik->get_name())
120  SG_UNREF(lik);
121 
122  SG_REF(data);
125  SG_UNREF(data);
126 
127  // evaluate mean
128  lik=m_method->get_model();
129  mu=lik->get_predictive_means(mu, s2);
130  SG_UNREF(lik);
131 
132  return mu;
133 }
134 
136  CFeatures* data)
137 {
138  // check whether given combination of inference method and likelihood
139  // function supports regression
140  REQUIRE(m_method, "Inference method should not be NULL\n")
142  REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
143  "regression\n", m_method->get_name(), lik->get_name())
144 
145  SG_REF(data);
148  SG_UNREF(data);
149 
150  // evaluate variance
151  s2=lik->get_predictive_variances(mu, s2);
152  SG_UNREF(lik);
153 
154  return s2;
155 }
156 
157 #endif
virtual const char * get_name() const =0
virtual CFeatures * get_features()
virtual CFeatures * get_latent_features()
Real Labels are real-valued labels.
The Inference Method base class.
virtual void set_features(CFeatures *feat)
A base class for Gaussian Processes.
SGVector< float64_t > get_posterior_variances(CFeatures *data)
#define SG_UNREF(x)
Definition: SGRefObject.h:35
virtual bool train_machine(CFeatures *data=NULL)
CLabels * m_labels
Definition: Machine.h:356
#define REQUIRE(x,...)
Definition: SGIO.h:208
SGVector< float64_t > get_posterior_means(CFeatures *data)
virtual SGVector< float64_t > get_predictive_variances(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab=NULL) const =0
virtual bool update_parameter_hash()
Definition: SGObject.cpp:187
virtual bool supports_regression() const
SGVector< float64_t > get_variance_vector(CFeatures *data)
#define SG_REF(x)
Definition: SGRefObject.h:34
virtual CLabels * get_labels()
SGVector< float64_t > get_mean_vector(CFeatures *data)
virtual CRegressionLabels * apply_regression(CFeatures *data=NULL)
The class Features is the base class of all feature objects.
Definition: Features.h:62
The Fully Independent Conditional Training inference method class.
virtual void set_latent_features(CFeatures *feat)
virtual EInferenceType get_inference_type() const
virtual SGVector< float64_t > get_predictive_means(SGVector< float64_t > mu, SGVector< float64_t > s2, const CLabels *lab=NULL) const =0
The Likelihood model base class.
CLikelihoodModel * get_model()
static CFITCInferenceMethod * obtain_from_generic(CInferenceMethod *inference)

SHOGUN Machine Learning Toolbox - Documentation