-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
This page takes you from nothing installed to a sampled model. There are two routes. The terminal route uses the laplace command-line tool directly. The R route uses cmdlaplacer, a small R package that wraps laplace and cmdstanr so you never have to leave R. Both produce exactly the same .stan file.
Commands are shown for macOS and Linux.
| To... | You need |
|---|---|
Install laplace
|
git and a Rust toolchain (Cargo 1.78 or newer, from rustup) |
Compile .laplace files to .stan
|
Only laplace. Stan is not required. |
| Sample a model | CmdStan, most easily through cmdstanr |
If you install Rust through your Linux distribution's package manager, check cargo --version first. Distribution packages are often too old to read Laplace's Cargo.lock. The rustup toolchain is always recent enough.
git clone https://github.com/mlatinov/laplace
cd laplace
cargo install --path . --root ~/.localAdd the install directory's bin/ to your PATH if it isn't already there (put this line in your ~/.bashrc or ~/.zshrc to make it permanent):
export PATH="$HOME/.local/bin:$PATH"Then check that it works:
laplace --helpTo update later, run git pull in the cloned repository and repeat the install command with --force added.
If git and cargo are already on your PATH, cmdlaplacer can do all of the above for you:
# install.packages("remotes")
remotes::install_github("mlatinov/cmdlaplacer")
library(cmdlaplacer)
laplace_install()laplace_install() clones the Laplace repository, runs the same cargo install command shown above, and adds ~/.local/bin to PATH for the current R session. It is a convenience wrapper, not a separate build: the binary is identical to the one you get from the terminal.
To sample models from R you also need cmdstanr and CmdStan:
install.packages("cmdstanr", repos = c("https://stan-dev.r-universe.dev", getOption("repos")))
cmdstanr::install_cmdstan()A Laplace project is just a directory containing your .laplace model. Create one, then add a library. This example uses transformations, a small library of data-scaling functions:
mkdir my-first-model
cd my-first-model
laplace add transformations \
--git https://github.com/mlatinov/laplace-transform --tag 0.1.0 --subdir laplaceThis creates two files next to your model:
-
laplace.tomlrecords what you asked for: thetransformationslibrary, from that repository, at tag0.1.0. You can edit it by hand. -
laplace.lockrecords exactly what was resolved, with a checksum. It is written by Laplace; you never edit it. Commit both files to git.
Save this as model.laplace. It is an ordinary Stan linear regression with one addition: a library { } block that imports transformations, whose standardize() function is then called as transformations::standardize().
library {
import transformations
}
data {
int<lower=1> N;
vector[N] x;
vector[N] y;
}
transformed data {
vector[N] x_std = transformations::standardize(x);
}
parameters {
real alpha;
real beta;
real<lower=0> sigma;
}
model {
alpha ~ normal(0, 2.5);
beta ~ normal(0, 2.5);
sigma ~ exponential(1);
y ~ normal(alpha + beta * x_std, sigma);
}To see what any library function does before you use it, look it up:
laplace doc transformations::standardizeRun the build from the project directory, where laplace.lock lives:
laplace build model.laplaceThis writes build/model.stan. Open it. You will find:
- The
library { }block is gone, because it means nothing to Stan. - A
functions { }block now contains the full source ofstandardize, renamedtransformations__standardize, including its documentation comment. - The call site now reads
transformations__standardize(x). - Every other line is exactly what you wrote. That file is plain Stan. It runs without Laplace installed, and it is meant to be committed alongside your source.
If you have stanc available, laplace build model.laplace --validate also type-checks the generated file. Set the LAPLACE_STANC environment variable if stanc is not on your PATH.
Because the output is plain Stan, cmdstanr uses it like any other model:
library(cmdstanr)
set.seed(1)
N <- 100
x <- rnorm(N, mean = 50, sd = 10)
y <- 2 + 0.5 * (x - mean(x)) / sd(x) + rnorm(N, sd = 0.3)
mod <- cmdstan_model("build/model.stan")
fit <- mod$sample(data = list(N = N, x = x, y = y))
fit$summary()laplace_model() runs the build step and hands the result to cmdstanr in one call. It returns an ordinary cmdstanr model object, so everything after that is standard cmdstanr:
library(cmdlaplacer)
mod <- laplace_model("model.laplace")
fit <- mod$sample(data = list(N = N, x = x, y = y))Pass stan_only = TRUE to get the path to the generated .stan file instead of a model object, if you want to inspect it or drive cmdstanr yourself.
Libraries hosted on git can also be added from R. laplace_install_git() wraps laplace add --git:
laplace_install_git(
"transformations",
"https://github.com/mlatinov/laplace-transform",
tag = "0.1.0",
subdir = "laplace"
)Clone the project, then run:
laplace install
laplace build model.laplacelaplace install reads only laplace.lock, so it installs exactly the library versions you used, byte for byte. Nothing is re-resolved.
-
Language Guide: everything the
library { }block can do - Packages and Dependencies: version ranges, updating libraries, and git sources in depth
- Writing a Library: turn your own Stan functions into a Laplace library
Laplace and cmdlaplacer rely on Stan for all modeling and inference, and cmdlaplacer uses cmdstanr, developed by Jonah Gabry, Rok Češnovar, Andrew Johnson and contributors, to compile and run models from R. If you publish results from a model built with Laplace, please cite Stan and cmdstanr. In R, citation("cmdstanr") gives the current reference, and the Stan project lists how to cite Stan at https://mc-stan.org/users/citations/.