Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

The Bayesian Workflow

Learn the steps of a Bayesian analysis: define the question, build a model, choose priors, compute the posterior, check the model and communicate results, with a runnable example.

A Bayesian analysis is not one formula; it is a loop. You build a model, fit it, check whether it deserves trust, and improve it. Knowing the loop in advance gives the rest of this course a map: each later chapter fills in one box.

flowchart TD
    A["1. Question and data"] --> B["2. Model: likelihood and priors"]
    B --> C["3. Compute the posterior"]
    C --> D["4. Check: diagnostics and predictive checks"]
    D -->|"problems"| B
    D -->|"ok"| E["5. Summarize and decide"]

Most real analyses go around steps 2 to 4 several times.

Step 1: Question and data

Start with a question you could act on: "What is the conversion rate of the new page?" or "How much faster is treatment A than B?" Note what data you have, how it was collected, and what you already know from past studies. The question decides what quantity (the parameter) you need a posterior for.

Step 2: Build the model

A Bayesian model has two parts.

  1. Likelihood: a story for how data arises given parameters. Conversions out of visitors suggest a Binomial. Counts of events suggest a Poisson. Measurements suggest a Normal.
  2. Prior: a distribution for each parameter before seeing data, summarizing real knowledge or weakly constraining plausible values (Choosing Priors).

Writing the model as code-like notation keeps it unambiguous:

Text
clicks ~ Binomial(n, ฮธ)
ฮธ      ~ Beta(2, 8)

Read ~ as "is distributed as". This notation is how PyMC-style tools describe models, and we use it throughout.

Step 3: Compute the posterior

Three families of methods:

Step 4: Check the model

A posterior is only an answer to "given this model". Before believing it, ask:

  • Did the algorithm work? (Convergence diagnostics, MCMC Diagnostics.)
  • Can the model reproduce the data it was fitted to? (Posterior predictive checks.)
  • Do results change if you change the prior or the likelihood slightly? (Sensitivity analysis.)

Step 5: Summarize and decide

Report the posterior mean or median, a credible interval, and the probabilities that matter for the decision, such as "the chance the new page beats the old by at least 1 percentage point". Plot the distributions. State your assumptions.

A complete mini-workflow

Here is the whole loop for a small problem, with a grid. A support team reads that 12 of 50 tickets in a sample were escalated. We want the escalation rate and the chance it exceeds 20%.

Python Workflow in 15 lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The analytic posterior here is Beta(2 + 12, 8 + 38) = Beta(14, 46) with mean 14/60 = 0.233, matching the grid.

Step 4 in practice: does the prior matter?

A quick sensitivity check refits with several priors. If the conclusions agree, you can be relaxed; if not, you need more data or better-justified priors, and should say so.

Python Prior sensitivity check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The flat and weak priors give similar answers (means 0.250 and 0.233); the strong prior (equivalent to 100 earlier tickets centered at 0.2) pulls the result toward 0.2 and lowers the probability of exceeding it. Knowing this is exactly the point of the check.

Common mistakes

  • Skipping step 4: a fitted model is not a validated model.
  • Treating the first model as final. Iteration is normal, not a sign of failure.
  • Hiding the prior. Always write it down next to the likelihood.
  • Reporting only a point estimate. The distribution is the product.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two parts make up a Bayesian model?

    The likelihood describes how data arises given parameters; the prior describes the parameters before the data.
  2. 2. Why does the workflow include a checking step?

    Convergence, predictive checks and sensitivity analysis tell you whether the posterior deserves trust.
  3. 3. In the notation clicks ~ Binomial(n, ฮธ), what does ~ mean?

    ~ reads as 'is distributed as' and states a probability model.
  4. 4. A strong prior and a flat prior give very different results on the same small data set. What should you do?

    Disagreement between reasonable priors means the data is not decisive; be transparent about that.

Summary

  • The workflow: question and data, model, compute posterior, check, summarize and decide, with loops back to the model.
  • A model is a likelihood plus priors, written in ~ notation.
  • Posteriors come from closed forms, grids or MCMC.
  • Always check convergence, predictive fit and prior sensitivity.
  • Report distributions and decision-relevant probabilities.

Next: Setting Up Python for Bayesian Work