Bayesian inference: learning a robot's grasp success rate

Learn Bayesian inference through a robot grasp example. Update a Beta prior with successes and failures, then distinguish uncertainty from prediction.

By 8 min read

What you will learn

  • Distinguish a prior, likelihood, posterior, and evidence.
  • Update a Beta distribution using observed successes and failures.
  • Explain how prior strength and sample size affect the result.
  • Separate uncertainty about a success rate from a prediction of the next outcome.

Before you start

Suppose a robot completes seven of eight attempted grasps. The observed success rate is 87.5%. How much should eight attempts change your estimate of its underlying success probability?

Bayesian inference updates a probability distribution using observed data. It gives us a way to combine a starting model with these results while keeping track of uncertainty. We'll use a hypothetical grasp experiment whose update can be calculated by hand.

Name the four parts

Let θ, pronounced “theta,” be the unknown probability that a grasp succeeds. Let D be the observed data. Bayes' rule connects four quantities:

p(θ | D) = p(D | θ) p(θ) / p(D)

  • Prior, p(θ): our distribution over possible success probabilities before these observations.
  • Likelihood, p(D | θ): how probable the observed data would be at each proposed θ, under the observation model.
  • Posterior, p(θ | D): the updated distribution over θ after observing D.
  • Evidence, p(D): the probability of these data averaged over the prior. It normalizes the posterior so its total area is one.

The likelihood holds the observed data fixed while we compare possible parameters. It is not itself a probability distribution over θ. Here θ is continuous, so the prior and posterior are densities: probability belongs to an interval, measured by area under the curve.

This is the same reversal used in the conditional probability sensor example. Here the unknown quantity is a success rate, and we update its full distribution.

Model successful grasps

Record a success as 1 and a failure as 0. Assume attempts are independent conditional on a fixed θ, with the same θ for each attempt. The count of successes then has a binomial distribution.

For our data, D means exactly seven successes in eight attempts. Its likelihood is 8θ⁷(1 − θ). The factor eight counts the possible positions of the one failure. Recording one particular ordered sequence would omit that factor, but would produce the same posterior for θ.

Choose a Beta(α, β) prior, a family of distributions on probabilities from zero to one. Its density is proportional to θ^(α − 1)(1 − θ)^(β − 1). Both shape parameters must be positive. The NIST beta distribution reference gives its density and moments.

Start with Beta(2, 2). This symmetric prior has mean 2 / (2 + 2) = 50% and places more density near the middle than near the endpoints. It is a teaching choice, not an estimate from a real robot.

Beta(1, 1) is uniform over θ. Larger equal parameters concentrate the prior around 50%. Changing their ratio shifts its mean.

Update the distribution

The dashed curve shows the prior. The solid curve shows the posterior after the success and failure counts you enter. The shaded span contains the middle 90% of posterior probability.

Interactive experiment

Update a robot's grasp success rate

Choose a Beta prior, then record successful and failed grasps. The posterior always uses the prior and counts shown below.

Probability densityScale: 0 to 3.6
Prior and posterior distributions for the grasp success probabilityDashed line: Beta(2, 2) prior. Solid line: Beta(9, 3) posterior after 7 successes and 1 failure. The posterior mean is 75.0%. The shaded span is the approximate middle 90% interval, 52.99% to 92.12%. Horizontal axis: success probability from zero to one. Vertical axis: density.

Unknown success probability θ

Dashed curve: prior. Solid amber curve: posterior. Shaded span: the middle 90% of posterior probability. The dotted line marks its mean. Each curve has total area one; the vertical scale adjusts to fit both.
Posterior
Beta(9, 3)
Observed success rate
87.5%
Prior mean
50.0%
Posterior mean
75.0%
Next-grasp success
75.0%
90% credible interval for θ
52.99% to 92.12%

α: 2 + 7 = 9. β: 2 + 1 = 3. The predicted chance of the next success averages over the posterior.

Shapes use integers from 1 to 20; each observation count runs from 0 to 100. The interval uses numerical integration.

Try these comparisons, predicting the direction of each change first:

  • Choose Strong prior (20, 20). The same seven successes and one failure now give a posterior mean of 56.3%, closer to the starting 50%.
  • Choose More data (70 / 10). The observed fraction stays at 87.5%, but the Beta(2, 2) prior has less influence. The posterior mean becomes 85.7%, and the interval narrows.
  • Choose Clear observations. The posterior returns to the selected prior. With no observations, the observed success rate is undefined, while the prior still supports a prediction.

