Getting Started#

Requirement: C++ toolchain#

Stan requires a C++ tool chain consisting of

  • A C++14 compiler. On Windows, MSCV is not supported, so something like MinGW GCC is required.

  • The Gnu make utility.

Here are complete instructions by platform for installing both, from the CmdStan installation instructions.

Downloading TinyStan#

Note

The Julia, Python, and R clients will download the source for you the first time you compile a model. This section is optional for users primarily interested in those interfaces.

Installing TinyStan is as simple as ensuring that the above requirements are installed and then downloading the source repository. All of the following ways of downloading TinyStan will additionally download the Stan and Stan Math libraries for you, and no additional dependencies are required to be installed separately for the C++ source code.

Downloading a released archive#

Downloads of a complete copy of the source code and interfaces are available on our GitHub releases page.

To use these, simply download the file associated with the version you wish to use, and unzip its contents into the folder you would like TinyStan to be in.

Installing the latest version with git#

If you have git installed, you may download TinyStan by navigating to the folder you’d like TinyStan to be in and running

git clone --recurse-submodules --shallow-submodules --depth=1 https://github.com/WardBrian/tinystan.git

If you clone without the --recurse-submodules argument, you can download the required submodules with make stan-update. The arguments --shallow-submodules and --depth=1 are to reduce the size of the download, but are not required.

Testing the Installation#

After this, TinyStan is installed. You can test a basic compilation by opening a terminal in your TinyStan folder and running

# MacOS and Linux
make test_models/multi/multi_model.so

This will compile the file test_models/multi/multi.stan into a shared library object for use with TinyStan. This will require internet access the first time you run it in order to download the appropriate Stan compiler for your platform into <tinystan-dir>/bin/stanc[.exe]

Installing an Interface#

To see instructions for installing the TinyStan client package in your language of choice, see the Language Interfaces page.

Optional: Customizing TinyStan#

TinyStan has many compiler flags and options set by default. Many of these defaults are the same as those used by the CmdStan interface to Stan. You can override the defaults or add new flags on the command line when invoking make, or make them persistent by creating or editing the file <tinystan dir>/make/local.

For example, setting the contents of make/local to the following includes compiler flags for optimization level and architecture.

# By default we use -O3, this sets a less aggressive C++ optimization level
O=2
# Adding other arbitrary C++ compiler flags
CXXFLAGS+= -march=native

Flags for stanc3 can also be set here

# pedantic mode and level 1 optimization
STANCFLAGS+= --warn-pedantic --O1

Using External C++ Code#

TinyStan supports the same capability to plug in external C++ code as CmdStan.

Namely, you can declare a function in your Stan model and then define it in a separate C++ file. This requires passing the --allow-undefined flag to the Stan compiler when building your model. The USER_HEADER variable must point to the C++ file containing the function definition. By default, this will be the file user_header.hpp in the same directory as the Stan model.

For a more complete example, consult the CmdStan documentation.

Using Pre-Existing Stan Installations#

If you wish to use TinyStan with a pre-existing download of the Stan repository, or with a custom fork or branch, you can set the STAN (and, optionally, MATH) variables to the path to your existing copy in calls to make, or more permanently by setting them in a make/local file as described above.

The easiest way to use a custom stanc3 is to place the built executable at bin/stanc[.exe].