如何在 Mathematica 中计算条件概率。是否可以?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上......我过去象征性地解决了这个问题,它涵盖了很多简单的(不受束缚的)概率。我想添加链接并不难(见下文)。欢迎您回复增强。符号方法比使用伯努利分布、创建贝叶斯定理过程并每次都考虑应用它的正确方法要灵活得多。
注意:这些功能没有绑定,就像上面的帖子
((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 中的符号。然后您可以这样使用它:
不需要“not cancer”,因为
P[!Cancer]
产生0.99
(Esc+not+Esc
输入一个非常漂亮的逻辑非符号,但Not[A]
、!A
或\[Not]A
也可以正常工作):
P[!测试 \\[条件]根据定义,Cancer]
将是1-P[Test \\[Conditioned] Cancer]
,除非您覆盖它。现在让我们查询这个模型:
返回
和
返回
我想定义
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.You then use it as such:
Don't need "not cancer" since
P[!Cancer]
yields0.99
(Esc+not+Esc
types a very pretty logical not symbol, butNot[A]
,!A
or\[Not]A
work just fine too)again:
P[!Test \\[Conditioned] Cancer]
will be1-P[Test \\[Conditioned] Cancer]
by definition, unless you override it.Now let's query this model:
returns
and
returns
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.我不会用
Print
语句和BernoulliDistribution
使问题复杂化。您知道概率,因此最简单的方法是直接计算它们,但也许使用向量来获取 P(B) ,并使用 pr(cancer) = 1-pr(not癌症)等等。贝叶斯定理指出 P(A|B)=(P(A ⋂ B))/(P(B))
交集计算为条件概率(给定癌症的测试)乘以癌症概率。
因此,类似以下内容应该有效:
是的,版本 8 中的 概率 功能确实允许您“自动”计算条件概率,但对于像伯努利分布事件这样的问题,它是矫枉过正的。
I wouldn't complicate the issue with
Print
statements andBernoulliDistribution
s. You know the probabilities, so the simplest thing to do is to calculate them directly, but perhaps using vectors to getP(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:
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.