Created
March 21, 2021 16:53
-
-
Save sbrugman/cbd3c519d4fc869cd8aaa9b1886bb060 to your computer and use it in GitHub Desktop.
Probability Theory: The Logic of Science, chapter 9.12 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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