Marginal and Conditional Probability

~10 mincode completion

Implement conditional_given_row(joint, row) returning as a 1-D array that sums to 1.

Examples

Conditioning on the first row renormalises [0.1, 0.2] to sum to 1

Input
conditional_given_row([[0.1, 0.2], [0.3, 0.4]], 0)
Output
[0.33333, 0.66667]

The second row is already twice as likely at its right-hand entry

Input
conditional_given_row([[0.1, 0.2], [0.3, 0.4]], 1)
Output
[0.42857, 0.57143]

A row that is already uniform stays uniform

Input
conditional_given_row([[0.25, 0.25], [0.25, 0.25]], 0)
Output
[0.5, 0.5]

Hints

Hint 1

Convert the input with before doing elementwise work.

Hint 2

Watch for this: returns the raw row without renormalising.

Requirements

  • joint: 2-D array of joint probabilities, shape (n, m), summing to 1

  • row: which value of X to condition on

  • Return array of shape (m,) summing to 1

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

8 employers weight this skill

4 quant funds, 2 health and bio companies, 2 big tech firms. Top match scores 93.

Python
import numpy as np


def conditional_given_row(joint, row):
    """
    Conditional distribution of Y given a fixed value of X.

    Args:
        joint: 2-D array of joint probabilities, shape (n, m), summing to 1
        row:   which value of X to condition on

    Returns:
        array of shape (m,) summing to 1
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.