Skip to content

Instantly share code, notes, and snippets.

@cbrnr
Last active September 29, 2021 10:54
Show Gist options
  • Save cbrnr/e6dd841d1747c699f237 to your computer and use it in GitHub Desktop.
Save cbrnr/e6dd841d1747c699f237 to your computer and use it in GitHub Desktop.
Compute measure of dependence
import numpy as np
def dependence_factor(a, b, norm=True):
"""Compute measure of dependence between a and b.
Independent means that Pab(i,j) - Pa(i) * Pa(j) == 0 for all i and j, where
i and j are the individual values that the variables a and b can take.
If norm=True, this function returns a value between 0 and 1, where 1 means
that the variables are perfectly dependent, and 0 means the variables are
perfectly independent.
If norm=False, this function returns the mean absolute deviation from 0.
The larger this result, the more dependent are a and b.
"""
Pa = [np.mean(a == 0), np.mean(a == 1)]
Pb = [np.mean(b == 0), np.mean(b == 1)]
Pab = np.array([[np.mean(np.logical_and(a == 0, b == 0)),
np.mean(np.logical_and(a == 0, b == 1))],
[np.mean(np.logical_and(a == 1, b == 0)),
np.mean(np.logical_and(a == 1, b == 1))]])
agreement = Pab - np.outer(Pa, Pb)
mad = np.mean(np.abs(agreement))
if norm:
return mad / dependence_factor(a, a, norm=False)
else:
return mad
A = '0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0'
a = np.array([float(a) for a in A.split()])
a, a_lagged = a[2:], a[:-2]
# we can also use a larger sample:
# a = np.random.binomial(1, 0.02, 1002)
# a, a_lagged = a[2:], a[:-2]
print('a[n], a[n]: {:.2f}'.format(dependence_factor(a, a)))
print('a[n], a[n-2]: {:.2f}'.format(dependence_factor(a, a_lagged)))
print('a[n], a[n] + a[n-2]: {:.2f}'.format(dependence_factor(a, a + a_lagged)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment