"What's the chance it rains AND traffic is bad" is one question. "Given that it's raining, what's the chance traffic is bad" is a different, more useful one, it's the question you'd actually ask before deciding whether to leave early. The first is a joint probability, both things happening together. The second, conditional probability, restricts your attention to only the world where the condition holds, then asks about the other variable within that restricted world.
Turning a joint distribution over two variables into marginals (one variable alone) and conditionals (one variable, given a specific value of the other) is the basic manipulation every probabilistic model relies on, Naive Bayes (Classical ML) is built by combining conditional probabilities exactly like these.
Theory represents a joint distribution over two discrete variables as a 2D array (joint[i, j] = P(X=i, Y=j)), gets a marginal by summing out the other variable, and gets a conditional by slicing to a fixed value of one variable and renormalizing so the slice becomes a valid probability distribution again.
Implement marginal_x(joint), marginal_y(joint) and conditional_x_given_y(joint, y_index) against that reasoning. The signatures and docstrings are already in the editor.
joint is a 2D array summing to 1.0 overall (a valid joint probability table).1.0.conditional_x_given_y must renormalize, not just return the raw column slice.Open one at a time. Each gives away a little more than the last.
Summing a 2D array along axis=1 collapses columns (summing out the second index); axis=0 collapses rows (summing out the first).
A single column of joint doesn't sum to 1 on its own, dividing by its own sum is what turns it into a valid conditional distribution.
Click "Run Tests" to test your implementation