Skip to content

Commit

Permalink
Merge pull request #28 from andrjohns/array-syntax
Browse files Browse the repository at this point in the history
Update deprecated syntax for future rstan compatibility
  • Loading branch information
jtimonen authored Sep 17, 2023
2 parents fff5764 + fa59496 commit c486ff4
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 198 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Imports:
Rcpp (>= 0.12.0),
RcppParallel (>= 5.0.2),
RCurl (>= 1.98),
rstan (>= 2.21.2),
rstan (>= 2.26.0),
rstantools (>= 2.3.1),
bayesplot (>= 1.7.0),
MASS (>= 7.3-50),
Expand All @@ -44,8 +44,8 @@ LinkingTo:
Rcpp (>= 1.0.6),
RcppEigen (>= 0.3.3.9.1),
RcppParallel (>= 5.0.2),
rstan (>= 2.21.2),
StanHeaders (>= 2.21.0-7)
rstan (>= 2.26.0),
StanHeaders (>= 2.26.0)
SystemRequirements: GNU make
NeedsCompilation: yes
RoxygenNote: 7.2.3
Expand Down
10 changes: 5 additions & 5 deletions inst/stan/_common/data-covariates.stan
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Covariates
vector[num_obs] x_cont[num_cov_cont];
vector[num_obs] x_cont_unnorm[num_cov_cont];
int x_cont_mask[num_cov_cont, num_obs];
int x_cat[num_cov_cat, num_obs];
array[num_cov_cont] vector[num_obs] x_cont;
array[num_cov_cont] vector[num_obs] x_cont_unnorm;
array[num_cov_cont, num_obs] int x_cont_mask;
array[num_cov_cat, num_obs] int x_cat;

// Expanding beta or teff to a vector of length equal to num_obs.
// The value idx_expand[j]-1 tells the index of the beta or teff parameter
// that should be used for observation j. If observation j doesn't
// correspond to any beta parameter, then idx_expand[j] should be 1.
int<lower=1, upper=num_bt+1> idx_expand[num_obs];
array[num_obs] int<lower=1, upper=num_bt+1> idx_expand;
12 changes: 6 additions & 6 deletions inst/stan/_common/data-general.stan
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
* kernel 0 = zero-sum kernel * [exp. quadratic]
* kernel 1 = categorical kernel * [exp. quadratic]
*/
int<lower=0> components[num_comps, 9];
array[num_comps, 9] int<lower=0> components;

// Observed effect times and uncertainty bounds for each case subject
vector[num_bt] teff_zero[num_uncrt>0];
vector[num_bt] teff_lb[num_uncrt>0];
vector[num_bt] teff_ub[num_uncrt>0];
array[num_uncrt>0] vector[num_bt] teff_zero;
array[num_uncrt>0] vector[num_bt] teff_lb;
array[num_uncrt>0] vector[num_bt] teff_ub;

// Misc
int<lower=0> x_cat_num_levels[num_cov_cat];
array[num_cov_cat] int<lower=0> x_cat_num_levels;
real delta; // jitter to ensure pos. def. kernel matrices
real vm_params[2]; // variance mask parameters (nonstat comps)
array[2] real vm_params; // variance mask parameters (nonstat comps)
18 changes: 9 additions & 9 deletions inst/stan/_common/data-priors.stan
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Priors types and hyperparameters
int<lower=0> prior_alpha[num_comps, 2]; // {prior_type, transform}
int<lower=0> prior_ell[num_ell, 2]; // {prior_type, transform}
int<lower=0> prior_wrp[num_ns, 2]; // {prior_type, transform}
int<lower=0> prior_teff[num_uncrt>0, 2]; // {prior type, is_backwards}
real hyper_alpha[num_comps, 3];
real hyper_ell[num_ell, 3];
real hyper_wrp[num_ns, 3];
real hyper_teff[num_uncrt>0, 3];
real hyper_beta[num_heter>0, 2];
array[num_comps, 2] int<lower=0> prior_alpha; // {prior_type, transform}
array[num_ell, 2] int<lower=0> prior_ell; // {prior_type, transform}
array[num_ns, 2] int<lower=0> prior_wrp; // {prior_type, transform}
array[num_uncrt>0, 2] int<lower=0> prior_teff; // {prior type, is_backwards}
array[num_comps, 3] real hyper_alpha;
array[num_ell, 3] real hyper_ell;
array[num_ns, 3] real hyper_wrp;
array[num_uncrt>0, 3] real hyper_teff;
array[num_heter>0, 2] real hyper_beta;
30 changes: 15 additions & 15 deletions inst/stan/_common/functions-basisfun.stan
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@

// Compute num_comps basis function matrices Phi, which each have shape
// [num_obs, num_basisfun]
matrix[] STAN_phi_matrix(data vector[] x, data int M, data real L,
data int[,] components){
array[] matrix STAN_phi_matrix(data array[] vector x, data int M, data real L,
data array[,] int components){
int n = num_elements(x[1]);
int J = size(components);
matrix[n, M] PHI[J];
array[J] matrix[n, M] PHI;
for(j in 1:J) {
matrix[n, M] PHI_j = rep_matrix(1.0, n, M); //
if(components[j,1] > 0) {
Expand All @@ -108,10 +108,10 @@
}

// Compute diagonals of diagonal matrices Lambda
vector[] STAN_lambda_matrix(real[] ell, data int M, data real L,
data int[,] components){
array[] vector STAN_lambda_matrix(array[] real ell, data int M, data real L,
data array[,] int components){
int J = size(components);
vector[M] Lambda[J];
array[J] vector[M] Lambda;
int j_ell = 0;
for(j in 1:J) {
if (components[j,1] > 0) {
Expand All @@ -127,9 +127,9 @@
}

// Get ranks of the constant kernel matrices
int[] STAN_ranks(data int[,] components, data int[] x_cat_num_levels){
array[] int STAN_ranks(data array[,] int components, data array[] int x_cat_num_levels){
int J = size(components);
int ranks[J] = rep_array(1, J);
array[J] int ranks = rep_array(1, J);
for (j in 1:J) {
int idx_cat = components[j,8];
if (idx_cat > 0) {
Expand All @@ -140,8 +140,8 @@
}

// Compute diagonals of Delta
vector STAN_delta_matrix(data matrix[] K_const, data int[] ranks,
data int[,] components){
vector STAN_delta_matrix(data array[] matrix K_const, data array[] int ranks,
data array[,] int components){
int J = size(ranks);
int n = cols(K_const[1]);
int R = sum(ranks);
Expand All @@ -161,8 +161,8 @@
}

// Compute eigenvector matrix Theta
matrix STAN_theta_matrix(data matrix[] K_const, data int[] ranks,
data int[,] components){
matrix STAN_theta_matrix(data array[] matrix K_const, data array[] int ranks,
data array[,] int components){
int J = size(ranks);
int n = cols(K_const[1]);
int R = sum(ranks);
Expand All @@ -184,8 +184,8 @@
// Create D, a vector of length num_basisfun*sum(ranks)
// - bfa_lambda = a an array of num_comps vectors of shape [num_basisfun]
// - bfa_delta = a vector of shape [R]
vector STAN_D_matrix(real[] alpha, vector[] bfa_lambda,
vector bfa_delta, int[] ranks) {
vector STAN_D_matrix(array[] real alpha, array[] vector bfa_lambda,
vector bfa_delta, array[] int ranks) {
int M = num_elements(bfa_lambda[1]);
int RM = sum(ranks)*M;
int J = size(ranks);
Expand All @@ -208,7 +208,7 @@
// - bfa_phi = array of matrices with shape [num_obs, num_basisfun], length
// num_comps
// - bfa_theta = matrix of shape [num_obs, R]
matrix STAN_V_matrix(matrix[] bfa_phi, matrix bfa_theta, int[] ranks) {
matrix STAN_V_matrix(array[] matrix bfa_phi, matrix bfa_theta, array[] int ranks) {
int J = size(bfa_phi);
int n = rows(bfa_phi[1]);
int M = cols(bfa_phi[1]);
Expand Down
62 changes: 31 additions & 31 deletions inst/stan/_common/functions-kernels.stan
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Categorical zero-sum kernel
matrix STAN_kernel_zerosum(data int[] x1, data int[] x2, data int num_cat) {
matrix STAN_kernel_zerosum(data array[] int x1, data array[] int x2, data int num_cat) {
int n1 = size(x1);
int n2 = size(x2);
matrix[n1, n2] K;
Expand All @@ -16,7 +16,7 @@
}

// Categorical kernel
matrix STAN_kernel_cat(data int[] x1, data int[] x2) {
matrix STAN_kernel_cat(data array[] int x1, data array[] int x2) {
int n1 = size(x1);
int n2 = size(x2);
matrix[n1,n2] K;
Expand All @@ -29,7 +29,7 @@
}

// Binary mask kernel
matrix STAN_kernel_bin(data int[] x1, data int[] x2) {
matrix STAN_kernel_bin(data array[] int x1, data array[] int x2) {
int n1 = size(x1);
int n2 = size(x2);
matrix[n1,n2] K;
Expand All @@ -43,7 +43,7 @@

// Compute one constant kernel matrix. Does not depend on parameters and
// therefore this function never needs to be evaluated during sampling.
matrix STAN_kernel_const(data int[] x1, data int[] x2,
matrix STAN_kernel_const(data array[] int x1, data array[] int x2,
data int kernel_type, data int ncat)
{
int n1 = num_elements(x1);
Expand All @@ -62,17 +62,17 @@

// Compute all constant kernel matrices. These do not depend on parameters and
// therefore this function never needs to be evaluated during sampling.
matrix[] STAN_kernel_const_all(
array[] matrix STAN_kernel_const_all(
data int n1, data int n2,
data int[,] x1, data int[,] x2,
data int[,] x1_mask, data int[,] x2_mask,
data int[] num_levels, data int[,] components)
data array[,] int x1, data array[,] int x2,
data array[,] int x1_mask, data array[,] int x2_mask,
data array[] int num_levels, data array[,] int components)
{
int num_comps = size(components);
matrix[n1, n2] K_const[num_comps];
array[num_comps] matrix[n1, n2] K_const;
for (j in 1:num_comps) {
matrix[n1, n2] K;
int opts[9] = components[j];
array[9] int opts = components[j];
int ctype = opts[1];
int ktype = opts[2];
int idx_cat = opts[8];
Expand All @@ -97,12 +97,12 @@

// Exponentiated quadratic kernel (with vector inputs)
matrix STAN_kernel_eq(vector x1, vector x2, real alpha, real ell) {
return(cov_exp_quad(to_array_1d(x1), to_array_1d(x2), alpha, ell));
return(gp_exp_quad_cov(to_array_1d(x1), to_array_1d(x2), alpha, ell));
}

// Multiplier matrix to enable variance masking
matrix STAN_kernel_varmask(vector x1, vector x2,
real steepness, data real[] vm_params)
real steepness, data array[] real vm_params)
{
real a = steepness * vm_params[2];
real r = inv(a)*logit(vm_params[1]);
Expand All @@ -113,7 +113,7 @@
}

// Multiplier matrix to enable heterogeneous effects
matrix STAN_kernel_beta(vector beta, int[] idx1_expand, int[] idx2_expand) {
matrix STAN_kernel_beta(vector beta, array[] int idx1_expand, array[] int idx2_expand) {
return(
to_matrix(STAN_expand(sqrt(beta), idx1_expand)) *
transpose(to_matrix(STAN_expand(sqrt(beta), idx2_expand)))
Expand All @@ -124,30 +124,30 @@
Compute all kernel matrices. These depend on parameters and
therefore this function needs to be evaluated repeatedly during sampling.
*/
matrix[] STAN_kernel_all(
array[] matrix STAN_kernel_all(
data int n1,
data int n2,
data matrix[] K_const,
data int[,] components,
data vector[] x1,
data vector[] x2,
data vector[] x1_unnorm,
data vector[] x2_unnorm,
real[] alpha,
real[] ell,
real[] wrp,
vector[] beta,
vector[] teff,
data real[] vm_params,
data int[] idx1_expand,
data int[] idx2_expand,
data vector[] teff_zero)
data array[] matrix K_const,
data array[,] int components,
data array[] vector x1,
data array[] vector x2,
data array[] vector x1_unnorm,
data array[] vector x2_unnorm,
array[] real alpha,
array[] real ell,
array[] real wrp,
array[] vector beta,
array[] vector teff,
data array[] real vm_params,
data array[] int idx1_expand,
data array[] int idx2_expand,
data array[] vector teff_zero)
{
int idx_ell = 0;
int idx_wrp = 0;
int idx_alpha = 0;
int num_comps = size(components);
matrix[n1, n2] KX[num_comps];
array[num_comps] matrix[n1, n2] KX;

// Loop through components
for(j in 1:num_comps){
Expand All @@ -158,7 +158,7 @@
vector[n2] X2;

// 2. Get component properties
int opts[9] = components[j];
array[9] int opts = components[j];
int ctype = opts[1];
int idx_cont = opts[9];
int is_heter = opts[4];
Expand Down
46 changes: 23 additions & 23 deletions inst/stan/_common/functions-kernels_diag.stan
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// prediction computations.

// Compute one constant kernel matrix diagonal.
vector STAN_kernel_const_diag(data int[] x, data int kernel_type)
vector STAN_kernel_const_diag(data array[] int x, data int kernel_type)
{
int n = num_elements(x);
vector[n] K_diag = rep_vector(1.0, n);
Expand All @@ -17,17 +17,17 @@
}

// Compute all constant kernel matrices' diagonals.
vector[] STAN_kernel_const_all_diag(
array[] vector STAN_kernel_const_all_diag(
data int n,
data int[,] x,
data int[,] x_mask,
data int[,] components)
data array[,] int x,
data array[,] int x_mask,
data array[,] int components)
{
int num_comps = size(components);
vector[n] K_const_diag[num_comps];
array[num_comps] vector[n] K_const_diag;
for (j in 1:num_comps) {
vector[n] K_diag;
int opts[9] = components[j];
array[9] int opts = components[j];
int ctype = opts[1];
int ktype = opts[2];
int idx_cat = opts[8];
Expand Down Expand Up @@ -56,7 +56,7 @@

// Multiplier matrix to enable variance masking
vector STAN_kernel_varmask_diag(vector x, real steepness,
data real[] vm_params)
data array[] real vm_params)
{
real a = steepness * vm_params[2];
real r = inv(a)*logit(vm_params[1]);
Expand All @@ -66,31 +66,31 @@
}

// Multiplier matrix to enable heterogeneous effects
vector STAN_kernel_beta_diag(vector beta, int[] idx_expand) {
vector STAN_kernel_beta_diag(vector beta, array[] int idx_expand) {
return(STAN_expand(beta, idx_expand));
}

/*
Compute diagonals of all kernel matrices.
*/
vector[] STAN_kernel_all_diag(
array[] vector STAN_kernel_all_diag(
data int n,
data vector[] K_const_diag,
data int[,] components,
data vector[] x,
data vector[] x_unnorm,
real[] alpha,
real[] wrp,
vector[] beta,
vector[] teff,
data real[] vm_params,
data int[] idx_expand,
data vector[] teff_zero)
data array[] vector K_const_diag,
data array[,] int components,
data array[] vector x,
data array[] vector x_unnorm,
array[] real alpha,
array[] real wrp,
array[] vector beta,
array[] vector teff,
data array[] real vm_params,
data array[] int idx_expand,
data array[] vector teff_zero)
{
int idx_wrp = 0;
int idx_alpha = 0;
int num_comps = size(components);
vector[n] KX_diag[num_comps];
array[num_comps] vector[n] KX_diag;

// Loop through components
for(j in 1:num_comps){
Expand All @@ -100,7 +100,7 @@
vector[n] X;

// 2. Get component properties
int opts[9] = components[j];
array[9] int opts = components[j];
int ctype = opts[1];
int idx_cont = opts[9];
int is_heter = opts[4];
Expand Down
Loading

0 comments on commit c486ff4

Please sign in to comment.