SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
GaussianProcessRegression.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2013 Roman Votyakov
00008  * Copyright (C) 2012 Jacob Walker
00009  * Copyright (C) 2013 Roman Votyakov
00010  *
00011  * Code adapted from Gaussian Process Machine Learning Toolbox
00012  * http://www.gaussianprocess.org/gpml/code/matlab/doc/
00013  */
00014 
00015 #include <shogun/regression/GaussianProcessRegression.h>
00016 
00017 #ifdef HAVE_EIGEN3
00018 
00019 #include <shogun/io/SGIO.h>
00020 #include <shogun/machine/gp/FITCInferenceMethod.h>
00021 
00022 using namespace shogun;
00023 
00024 CGaussianProcessRegression::CGaussianProcessRegression()
00025         : CGaussianProcessMachine()
00026 {
00027 }
00028 
00029 CGaussianProcessRegression::CGaussianProcessRegression(CInferenceMethod* method)
00030         : CGaussianProcessMachine(method)
00031 {
00032     // set labels
00033     m_labels=method->get_labels();
00034 }
00035 
00036 CGaussianProcessRegression::~CGaussianProcessRegression()
00037 {
00038 }
00039 
00040 CRegressionLabels* CGaussianProcessRegression::apply_regression(CFeatures* data)
00041 {
00042     // check whether given combination of inference method and likelihood
00043     // function supports regression
00044     REQUIRE(m_method, "Inference method should not be NULL\n")
00045     CLikelihoodModel* lik=m_method->get_model();
00046     REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
00047             "regression\n", m_method->get_name(), lik->get_name())
00048     SG_UNREF(lik);
00049 
00050     CRegressionLabels* result;
00051 
00052     // if regression data equals to NULL, then apply regression on training
00053     // features
00054     if (!data)
00055     {
00056         CFeatures* feat;
00057 
00058         // use latent features for FITC inference method
00059         if (m_method->get_inference_type()==INF_FITC)
00060         {
00061             CFITCInferenceMethod* fitc_method=
00062                 CFITCInferenceMethod::obtain_from_generic(m_method);
00063             feat=fitc_method->get_latent_features();
00064             SG_UNREF(fitc_method);
00065         }
00066         else
00067             feat=m_method->get_features();
00068 
00069         result=new CRegressionLabels(get_mean_vector(feat));
00070 
00071         SG_UNREF(feat);
00072     }
00073     else
00074     {
00075         result=new CRegressionLabels(get_mean_vector(data));
00076     }
00077 
00078     return result;
00079 }
00080 
00081 bool CGaussianProcessRegression::train_machine(CFeatures* data)
00082 {
00083     // check whether given combination of inference method and likelihood
00084     // function supports regression
00085     REQUIRE(m_method, "Inference method should not be NULL\n")
00086     CLikelihoodModel* lik=m_method->get_model();
00087     REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
00088             "regression\n", m_method->get_name(), lik->get_name())
00089     SG_UNREF(lik);
00090 
00091     if (data)
00092     {
00093         // set latent features for FITC inference method
00094         if (m_method->get_inference_type()==INF_FITC)
00095         {
00096             CFITCInferenceMethod* fitc_method=
00097                 CFITCInferenceMethod::obtain_from_generic(m_method);
00098             fitc_method->set_latent_features(data);
00099             SG_UNREF(fitc_method);
00100         }
00101         else
00102             m_method->set_features(data);
00103     }
00104 
00105     // perform inference
00106     if (m_method->update_parameter_hash())
00107         m_method->update();
00108 
00109     return true;
00110 }
00111 
00112 SGVector<float64_t> CGaussianProcessRegression::get_mean_vector(CFeatures* data)
00113 {
00114     // check whether given combination of inference method and likelihood
00115     // function supports regression
00116     REQUIRE(m_method, "Inference method should not be NULL\n")
00117     CLikelihoodModel* lik=m_method->get_model();
00118     REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
00119             "regression\n", m_method->get_name(), lik->get_name())
00120     SG_UNREF(lik);
00121 
00122     SG_REF(data);
00123     SGVector<float64_t> mu=get_posterior_means(data);
00124     SGVector<float64_t> s2=get_posterior_variances(data);
00125     SG_UNREF(data);
00126 
00127     // evaluate mean
00128     lik=m_method->get_model();
00129     mu=lik->get_predictive_means(mu, s2);
00130     SG_UNREF(lik);
00131 
00132     return mu;
00133 }
00134 
00135 SGVector<float64_t> CGaussianProcessRegression::get_variance_vector(
00136         CFeatures* data)
00137 {
00138     // check whether given combination of inference method and likelihood
00139     // function supports regression
00140     REQUIRE(m_method, "Inference method should not be NULL\n")
00141     CLikelihoodModel* lik=m_method->get_model();
00142     REQUIRE(m_method->supports_regression(), "%s with %s doesn't support "
00143             "regression\n", m_method->get_name(), lik->get_name())
00144 
00145     SG_REF(data);
00146     SGVector<float64_t> mu=get_posterior_means(data);
00147     SGVector<float64_t> s2=get_posterior_variances(data);
00148     SG_UNREF(data);
00149 
00150     // evaluate variance
00151     s2=lik->get_predictive_variances(mu, s2);
00152     SG_UNREF(lik);
00153 
00154     return s2;
00155 }
00156 
00157 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation