定义依赖于单个条件的两个随机变量 [英] Defining two random variables that depend on a single condition
本文介绍了定义依赖于单个条件的两个随机变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 sympy 中,我如何定义两个依赖于共同条件的随机变量 X 和 Y?例如,我如何解决如下问题:
In sympy, how can I define two random variables, X and Y, that depend on a common condition? For example, how do I solve a problem such as the following:
- 我们掷骰子.如果它落在 1 上,则 X=1 且 Y=0.如果它落在 2 上,则 X=0 且 Y=1.否则,X=Y=0.X,Y 的协方差是多少?
推荐答案
如果 X 和 Y 是某个 Z 的函数,则创建 Z 并通过它定义 X、Y.Piecewise
有助于解决这个问题:
If X and Y are functions of some Z, then create Z and define X, Y through it. Piecewise
helps with this:
from sympy.stats import *
Z = Die("Z", 6)
X = Piecewise((1, Eq(Z, 1)), (0, True))
Y = Piecewise((1, Eq(Z, 2)), (0, True))
print(covariance(X, Y)) # -1/36
<小时>
旁白:如果 Y 是 X 的函数,则先创建 X,然后根据它定义 Y.
Aside: If Y is a function of X, then create X first and then define Y in terms of it.
from sympy.stats import Bernoulli, covariance
X = Bernoulli("X", 1/6)
Y = 1 - X
print(covariance(X, Y))
返回 -0.138888888888889.
Returns -0.138888888888889.
这篇关于定义依赖于单个条件的两个随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文