Skip to content

Commit

Permalink
Merge pull request #839 from louisfh/issue_809_GP_cov_functions
Browse files Browse the repository at this point in the history
Issue 809 gp cov functions
  • Loading branch information
bob-carpenter authored Dec 6, 2024
2 parents 9599d87 + 343a670 commit c6bf4e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/bibtex/all.bib
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,7 @@ @article{Magnusson+etal:2024:posteriordb
author={Magnusson, M{\aa}ns and Torgander, Jakob and B{\"u}rkner, Paul-Christian and Zhang, Lu and Carpenter, Bob and Vehtari, Aki},
journal={arXiv preprint arXiv:2407.04967},
year={2024}
}

@article{egozcue+etal:2003,
title={Isometric logratio transformations for compositional data analysis},
Expand Down
26 changes: 13 additions & 13 deletions src/stan-users-guide/gaussian-processes.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ data {
array[N] real x;
}
transformed data {
matrix[N, N] K = cov_exp_quad(x, 1.0, 1.0);
matrix[N, N] K = gp_exp_quad_cov(x, 1.0, 1.0);
vector[N] mu = rep_vector(0, N);
for (n in 1:N) {
K[n, n] = K[n, n] + 0.1;
Expand Down Expand Up @@ -345,7 +345,7 @@ parameters {
}
model {
matrix[N, N] L_K;
matrix[N, N] K = cov_exp_quad(x, alpha, rho);
matrix[N, N] K = gp_exp_quad_cov(x, alpha, rho);
real sq_sigma = square(sigma);
// diagonal elements
Expand Down Expand Up @@ -414,7 +414,7 @@ model {
vector[N] f;
{
matrix[N, N] L_K;
matrix[N, N] K = cov_exp_quad(x, alpha, rho);
matrix[N, N] K = gp_exp_quad_cov(x, alpha, rho);
// diagonal elements
for (n in 1:N) {
Expand Down Expand Up @@ -550,12 +550,12 @@ modeled hierarchically.
The implementation of automatic relevance determination in Stan is
straightforward, though it currently requires the user to directly code the
covariance matrix. We'll write a function to generate the Cholesky of the
covariance matrix called `L_cov_exp_quad_ARD`.
covariance matrix called `L_gp_exp_quad_cov_ARD`.


```stan
functions {
matrix L_cov_exp_quad_ARD(array[] vector x,
matrix L_gp_exp_quad_cov_ARD(array[] vector x,
real alpha,
vector rho,
real delta) {
Expand Down Expand Up @@ -592,7 +592,7 @@ parameters {
model {
vector[N] f;
{
matrix[N, N] L_K = L_cov_exp_quad_ARD(x, alpha, rho, delta);
matrix[N, N] L_K = L_gp_exp_quad_cov_ARD(x, alpha, rho, delta);
f = L_K * eta;
}
Expand Down Expand Up @@ -772,7 +772,7 @@ transformed parameters {
vector[N] f;
{
matrix[N, N] L_K;
matrix[N, N] K = cov_exp_quad(x, alpha, rho);
matrix[N, N] K = gp_exp_quad_cov(x, alpha, rho);
// diagonal elements
for (n in 1:N) {
Expand Down Expand Up @@ -858,7 +858,7 @@ transformed parameters {
vector[N] f;
{
matrix[N, N] L_K;
matrix[N, N] K = cov_exp_quad(x, alpha, rho);
matrix[N, N] K = gp_exp_quad_cov(x, alpha, rho);
// diagonal elements
for (n in 1:N) {
Expand Down Expand Up @@ -944,17 +944,17 @@ functions {
matrix[N2, N2] cov_f2;
matrix[N2, N2] diag_delta;
matrix[N1, N1] K;
K = cov_exp_quad(x1, alpha, rho);
K = gp_exp_quad_cov(x1, alpha, rho);
for (n in 1:N1) {
K[n, n] = K[n, n] + square(sigma);
}
L_K = cholesky_decompose(K);
K_div_y1 = mdivide_left_tri_low(L_K, y1);
K_div_y1 = mdivide_right_tri_low(K_div_y1', L_K)';
k_x1_x2 = cov_exp_quad(x1, x2, alpha, rho);
k_x1_x2 = gp_exp_quad_cov(x1, x2, alpha, rho);
f2_mu = (k_x1_x2' * K_div_y1);
v_pred = mdivide_left_tri_low(L_K, k_x1_x2);
cov_f2 = cov_exp_quad(x2, alpha, rho) - v_pred' * v_pred;
cov_f2 = gp_exp_quad_cov(x2, alpha, rho) - v_pred' * v_pred;
diag_delta = diag_matrix(rep_vector(delta, N2));
f2 = multi_normal_rng(f2_mu, cov_f2 + diag_delta);
Expand All @@ -981,7 +981,7 @@ parameters {
model {
matrix[N1, N1] L_K;
{
matrix[N1, N1] K = cov_exp_quad(x1, alpha, rho);
matrix[N1, N1] K = gp_exp_quad_cov(x1, alpha, rho);
real sq_sigma = square(sigma);
// diagonal elements
Expand Down Expand Up @@ -1095,7 +1095,7 @@ parameters {
model {
matrix[N, D] f;
{
matrix[N, N] K = cov_exp_quad(x, 1.0, rho);
matrix[N, N] K = gp_exp_quad_cov(x, 1.0, rho);
matrix[N, N] L_K;
// diagonal elements
Expand Down

0 comments on commit c6bf4e9

Please sign in to comment.