贝叶斯推理

Bayesian inference

我有一个仪器可以通过或不通过一系列三项测试。该仪器必须通过所有三项测试才能被视为成功。我如何使用贝叶斯推理来查看每个案例基于证据的通过概率? (基于依次通过每个过去测试的仪器)。

只看第一个测试 - 我从仪器测试的历史记录中知道这一点。您还可以看到每个测试的接受范围为 -3% 到 +3%:

我的假设:

根据我的理解,我需要在给定数据 (D) 的情况下以贝叶斯术语找到 P(H) - 然后根据测试 A 的结果更新 P(H) -

**P(H|D) = P(H) P(D|H) / P(D)**   Where:

**P(D) = P(D|H)*P(H)  + P(D|’H) P(‘H)**

这是我迷路的地方,我认为这是正确的:

P(H)    = P('H) = 0.5  // prob of passing/failing test-A without any information  

P(D|H)  = 0.84          // prob of passing test-A from historical records

P('D|H) = 0.16         // prob of failing test-A from historical records

P(D) = P(D|H)*P(H) + P(D|’H) P(‘H) = 0.84*0.5 + 0.16*0.5
P(D) = 0.5

给出贝叶斯值: P(H|D) = P(H) P(D|H) / P(D) = 0.5*0.84 / 0.5, P(H|D) = 0.84 这是我在测试 B 中新更新的 P(H) 值?


出于兴趣,所有三个测试看起来都很相似:

所以这里有几件事需要考虑。首先你是对的,使用的先验概率分别是 .5 和 .5,因为这是我们在数学上编码的方式 不知道 发生了什么,但是你独立地显示了三个图彼此并编写只有一维的贝叶斯方程,这违反了您的依赖假设。此外,无需在此设置中使用您的边缘化 P(D) 来获得您所询问的条件概率。

你真正想要的是仪器通过测试 C 的条件概率,因为它在测试 A 和/或测试 B 中的表现

如果你只做了测试 A 那么贝叶斯说:

P(C|A) = P(A|C)P(C)/P(A) or P(B|A) = P(A|B)P(B)/P(A)

其中 A、B 和 C 可以具有通过或失败的值。

如果你已经完成了测试 A 和 B,那么你想知道通过贝叶斯所说的测试 C 的概率是:

P(C|A,B) = P(A,B|C)P(C)/P(A,B)

这看起来要复杂得多,但问题是你真的不需要做贝叶斯推理来得到你要求的条件概率:

What is my probability of passing the next test given that I have already passed or failed this test?

您拥有直接计算所需的所有信息。当人们没有那么奢侈时,他们通常会使用贝叶斯推理。

要回答有关如何根据未来测试是否已经通过一项或多项测试来计算通过概率的问题,请考虑您想要的值的含义。

“Given that the instrument passed (or failed) test 1, what is the chance it will pass test 2 and test 3”

根据你的历史数据,你可以直接回答这个问题。

你的问题表明你关心 pass/fail 的概率,所以每个测试有 2 个可能的结果,这意味着你实际上只有 8 个状态要考虑每个仪器测试集

(Number of TestA Outcomes)* (Number of TestB Outcomes)* (Number of TestC Outcomes) = 2*2*2 = 8

要计算所需的概率,请考虑一个 3D 矩阵,我们将其称为 ProbabilityHistogram,每个结果都有一个单元格。因此矩阵是 2*2*2。矩阵的索引是历史上是否通过了测试。我们将使用这个矩阵来构建历史通过/失败数据的直方图,然后参考该直方图来构建您在下面的代码中感兴趣的概率。

In our approach, the number of times that any instrument previously tested passed test A, failed test B, and Passed Test C would be found in ProbabilityHistogram [1,0,1], passing all three would be found in ProbabilityHistogram [1,1,1], failing all three ProbabilityHistogram [0,0,0], etc.

这里是计算你想要的值的方法

所需直方图的设置

  • 首先定义一个 2*2*2 矩阵来保存直方图数据
  • 正在读取您的历史数据
  • 对于数据集中的每个历史测试,使用下面的 UpdateProbHisto 代码更新 ProbabilityHistogram

计算感兴趣的概率:

  • 使用下面的CProb_BCgA计算一次测试后的条件概率
  • 使用下面的CProb_CgAB计算两次测试后的条件概率

代码:(抱歉,它是用 C# 编写的,因为我在 Python 方面的经验有限,如果您有任何疑问,请发表评论,我会进一步解释)

设置 3D 矩阵

//Define Probability Histogram
        double[, ,] ProbHisto = new double[2, 2, 2];// [A Test Outcome, B Test Outcome, C Test Outcome]

更新直方图

//Update Histogram based on historical data. 
        //pass in how the instrument did on each test as one dataset
        void updateProbHisto(bool APassed, bool BPassed, bool CPassed) {
            ProbHisto[Convert.ToInt16(APassed), Convert.ToInt16(BPassed), Convert.ToInt16(CPassed)]++;
        }

计算一次测试后的概率

//calculate the conditional probability that test B and test C will Pass given A's test reult
        double[] CProb_BCgA(bool ATestResult) {
            //Calculate probability of test B and test C success looking only at tests that passed or failed the same way  this instrument did given the A test result
        double[] rvalue = {0.0,0.0};//P(B|A), P(C|A)
            double BPassesGivenA = ProbHisto[Convert.ToInt16(ATestResult),1,0] + ProbHisto[Convert.ToInt16(ATestResult),1,1];
            double CPassesGivenA = ProbHisto[Convert.ToInt16(ATestResult),1,1] + ProbHisto[Convert.ToInt16(ATestResult),0,1];
            rvalue[0] = BPassesGivenA /(BPassesGivenA+ProbHisto[Convert.ToInt16(ATestResult),0,0] + ProbHisto[Convert.ToInt16(ATestResult),0,1]); // BPasses over BPasses + BFailures
            rvalue[1] = CPassesGivenA /(CPassesGivenA+ProbHisto[Convert.ToInt16(ATestResult),0,0] + ProbHisto[Convert.ToInt16(ATestResult),1,0]);// CPasses over CPasses + CFailures
            return rvalue;
        }

计算两次测试后的概率

//Calculate the conditional probability that test C will pass looking only at tests that passed or failed the same way this instrument did given the A and B test results
        double CProb_CgAB(bool ATestResult, bool BTestResult)
        {
            //Calculate probability of test C success given A and B test results
            double rvalue = 0.0;// P(C|A,B)
            double CPassesGivenAB = ProbHisto[Convert.ToInt16(ATestResult),Convert.ToInt16(BTestResult),1];
            rvalue= CPassesGivenAB /(CPassesGivenAB + ProbHisto[Convert.ToInt16(ATestResult),Convert.ToInt16(BTestResult),0]);// CPasses over CPasses + CFailures
            return rvalue;
        }

条件概率代码的设置假设你做测试A然后测试B然后测试C(BCgA = B通过的概率和C通过给定测试A的结果),但是直接在B 或 C 的测试结果与 A 的结果相同,只需记住要将测试 pass/fail 数据放入哪个索引即可。

正如 Semicolons 和 Duct Tape 所说,我也不认为您根本不需要 P(H) 来回答这个问题。要回答什么 P(C|A) 即通过测试 C 的概率是给定你通过测试,你只需要 P(A & C) 和 P(A),这似乎已经对你可用。 P(B|A)也是如此。

这里有一个 python 片段,显示了实际情况。假设结构实验是一个测试列表,其中每个测试是三个数字的列表,分别对应测试A、测试B和测试C的结果(1表示通过,0表示失败)。

def prob_yx(y, x, exp):
    "P(y|x). Data is the past experimental runs"

    # P (X & Y)
    c_xy = filter(lambda _: _[x] & _[y], exp)
    # P (Y)
    c_x = filter(lambda _: _[x], exp)

    return len(c_xy) / float(len(c_x))


experiment = [
    [0, 0, 1],
    [1, 1, 1],
    [1, 0, 0],
    [1, 1, 1],
    [1, 1, 0]
]

A = 0
B = 1
C = 2

# B given A
print prob_yx(B, A, experiment)
# C given A
print prob_yx(C, A, experiment)
# C given B
print prob_yx(C, B, experiment)

这给出了

0.75
0.5
0.666666666667

希望这对您有所帮助..