Estimating Coin Toss Probability with Maximum Likelihood in Python
This article explains the concept of maximum likelihood estimation (MLE) and demonstrates how to estimate the probability of heads in a coin‑toss experiment using Python simulation, sympy for the likelihood function, and visualisation of the resulting discrete probability distribution.
Maximum likelihood estimation (MLE) is a common parameter estimation method. The intuitive idea is that if a random experiment has possible outcomes A, B, C… and outcome A occurs, the experimental conditions favor A, i.e., the probability P(A) is large. When a random sample follows a known distribution but its parameters are unknown, we estimate the parameters by observing outcomes and choosing the parameter that maximizes the sample probability.
This article uses a simple discrete distribution example to simulate a coin toss and estimate the probability of heads. A coin toss is a Bernoulli experiment with outcomes head (1) or tail (0), whose probability mass function is:
For n independent tosses the likelihood function is:
We now estimate p by maximizing this likelihood.
First we simulate coin tosses in Python:
Using the sympy library we write the likelihood function:
In a simulation of 100 Bernoulli trials we observed 53 heads and 47 tails, close to the assumed true value 0.5.
Next we solve for p that maximizes the likelihood using Python:
The result is unsurprising: 53/100 ≈ 0.5.
After taking logarithms, the algorithm actually solves the equation:
This equation underlies the MLE for logistic regression parameters.
To visualize the discrete probability mass function we compute the pmf centered at the mean (100 × 0.5 = 50):
The following plot helps understand the distribution:
Displaying all 20 discrete probabilities shows the maximum around 0.08.
The article demonstrates MLE for a simple discrete PMF using Python; the same approach applies to continuous distributions by deriving the likelihood from the probability density function. Readers are encouraged to run the code and compare results with R, SAS, and other tools to explore better MLE implementations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
