Created
January 14, 2020 20:28
-
-
Save DuaneNielsen/cd45e3b75e1e1ec4860776077645891c to your computer and use it in GitHub Desktop.
Hypothesis testing using Bayesian inferance.
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 torch | |
from torch.distributions.normal import Normal | |
import matplotlib.pyplot as plt | |
""" | |
Using Bayes to estimate the relative probability of 2 Hypotheses given the value of a single data point | |
Both hypothesis given equal prior probability of being correct | |
""" | |
x_axis = torch.linspace(-3.0, 6.0, 50).view(-1, 1) | |
h = Normal(torch.tensor([1.0, 3.0]), torch.tensor([1.0, 1.0])) | |
prior = torch.tensor([0.5, 0.5]) | |
eps = torch.finfo(torch.float32).eps | |
likelyhood = torch.exp(h.log_prob(x_axis)) | |
posterior = likelyhood * prior / ( torch.sum(likelyhood * prior, dim=1, keepdim=True) + eps) | |
fig, ax = plt.subplots(nrows=2, ncols=1) | |
ax[0].title.set_text('hypothesis - H') | |
ax[0].plot(x_axis.squeeze(), torch.exp(h.log_prob(x_axis))) | |
ax[1].title.set_text('p (H | x)') | |
ax[1].bar(x_axis.squeeze(), posterior.T[0]) | |
ax[1].bar(x_axis.squeeze(), posterior.T[1], bottom=posterior.T[0]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment