Sometimes P(B) is hard to see directly but easy once you know which "case" you are in: which machine made the part, which bag the marble came from. The law of total probability splits a problem into such cases. It is the joint-from-conditional rule, applied once per case, followed by the addition axiom.
🔑 Key idea
If $A_1, \ldots, A_n$ are disjoint and together cover the sample space (a partition), then $$P(B) = P(B \mid A_1)P(A_1) + \cdots + P(B \mid A_n)P(A_n).$$
💡 Why it works
$B$ is the disjoint union of the pieces $B \cap A_1, \ldots, B \cap A_n$, because every outcome lies in exactly one $A_i$. By the addition axiom, $P(B) = \sum P(B \cap A_i)$, and each piece is $P(B \cap A_i) = P(B \mid A_i)P(A_i)$. So $P(B)$ is a weighted average of the conditional probabilities, with the case probabilities as weights.
A probability tree for three machines: multiply along each branch, then add the defective outcomes.
Worked examples
✏️ Worked example: Three machines
Machines A, B and C make 50%, 30% and 20% of a factory's parts, and 1%, 2% and 4% of their parts are defective. Find the probability that a random part is defective.
The machine is the case: the three cases are disjoint and cover every part.
$$P(D) = 0.01(0.5) + 0.02(0.3) + 0.04(0.2)$$
$$= 0.005 + 0.006 + 0.008 = 0.019$$
Answer: $0.019$, a weighted average of 1%, 2% and 4%, pulled toward machine A's rate because A makes the most parts.
✏️ Worked example: Two bags
Flip a fair coin. Heads: draw from bag I with 3 red and 2 blue marbles. Tails: draw from bag II with 1 red and 4 blue. Find P(red).
A box has 5 red and 3 blue marbles. Draw one and set it aside without looking, then draw a second. Find the probability that the second is red.
Condition on the first draw. If it was red (probability $\frac{5}{8}$), 4 of the remaining 7 are red; if blue (probability $\frac{3}{8}$), 5 of 7 are red.
The same as for the first draw. Without seeing the first marble, the second is equally likely to be any of the 8.
Answer: $\frac{5}{8}$
The law also runs backward. If the overall defect rate is known and one machine's rate is not, write the weighted sum with that rate as the unknown and solve: with the numbers above, $0.019 = 0.005 + 0.006 + 0.2x$ gives $x = 0.04$.
⚠️ Common mistake
Adding the conditional probabilities without the weights: $1\% + 2\% + 4\% = 7\%$ is not a probability of anything here, and the plain average $2.33\%$ ignores that machine A makes half the parts. Check: the answer must lie between the smallest and largest conditional probability, nearer the ones with big weights.
Practice
Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.
Grinstead and Snell, Introduction to Probability (2nd revised edition, American Mathematical Society; free from Dartmouth College), chapter 4 (Conditional Probability), section 4.1
MIT OpenCourseWare 18.05, Introduction to Probability and Statistics (Spring 2014), readings on conditional probability and the law of total probability