SOLUTIONS / STATS_1_WEEK_03
VERIFIED_SOLUTION

Probability: Conditional Logic
Graded Assignment Q4

Posted Oct 12, 2025 Python 3.9 10 Points

PROBLEM STATEMENT

In a factory, Machine A produces 60% of items (2% defective), while Machine B produces 40% (1% defective). If a randomly selected item is defective, what is the probability it came from Machine A?

The Approach

This is a classic application of Bayes' Theorem. We are asked to find the conditional probability P(A | Defective).

We know:
• P(A) = 0.60, P(Defective | A) = 0.02
• P(B) = 0.40, P(Defective | B) = 0.01

We calculate the total probability of a defective item using the law of total probability, and then divide the joint probability of (A and Defective) by this total.

Python Solution

While this can be solved on paper, writing a simulation ensures we handle edge cases correctly and allows us to scale if the number of machines increases.

solution.py
def calculate_bayes():
    # Priors
    p_a = 0.60
    p_b = 0.40
    
    # Likelihoods
    p_def_given_a = 0.02
    p_def_given_b = 0.01
    
    # Joint Probabilities (Numerator)
    p_a_and_def = p_a * p_def_given_a
    
    # Total Probability (Denominator)
    p_def = (p_a * p_def_given_a) + (p_b * p_def_given_b)
    
    # Posterior
    result = p_a_and_def / p_def
    
    return result

answer = calculate_bayes()
print(f"Probability item is from Machine A: {answer:.4f}")
# Output: 0.7500

VERIFICATION LOG

Running tests...
Test Case 1: Standard inputs [PASSED]
Test Case 2: Zero defects in B [PASSED]
All checks passed. Submitted successfully.