I’ve built a prototype of ‘Pedantic Mode’, a tool for finding issues with Stan programs. It’s based some of the ideas from a previous discussion here. It’s an early version, but it’s ready for your testing and feedback (and bug reports)!
What is pedantic mode
When you compile a Stan program with Pedantic Mode turned on, it will search through your program for potential issues and point them out to you.
For example, if you compile the following program with Pedantic Mode:
data {
int N;
real x[N];
}
parameters {
real sigma;
}
model {
real mu;
x ~ normal(mu, sigma);
}
It will spit out:
Warning:
The parameter sigma has 0 priors.
Warning at line 10, column 13 to column 15:
The variable mu may not have been initialized before its use.
Warning at line 10, column 17 to column 22:
A normal distribution is given parameter sigma as a scale parameter
(argument 2), but sigma was not constrained to be strictly positive.
Programmers might recognize this as a linter or -Wall
. Pedantic mode aims to be a linter for statistical as well as programming issues.
Here are the kinds of issues that Pedantic Mode can currently look for:
- Distribution arguments don’t match the distribution specification
- Some distribution is used in an inadvisable way (e.g. uniform distributions)
- Very large or very small constants are used as distribution arguments
- Branching control flow (like if/else) depends on a parameter value, potentially introducing discontinuity
- A parameter is defined but doesn’t contribute to target
- A parameter is on the left-hand side of multiple twiddles
- A parameter has more than one prior distribution
- A parameter is assigned questionable bounds
- A variable is used before being assigned a value
Here are some known limitations:
- Indexed variables are not handled intelligently, so they’re treated conservatively (erring toward no warnings)
- Data variables used as distribution arguments or variates are not currently checked against distribution specifications
- Sometimes it’s impossible to know a variable’s value, like a distribution argument, before the program is run
There’s some more detailed information here (I’m working on better docs!)
How can you get it?
Pedantic mode is not currently included in the main version of the compiler, but it isn’t hard to get the test version thanks to @rok_cesnovar, @serban-nicusor and @mitzimorris.
Option 1: CmdStanR (easiest)
You can install the test version of CmdStan using CmdStanR with this command:
install_cmdstan(repo_clone = TRUE, repo_branch = "pedantic_mode", cores = 4)
This might take a few minutes.
Alternatively, you could install the Pedantic Mode branch of CmdStan manually and use it with CmdStanR. See Option 2, then use set_cmd_path()
within CmdStanR.
To confirm that it’s installed correctly, try running:
library("cmdstanr")
#use the example model with an unused sigma
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file, force_recompile = TRUE)
Since the “bernoulli.stan” test file has an artificial issue in it, you should see:
Compiling Stan program...
Warning:
The parameter sigma was declared but does not participate in the model.
Option 2: CmdStan
You can download the Pedantic Mode enabled CmdStan from GitHub on the pedantic_mode branch, here.
Pedantic Mode should be enabled by default with this version.
Option 3: Stanc3
You can find the development version of Stanc3 with Pedantic Mode here.
In order to build Stanc3, you will have to set up the appropriate OCaml environment, so this option is not as easy.
Use the --warn-pedantic
flag to enable Pedantic Mode.
How can you help?
This tool will certainly have issues and bugs, so all eyes on it will help! You could run your existing models with pedantic mode turned on, or see if it helps you when you develop new ones.
I would appreciate all feedback and bug reports, such as:
- How could the warning messages be more helpful?
- Is it warning you about something that’s actually fine?
- Is there something else you think it should warn you about?
- Is it saying something totally wrong?
Please write your comments here or on the GitHub pull request here.
Let me know what issues you run into. Thank you!
@andrewgelman @bbbales2 @Bob_Carpenter @Matthijs
Thanks again to @rok_cesnovar, @serban-nicusor and @mitzimorris for help setting up the CmdStan branch