Conditional Probability from Data
Probability Theory DS practice problem on Onlearn.
Difficulty: easy.
Topics: Conditional Probability from Data, Bayes Theorem, Maximum Likelihood Estimation, One-Hot Encoding, Kullback-Leibler Divergence, Conjugate Priors, Probability Theory, Statistical Inference, Data Preprocessing, Information Theory, Bayesian Modeling, Joint Distributions, Frequentist Estimation, Feature Engineering, Entropy Measures, Prior-Posterior Analysis.
Given a dataset of observations as a list of tuples, each tuple is of the form (X, Y), where X and Y are categorical variables (e.g., color, animal). Implement a function to compute the conditional probability $P(Y=y|X=x)$, the probability that Y equals a specific value y, given that X equals a specific value x. Your Task: Write a function conditional probability(data, x, y) that takes as input: data: List of (X, Y) tuples. x: Value for variable X to condition on. y: Value for variable Y whose probability you want given X=x. The function should return the probability $P(Y=y|X=x)$. Return 0.0 if there are no instances where X=x. Example: Reasoning: For X='red', there are 4 instances: 2 with Y='cat' and 2 with Y='dog'. So $P(Y=cat|X=red) = 2/4 = 0.5$. For X='blue', 2 instances: 1 with Y='cat', 1 with Y='dog'. So $P(Y=cat|X=blue) = 1/2 = 0.5$. For X='green', there are 0 instances, so output 0.0.