Created
September 8, 2014 00:54
-
-
Save guipn/4d35c4e6f4c50e500b20 to your computer and use it in GitHub Desktop.
Simple Bayesian inference of the chance someone is infected with a disease.
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
{-- | |
- These functions handle the common scenario presented in introductory classes on Bayesian Inference. | |
- | |
--} | |
type Probability = Double | |
type Prior = Probability | |
type AccuracyPos = Probability | |
type AccuracyNeg = Probability | |
type BayesianInf = Probability | |
{-- pTruePositive gives the chance of an individual actually being infected given that they tested positive. | |
- | |
- Parameters: | |
- | |
- 1. The estimated frequency of infection within that individual's population | |
- 2. The taken test's estimated accuracy for individuals that are infected | |
- 3. The taken test's estimated accuracy for individuals that are not infected | |
- | |
--} | |
pTruePositive :: Prior -> AccuracyPos -> AccuracyNeg -> BayesianInf | |
pTruePositive prior p n = truePos / (truePos + falsePos) | |
where truePos = prior * p | |
falsePos = (1 - prior) * (1 - n) | |
{-- pTruePositiveAfter gives the probability of an individual actually being infected given that x | |
- tests have yielded positive results. Following the fundamental Bayesian model, the initial Prior | |
- is iteratively updated with the information of a positive outcome, yielding the final result. | |
- | |
- The only difference here is that the first parameter is the number of positive results for this individual. | |
-} | |
type Experiments = Integer | |
pTruePositiveAfter :: Experiments -> Prior -> AccuracyPos -> AccuracyNeg -> BayesianInf | |
pTruePositiveAfter x prior p n = case x of 1 -> pTruePositive prior p n | |
_ -> pTruePositiveAfter (x - 1) (pTruePositive prior p n) p n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment