phpstats / cmdstanphp
A PHP interface to CmdStan inspired by CmdStanR
Requires
- php: >=8.2 <8.6
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: ^3.13
Suggests
- ext-ffi: Required for model methods (log_prob, gradients, constrain/unconstrain) and expose_functions()
- ext-phit: Required for loo(moment_match: true): provides the Phit\draws_moments, Phit\draws_covariance_map, and Phit\draws_affine kernels; StanData::fromFormula() also needs its Phit\model_matrix()
- phpstats/phibble: Data frames: pass a Phibble\DataFrame as model data, and get draws and summaries back with drawsTable() and summaryTable() (phibble requires PHP 8.3 or later)
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-02 10:33:07 UTC
README
Compile Stan models, run Bayesian inference, and inspect posterior draws from PHP.
CmdStanPHP runs Stan programs through CmdStan, Stan's command-line interface. It is a port of CmdStanR 1.0 and keeps CmdStanR's function and option names.
Documentation: https://cmdstanphp.phpstats.org/docs
Installation
composer require phpstats/cmdstanphp
CmdStanPHP needs PHP 8.2 to 8.5, CmdStan 2.37 or later, and a C++ toolchain, and does not need R. Installation covers the requirements, CmdStan, and the optional extensions.
Example
This Stan program estimates the probability theta of a success from N yes-or-no outcomes y. Save it as bernoulli.stan:
data { int<lower=0> N; array[N] int<lower=0, upper=1> y; } parameters { real<lower=0, upper=1> theta; } model { theta ~ beta(1, 1); y ~ bernoulli(theta); }
Compile it, sample four chains, and summarize theta:
use function CmdStan\cmdstan_model; $model = cmdstan_model(__DIR__.'/bernoulli.stan'); $data = [ 'N' => 10, 'y' => [0, 1, 0, 0, 0, 0, 0, 0, 0, 1], ]; $fit = $model->sample( data: $data, seed: 123, chains: 4, parallel_chains: 4, show_messages: false, ); echo "variable mean sd q5 q95 rhat ess_bulk\n"; foreach ($fit->summary('theta') as $r) { printf( "%-8s %6.3f %5.3f %5.3f %5.3f %5.3f %8.0f\n", $r['variable'], $r['mean'], $r['sd'], $r['q5'], $r['q95'], $r['rhat'], $r['ess_bulk'], ); }
variable mean sd q5 q95 rhat ess_bulk
theta 0.257 0.124 0.080 0.485 1.001 1232
Getting started explains the output and the convergence checks.
Documentation
The documentation has guides, worked examples, and Functions and methods, an index of every function and every model and fit method.
Compatibility
CmdStanPHP does not implement every CmdStanR feature. The compatibility page maps CmdStanR's names to CmdStanPHP's and lists the differences and the missing features. Verification reports the comparisons with CmdStanR, posterior, loo, and bayesplot.
Development
composer install composer test # unit tests, no CmdStan needed composer test:integration # compiles and runs real models composer test:compatibility # compares with R; needs CmdStanR and R packages composer lint
See Contributing.
License
MIT. The example models in resources/examples/ come from CmdStanR under its BSD 3-Clause license (see the NOTICE there). CmdStan and CmdStanR are separate Stan projects. CmdStanPHP is an independent implementation and not an official Stan interface.