$npx -y skills add pymc-labs/python-analytics-skills --skill prior-elicitationLoad when the user is choosing priors, running prior predictive checks, calling find_constrained_prior, using PreliZ, or otherwise eliciting domain knowledge into a Bayesian model. Covers weakly informative priors, constrained priors, sensitivity analysis, and elicitation workflo
| 1 | # Prior Elicitation for PyMC Models |
| 2 | |
| 3 | ## Decision Flowchart: Choosing a Prior Strategy |
| 4 | |
| 5 | ``` |
| 6 | Do you have domain expertise or expert access? |
| 7 | ├── YES: Can the expert quantify beliefs precisely? |
| 8 | │ ├── YES → Expert elicitation (SHELF protocol, PreliZ roulette/quartile) |
| 9 | │ └── NO → Constrained priors (find_constrained_prior, PreliZ maxent) |
| 10 | └── NO: Do you know the plausible scale of the parameter? |
| 11 | ├── YES → Weakly informative priors (Normal, HalfNormal, Student-t) |
| 12 | └── NO → Use prior predictive checks to calibrate |
| 13 | └── Generate predictions → Do they cover plausible outcomes? |
| 14 | ├── YES → Prior is acceptable |
| 15 | └── NO → Tighten or widen prior, repeat |
| 16 | ``` |
| 17 | |
| 18 | ### When to Use Each Strategy |
| 19 | |
| 20 | | Strategy | Use When | Example | |
| 21 | |---|---|---| |
| 22 | | Weakly informative | You know rough scale but not shape | `pm.Normal("beta", 0, 10)` for standardized predictors | |
| 23 | | Constrained | "95% sure the value is between A and B" | `pm.find_constrained_prior(pm.Normal, ...)` | |
| 24 | | MaxEnt | You have bounds and want least-informative prior | `preliz.maxent(preliz.Normal(), -1, 1, 0.94)` | |
| 25 | | Expert-elicited | Domain expert provides quantiles or probabilities | SHELF protocol with PreliZ | |
| 26 | | Hierarchical | Group-level parameters with partial pooling | `pm.Normal("mu_group", mu=mu_hyper, sigma=sigma_hyper)` | |
| 27 | |
| 28 | ## pm.find_constrained_prior |
| 29 | |
| 30 | Finds distribution parameters such that a specified mass falls within given bounds. |
| 31 | |
| 32 | ```python |
| 33 | import pymc as pm |
| 34 | |
| 35 | # "I'm 95% sure the effect is between -2 and 2" |
| 36 | params = pm.find_constrained_prior( |
| 37 | pm.Normal, |
| 38 | lower=-2, upper=2, |
| 39 | mass=0.95, |
| 40 | init_guess={"mu": 0, "sigma": 1}, |
| 41 | ) |
| 42 | # Returns: {"mu": 0.0, "sigma": 1.02} |
| 43 | |
| 44 | # Use directly in model |
| 45 | with pm.Model() as model: |
| 46 | beta = pm.Normal("beta", **params) |
| 47 | ``` |
| 48 | |
| 49 | ### Common Patterns |
| 50 | |
| 51 | ```python |
| 52 | # Positive parameter, 90% between 0.1 and 10 |
| 53 | params = pm.find_constrained_prior( |
| 54 | pm.LogNormal, |
| 55 | lower=0.1, upper=10, |
| 56 | mass=0.90, |
| 57 | init_guess={"mu": 0, "sigma": 1}, |
| 58 | ) |
| 59 | |
| 60 | # Rate parameter, 80% between 0.01 and 0.5 |
| 61 | params = pm.find_constrained_prior( |
| 62 | pm.Gamma, |
| 63 | lower=0.01, upper=0.5, |
| 64 | mass=0.80, |
| 65 | init_guess={"alpha": 2, "mu": 0.1}, |
| 66 | ) |
| 67 | |
| 68 | # Bounded parameter (probability), 95% between 0.2 and 0.8 |
| 69 | params = pm.find_constrained_prior( |
| 70 | pm.Beta, |
| 71 | lower=0.2, upper=0.8, |
| 72 | mass=0.95, |
| 73 | init_guess={"alpha": 5, "beta": 5}, |
| 74 | ) |
| 75 | ``` |
| 76 | |
| 77 | See `references/find_constrained_prior.md` for full API details. |
| 78 | |
| 79 | ## PreliZ Integration |
| 80 | |
| 81 | PreliZ is a library for prior elicitation. It provides tools to translate domain knowledge into probability distributions. |
| 82 | |
| 83 | ### Maximum Entropy (maxent) |
| 84 | |
| 85 | Find the least-informative distribution consistent with constraints: |
| 86 | |
| 87 | ```python |
| 88 | import preliz as pz |
| 89 | |
| 90 | # Least-informative Normal with 94% mass in [-1, 1] |
| 91 | dist = pz.maxent(pz.Normal(), -1, 1, 0.94) |
| 92 | |
| 93 | # Least-informative HalfNormal with 94% mass below 5 |
| 94 | dist = pz.maxent(pz.HalfNormal(), 0, 5, 0.94) |
| 95 | |
| 96 | # Use result in PyMC |
| 97 | with pm.Model(): |
| 98 | sigma = pm.HalfNormal("sigma", sigma=dist.sigma) |
| 99 | ``` |
| 100 | |
| 101 | ### Roulette (chip-and-bin elicitation) |
| 102 | |
| 103 | Interactive method where an expert allocates "chips" to bins: |
| 104 | |
| 105 | ```python |
| 106 | # Define bins and expert-allocated chip counts |
| 107 | pz.roulette(x_min=0, x_max=100, nrows=10) |
| 108 | # Opens interactive widget — expert distributes chips across bins |
| 109 | # Returns fitted distribution |
| 110 | ``` |
| 111 | |
| 112 | ### Predictive Finder (predictive_elicitation) |
| 113 | |
| 114 | Elicit priors by reasoning about observable predictions: |
| 115 | |
| 116 | ```python |
| 117 | def my_model(x, beta0, beta1, sigma): |
| 118 | mu = beta0 + beta1 * x |
| 119 | return pz.Normal(mu=mu, sigma=sigma) |
| 120 | |
| 121 | # Expert specifies: "when x=5, y is typically between 10 and 30" |
| 122 | pz.predictive_finder(my_model, target=pz.Normal()) |
| 123 | ``` |
| 124 | |
| 125 | ### Quartile Method |
| 126 | |
| 127 | Specify distribution via expert-provided quartiles: |
| 128 | |
| 129 | ```python |
| 130 | # Expert says: "median is 50, Q1 is 30, Q3 is 70" |
| 131 | dist = pz.quartile(pz.Normal(), q1=30, q2=50, q3=70) |
| 132 | ``` |
| 133 | |
| 134 | See `references/preliz.md` for comprehensive PreliZ reference. |
| 135 | |
| 136 | ## Prior Predictive Interpretation Checklist |
| 137 | |
| 138 | Always run prior predictive checks before sampling: |
| 139 | |
| 140 | ```python |
| 141 | with model: |
| 142 | prior_pred = pm.sample_prior_predictive(draws=500, random_seed=42) |
| 143 | |
| 144 | # prior_pred is a DataTree (ArviZ 1.1) |
| 145 | prior_samples = prior_pred["prior"].ds |
| 146 | prior_predictive = prior_pred["prior_predictive"].ds |
| 147 | ``` |
| 148 | |
| 149 | ### What to Check |
| 150 | |
| 151 | 1. **Range of predictions**: Do simulated outcomes cover the plausible data range? |
| 152 | - Plot: `az.plot_ppc_dist(prior_pred, group="prior_predictive", kind="ecdf")` |
| 153 | 2. **Impossible values**: Are any simulated outcomes physically imposs |