Skip to content

Commit

Permalink
Merge pull request #5 from onnela-lab/graph-length-scales
Browse files Browse the repository at this point in the history
Support different length scales for different dimensions in graph GPs.
  • Loading branch information
tillahoffmann authored Dec 12, 2024
2 parents a2067cc + 2ca176e commit 1b8601b
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 21 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ on:
branches: ["main"]
workflow_dispatch:

env:
cmdstanVersion: "2.31.0"

jobs:
build:
name: Build
strategy:
fail-fast: false
matrix:
runner:
- ubuntu-latest
Expand All @@ -28,6 +26,6 @@ jobs:
with:
extra-packages: |
any::rcmdcheck
cmdstanr=url::https://mc-stan.org/r-packages/src/contrib/cmdstanr_0.7.0.tar.gz
cmdstanr=url::https://stan-dev.r-universe.dev/src/contrib/cmdstanr_0.8.1.tar.gz
needs: check
- uses: r-lib/actions/check-r-package@v2
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gptoolsStan
Title: Gaussian Processes on Graphs and Lattices in 'Stan'
Version: 0.1.0
Version: 0.2.0
Authors@R: c(
person("Till", "Hoffmann", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4403-0722")),
Expand All @@ -23,5 +23,5 @@ Suggests:
rmarkdown,
cmdstanr
Additional_repositories:
https://mc-stan.org/r-packages/
https://stan-dev.r-universe.dev
VignetteBuilder: knitr
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# gptoolsStan 0.1.0

Initial release.

# gptoolsStan 0.2.0

- Graph-based approximations of Gaussian processes now support different length scales along different dimensions (ported from https://github.com/onnela-lab/gptools/pull/23). This change requires cmdstan 2.36.0 or above due to a bug in `gp_matern32_cov` (see https://github.com/stan-dev/math/pull/3084).
- Functions ending `_log_abs_det_jacobian` have been renamed to `_log_abs_det_jac` to comply with upcoming changes (see https://github.com/stan-dev/stanc3/issues/1470 for details).
22 changes: 19 additions & 3 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@
#'
#' # Compile the model with paths set up to include 'Stan' sources from 'gptoolsStan'.
#' model <- cmdstan_model(
#' stan_file="/path/to/your/model.stan",
#' include_paths=gptools_include_path(),
#' stan_file = "/path/to/your/model.stan",
#' include_paths = gptools_include_path(),
#' )
#' }
gptools_include_path <- function() {
system.file("extdata", package="gptoolsStan")
version <- package_version(cmdstanr::cmdstan_version())
if (version < package_version("2.36.0")) {
warning(
sprintf(
paste(
"cmdstan<2.36.0 had a bug in the evaluation of Matern 3/2 kernels",
"with different length scales for different dimensions; see",
"https://github.com/stan-dev/math/pull/3084 for details. Your",
"model may yield unexpected results or crash if you use",
"nearest-neighbor Gaussian processes with Matern 3/2 kernels.",
"Your cmdstan version is %s; consider upgrading."
),
version
)
)
}
system.file("extdata", package = "gptoolsStan")
}
7 changes: 5 additions & 2 deletions inst/extdata/gptools/fft1.stan
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ Evaluate the log absolute determinant of the Jacobian associated with
:param n: Size of the real signal. Necessary because the size cannot be inferred from :code:`rfft`.
:returns: Log absolute determinant of the Jacobian.
*/
real gp_rfft_log_abs_det_jacobian(vector cov_rfft, int n) {
real gp_rfft_log_abs_det_jac(vector cov_rfft, int n) {
vector[n %/% 2 + 1] rfft_scale = gp_evaluate_rfft_scale(cov_rfft, n);
return - sum(log(rfft_scale[1:n %/% 2 + 1])) -sum(log(rfft_scale[2:(n + 1) %/% 2]))
- log(2) * ((n - 1) %/% 2) + n * log(n) / 2;
}
real gp_rfft_log_abs_det_jacobian(vector cov_rfft, int n) {
reject("`gp_rfft_log_abs_det_jacobian` has been renamed to `gp_rfft_log_abs_det_jac` to comply with upcoming changes (see https://github.com/stan-dev/stanc3/issues/1470 for details).");
}


/**
Expand All @@ -94,7 +97,7 @@ real gp_rfft_lpdf(vector y, vector loc, vector cov_rfft) {
int n = size(y);
int nrfft = n %/% 2 + 1;
vector[n] z = gp_rfft(y, loc, cov_rfft);
return std_normal_lpdf(z) + gp_rfft_log_abs_det_jacobian(cov_rfft, n);
return std_normal_lpdf(z) + gp_rfft_log_abs_det_jac(cov_rfft, n);
}


Expand Down
7 changes: 5 additions & 2 deletions inst/extdata/gptools/fft2.stan
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Evaluate the log absolute determinant of the Jacobian associated with
Fourier transform :code:`cov_rfft2`).
:returns: Log absolute determinant of the Jacobian.
*/
real gp_rfft2_log_abs_det_jacobian(matrix cov_rfft2, int width) {
real gp_rfft2_log_abs_det_jac(matrix cov_rfft2, int width) {
int height = rows(cov_rfft2);
int n = width * height;
int fftwidth = width %/% 2 + 1;
Expand Down Expand Up @@ -171,6 +171,9 @@ real gp_rfft2_log_abs_det_jacobian(matrix cov_rfft2, int width) {
ladj += - log2() * nterms + n * log(n) / 2;
return ladj;
}
real gp_rfft2_log_abs_det_jacobian(matrix cov_rfft2, int width) {
reject("`gp_rfft2_log_abs_det_jacobian` has been renamed to `gp_rfft2_log_abs_det_jac` to comply with upcoming changes (see https://github.com/stan-dev/stanc3/issues/1470 for details).");
}


/**
Expand All @@ -185,7 +188,7 @@ Evaluate the log probability of a two-dimensional Gaussian process realization i
*/
real gp_rfft2_lpdf(matrix y, matrix loc, matrix cov_rfft2) {
return std_normal_lpdf(to_vector(gp_rfft2(y, loc, cov_rfft2)))
+ gp_rfft2_log_abs_det_jacobian(cov_rfft2, cols(y));
+ gp_rfft2_log_abs_det_jac(cov_rfft2, cols(y));
}


Expand Down
Loading

0 comments on commit 1b8601b

Please sign in to comment.