SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
regression
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
15
#include <
shogun/regression/GaussianProcessRegression.h
>
16
17
#ifdef HAVE_EIGEN3
18
19
#include <
shogun/io/SGIO.h
>
20
#include <
shogun/machine/gp/FITCInferenceMethod.h
>
21
22
using namespace
shogun;
23
24
CGaussianProcessRegression::CGaussianProcessRegression
()
25
:
CGaussianProcessMachine
()
26
{
27
}
28
29
CGaussianProcessRegression::CGaussianProcessRegression
(
CInferenceMethod
* method)
30
:
CGaussianProcessMachine
(method)
31
{
32
// set labels
33
m_labels
=method->
get_labels
();
34
}
35
36
CGaussianProcessRegression::~CGaussianProcessRegression
()
37
{
38
}
39
40
CRegressionLabels
*
CGaussianProcessRegression::apply_regression
(
CFeatures
* data)
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"
)
45
CLikelihoodModel
* lik=
m_method
->
get_model
();
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
59
if
(
m_method
->
get_inference_type
()==
INF_FITC
)
60
{
61
CFITCInferenceMethod
* fitc_method=
62
CFITCInferenceMethod::obtain_from_generic
(
m_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
81
bool
CGaussianProcessRegression::train_machine
(
CFeatures
* data)
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"
)
86
CLikelihoodModel
* lik=
m_method
->
get_model
();
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
94
if
(
m_method
->
get_inference_type
()==
INF_FITC
)
95
{
96
CFITCInferenceMethod
* fitc_method=
97
CFITCInferenceMethod::obtain_from_generic
(
m_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
106
m_method
->
update
();
107
108
return
true
;
109
}
110
111
SGVector<float64_t>
CGaussianProcessRegression::get_mean_vector
(
CFeatures
* data)
112
{
113
// check whether given combination of inference method and likelihood
114
// function supports regression
115
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
116
CLikelihoodModel
* lik=
m_method
->
get_model
();
117
REQUIRE
(
m_method
->
supports_regression
(),
"%s with %s doesn't support "
118
"regression\n"
,
m_method
->
get_name
(), lik->
get_name
())
119
SG_UNREF
(lik);
120
121
SG_REF
(data);
122
SGVector<float64_t>
mu=
get_posterior_means
(data);
123
SGVector<float64_t>
s2=
get_posterior_variances
(data);
124
SG_UNREF
(data);
125
126
// evaluate mean
127
lik=
m_method
->
get_model
();
128
mu=lik->
get_predictive_means
(mu, s2);
129
SG_UNREF
(lik);
130
131
return
mu;
132
}
133
134
SGVector<float64_t>
CGaussianProcessRegression::get_variance_vector
(
135
CFeatures
* data)
136
{
137
// check whether given combination of inference method and likelihood
138
// function supports regression
139
REQUIRE
(
m_method
,
"Inference method should not be NULL\n"
)
140
CLikelihoodModel
* lik=
m_method
->
get_model
();
141
REQUIRE
(
m_method
->
supports_regression
(),
"%s with %s doesn't support "
142
"regression\n"
,
m_method
->
get_name
(), lik->
get_name
())
143
144
SG_REF
(data);
145
SGVector<float64_t>
mu=
get_posterior_means
(data);
146
SGVector<float64_t>
s2=
get_posterior_variances
(data);
147
SG_UNREF
(data);
148
149
// evaluate variance
150
s2=lik->
get_predictive_variances
(mu, s2);
151
SG_UNREF
(lik);
152
153
return
s2;
154
}
155
156
#endif
SHOGUN
Machine Learning Toolbox - Documentation