class ProbabilityRules:
@staticmethod
def sum_rule(p_XY):
p_X = {}
for X, Y in p_XY:
p_X[X] = p_X.get(X, 0) + p_XY[(X, Y)]
return p_X
@staticmethod
def product_rule(p_Y_given_X, p_X):
p_XY = {}
for Y, X in p_Y_given_X:
p_XY[(X, Y)] = p_Y_given_X[(Y, X)] * p_X[X]
return p_XY
@staticmethod
def bayes_theorem(p_X_given_Y, p_Y, p_X):
p_Y_given_X = {}
for X, Y in p_X_given_Y:
p_Y_given_X[(Y, X)] = (p_X_given_Y[(X, Y)] * p_Y[Y]) / p_X[X]
return p_Y_given_X