Skip to content
Metodi Latinov edited this page Sep 18, 2026 · 3 revisions

Laplace

Share Stan code like packages. Run the result as plain Stan.

Laplace is a source-to-source compiler for the [Stan](https://mc-stan.org) probabilistic programming language. You write a .laplace file, which is ordinary Stan plus a library {} block, and you call functions from versioned, shareable libraries as package::function. When you run laplace build, Laplace resolves those libraries and writes a complete, plain .stan file with every library function you used written out in full. Stan then compiles and samples that file like any other Stan model.

At a glance

library {
  import gaussian_process
}

data {
  int<lower=1> N;
  array[N] real x;
  vector[N] y;
}

parameters {
  real<lower=0> alpha;
  real<lower=0> rho;
  real<lower=0> sigma;
}

model {
  matrix[N, N] K = gaussian_process::matern32_cov(x, alpha, rho) + diag_matrix(rep_vector(square(sigma), N));

  alpha ~ std_normal();
  rho   ~ inv_gamma(5, 5);
  sigma ~ std_normal();
  y ~ multi_normal(rep_vector(0, N), K);
}
laplace add gaussian_process \
  --git https://github.com/mlatinov/laplace-gaussian_processes- --tag 0.1.0 --subdir laplace
laplace build model.laplace    # writes build/model.stan

The generated build/model.stan contains the full source of the Matérn 3/2 kernel, renamed gaussian_process__matern32_cov, at the top of its functions block. The call site is rewritten to match, and every other line of the model is left exactly as you wrote it. Nothing is linked or loaded at runtime, and nothing is hidden.

Our promises

  • Stan is always the backend. Laplace has no sampler and no runtime of its own. The NUTS sampler you already trust does all the inference.
  • Nothing is hidden. Every function that comes from a library is written out in full, as readable Stan, in the source Laplace generates: inlined into your .stan file by default, or placed in a companion .stanfunctions file if you ask for that shape. You can always read exactly what your model does.
  • The output is portable. The generated .stan file runs without Laplace installed. You can commit it, send it to a colleague, or attach it to a paper, and it is still just Stan.
  • Dependencies are reproducible. laplace.toml holds the version ranges you edit by hand. laplace.lock holds exact pins and checksums, and laplace install reads only the lockfile. A cloned project on a new machine resolves to exactly the same code.
  • Complexity is opt-in. Any feature that changes the structure of the output is behind a flag. The defaults always preserve the guarantees above.

Why Laplace exists

Almost every Stan user already has a personal library

If you have written Stan for a while, you probably keep a folder of functions and models you reuse. When someone else wants to use them, they first have to know your repository exists. Then they have to copy the right functions and track down the helper functions those depend on, which may live in other files. Usually there is no record of which version of the code, or which Stan version, it was written against. In practice most of us give up and rewrite the function ourselves. With enough Stan experience that is possible, but it is slow, and it is easy to get subtly wrong.

Laplace turns those personal folders into versioned packages. Anyone can install them with one command, and their dependencies are resolved for you.

A place to share knowledge and be recognised for it

There are talented Bayesians all over the world whose best work stays on their own machines. Laplace aims to give them a platform: a way to publish their methods to the community as usable libraries, and to be recognised for the effort.

Concepts first, mathematics with time

We believe the conceptual understanding of what you are trying to do should come first, and the deep mathematical understanding can follow. Stan already lowered the barrier to state-of-the-art inference: nobody has to implement NUTS by hand. Laplace lowers the barrier one level further, with higher-level building blocks, so a beginner can assemble a working model without first specifying every line.

This is exactly why the output stays transparent. When the beginner is ready to go deeper, the full implementation is already in their .stan file, waiting to be read.

Faster experimentation

Trying a fundamentally different model usually means rewriting a large part of it from scratch. When time is limited, that cost decides which alternatives get explored, and good models get skipped because there was no time to build them. With Laplace, swapping a kernel, a likelihood, or a model component is often a change to one line, so you can compare more candidates in the same time.

Why not brms or other Stan-based packages?

Packages such as brms and domain-specific packages for time series or SEM/CFA are excellent, and Laplace does not aim to replace them. The difference is where you work.

With those packages, you learn a separate interface for each one, each with its own API and release cycle. With Laplace, you stay in the Stan language. Libraries from different authors compose inside one model, and everything you learn transfers directly to writing Stan yourself.

Where to go next


Laplace is an independent project and is not affiliated with the Stan Development Team.

Clone this wiki locally