BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Probability and statistics6 of 24 in topic

57. Update a discrete posterior over hypotheses after each observation

EasyProbability and statistics

Why it matters Discrete Bayes updates power spam filters, diagnostic tests, A/B test reasoning and the belief tracking inside many agents and robots.

Bayes' rule turns a prior belief and evidence into a posterior. With a few discrete hypotheses it is just multiply and normalise.

Write discrete_posterior(prior, likelihood, observations). prior has length H. likelihood is an (H, K) table where likelihood[h][k] = P(observation k | hypothesis h). observations is a list of observation indices, assumed independent given the hypothesis. Return the posterior after all observations as a float array of length H. Normalise after each observation.

Functiondef discrete_posterior(prior, likelihood, observations)

Example 1

Input: prior [0.5, 0.5], likelihood [[0.5, 0.5], [0.9, 0.1]] (a fair coin and a coin biased to heads, observation 0 = heads), observations [0, 0, 0]

Output: [0.14637, 0.85363] (rounded)

Example 2

Input: prior = [0.5, 0.5], likelihood = [[0.5, 0.5], [0.9, 0.1]], observations = [0, 0, 0]

Output: [0.14637, 0.85363]

Example 3

Input: prior = [0.95, 0.05], likelihood = [[0.5, 0.5], [0.9, 0.1]], observations = [0, 0, 0]

Output: [0.765142, 0.234858]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

posterior is proportional to prior times likelihood of each observation.

Hint 2

likelihood[:, k] is the column for observation k.

Hint 3

Divide by the sum after each multiply.

Hint 4

With no observations the answer is the prior.

Common mistakes
  • Ignoring the prior.
  • Forgetting to normalise.
  • Multiplying by rows instead of columns of the likelihood table.
Understand the idea

Bayes' rule says: new belief is proportional to old belief times how well each hypothesis explains what you saw.

Worked example

a bag has two coins, one fair and one that lands heads 90% of the time. You pick one at random (prior 0.5 each) and see heads. Fair coin: 0.5 * 0.5 = 0.25. Biased coin: 0.5 * 0.9 = 0.45. Normalising gives 0.357 and 0.643. After a second heads: 0.357 * 0.5 and 0.643 * 0.9, which normalise to 0.236 and 0.764. Each heads pushes belief toward the biased coin.

posterior(h) = prior(h) * product over observations of P(obs | h), divided by the sum over all h

The prior matters. If biased coins are rare (prior 0.05), three heads in a row still leave the fair coin more likely. This is the classic base-rate lesson from medical tests. The same multiply-and-normalise loop is used in spam filtering and in tracking an agent's belief about hidden state.

Topics
Probability and statistics
Similar questions
  • Track mean and variance of a stream in one passEasy
  • VAE loss: KL divergence between the encoder's Gaussian and the priorEasy
  • Compare two token distributions with Jensen-Shannon divergenceEasy
  • Sample categories from fixed uniforms with the inverse CDFEasy
All Probability and statistics problems
Report
57 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def discrete_posterior(prior, likelihood, observations):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS