Statistics, Modeling & Visualization · Models, inference, and charts
CmdStanPHP
Stan modeling and inference from PHP.
composer require phpstats/cmdstanphp
- Requires
- JSON extension proc_open() enabled
- Optional
- FFI extension (in-process log densities, gradients, and transforms, and calling Stan functions from PHP) phit extension (moment matching in loo() and StanData::fromFormula()) phpstats/phibble (data frames as model data, and draws and summaries as data frames) vl-convert (figures saved as SVG or PNG)
- Builds with
- CmdStan 2.37 or later make A C++ compiler supported by CmdStan
- Platforms
- Linux macOS
CmdStanPHP compiles Stan programs from PHP, runs inference through CmdStan, and returns the posterior draws as typed PHP objects. It ports CmdStanR's model, executable, and CSV workflow and keeps CmdStanR's snake_case names for its standalone functions and for CmdStan's options, with camelCase methods, so a translated R script reads much like its original. Inference methods take PHP named arguments or one associative options array, and they reject an unknown option instead of ignoring it.
The inference methods are MCMC with parallel chains, L-BFGS, BFGS, and Newton optimization, meanfield and fullrank ADVI, single- and multi-path Pathfinder, Laplace approximation, standalone generated quantities, and finite-difference gradient checking. Each one returns a fit object carrying draws in posterior's formats, summaries with rank-normalized R-hat and bulk and tail effective sample size, per-chain sampler diagnostics, and PSIS-LOO and WAIC, with optional moment matching in LOO.
PHP's FFI extension gives in-process access to a compiled model: its log density, gradients, Hessian, constrain and unconstrain transforms, and its user-defined Stan functions called as PHP methods. The bayesplot diagnostics are computed in PHP, including trace, histogram, density, intervals, rank histograms, scatter, autocorrelation, and loo's Pareto k plot. Each one renders as ASCII text for a terminal or a log, a Vega-Lite spec for a front end, a standalone HTML page, or an SVG or PNG image through vl-convert.
The package needs PHP 8.2 to 8.5, the JSON extension, PHP's proc_open(), and CmdStan 2.37 or later with make and a C++ compiler. It has no Composer dependencies, and its installer downloads and builds an official CmdStan release. Model data can be a phibble data frame or any object that exposes its columns, StanData builds hierarchical regression data from columns or from an R-style formula, and draws and summaries come back as plain column arrays or as a phibble DataFrame. None of the other phpstats packages is required.
Example
use function CmdStan\cmdstan_model;
$model = cmdstan_model('bernoulli.stan');
$fit = $model->sample(
data: ['N' => 10, 'y' => [0, 1, 0, 0, 0, 0, 0, 0, 0, 1]],
seed: 123,
chains: 4,
parallel_chains: 2,
iter_warmup: 500,
iter_sampling: 500,
show_messages: false,
);
print_r($fit->summary('theta', measures: ['mean', 'sd', 'quantile2', 'rhat', 'ess_bulk']));
Output
Array
(
[0] => Array
(
[variable] => theta
[mean] => 0.245767235518
[sd] => 0.11840083300925
[q5] => 0.0742962453
[q95] => 0.460095425
[rhat] => 0.99947503401977
[ess_bulk] => 813.95155410288
)
)