如何在 Mathematica 中计算条件概率。是否可以?

发布于 2024-12-11 22:28:43 字数 1794 浏览 1 评论 0原文

Mathematica 可以进行贝叶斯规则条件概率计算,而无需手动进行计算吗?如果是这样怎么办?

我一直在 Mathemtaica doco 和网络上搜索提示,但找不到任何东西。我不关心如何通过 Mathematica 手动执行贝叶斯规则,我想知道是否有一种方法可以定义条件概率并自动计算其他概率。

因此,使用假设伯努利分布的玩具示例

P(Cancer+) = 0.01
P(Cancer-) = 0.99

P(Test+|Cancer+) = 0.9
P(Test-|Cancer+) = 0.1
P(Test+|Cancer-) = 0.2
P(Test-|Cancer-) = 0.8

是否可以

P(Cancer+|Test+) = 0.0434

使用以下内容来计算出所以。

Print["P(C+) = ", PCancerT=BernoulliDistribution[0.01]];
Print["P(C-) = ", PCancerF=BernoulliDistribution[0.99]];
Print[]
Print["P(T+|C+) = ", PTestTGivenCancerT=BernoulliDistribution[0.9]];
Print["P(T-|C+) = ", PTestFGivenCancerT=BernoulliDistribution[0.1]];
Print["P(T+|C-) = ", PTestTGivenCancerF=BernoulliDistribution[0.2]];
Print["P(T-|C-) = ", PTestFGivenCancerF=BernoulliDistribution[0.8]];
Print[]
Print["P(T+,C+) = ", PTestTAndCancerT = Probability[vCT&&vTTCT,{vCT\[Distributed]PCancerT,vTTCT\[Distributed]PTestTGivenCancerT}]];
Print["P(T-,C+) = ", PTestFAndCancerT = Probability[vCT&&vTFCF,{vCT\[Distributed]PCancerT,vTFCF\[Distributed]PTestFGivenCancerT}]];
Print["P(T+,C-) = ", PTestTAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestTGivenCancerF}]];
Print["P(T-,C-) = ", PTestFAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestFGivenCancerF}]];
Print[]
Print["P(C+|T+) = ?"];
Print["P(C+|T-) = ?"];
Print["P(C-|T+) = ?"];
Print["P(C-|T-) = ?"];

我可以通过手动定义所有概率表来计算联合概率,但是有没有办法让 Mathematica 来完成繁重的工作呢? 有没有办法定义和计算这些条件概率?

非常感谢您的帮助,即使它是“你不能...停止尝试”:)

PS:这是尝试做一些类似的事情吗? Mathematica 中的符号条件期望

Can Mathematica do Bayes Rule conditional probability calculations, without doing the calculation manually? If so how?

I have been searching both the Mathemtaica doco and the web for a hint but cannot find anything. I am not after how to do Bayes Rule manually via Mathematica, I want to know if there is a way to define the conditional probabilities and calculate other ones automagically.

So to use the toy example assuming Bernoulli distributions

P(Cancer+) = 0.01
P(Cancer-) = 0.99

P(Test+|Cancer+) = 0.9
P(Test-|Cancer+) = 0.1
P(Test+|Cancer-) = 0.2
P(Test-|Cancer-) = 0.8

Is it possible to work out

P(Cancer+|Test+) = 0.0434

So using the below.

Print["P(C+) = ", PCancerT=BernoulliDistribution[0.01]];
Print["P(C-) = ", PCancerF=BernoulliDistribution[0.99]];
Print[]
Print["P(T+|C+) = ", PTestTGivenCancerT=BernoulliDistribution[0.9]];
Print["P(T-|C+) = ", PTestFGivenCancerT=BernoulliDistribution[0.1]];
Print["P(T+|C-) = ", PTestTGivenCancerF=BernoulliDistribution[0.2]];
Print["P(T-|C-) = ", PTestFGivenCancerF=BernoulliDistribution[0.8]];
Print[]
Print["P(T+,C+) = ", PTestTAndCancerT = Probability[vCT&&vTTCT,{vCT\[Distributed]PCancerT,vTTCT\[Distributed]PTestTGivenCancerT}]];
Print["P(T-,C+) = ", PTestFAndCancerT = Probability[vCT&&vTFCF,{vCT\[Distributed]PCancerT,vTFCF\[Distributed]PTestFGivenCancerT}]];
Print["P(T+,C-) = ", PTestTAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestTGivenCancerF}]];
Print["P(T-,C-) = ", PTestFAndCancerF = Probability[vCF&&vTTCF,{vCF\[Distributed]PCancerF,vTTCF\[Distributed]PTestFGivenCancerF}]];
Print[]
Print["P(C+|T+) = ?"];
Print["P(C+|T-) = ?"];
Print["P(C-|T+) = ?"];
Print["P(C-|T-) = ?"];

