Skip to content

Getting Started with CmdStan

Rok Češnovar edited this page Aug 8, 2022 · 15 revisions

Installation

Prerequisites

CmdStan requires:

  • A C++11 compiler
  • The Gnu make utility for *nix, mingw_64 for Windows

See the Stan wiki page for a list of supported compilers. Note that for running CmdStan on Windows we recommend the toolchain that is bundled with Rtools40.

Installing CmdStan from Python

Python 3 users can install CmdStan using the install_cmdstan script included with CmdStanPy

Installing from GitHub releases page

The CmdStan releases page includes a link to a tar.gz file which contains CmdStan and all submodules. Download and unpack the latest release.

Installing from GitHub repo

  1. Clone CmdStan

    git clone https:/stan-dev/cmdstan.git --recursive
    
  2. run make target stan-update which recursively installs the stan and stan-math submodules and the stanc compiler.

    make stan-update
    

Build a Stan Model

The make target help will display options.

  1. Build all tools (optional): make build

  2. Build a model foo/bar.stan

    linux / mac: make foo/bar

    windows: make foo/bar.exe

An example

From the cmdstan home directory, compile the bernoulli.stan model specified in the examples/bernoulli folder:

make examples/bernoulli/bernoulli

Run the default sampling algorithm using the data specified in bernoulli.data.R. This prints out information while the algorithm runs and outputs a file output.csv in the current directory:

examples/bernoulli/bernoulli sample data file=examples/bernoulli/bernoulli.data.R

Use the stansummary script to summarize the information and print to standard output:

bin/stansummary output.csv

Other tips

Using a different Stan branch

If the branch is feature/foo-bar, from the cmdstan home directory:

make stan-update/feature/foo-bar

Keeping CmdStan up-to-date

Simply run make stan-update from the cmdstan home directory.

On the file structure

It may be helpful at times to run manual git commands in the repos. Stan is a submodule within CmdStan and is located in the stan/ folder. The math library is a submodule within Stan and is located in the stan/lib/stan_math directory.

To keep everything up-to-date manually, run git pull within the home directory of each repo (cmdstan, stan, and math). Similarly, to change branches for one of these repos, simply git checkout that branch within the home directory of that repo.

Using parallel computing with threading

See Threading support at stan-dev/math

Using parallel computing with MPI

See MPI Parallelism at stan-dev/math

More information

Comprehensive documentation is available from the CmdStan Interface User's Guide, distributed with each release.