Skip to content

Simplest example of PyMC to understand its working on a toy dataset (e.g., a coin flip model) #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
muhammadalmaskhan opened this issue Jan 14, 2025 · 0 comments
Labels
proposal New notebook proposal still up for discussion

Comments

@muhammadalmaskhan
Copy link

muhammadalmaskhan commented Jan 14, 2025

Notebook proposal

Title: Simplest example of PyMC to understand its working on a toy dataset (e.g., a coin flip model)

Why should this notebook be added to pymc-examples?

This notebook aims to provide a highly accessible and transparent example for beginners to understand the fundamental structure of a PyMC model. It demonstrates a single-parameter inference (e.g., coin flip probability) using PyMC, making it an excellent starting point for newcomers.

`
import pymc as pm
import numpy as np
import matplotlib.pyplot as plt

Data: 10 coin flips with 7 heads (1 = heads, 0 = tails)

data = np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 0])

Step 1: Define a PyMC Model

with pm.Model() as model:
# Prior: Assume the probability of heads follows a Beta distribution (uniform prior, so alpha=1, beta=1)
p = pm.Beta('p', alpha=1, beta=1)

# Step 2: Define the Likelihood: This is a Bernoulli distribution based on the coin flips
obs = pm.Bernoulli('obs', p=p, observed=data)

# Step 3: Inference: Draw samples from the posterior distribution using MCMC
trace = pm.sample(2000, return_inferencedata=False)

Step 4: Visualize the posterior distribution of 'p' (the probability of heads)

pm.plot_posterior(trace, var_names=['p'])
plt.title('Posterior distribution of the probability of heads')
plt.show()
`

Suggested categories:

  • Level: Beginner
  • Diataxis type: Tutorial

Related notebooks

[Beginner Tutorials]: If existing beginner-level examples cover complex or multi-parameter models, this notebook will focus on a simple, single-parameter inference (e.g., Bernoulli trials for coin flips).
[Modeling Probabilistic Inference]: Can reference as a foundation to build intuition about more complex models.

References

PyMC documentation: https://www.pymc.io/projects/examples/en/latest/
Probability theory and Bayesian inference resources for beginners.

@muhammadalmaskhan muhammadalmaskhan added the proposal New notebook proposal still up for discussion label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal New notebook proposal still up for discussion
Projects
None yet
Development

No branches or pull requests

1 participant