Created
May 11, 2016 15:23
-
-
Save joashc/cdaa135f1da1b8712849c2059c39cd97 to your computer and use it in GitHub Desktop.
Bayesian interpretation of medical tests
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
public class TestResult | |
{ | |
public TestResult(bool isSick, bool testedPositive, double falsePositiveRate) | |
{ | |
IsSick = isSick; | |
Positive = testedPositive; | |
FalsePositiveRate = falsePositiveRate; | |
} | |
public bool IsSick; | |
public bool Positive; | |
public double FalsePositiveRate; | |
public string ShowSick | |
{ | |
get | |
{ | |
if (IsSick) return "Sick"; | |
return "Not Sick"; | |
} | |
} | |
} | |
public void ScenarioA() | |
{ | |
var falsePositive = from high in BernoulliF(0.5) | |
select high ? 0.4 : 0.2; | |
var test = from isSick in BernoulliF(0.1) | |
from fp in falsePositive | |
let prPositive = isSick ? 0.9 : fp | |
from positiveResult in BernoulliF(prPositive) | |
select new TestResult(isSick, positiveResult, fp); | |
var testedPositive = test.ConditionHard(t => t.Positive); | |
var twoPeople = from p1 in testedPositive | |
from p2 in testedPositive | |
select Pair(p1, p2); | |
Debug.WriteLine(twoPeople.Histogram(pair => $"{pair.Item1.ShowSick}/ {pair.Item2.ShowSick}")); | |
} | |
public void ScenarioB() | |
{ | |
var falsePositive = Func( | |
(double prHigh) => | |
from high in BernoulliF(prHigh) | |
select high ? 0.4 : 0.2 | |
); | |
var test = Func( | |
(double prHigh) => | |
from isSick in BernoulliF(0.1) | |
from falsePos in falsePositive(prHigh) | |
let prPositive = isSick ? 0.9 : falsePos | |
from positiveResult in BernoulliF(prPositive) | |
select new TestResult(isSick, positiveResult, falsePos) | |
); | |
var testedPositive = Func((double prHigh) => test(prHigh).ConditionHard(t => t.Positive)); | |
var updateTest = Func( | |
(FiniteDist<TestResult> dist) => | |
from t1 in dist | |
let updatedFP = dist.ProbOf(t => t.FalsePositiveRate == 0.4) | |
from t2 in testedPositive(updatedFP.Value) | |
select t2 | |
); | |
var firstTest = testedPositive(0.5); | |
var twoTests = | |
from t1 in firstTest | |
from t2 in updateTest(firstTest) | |
select Pair(t1, t2); | |
Debug.WriteLine(twoTests.Histogram(pair => $"{pair.Item1.ShowSick} / {pair.Item2.ShowSick}")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment