Skip to content

Instantly share code, notes, and snippets.

@oldratlee
Last active August 29, 2015 14:04
Show Gist options
  • Save oldratlee/5732339b9552c403a152 to your computer and use it in GitHub Desktop.
Save oldratlee/5732339b9552c403a152 to your computer and use it in GitHub Desktop.
条件概率
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
条件概率公式:
P(A|B) = P(AB)/P(B)
P(AB) = P(A)P(B|A) = P(B)P(A|B)
P(A|B) = P(A)P(B|A)/P(B)
全概率公式:
P(A) = P(B1)P(A|B1) + P(B2)P(A|B2) + ... // B1,B2...是全集
参考:
http://baike.baidu.com/view/965891.htm
问题:
得病概率为x,假阳性概率为y,假阴性概率为z。则检查出了阳性,得病概率是?
事件定义:
D: 得病 // Disease
C: 检查出阳性 // Check out
已知:
得病概率为x,P(D) = x, P(!D) = 1 - x
假阳性概率为y,即没有得病但检查出阳性概率 P(C|!D) = y, P(!C|!D) = 1 - y
假阴性概率为z,即得病检查但出阴性概率 P(!C|D) = z, P(C|D) = 1 - z
求解:
检查出了阳性,得病概率 P(D|C) = ?
解答:
P(D|C) = P(D)P(C|D) / P(C) = P(D)P(C|D) / ( P(D)P(C|D) + P(!D)P(C|!D) )
= x * (1-z) / ( x * (1-z) + (1-x) * y )
"""
import sys
if len(sys.argv) != 4:
print("Usage: python %s <Disease Probability> <False Positive Probability> <False Negative Probability>"
% sys.argv[0])
sys.exit(-1)
x = float(sys.argv[1])
y = float(sys.argv[2])
z = float(sys.argv[3])
print(x * (1 - z) / (x * (1 - z) + (1 - x) * y))
# 几个典型结果
# 0.01 0 0 => 1
# 0.01 0 0.5 => 1
# 0.01 0.01 0.01 => 0.5
#
# 0.0001 0 0 => 1
# 0.0001 0.02 0 => 0.00497561946462
# 0.0001 0.01 0 => 0.00990197049213
# 0.0001 0.01 0.01 => 0.00980392156863
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment