5.8 Variable declaration

Variables in Stan are declared by giving a type and a name. For example

int N;
vector[N] y;
array[5] matrix[3, 4] A;

declares a variable N that is an integer, a variable y that is a vector of length N (the previously declared variable), and a variable A, which is a length-5 array where each element is a 3 by 4 matrix.

There are several different places a variable is declared in Stan. They are block variables, like those inside data, which can have constraints and must include sizes for their types, like in the above examples. Local variables, like those defined inside loops or local blocks cannot be constrained, but still include sizes. Finally, variables declared as function parameters are not constrained types and exclude sizes.

// valid block variables, but not locals or function parameters
vector<lower=0>[N] u;

// valid as a block or local variable, but not a function parameter
array[3] int is;

// function parameters exclude sizes and cannot be constrained
void pretty_print_tri_lower(matrix x) { ... }

In the following table, the leftmost column is a list of the unconstrained and undimensioned basic types; these are used as function return types and argument types. The middle column is of unconstrained types with dimensions; these are used as local variable types. The variables M and N indicate number of columns and rows, respectively. The variable K is used for square matrices, i.e., K denotes both the number of rows and columns. The rightmost column lists the corresponding constrained types. An expression of any right-hand column type may be assigned to its corresponding left-hand column basic type. At runtime, dimensions are checked for consistency for all variables; containers of any sizes may be assigned to function arguments. The constrained matrix types cov_matrix[K], corr_matrix[K], cholesky_factor_cov[K], and cholesky_factor_corr[K] are only assignable to matrices of dimensions matrix[K, K] types.

Function Argument (unsized) Local (unconstrained) Block (constrained)
int int int
int<lower=L, upper=U>
int<offset=O, multiplier=M>
real real real
real<lower=L, upper=U>
real<offset=O, multiplier=M>
vector vector[N] vector[N]
vector[N]<lower=L, upper=U>
vector[N]<offset=O, multiplier=M>
row_vector row_vector[N] row_vector[N]
row_vector[N]<lower=L, upper=U>
row_vector[N]<offset=O, multiplier=M>
matrix matrix[M, N] matrix[M, N]
matrix[M, N]<lower=L>
matrix[M, N]<upper=U>
matrix[M, N]<lower=L, uppers=U>
matrix[M, N]<offset=O>
matrix[M, N]<multiplier=M>
matrix[M, N]<offset=O, multiplier=M>
matrix[K, K] corr_matrix[K]
matrix[K, K] cov_matrix[K]
matrix[K, K] cholesky_factor_corr[K]
matrix[K, K] cholesky_factor_cov[K]
array[] vector array[M] vector[N] array[M] vector[N]
array[M] vector[N]<lower=L>
array[M] vector[N]<upper=U>
array[M] vector[N]<lower=L, upper=U>
array[M] vector[N]<offset=O>
array[M] vector[N]<multiplier=M>
array[M] vector[N]<offset=O, multiplier=M>
array[M] ordered[N]
array[M] positive_ordered[N]
array[M] simplex[N]
array[M] unit_vector[N]

Additional array types follow the same basic template as the final example in the table and can contain any of the previous types. The unsized version of arrays with more than one dimension is specified by using commas, e.g. array[ , ] is a 2-D array.

For more on how function arguments and return types are declared, consult the User’s Guide chapter on functions.