A
The
For example, our population could be men in Bolivia. The value we are measuring could be height. The
The
This is designed so that an accurate
For example, this website says that the mean height of men in Bolivia is
How do we know when our error
"$p$-value" means "probability value". Assuming that
If
Knowing
Note that the left column "df" is the degrees of freedom, and the values in the table are
In our example, we had
This is less than the
Recall that
Then
As
Let's say our population is a bunch of frogs. There is a certain probability that a randomly chosen frog will display the y gene, which is correlated to another gene.
Precision
Of those classified as Will return, what proportion actually did? True positive / (True positive + False positive)
Recall: Of those that in fact Returned, what proportion were classified that way? True positive / (True positive + False negative)
Let's say that
is some event that could occur (or not occur), and
is the probability that
is the probability that both
is the probability that
Switching the roles of
Finally, we can set these equal to each other
and if we'd like, solve for
This final equation is known as Bayes' Rule.
Firstly, what is the 'posterior distribution'?
Let
We have an initial predicted distribution
which is not only true for probabilities, but for probability density functions / distributions:
Here,
First we need to introduce some vocabulary. In the posterior equation
We are often in a situation where we know the distributions on the right-hand-side of the equation, but it is still hard to multiply them and get a nice posterior distribution.
This is where a 'conjugate' prior is so helpful! It turns out that if the prior distribution is conjugate to the likelihood, then the posterior distribution will be very similar to the prior distribution, thus making our lives easy!
Definition: A family F of prior distributions is called conjugate to a likelihood distribution if the resulting posterior distribution is in F.
Here is a table of useful conjugate priors:
"likelihood" conjugate to "prior"... Bernouilli conjugate to Beta Binomial conjugate to Beta Beta conjugate to Beta Normal conjugate to Normal (for the mean)
Here is some code that does posterior sampling.
#!/usr/bin/env python3
# pretend we have a population of 100 people, and each person has a height. The heights are normally distributed with mean MEAN_ACTUAL = 67 inches. But WE THINK that the mean is m_guess = 63. Then we draw a sample X of 10 people from the population. Finally, we use the sample X to calculate the posterior distribution of people's heights.
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
from scipy import stats
# GLOBALS
MEAN_ACTUAL = 67
STD_DEV_ACTUAL = 4
# p(T)
MEAN_GUESS = 63
STD_DEV_GUESS = 4
# other
NUM_SAMPLES = 10
# p(T | X) = p(X | T) * p(T) / p(X)
# plot setup
plt.xlabel('mean')
plt.ylabel('probability density')
x_ = np.arange(30, 90, 0.2)
# prior distribution:
# p(T)
def p_T(x):
return sp.stats.norm.pdf(x, loc=MEAN_GUESS, scale=STD_DEV_GUESS)
y_ = [p_T(x) for x in x_]
plt.plot(x_, y_, 'r')
# actual distribution:
def p_true(x):
return sp.stats.norm.pdf(x, loc=MEAN_ACTUAL, scale=STD_DEV_ACTUAL)
y_ = [p_true(x) for x in x_]
plt.plot(x_, y_, 'g')
# likelihood:
# p(X | T) = prod_j p(x_j | T)
# take some samples from the actual distribution
samples = np.random.normal(loc=MEAN_ACTUAL, scale=STD_DEV_ACTUAL, size=NUM_SAMPLES)
mean_samples = np.mean(samples)
std_dev_samples = np.std(samples)
# p(X)
# is just a constant when we consider the mean T to be the variable.
# p(T | X)
# must be normal, since likelihood and prior distribution are normal. Therefore, we need only find the mean and standard deviation.
variance_new = 1 / (1/STD_DEV_GUESS**2 + NUM_SAMPLES * 1/std_dev_samples**2)
std_dev_new = np.sqrt(variance_new)
mean_new = variance_new * (MEAN_GUESS/STD_DEV_GUESS**2 + NUM_SAMPLES * mean_samples/std_dev_samples**2)
def p_T_X(x):
return sp.stats.norm.pdf(x, loc=mean_new, scale=std_dev_new)
y_ = [p_T_X(x) for x in x_]
plt.plot(x_, y_, 'b')
# output
print('new mean = {}'.format(mean_new))
print('new std dev = {}'.format(std_dev_new))
plt.show()
You see that the new predicted mean is now much closer to 67 than before.
- Wikipedia: Student's t-test
- Wikipedia: t-distribution
- Wikipedia: p-value
- sjsu.edu: t-table
- Wikipedia: Bayes theorem
- YouTube: Bayesian posterior sampling
- YouTube: Conjugate priors
- pballew.net: Combining distributions
- johndcook.com: Product of normal pdfs
- [Wikipedia: Sampling](https://en.wikipedia.org/wiki/Sampling_(statistics)
- Wikipedia: Gibbs sampling
- averageheight.co: data