Skip to content

Instantly share code, notes, and snippets.

@sbrugman
Created March 21, 2021 16:53
Show Gist options
  • Save sbrugman/cbd3c519d4fc869cd8aaa9b1886bb060 to your computer and use it in GitHub Desktop.
Save sbrugman/cbd3c519d4fc869cd8aaa9b1886bb060 to your computer and use it in GitHub Desktop.
Probability Theory: The Logic of Science, chapter 9.12 example
import numpy as np
H1 = [0.499, 0.499, 0.002]
H2 = [0.3333333333, 0.3333333333, 0.3333333333]
obs = [14, 14, 1]
def psi(hypothesis, observed):
assert np.isclose(sum(hypothesis), 1)
statistic = 0
n_obs = sum(observed)
for o, p in zip(observed, hypothesis):
statistic += o * np.log10((o / (n_obs * p)))
return 10 * statistic
def chi2(hypothesis, observed):
assert np.isclose(sum(hypothesis), 1)
statistic = 0
n_obs = sum(observed)
for o, p in zip(observed, hypothesis):
statistic += (o - n_obs * p)**2 / (n_obs * p)
return statistic
print(psi(H1, obs), 'db')
print(psi(H2, obs), 'db')
print(chi2(H1, obs))
print(chi2(H2, obs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment