Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-041

Correlation of Bernoulli Variables

Problem

Correlation coefficient of two Bernoulli variables.

rho = (P(A and B) - P(A) * P(B)) / sqrt(P(A) * (1 - P(A)) * P(B) * (1 - P(B))). Return 0.0 when the denominator is 0.

Hint

Normalize the covariance by the product of the two standard deviations.

Test cases

case 1
in: [0.5,0.5,0.25]
expected: 0
case 2
in: [0.5,0.5,0.4]
expected: 0.6000000000000001
case 3
in: [0.2,0.3,0.06]
expected: 0
case 4
in: [0.5,0.5,0]
expected: -1
case 5
in: [0,0.5,0]
expected: 0
Starter codePython
import math


def correlation_bernoulli(p_a, p_b, p_ab):
    # Your code here
    pass

5 test cases · run your code against them in the editor