The vertical scale adjusts to fit both curves. Use the interval widths and numerical results to compare settings. Peak heights depend on the displayed scale.

Work through seven successes

Multiplying the Beta prior by the binomial likelihood adds successes to α and failures to β. The posterior remains a Beta distribution, which makes the Beta a conjugate prior for this likelihood. The Book of Statistical Proofs derives this update.

Beta(α, β) + s successes + f failures → Beta(α + s, β + f)

Our example becomes Beta(2 + 7, 2 + 1) = Beta(9, 3). Its mean is:

E[θ | D] = 9 / (9 + 3) = 75%

The mean lies between the prior mean and the observed success rate. In this example, it is their weighted average: (4 / 12) × 50% + (8 / 12) × 87.5% = 75%. The prior's α + β controls its weight in that calculation. These parameters do not mean we actually logged four earlier trials.

The evidence for the count seven out of eight is 16 / 165, about 9.70%, under the starting model. This number normalizes the update; it is not the probability that our model is correct.

Updating one attempt at a time gives the same posterior as adding the whole batch. After carrying a posterior forward as a new prior, add only new observations. Reusing the old counts would count that evidence twice.

Separate uncertainty from prediction

The Beta(9, 3) posterior's equal-tailed 90% credible interval runs from about 53.0% to 92.1%. Conditional on this model, prior, and data, θ has 90% posterior probability of lying in that interval. Each remaining tail has 5%.

This interval describes the unknown rate. The next grasp will produce a binary outcome: success or failure. Its predicted success probability averages θ over the posterior, giving 75%. The Stan guide to posterior prediction explains this averaging over parameter uncertainty.

The widget approximates its interval with numerical integration. The posterior parameters and mean use the formulas directly. No random sampling is needed for this conjugate update.

Reproduce the update in Python

This standard-library example reproduces the initial posterior, mean, and prediction. The standard deviation describes the posterior's spread around its mean; it is not the credible interval.

from math import sqrt

prior_alpha, prior_beta = 2, 2
outcomes = [1, 1, 0, 1, 1, 1, 1, 1]
successes = sum(outcomes)
failures = len(outcomes) - successes

alpha = prior_alpha + successes
beta = prior_beta + failures
mean = alpha / (alpha + beta)
variance = alpha * beta / ((alpha + beta) ** 2 * (alpha + beta + 1))

print(f"Posterior: Beta({alpha}, {beta})")
print(f"Observed success rate: {successes / len(outcomes):.1%}")
print(f"Posterior mean: {mean:.1%}")
print(f"Next-grasp success: {mean:.1%}")
print(f"Posterior standard deviation: {sqrt(variance):.4f}")

The output is Beta(9, 3), 87.5%, 75.0%, 75.0%, and a standard deviation of 0.1201. Change an outcome from 1 to 0 and check how both posterior parameters move.

Check the model's assumptions

The arithmetic cannot tell us whether one constant success probability fits a real grasping task. Before applying this model:

  • Define success, the object population, and the grasp procedure.
  • Check whether hardware, lighting, object difficulty, or the controller changed during the run.
  • Examine dependence. Repeated attempts on the same slippery object may share a cause of failure.
  • Choose a defensible prior and compare reasonable alternatives. Small datasets can leave the answer sensitive to that choice.

More observations can make a misspecified model look very certain. Compare its predictions with fresh outcomes under the intended operating conditions. For a continuous quantity such as position error, the Laplace distribution introduces a possible observation model. Its density can supply a likelihood, while areas under the density describe probabilities for error intervals.

Check your understanding

Exercise 1. Start with a uniform Beta(1, 1) prior and observe three successes and one failure. Find the posterior and the probability of success on the next attempt.

Show the worked solution

The posterior is Beta(1 + 3, 1 + 1) = Beta(4, 2). Its mean, and the predicted probability of the next success, is 4 / 6 ≈ 66.7%. The observed rate is 3 / 4 = 75%; the uniform prior pulls this estimate toward 50%.

Exercise 2. You have already updated to Beta(9, 3). The next grasp fails. What is the new posterior? Which observations should you add?

Show the worked solution

Add only the new failure: Beta(9, 4). Its mean is 9 / 13 ≈ 69.2%. The original eight attempts are already represented in Beta(9, 3). Adding those counts again would incorrectly reuse the same evidence.

Sources and further study