-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython
More file actions
53 lines (43 loc) · 1.39 KB
/
python
File metadata and controls
53 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Example 1: Simple Normal model — the simplest possible case.
PyMC model:
mu ~ Normal(0, 10)
sigma ~ HalfNormal(5)
y ~ Normal(mu, sigma) [observed]
This generates ~300 lines of Rust that computes logp + gradient
for 2 unconstrained parameters: mu and log(sigma).
"""
import numpy as np
import pymc as pm
from alchemize import compile_model
# Generate synthetic data
np.random.seed(42)
true_mu, true_sigma = 5.0, 1.2
y_obs = np.random.normal(true_mu, true_sigma, size=100)
# Define PyMC model
SOURCE = """
mu ~ Normal(0, 10)
sigma ~ HalfNormal(5)
y ~ Normal(mu, sigma), observed
"""
with pm.Model() as model:
mu = pm.Normal("mu", mu=0, sigma=10)
sigma = pm.HalfNormal("sigma", sigma=5)
y = pm.Normal("y", mu=mu, sigma=sigma, observed=y_obs)
print(f"True values: mu={true_mu}, sigma={true_sigma}")
print(f"Data: n={len(y_obs)}, mean={y_obs.mean():.3f}, std={y_obs.std():.3f}")
print()
# Compile to Rust
result = compile_model(
model,
source_code=SOURCE,
build_dir="compiled_models/normal",
verbose=True,
)
if result.success:
print(f"\nCompilation successful in {result.n_attempts} attempt(s)!")
print(f"Timings: {result.timings}")
print(f"\nGenerated Rust code saved to: compiled_models/normal/src/generated.rs")
else:
print(f"\nCompilation FAILED after {result.n_attempts} attempts")
for err in result.validation_errors:
print(f" - {err}")