GaussianLikelihood.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  * Copyright (C) 2012 Jacob Walker
00008  */
00009 
00010 #include <shogun/regression/gp/GaussianLikelihood.h>
00011 #ifdef HAVE_EIGEN3
00012 #include <shogun/modelselection/ParameterCombination.h>
00013 #include <shogun/mathematics/eigen3.h>
00014 
00015 #include <shogun/base/Parameter.h>
00016 
00017 using namespace shogun;
00018 using namespace Eigen;
00019 
00020 CGaussianLikelihood::CGaussianLikelihood() : CLikelihoodModel()
00021 {
00022     init();
00023 }
00024 
00025 void CGaussianLikelihood::init()
00026 {
00027     m_sigma = 0.01;
00028     SG_ADD(&m_sigma, "sigma", "Observation Noise.", MS_AVAILABLE);
00029 }
00030 
00031 CGaussianLikelihood::~CGaussianLikelihood()
00032 {
00033 }
00034 
00035 
00036 SGVector<float64_t> CGaussianLikelihood::evaluate_means(
00037         SGVector<float64_t>& means)
00038 {
00039     return SGVector<float64_t>(means);
00040 }
00041 
00042 SGVector<float64_t> CGaussianLikelihood::evaluate_variances(
00043         SGVector<float64_t>& vars)
00044 {
00045     SGVector<float64_t> result(vars);
00046 
00047     for (index_t i = 0; i < result.vlen; i++)
00048         result[i] += (m_sigma*m_sigma);
00049 
00050     return result;
00051 }
00052 
00053 float64_t CGaussianLikelihood::get_log_probability_f(CRegressionLabels* labels,
00054         SGVector<float64_t> m_function)
00055 {
00056     Map<VectorXd> function(m_function.vector, m_function.vlen);
00057 
00058     VectorXd result(function.rows());
00059 
00060     for (index_t i = 0; i < function.rows(); i++)
00061         result[i] = labels->get_labels()[i] - function[i];
00062 
00063     result = result.cwiseProduct(result);
00064 
00065     result /= -2*m_sigma*m_sigma;
00066 
00067     for (index_t i = 0; i < function.rows(); i++)
00068         result[i] -= log(2*CMath::PI*m_sigma*m_sigma)/2.0;
00069 
00070     return result.sum();
00071 }
00072 
00073 SGVector<float64_t> CGaussianLikelihood::get_log_probability_derivative_f(
00074         CRegressionLabels* labels, SGVector<float64_t> m_function, index_t j)
00075 {
00076     Map<VectorXd> function(m_function.vector, m_function.vlen);
00077     VectorXd result(function.rows());
00078 
00079     for (index_t i = 0; i < function.rows(); i++)
00080         result[i] = labels->get_labels()[i] - function[i];
00081 
00082     if (j == 1)
00083         result = result/(m_sigma*m_sigma);
00084 
00085     else if (j == 2)
00086         result = -VectorXd::Ones(result.rows())/(m_sigma*m_sigma);
00087 
00088     else if (j == 3)
00089         result = VectorXd::Zero(result.rows());
00090 
00091     else
00092         SG_ERROR("Invalid Index for Likelihood Derivative\n");
00093 
00094     SGVector<float64_t> sgresult(result.rows());
00095     
00096     for (index_t i = 0; i < result.rows(); i++)
00097         sgresult[i] = result[i];
00098 
00099     return sgresult;
00100 }
00101 
00102 SGVector<float64_t> CGaussianLikelihood::get_first_derivative(CRegressionLabels* labels,
00103         TParameter* param,  CSGObject* obj, SGVector<float64_t> m_function)
00104 {
00105     Map<VectorXd> function(m_function.vector, m_function.vlen);
00106 
00107     VectorXd result(function.rows());
00108 
00109     SGVector<float64_t> sgresult(result.rows());
00110 
00111     if (strcmp(param->m_name, "sigma") || obj != this)
00112     {
00113         sgresult[0] = CMath::INFTY;
00114         return sgresult;
00115     }
00116 
00117     for (index_t i = 0; i < function.rows(); i++)
00118         result[i] = labels->get_labels()[i] - function[i];
00119 
00120     result = result.cwiseProduct(result);
00121 
00122     result /= m_sigma*m_sigma;
00123 
00124     for (index_t i = 0; i < function.rows(); i++)
00125         result[i] -= 1;
00126     
00127     for (index_t i = 0; i < result.rows(); i++)
00128         sgresult[i] = result[i];
00129 
00130     return sgresult;
00131 }
00132 
00133 SGVector<float64_t> CGaussianLikelihood::get_second_derivative(CRegressionLabels* labels,
00134         TParameter* param, CSGObject* obj, SGVector<float64_t> m_function)
00135 {
00136     Map<VectorXd> function(m_function.vector, m_function.vlen);
00137     VectorXd result(function.rows());
00138 
00139     SGVector<float64_t> sgresult(result.rows());
00140 
00141     if (strcmp(param->m_name, "sigma") || obj != this)
00142     {
00143         sgresult[0] = CMath::INFTY;
00144         return sgresult;
00145     }
00146 
00147     result = 2*VectorXd::Ones(function.rows())/(m_sigma*m_sigma);
00148 
00149     for (index_t i = 0; i < result.rows(); i++)
00150         sgresult[i] = result[i];
00151 
00152     return sgresult;
00153 }
00154 
00155 #endif //HAVE_EIGEN3
00156 
00157 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation