Gaussian Processes
A homebrewed C++ library for Gaussian Processes.
rbf_kernel.cpp
1 /*
2  * Copyright (c) 2017, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 ///////////////////////////////////////////////////////////////////////////////
37 //
38 // Defines the RbfKernel class, which is derived from the Kernel base class.
39 // The RBF kernel is a function k(x, y) = exp(-0.5 (x-y)^T inv(L) (x-y)), where
40 // x, y are points in R^n and L is a diagonal matrix of squared length scales.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #include <kernels/rbf_kernel.hpp>
45 
46 #include <math.h>
47 
48 namespace gp {
49 
50  // Factory method.
51  Kernel::Ptr RbfKernel::Create(const VectorXd& lengths) {
52  Kernel::Ptr ptr(new RbfKernel(lengths));
53  return ptr;
54  }
55 
56  // Constructor.
57  RbfKernel::RbfKernel(const VectorXd& lengths)
58  : Kernel(lengths) {}
59 
60  // Pure virtual methods to be implemented in a derived class.
61  double RbfKernel::Evaluate(const VectorXd& x, const VectorXd& y) const {
62  const VectorXd diff = x - y;
63 
64  return std::exp(-0.5 * diff.cwiseQuotient(params_).squaredNorm());
65  }
66 
67  double RbfKernel::Partial(const VectorXd& x, const VectorXd& y,
68  size_t ii) const {
69  CHECK_LT(ii, params_.size());
70  const VectorXd diff = x - y;
71 
72  // Evaluate the kernel.
73  const double kernel =
74  std::exp(-0.5 * diff.cwiseQuotient(params_).squaredNorm());
75 
76  return kernel * diff(ii) * diff(ii) /
77  (params_(ii) * params_(ii) * params_(ii));
78  }
79 
80  void RbfKernel::Gradient(const VectorXd& x, const VectorXd& y,
81  VectorXd& gradient) const {
82  const VectorXd diff = x - y;
83 
84  // Evaluate the kernel.
85  const double kernel =
86  std::exp(-0.5 * diff.cwiseQuotient(params_).squaredNorm());
87 
88  gradient = kernel * diff.cwiseProduct(diff).cwiseQuotient(
89  params_.cwiseProduct(params_).cwiseProduct(params_));
90  }
91 
92 
93 } //\namespace gp