A reusable, distributable Swift library for orchestrating cmdstan and generating Stan models from a Swift DSL. macOS 14+; Swift 6. No external dependencies.
Extracted and curated from SwiftStan, which also bundles a command-line tool. This package is library-only.
SwiftStan— the library module (import asimport SwiftStan).
| Function | Description |
|---|---|
compile(model:arguments:cmdstan:verbose:install:force:) |
Compile a Stan model via make |
sample(model:arguments:cmdstan:verbose:nosummary:install:) |
Run HMC/NUTS sampling |
optimize(model:arguments:cmdstan:verbose:) |
MAP optimization |
pathfinder(model:arguments:cmdstan:verbose:) |
Pathfinder variational |
laplace(model:arguments:cmdstan:verbose:) |
Laplace approximation |
generated_Quantities(model:arguments:cmdstan:verbose:) |
Post-sampling generated quantities |
stansummary(model:arguments:cmdstan:verbose:) |
Run cmdstan's stansummary |
ulamPipeline(model:cmdstan:verbose:force:arguments:) |
Full pipeline: alist→Stan→compile→sample |
All cmdstan commands take cmdstan: String (path to the cmdstan installation) and model: String (name of the case under ~/Documents/StanCases/, configurable via $STAN_CASES). They return (String, String) — .0 is a human-readable status, .1 is an error message ("" on success).
| Function | Description |
|---|---|
stancode(model:verbose:) |
alist.R → Stan source (in-process, fast) |
stan2alist(model:verbose:force:) |
Stan source → alist.R (reverse) |
alist2dsl(model:verbose:) |
alist.R → Swift result-builder DSL driver |
csv2json(model:verbose:) |
CSV data file → data.json for cmdstan |
runinfo(model:verbose:) |
Clean and rewrite the cmdstan run-info JSON |
Build Stan models directly in Swift without an R alist file:
import SwiftStan
let model = UlamModel(data: ["y": .integer([0,1,0,1,1]), "x": .real([1.0,2.0,3.0,4.0,5.0])]) {
Likelihood("y", .bernoulli(p: "p"))
Link(.logit, lhs: "p", rhs: "a + b*x")
Prior("a", .normal(0, 1.5))
Prior("b", .normal(0, 0.5))
}
let stanCode = try stancode(model) // → Stan source StringAll commands read/write under $STAN_CASES/<name>/{Preliminaries,Results}/ (default ~/Documents/StanCases/). Use casePaths(for: model) to navigate the layout. Run info is accessible via readRunInfo(dirUrl:modelName:).
- macOS 14+ only — the library shells out to
make,swiftc, and cmdstan binaries viaFoundation.Process. dsl2stanis not included (it requires the package source tree to recompile viaswiftc). The in-processstancodepath covers alist→Stan withoutswiftc.$CMDSTANmust point to a cmdstan installation; alternatively pass the path explicitly to each command.
swift build
swift test # unit tests only (no cmdstan required)
CMDSTAN=/path/to/cmdstan swift test # unit + integration testsMIT — see LICENSE.