gpt4 book ai didi

probability - 贝叶斯推理

转载 作者:行者123 更新时间:2023-12-03 15:47:21 25 4
gpt4 key购买 nike

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

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

我的假设:

  • 概率相互依赖 - 我们在所有三个测试中都在查看相同的工具
  • 从这个历史数据我看到通过测试 A 的概率是 P(A)=0.84,所以失败是 P(‘A)=0.16
  • 在对仪器一无所知的情况下,一个好的假设是通过和失败第一次测试的等概率 - 假设 (H) 是仪器通过了 P(H) = 0.5;这也给了我们失败的概率 P(‘H) = 0.5。

  • 根据我的理解,我需要在给定数据 (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 这是我在 test-B 中 P(H) 的新更新值?

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

    最佳答案

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

    考虑到它在测试 A 和/或测试 B 上的表现,您真正想要的是仪器通过测试 C 的条件概率

    如果你只做过测试 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?



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

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

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



    凭借您的历史数据,您可以直接回答这个问题。

    您的问题表明您关心通过/失败的概率,因此每个测试有 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 的结果,只需记住您将测试通过/失败数据放入哪个索引。

    关于probability - 贝叶斯推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31516732/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com