I can work out the joint probabilities by defining all the probability tables manually, but is there a way to get Mathematica to do the heavy lifting?
Is there a way to define and calculate these kind of conditional probabilities?

Many thanks for any assistance, even it its “You can’t... stop trying” :)

PS : was this an attempt at doing something along these lines? Symbolic Conditional Expectation in Mathematica

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-12-18 22:28:43

实际上......我过去象征性地解决了这个问题,它涵盖了很多简单的(不受束缚的)概率。我想添加链接并不难(见下文)。欢迎您回复增强。符号方法比使用伯努利分布、创建贝叶斯定理过程并每次都考虑应用它的正确方法要灵活得多。

注意:这些功能没有绑定,就像上面的帖子 ((0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1) && (0 < pTNC < 1) && (0 < pTNC < 1)) 因为有时您想要“未加权”结果,即产生 0-1 范围之外的数字,那么您可以通过除以某种归一化概率或概率乘积将其带回该范围。如果您确实想添加错误检查范围,请执行以下操作:
P[A_ /;0<=A<=1] := some_function_of_A;

使用 Esc+cond+Esc 输入 \\[Conditioned] Mathematica 中的符号。

Remove[P];
Unprotect@Intersection;
Intersection[A_Symbol, B_Symbol] := {A, B}
Intersection[A_Not, B_Symbol] := {A, B}
Intersection[A_Symbol, B_Not] := {A, B}
P[Int_List/; Length@Int == 2] := P[Int[[2]] \[Conditioned] Int[[1]]] P[Int[[1]]]
   (*//  P(B) given knowledge of P(A)  //*)
P[B_, A_] := If[NumericQ@B, B, 
                P[B \[Conditioned] A] P[A] + P[B \[Conditioned] Not@A] P[Not@A]]
P[Not@B_, A_: 1] := If[NumericQ@A, 1 - P[B], 1 - P[B, A]]
P[A_ \[Conditioned] B_] := P[A \[Intersection] B]/P[B, A]
P[Not@A_ \[Conditioned] B_] := 1 - P[A \[Conditioned] B];

然后您可以这样使用它:

P[Cancer]=0.01;

不需要“not cancer”,因为 P[!Cancer] 产生 0.99 (Esc+not+Esc输入一个非常漂亮的逻辑非符号,但 Not[A]!A\[Not]A 也可以正常工作)

P[Test \[Conditioned] Cancer] = 0.9
P[Test \[Conditioned] ! Cancer] = 0.2

P[!测试 \\[条件]根据定义,Cancer] 将是 1-P[Test \\[Conditioned] Cancer],除非您覆盖它。

现在让我们查询这个模型:

P[Test, Cancer]
P[!Test, Cancer]

返回

0.207
0.793

P[Cancer \[Conditioned] Test]
P[!Cancer \[Conditioned] Test]
P[Cancer \[Conditioned] !Test]
P[!Cancer \[Conditioned] !Test]

返回

0.0434783
0.956522
0.00126103
0.998739

我想定义 P(B|A1,A2,A3,...,An) 是一个好主意,任何准备编写链式法则的人使用 NestList 或类似的东西?我的项目不需要它,但如果有人需要它,添加它并不困难。

Actually... I worked this out symbolically in the past, and it covers a lot of simple (unchained) probabilities. I guess it wouldn't be that hard to add chaining(see below). You're welcome to reply with augmentation. The symbolic approach is far more flexible than working with Bernoulli distributions and creating a proc for Bayes theorem and thinking about the right way to apply it every time.

NOTE: The functions are not bound, like in the post above ((0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1)) because sometimes you want "unweighted" results, which produce numbers outside of 0-1 range, then you can bring back into the range by dividing by some normalizing probability or product of probabilities. If you do want to add bounds for error checking, do this:
P[A_ /;0<=A<=1] := some_function_of_A;

use Esc+cond+Esc to enter \\[Conditioned] symbol in Mathematica.

Remove[P];
Unprotect@Intersection;
Intersection[A_Symbol, B_Symbol] := {A, B}
Intersection[A_Not, B_Symbol] := {A, B}
Intersection[A_Symbol, B_Not] := {A, B}
P[Int_List/; Length@Int == 2] := P[Int[[2]] \[Conditioned] Int[[1]]] P[Int[[1]]]
   (*//  P(B) given knowledge of P(A)  //*)
P[B_, A_] := If[NumericQ@B, B, 
                P[B \[Conditioned] A] P[A] + P[B \[Conditioned] Not@A] P[Not@A]]
P[Not@B_, A_: 1] := If[NumericQ@A, 1 - P[B], 1 - P[B, A]]
P[A_ \[Conditioned] B_] := P[A \[Intersection] B]/P[B, A]
P[Not@A_ \[Conditioned] B_] := 1 - P[A \[Conditioned] B];

You then use it as such:

P[Cancer]=0.01;

Don't need "not cancer" since P[!Cancer] yields 0.99 (Esc+not+Esc types a very pretty logical not symbol, but Not[A], !A or \[Not]A work just fine too)

P[Test \[Conditioned] Cancer] = 0.9
P[Test \[Conditioned] ! Cancer] = 0.2

again: P[!Test \\[Conditioned] Cancer] will be 1-P[Test \\[Conditioned] Cancer] by definition, unless you override it.

Now let's query this model:

P[Test, Cancer]
P[!Test, Cancer]

returns

0.207
0.793

and

P[Cancer \[Conditioned] Test]
P[!Cancer \[Conditioned] Test]
P[Cancer \[Conditioned] !Test]
P[!Cancer \[Conditioned] !Test]

returns

0.0434783
0.956522
0.00126103
0.998739

I guess it would be a nice idea to define P(B|A1,A2,A3,...,An), anyone up for coding the chain rule using NestList or something like it? I didn't need it for my project, but it wouldn't be that difficult to add, should someone need it.

明明#如月 2024-12-18 22:28:43

我不会用 Print 语句和 BernoulliDistribution 使问题复杂化。您知道概率,因此最简单的方法是直接计算它们,但也许使用向量来获取 P(B) ,并使用 pr(cancer) = 1-pr(not癌症)等等。

贝叶斯定理指出 P(A|B)=(P(A ⋂ B))/(P(B))

交集计算为条件概率(给定癌症的测试)乘以癌症概率。

因此,类似以下内容应该有效:

conditionalProb[pC_, pTC_, pTNC_] /; 
 (0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1) :=
 (pTC * pC)/({pTC, pTNC}.{pC, 1 - pC})

conditionalProb[0.01, 0.9, 0.2]

0.0434783

是的,版本 8 中的 概率 功能确实允许您“自动”计算条件概率,但对于像伯努利分布事件这样的问题,它是矫枉过正的。

I wouldn't complicate the issue with Print statements and BernoulliDistributions. You know the probabilities, so the simplest thing to do is to calculate them directly, but perhaps using vectors to get P(B), and using the fact that pr(cancer) = 1-pr(not cancer) and so on.

Bayes' Theorem states that P(A|B)=(P(A ⋂ B))/(P(B))

The intersection is calculated as the conditional probability (test given cancer) times the probability of cancer.

So something like the following should work:

conditionalProb[pC_, pTC_, pTNC_] /; 
 (0 < pC < 1) && (0 < pTC < 1) && (0 < pTNC < 1) :=
 (pTC * pC)/({pTC, pTNC}.{pC, 1 - pC})

conditionalProb[0.01, 0.9, 0.2]

0.0434783

And yes, the Probability functionality in version 8 does allow you to calculate conditional probabilities "automagically", but for a problem like this with Bernoulli-distributed events, it's overkill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文