Skip to main content

StatSim compiler — parse .sm models and compile to PyMC, Stan, TFP.js, Z3

Project description

statsim-compiler

Compiler for the StatSim modeling language. Parses .sm models and compiles them to multiple probabilistic programming backends.

Install

pip install statsim-compiler

Usage

from statsim_compiler import parse, compile, model

# Parse and compile to PyMC code
blocks = parse('p ~ Beta(1, 1)\nx ~ Bin(10, p) | 7\nsample(5000)')
result = compile(blocks, 'pymc')
print(result['code'])        # PyMC source code
print(result['param_names']) # ['p']

# High-level: parse + compile + get a runner
runner = model('p ~ Beta(1, 1)\nx ~ Bin(10, p) | 7\nsample(5000)')
print(runner.code)           # generated PyMC code
trace = runner.run()         # run MCMC inference (requires pymc)

Targets

Target Output
pymc PyMC Python code
stan Stan code
tfpjs TensorFlow Probability (JavaScript)
z3 Z3 constraint solver (Python)

API

  • parse(source) — parse .sm source, returns a BlockList
  • compile(blocks, target) — compile to target, returns dict with code, data_names, param_names, inference_mode
  • compile_script(blocks, target) — compile to raw code string
  • model(source, target) — parse + compile + return a runner (PyMC runner for target='pymc')

Runner API (PyMC)

runner = model(source, 'pymc')
runner.code                    # generated source
runner.run(data, **kwargs)     # run inference, returns InferenceData
runner.build(data, **kwargs)   # get pm.Model for extension

Language

One line, one concept. Math notation:

p ~ Beta(1, 1)                    # prior
x ~ Bin(10, p) | 7                # observation
delta = p_B - p_A                 # deterministic
y[i] ~ N(mu, sigma), i=1:N       # plated
sample(5000)                      # directive

See the language reference for the full spec.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

statsim_compiler-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

statsim_compiler-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (309.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file statsim_compiler-0.1.0.tar.gz.

File metadata

  • Download URL: statsim_compiler-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for statsim_compiler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 50d66f0cfed2f24ff3b5d1ed19306bf2afe65617fafc69976ce9fe219e2c383c
MD5 afc3a23d7876bf247c74a852b77bb378
BLAKE2b-256 5ded5438a77fe5ec90c78af7d7194bee41ceda3fc8749dc94975e2206876cd3d

See more details on using hashes here.

File details

Details for the file statsim_compiler-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for statsim_compiler-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1b22efd398543976ecf1f4eee0ed6110396e391536df501430e430a5f12627e4
MD5 d704a1795223edc508ae776b5545fdeb
BLAKE2b-256 6eb261242945290703b6044cc22cd8066722eb53be1ce03908ec20704499bfa2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page