Typescript (JavaScript) Interface#
Targetting WASM with TinyStan#
This interface is intended to be used to access models built with Emscripten, which compiles C++ code to WebAssembly.
This requires some extra configuration compared to normal builds. Examples of the build (and usage) can be found in the following projects:
Note that, among other things, a more recent version of TBB must be used, as WebAssembly is not supported in the version Stan currently vendors.
Installation#
The Typescript interface is available on npm. To install it, run:
npm install tinystan --save
or
yarn add tinystan
API Reference#
Classes#
Name |
Description |
---|---|
StanModel is a class that wraps the WASM module and provides a higher-level interface to the Stan library, abstracting away things like memory management and error handling. |
Typedefs#
Name |
Description |
---|---|
A type holding named inputs to a Stan model, e.g. the data or initial values. |
|
A callback for printing output from the Stan model. |
|
A type holding the result of a Stan sampling run. |
|
Parameters for the HMC sampler. |
|
Parameters for the Pathfinder algorithm. |
StanModel#
StanModel is a class that wraps the WASM module and provides a higher-level interface to the Stan library, abstracting away things like memory management and error handling.
Kind: global class
-
instance
static
stanModel.sample(p)#
Sample using NUTS-HMC.
Kind: instance method of StanModel
Returns: StanDraws -
A StanDraws object containing the parameter names and the draws
Param |
Type |
Description |
---|---|---|
p |
A (partially-specified) |
stanModel.pathfinder(p)#
Approximate the posterior using Pathfinder.
Kind: instance method of StanModel
Returns: StanDraws -
A StanDraws object containing the parameter names and the approximate draws
Param |
Type |
Description |
---|---|---|
p |
A (partially-specified) |
stanModel.stanVersion()#
Get the version of the Stan library being used.
Kind: instance method of StanModel
Returns: string
-
The version of the Stan library being used, in the form “major.minor.patch”
StanModel.load(createModule, printCallback)#
Load a StanModel from a WASM module.
Kind: static method of StanModel
Returns: Promise.<StanModel>
-
A promise that resolves to a StanModel
Param |
Type |
Description |
---|---|---|
createModule |
|
A function that resolves to a WASM module. This is much like the one Emscripten creates for you with |
printCallback |
PrintCallback | |
A callback that will be called with any print statements from Stan. If null, this will default to |
HMCMetric#
The metric used for the HMC sampler.
Kind: global enum
Read only: true
Properties
Name |
Type |
Description |
---|---|---|
UNIT |
|
Unit metric. |
DENSE |
|
Dense metric. |
DIAGONAL |
|
Diagonal metric. |
StanVariableInputs#
A type holding named inputs to a Stan model, e.g. the data or initial values.
Kind: global typedef
PrintCallback#
A callback for printing output from the Stan model.
Kind: global typedef
Param |
Type |
Description |
---|---|---|
s |
|
The string to print. |
StanDraws#
A type holding the result of a Stan sampling run.
Kind: global typedef
Properties
Name |
Type |
Description |
---|---|---|
paramNames |
|
The names of the parameters in the model |
draws |
|
A 2D array of draws from the posterior. The first dimension is the number of samples, and the second dimension is the number of parameters. |
metric |
|
The metric used for the HMC sampler. If the metric is not saved, this field is not present. |
SamplerParams#
Parameters for the HMC sampler.
Kind: global typedef
Properties
Name |
Type |
Default |
Description |
---|---|---|---|
[data] |
|
|
The data for the model |
[num_chains] |
|
|
The number of chains to run |
[inits] |
|
|
The initial values for the sampler. If an array, must have length |
[seed] |
|
The seed for the random number generator. If unspecified, a random seed will be generated. |
|
[id] |
|
|
The ID for the first chain |
[init_radius] |
|
|
Radius to initialize unspecified parameters within. The parameter values are drawn uniformly from the interval |
[num_warmup] |
|
|
The number of warmup iterations to run |
[num_samples] |
|
|
The number of samples to draw after warmup |
[metric] |
|
|
The type of mass matrix to use in the sampler |
[save_metric] |
|
|
Whether to report the final mass matrix |
[init_inv_metric] |
|
The initial inverse metric to use. Currently, this argument is unused. |
|
[adapt] |
|
|
Whether the sampler should adapt the step size and metric |
[delta] |
|
|
Target acceptance rate |
[gamma] |
|
|
Adaptation regularization scale |
[kappa] |
|
|
Adaptation relaxation exponent |
[t0] |
|
|
Adaptation iteration offset |
[init_buffer] |
|
|
Number of warmup samples to use for initial step size adaptation. |
[term_buffer] |
|
|
Number of warmup samples to use for step size adaptation after the metric is adapted |
[window] |
|
|
Initial number of iterations to use for metric adaptation, which is doubled each time the adaptation window is hit |
[save_warmup] |
|
|
Whether to save the warmup draws |
[stepsize] |
|
|
Initial step size for the sampler |
[stepsize_jitter] |
|
|
Amount of random jitter to add to the step size |
[max_depth] |
|
|
Maximum tree depth for the NUTS sampler |
[refresh] |
|
|
Number of iterations between progress messages. If 0, no output is printed. |
[num_threads] |
|
|
Number of threads to use for sampling. If -1, the number of threads is determined by the number of available CPU cores. May not be supported in all environments, and requires extra configuration during the Emscripten compilation. |
PathfinderParams#
Parameters for the Pathfinder algorithm.
Kind: global typedef
Properties
Name |
Type |
Default |
Description |
---|---|---|---|
[data] |
|
|
The data for the model |
[num_paths] |
|
|
The number of individual paths to run |
[inits] |
|
|
The initial values for the algorithm. If an array, must have length |
[seed] |
|
The seed for the random number generator. If unspecified, a random seed will be generated. |
|
[id] |
|
|
The ID for the first path |
[init_radius] |
|
|
Radius to initialize unspecified parameters within. The parameter values are drawn uniformly from the interval |
[num_draws] |
|
|
The number of draws to take for each path |
[max_history_size] |
|
|
History size used by the internal L-BFGS algorithm to approximate the Hessian |
[init_alpha] |
|
|
Initial step size for the internal L-BFGS algorithm |
[tol_obj] |
|
|
Convergence tolerance for the objective function |
[tol_rel_obj] |
|
|
Relative convergence tolerance for the objective function |
[tol_grad] |
|
|
Convergence tolerance for the gradient norm |
[tol_rel_grad] |
|
|
Relative convergence tolerance for the gradient norm |
[tol_param] |
|
|
Convergence tolerance for the changes in parameters |
[num_iterations] |
|
|
Maximum number of iterations for the internal L-BFGS algorithm |
[num_elbo_draws] |
|
|
Number of Monte Carlo draws used to estimate the ELBO |
[num_multi_draws] |
|
|
Number of draws returned by Multi-Pathfinder |
[calculate_lp] |
|
|
Whether to calculate the log probability of the approximate draws. If false, this also implies |
[psis_resample] |
|
|
Whether to use Pareto smoothed importance sampling on the approximate draws. If false, all |
[refresh] |
|
|
Number of iterations between progress messages. If 0, no output is printed. |
[num_threads] |
|
|
Number of threads to use for Pathfinder. If -1, the number of threads is determined by the number of available CPU cores. May not be supported in all environments, and requires extra configuration during the Emscripten compilation. |