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 * Code adapted from Gaussian Process Machine Learning Toolbox 00010 * http://www.gaussianprocess.org/gpml/code/matlab/doc/ 00011 * 00012 */ 00013 00014 #ifndef CLAPLACIANINFERENCEMETHOD_H_ 00015 #define CLAPLACIANINFERENCEMETHOD_H_ 00016 00017 #include <shogun/lib/config.h> 00018 #ifdef HAVE_EIGEN3 00019 #ifdef HAVE_LAPACK 00020 00021 #include <shogun/regression/gp/InferenceMethod.h> 00022 00023 namespace shogun 00024 { 00025 00047 class CLaplacianInferenceMethod: public CInferenceMethod 00048 { 00049 00050 public: 00051 00052 /*Default Constructor*/ 00053 CLaplacianInferenceMethod(); 00054 00055 /* Constructor 00056 * @param kernel covariance function 00057 * @param features features to use in inference 00058 * @param mean mean function 00059 * @param labels labels of the features 00060 * @param model Likelihood model to use 00061 ] */ 00062 CLaplacianInferenceMethod(CKernel* kernel, CFeatures* features, 00063 CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); 00064 00065 /*Destructor*/ 00066 virtual ~CLaplacianInferenceMethod(); 00067 00077 virtual float64_t get_negative_marginal_likelihood(); 00078 00087 virtual CMap<TParameter*, SGVector<float64_t> > 00088 get_marginal_likelihood_derivatives( 00089 CMap<TParameter*, CSGObject*>& para_dict); 00090 00100 virtual SGVector<float64_t> get_alpha(); 00101 00102 00113 virtual SGMatrix<float64_t> get_cholesky(); 00114 00125 virtual SGVector<float64_t> get_diagonal_vector(); 00126 00132 virtual const char* get_name() const 00133 { 00134 return "LaplacianInferenceMethod"; 00135 } 00136 00137 /*Get the gradient 00138 * 00139 * @return Map of gradient. Keys are names of parameters, values are 00140 * values of derivative with respect to that parameter. 00141 */ 00142 virtual CMap<TParameter*, SGVector<float64_t> > get_gradient( 00143 CMap<TParameter*, CSGObject*>& para_dict) 00144 { 00145 return get_marginal_likelihood_derivatives(para_dict); 00146 } 00147 00148 /*Get the function value 00149 * 00150 * @return Vector that represents the function value 00151 */ 00152 virtual SGVector<float64_t> get_quantity() 00153 { 00154 SGVector<float64_t> result(1); 00155 result[0] = get_negative_marginal_likelihood(); 00156 return result; 00157 } 00158 00159 /*Get tolerance for Newton Iterations 00160 * 00161 * @return Tolerance for Newton Iterations 00162 */ 00163 virtual float64_t get_newton_tolerance() { 00164 return m_tolerance;} 00165 00166 /*Set tolerance for Newton Iterations 00167 * 00168 * @param Tolerance for Newton Iterations 00169 */ 00170 virtual void set_newton_tolerance(float64_t tol) { 00171 m_tolerance = tol;} 00172 00173 /*Get tolerance for Brent's Minimization Method 00174 * 00175 * @return tolerance for Brent's Minimization Method 00176 */ 00177 virtual float64_t get_minimization_tolerance() { 00178 return m_opt_tolerance;} 00179 00180 /*Set tolerance for Brent's Minimization Method 00181 * 00182 * @param tolerance for Brent's Minimization Method 00183 */ 00184 virtual void set_minimization_tolerance(float64_t tol) { 00185 m_opt_tolerance = tol;} 00186 00187 /*Get max iterations for Brent's Minimization Method 00188 * 00189 * @return max iterations for Brent's Minimization Method 00190 */ 00191 virtual int32_t get_minimization_iterations() { 00192 return m_max;} 00193 00194 /*Set max iterations for Brent's Minimization Method 00195 * 00196 * @param max iterations for Brent's Minimization Method 00197 */ 00198 virtual void set_minimization_tolerance(int32_t itr) { 00199 m_max = itr;} 00200 00201 /*Get max Newton iterations 00202 * 00203 * @return max Newton iterations 00204 */ 00205 virtual int32_t get_newton_iterations() { 00206 return m_max_itr;} 00207 00208 /*Set max Newton iterations 00209 * 00210 * @param max Newton iterations 00211 */ 00212 virtual void set_newton_tolerance(int32_t itr) { 00213 m_max_itr = itr;} 00214 00215 00216 00217 protected: 00220 virtual void update_alpha(); 00221 virtual void update_chol(); 00222 virtual void update_train_kernel(); 00223 virtual void update_all(); 00224 00225 private: 00226 00227 void init(); 00228 00229 private: 00230 00234 void check_members(); 00235 00236 /*Amount of tolerance for Newton's Iterations*/ 00237 float64_t m_tolerance; 00238 00239 /*Amount of tolerance for Brent's Minimization Method*/ 00240 float64_t m_opt_tolerance; 00241 00242 /*Max iterations for Brent's Minimization Method*/ 00243 float64_t m_max; 00244 00245 /*Max Newton Iterations*/ 00246 index_t m_max_itr; 00247 00248 /*Kernel Matrix*/ 00249 SGMatrix<float64_t> temp_kernel; 00250 00251 /*Eigen version of alpha vector*/ 00252 SGVector<float64_t> temp_alpha; 00253 00254 /*Function Location*/ 00255 SGVector<float64_t> function; 00256 00257 /*Noise Matrix*/ 00258 SGVector<float64_t> W; 00259 00260 /*Square root of W*/ 00261 SGVector<float64_t> sW; 00262 00263 /*Eigen version of means vector*/ 00264 SGVector<float64_t> m_means; 00265 00266 /*Derivative of log likelihood with respect 00267 * to function location 00268 */ 00269 SGVector<float64_t> dlp; 00270 00271 /*Second derivative of log likelihood with respect 00272 * to function location 00273 */ 00274 SGVector<float64_t> d2lp; 00275 00276 /*Third derivative of log likelihood with respect 00277 * to function location 00278 */ 00279 SGVector<float64_t> d3lp; 00280 00281 /*log likelihood*/ 00282 float64_t lp; 00283 00284 }; 00285 00286 } 00287 #endif // HAVE_EIGEN3 00288 #endif // HAVE_LAPACK 00289 00290 #endif /* CLAPLACIANINFERENCEMETHOD_H_ */