1. 编码
content: ASCII,UTF-8, Latin-1
comment: #, //, /* ... */
2. includes
#include xxx.stan
No recursive include.
Add path
5. 数据类型及声明
5.1-2 数据类型
Primitive types
+ int: overflow ; round when dividing
+ real: overflow ; Nan, inf, -inf
Matrix
vector 列向量,row_vector 行向量,matrix
Array
任意数据类型,e.g., matrix[3, 3] m[6, 7];
Constrained data types
e.g., vector<lower = -1, upper = 1>[3] rho;
Four constrained vector data types,
+ simplex for unit simplexes
+ unit_vector for unit-length vectors,
+ ordered for ordered vectors of scalars
+ positive_ordered for vectors of positive ordered scalars.
5.3 Univariate data types and variable declarations
int N;
int<lower=1> N;
int<lower=0,upper=1> cond; #binary
real theta;
real<lower=0> sigma;
real<lower=-1,upper=1> rho;
positive_infinity(), negative_infinity()
Affinely transformed: real<offset=1,multiplier=2> x;
Expression as bound/offset/multiplier: 表达式必须在使用前在data/tranformed data block中声明
data {
int<lower=1> N;
real y[N];
}
parameters {
real<lower=min(y), upper=max(y)> phi;
}
Declaring optional variables: A variable may be declared with a size that depends on a boolean constant.
data {
int<lower = 0, upper = 1> include_alpha;
...
}
parameters {
vector[include_alpha ? N : 0] alpha
...
}
5.4 Vector and matrix data types
Vectors and matrices are restricted to real values.
Index from 1
Vectors
vector<lower=0>[3] u;
simplex[5] theta;
unit_vector[5] theta;
ordered[5] c; # ascending, often employed as cut points in ordered logistic regression models
positive_ordered[5] d;
row_vector[1093] u;
Matrices
matrix[M, N] a;
row_vector[N] b;
a[1] = b; #行向量为矩阵赋值, 复制, 修改a/b不影响b/a
cov_matrix[K] Omega; #A matrix is a covariance matrix if it is symmetric and positive definite
corr_matrix[3] Sigma; #A matrix is a correlation matrix if it is symmetric and positive definite, has entries between−1and1, and has a unit diagonal.
Cholesky factors of covariance and correlation matrices
Often more convenient or more efficient
A Cholesky factor L is an M×N lower-triangular matrix with a strictly positive diagonal and. If L is a Cholesky factor, then
is a covariance matrix (i.e., it is positive definite).
The mapping between positive definite matrices and their Cholesky factors is bijective — every covariance matrix has a unique Cholesky factorization.
The typical case of a square Cholesky factor: cholesky_factor_cov[4] L;
The type cholesky_factor_cov[M, N] may be used for the general M×N case to produce positive semi-definite matrices of rank M.
A Cholesky factor for a correlation matrix L is a K×K lower-triangular matrix with positive diagonal entries and rows that are of length 1 (norm-2). If L is a Cholesky factor for a correlation matrix, then is a correlation matrix (i.e., symmetric positive definite with a unit diagonal).
cholesky_factor_corr[K] L;
Accessing element
matrix[M, N] m;
row_vector[N] v;
real x;
v = m[2]; #m的行向量
x = m[2, 3];
Size declarations
Any integer-denoting expression may be used for the size declaration, providing all variables involved are either data, transformed data, or local variables.
data {
int<lower=0> N_observed; int<lower=0> N_missing;
}
transformed parameters {
vector[N_observed + N_missing] y;
}
5.5 Array data types
唯一存储整数列的类型
array[5] int n;
array[5, 4, 2] real<lower=0> z;
array[2, 3, 4] cholesky_factor_cov[5, 6] mu;
Arrays, row vectors, column vectors, and matrices are not interchangeable in Stan.
5.6 Variable types vs. constraints and sizes
The type information associated with a variable only contains the underlying type and dimensionality of the variable.
Sizes
The size associated with a given variable is not part of its data type. Sizes are determined dynamically (at run time) and thus cannot be type-checked statically when the program is compiled. As a result, any conformance error on size will raise a run-time error.
Constraints
Like sizes, constraints are not treated as part of a variable’s type in Stan when it comes to the compile-time check of operations it may participate in. Anywhere Stan accepts a matrix as an argument, it will syntactically accept a correlation matrix or covariance matrix or Cholesky factor. Thus a covariance matrix may be assigned to a matrix and vice-versa. Similarly, a bounded real may be assigned to an unconstrained real and vice-versa.
Constraints check
For arguments to functions, constraints are sometimes, but not always checked when the function is called. Exclusions include C++ standard library functions. All probability functions and cumulative distribution functions check that their arguments are appropriate at run time as the function is called.
For data variables, constraints are checked after the variable is read from a data file or other source. For transformed data variables, the check is done after the statements in the transformed data block have been executed. Thus it is legal for intermediate values of variables to not satisfy declared constraints.
For parameters, constraints are enforced by the transform applied and do not need to be checked. For transformed parameters, the check is done after the statements in the transformed parameter block have been executed.
For all blocks defining variables (transformed data, transformed parameters, generated quantities), real values are initialized to NaN and integer values are initialized to the smallest legal integer (i.e., a large absolute value negative number).
For generated quantities, constraints are enforced after the statements in the generated quantities block have been executed.
5.7 Compound variable declaration and definition
Stan allows assignable variables to be declared and defined in a single statement. Assignable variables are local variables, and variables declared in the transformed data, transformed parameters, or generated quantities blocks.