-
-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add prim and rev gp_exp_cov #859
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
098613d
add prim and rev gp_exp_cov
bd69dc2
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot f9a960b
Update gp_exponential_cov.hpp
3df0bc9
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot e46bd66
add rev mat.hpp
77e77da
temp
5212d97
temp
26ca567
use ChainableStack::instance()
1cc69b9
Merge commit '5cd004ca651c13d63a460c62b86c8a366a8fe830' into HEAD
yashikno 644d11d
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot 13b7475
use ChainableStack::instance()
47b2805
fix merge conflix
d565a9e
proper includes
67c0557
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot 8599e40
working updates for PR review
7a4ea7b
[Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/…
stan-buildbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
#ifndef STAN_MATH_PRIM_MAT_FUN_GP_EXPONENTIAL_COV_HPP | ||
#define STAN_MATH_PRIM_MAT_FUN_GP_EXPONENTIAL_COV_HPP | ||
|
||
#include <stan/math/prim/mat/fun/Eigen.hpp> | ||
#include <stan/math/prim/scal/err/check_positive_finite.hpp> | ||
#include <stan/math/prim/scal/fun/square.hpp> | ||
#include <stan/math/prim/scal/fun/squared_distance.hpp> | ||
#include <stan/math/prim/scal/meta/return_type.hpp> | ||
#include <cmath> | ||
#include <vector> | ||
|
||
namespace stan { | ||
namespace math { | ||
|
||
/** | ||
* Returns a Matern exponential covariance matrix with one input vector | ||
* | ||
* \f[ k(x, x') = \sigma^2 exp(-\frac{d(x, x')}{l}) \f] | ||
* | ||
* where \f$ d(x, x') \f$ is the squared distance, or the dot product. | ||
* | ||
* See Rausmussen & Williams et al 2006 Chapter 4. | ||
* | ||
* @param x std::vector of elements that can be used in stan::math::distance | ||
* @param sigma standard deviation that can be used in stan::math::square | ||
* @param length_scale length scale | ||
* @throw std::domain error if sigma <= 0, l <= 0, or x is nan or inf | ||
* | ||
*/ | ||
template <typename T_x, typename T_s, typename T_l> | ||
inline typename Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, | ||
Eigen::Dynamic, Eigen::Dynamic> | ||
gp_exponential_cov(const std::vector<T_x> &x, const T_s &sigma, | ||
const T_l &length_scale) { | ||
using std::exp; | ||
using std::pow; | ||
|
||
size_t x_size = x.size(); | ||
for (size_t n = 0; n < x_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x", x[n]); | ||
|
||
check_positive_finite("gp_exponential_cov", "marginal variance", sigma); | ||
check_positive_finite("gp_exponential_cov", "length-scale", length_scale); | ||
|
||
Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, Eigen::Dynamic, | ||
Eigen::Dynamic> | ||
cov(x_size, x_size); | ||
|
||
if (x_size == 0) | ||
return cov; | ||
|
||
T_s sigma_sq = square(sigma); | ||
T_l neg_inv_l = -1.0 / length_scale; | ||
|
||
for (size_t i = 0; i < (x_size - 1); ++i) { | ||
cov(i, i) = sigma_sq; | ||
for (size_t j = i + 1; j < x_size; ++j) { | ||
cov(i, j) = sigma_sq * exp(neg_inv_l * squared_distance(x[i], x[j])); | ||
cov(j, i) = cov(i, j); | ||
} | ||
} | ||
cov(x_size - 1, x_size - 1) = sigma_sq; | ||
return cov; | ||
} | ||
|
||
/** | ||
* Returns a Matern exponential covariance matrix with one input vector | ||
* with automatic relevance determination (ARD) | ||
* | ||
* \f[ k(x, x') = \sigma^2 exp(-\sum_{k=1}^K\frac{d(x, x')}{l_k}) \f] | ||
* | ||
* where \f$ d(x, x') \f$ is the squared distance, or dot product. | ||
* | ||
* See Rausmussen & Williams et al 2006 Chapter 4. | ||
* | ||
* @param x std::vector of elements that can be used in stan::math::distance | ||
* @param sigma standard deviation that can be used in stan::math::square | ||
* @param length_scale length scale | ||
* @throw std::domain error if sigma <= 0, l <= 0, or x is nan or inf | ||
* | ||
*/ | ||
template <typename T_x, typename T_s, typename T_l> | ||
inline typename Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, | ||
Eigen::Dynamic, Eigen::Dynamic> | ||
gp_exponential_cov(const std::vector<T_x> &x, const T_s &sigma, | ||
const std::vector<T_l> &length_scale) { | ||
using std::exp; | ||
using std::pow; | ||
|
||
size_t x_size = x.size(); | ||
size_t l_size = length_scale.size(); | ||
for (size_t n = 0; n < x_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x", x[n]); | ||
|
||
check_positive_finite("gp_exponential_cov", "marginal variance", sigma); | ||
check_positive_finite("gp_exponential_cov", "length-scale", length_scale); | ||
|
||
Eigen::Matrix<typename stan::return_type<T_x, T_s, T_l>::type, Eigen::Dynamic, | ||
Eigen::Dynamic> | ||
cov(x_size, x_size); | ||
|
||
if (x_size == 0) | ||
return cov; | ||
|
||
T_s sigma_sq = square(sigma); | ||
T_l temp; | ||
|
||
for (size_t i = 0; i < (x_size - 1); ++i) { | ||
cov(i, i) = sigma_sq; | ||
for (size_t j = i + 1; j < x_size; ++j) { | ||
temp = 0; | ||
for (size_t k = 0; k < l_size; ++k) | ||
temp += 1.0 / length_scale[k]; | ||
cov(i, j) = sigma_sq * exp(-1.0 * squared_distance(x[i], x[j]) * temp); | ||
cov(j, i) = cov(i, j); | ||
} | ||
} | ||
cov(x_size - 1, x_size - 1) = sigma_sq; | ||
return cov; | ||
} | ||
|
||
/** | ||
* Returns a Matern exponential covariance matrix with two input vectors | ||
* | ||
* \f[ k(x, x') = \sigma^2 exp(-\frac{d(x, x')}{l}) \f] | ||
* | ||
* where \f$ d(x, x') \f$ is the squared distance, or dot product. | ||
* | ||
* See Rausmussen & Williams et al 2006 Chapter 4. | ||
* | ||
* @param x1 std::vector of elements that can be used in stan::math::distance | ||
* @param x2 std::vector of elements that can be used in stan::math::distance | ||
* @param sigma standard deviation that can be used in stan::math::square | ||
* @param length_scale length scale | ||
* @throw std::domain error if sigma <= 0, l <= 0, or x is nan or inf | ||
* | ||
*/ | ||
template <typename T_x1, typename T_x2, typename T_s, typename T_l> | ||
inline typename Eigen::Matrix< | ||
typename stan::return_type<T_x1, T_x2, T_s, T_l>::type, Eigen::Dynamic, | ||
Eigen::Dynamic> | ||
gp_exponential_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2, | ||
const T_s &sigma, const T_l &length_scale) { | ||
using std::exp; | ||
using std::pow; | ||
|
||
size_t x1_size = x1.size(); | ||
size_t x2_size = x2.size(); | ||
|
||
for (size_t n = 0; n < x1_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x1", x1[n]); | ||
for (size_t n = 0; n < x2_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x2", x2[n]); | ||
|
||
check_positive_finite("gp_exponential_cov", "marginal variance", sigma); | ||
check_positive_finite("gp_exponential_cov", "length-scale", length_scale); | ||
|
||
Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_s, T_l>::type, | ||
Eigen::Dynamic, Eigen::Dynamic> | ||
cov(x1_size, x2_size); | ||
|
||
if (x1_size == 0 || x2_size == 0) | ||
return cov; | ||
|
||
T_s sigma_sq = square(sigma); | ||
T_l neg_inv_l = -1.0 / length_scale; | ||
|
||
for (size_t i = 0; i < x1_size; ++i) { | ||
for (size_t j = 0; j < x2_size; ++j) { | ||
cov(i, j) = sigma_sq * exp(neg_inv_l * squared_distance(x1[i], x2[j])); | ||
} | ||
} | ||
return cov; | ||
} | ||
|
||
/** | ||
* Returns a Matern exponential covariance matrix with two input vectors | ||
* with automatic relevance determination (ARD) | ||
* | ||
* \f[ k(x, x') = \sigma^2 exp(-\sum_{k=1}^K\frac{d(x, x')}{l_k}) \f] | ||
* | ||
* where \f$ d(x, x') \f$ is the squared distance, or dot product. | ||
* | ||
* See Rausmussen & Williams et al 2006 Chapter 4. | ||
* | ||
* @param x1 std::vector of elements that can be used in stan::math::distance | ||
* @param x2 std::vector of elements that can be used in stan::math::distance | ||
* @param sigma standard deviation that can be used in stan::math::square | ||
* @param length_scale length scale | ||
* @throw std::domain error if sigma <= 0, l <= 0, or x is nan or inf | ||
* | ||
*/ | ||
template <typename T_x1, typename T_x2, typename T_s, typename T_l> | ||
inline typename Eigen::Matrix< | ||
typename stan::return_type<T_x1, T_x2, T_s, T_l>::type, Eigen::Dynamic, | ||
Eigen::Dynamic> | ||
gp_exponential_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2, | ||
const T_s &sigma, const std::vector<T_l> &length_scale) { | ||
using std::exp; | ||
using std::pow; | ||
|
||
size_t x1_size = x1.size(); | ||
size_t x2_size = x2.size(); | ||
size_t l_size = length_scale.size(); | ||
|
||
for (size_t n = 0; n < x1_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x1", x1[n]); | ||
for (size_t n = 0; n < x2_size; ++n) | ||
check_not_nan("gp_exponential_cov", "x2", x2[n]); | ||
|
||
check_positive_finite("gp_exponential_cov", "marginal variance", sigma); | ||
check_positive_finite("gp_exponential_cov", "length-scale", length_scale); | ||
|
||
Eigen::Matrix<typename stan::return_type<T_x1, T_x2, T_s, T_l>::type, | ||
Eigen::Dynamic, Eigen::Dynamic> | ||
cov(x1_size, x2_size); | ||
|
||
if (x1_size == 0 || x2_size == 0) | ||
return cov; | ||
|
||
T_s sigma_sq = square(sigma); | ||
T_l temp; | ||
|
||
for (size_t i = 0; i < x1_size; ++i) { | ||
for (size_t j = 0; j < x2_size; ++j) { | ||
temp = 0; | ||
for (size_t k = 0; k < l_size; ++k) | ||
temp += 1.0 / length_scale[k]; | ||
cov(i, j) = sigma_sq * exp(-1.0 * squared_distance(x1[i], x2[j]) * temp); | ||
} | ||
} | ||
return cov; | ||
} | ||
} // namespace math | ||
} // namespace stan | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking at equation (4.18) in RW and not seeing the
sigma
term in there. Did you include this for convenience?@avehtari, would you mind commenting on two things:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@syclik see Betancourt's post on discourse: http://discourse.mc-stan.org/t/adding-gaussian-process-covariance-functions/237/42?u=drezap
I am following the conventions there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing out the discussion. One thing that it's missing is the
jitter
term (also referred to asnugget
).I'm going to kick this question over to @avehtari. Specifically, Aki, what's the most common form of this kernel?
@drezap, if we stick to this, I'd just like to see the note indicate that we're not using R&W's parameterization as-is.