[英]Calculate marginal distribution from joint distribution in Python
I have these two arrays/matrices which represent the joint distribution of 2 discrete random variables X and Y. I represented them in this format because I wanted to use the numpy.cov
function and that seems to be the format cov
requires. 我有这两个数组/矩阵,它们表示2个离散随机变量X和Y的联合分布。我以这种格式表示它们,因为我想使用
numpy.cov
函数,而这似乎是cov
所需的格式。
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.cov.html https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.cov.html
joint_distibution_X_Y = [
[0.01, 0.02, 0.03, 0.04,
0.01, 0.02, 0.03, 0.04,
0.01, 0.02, 0.03, 0.04,
0.01, 0.02, 0.03, 0.04],
[0.002, 0.002, 0.002, 0.002,
0.004, 0.004, 0.004, 0.004,
0.006, 0.006, 0.006, 0.006,
0.008, 0.008, 0.008, 0.008],
]
join_probability_X_Y = [
0.01, 0.02, 0.04, 0.04,
0.03, 0.24, 0.15, 0.06,
0.04, 0.10, 0.08, 0.08,
0.02, 0.04, 0.03, 0.02
]
How do I calculate the marginal distribution of X (and also of Y) from the so given joint distribution of X and Y? 如何从给定的X和Y联合分布计算X(以及Y)的边际分布? I mean... is there any library method which I can call?
我的意思是...有没有可以调用的库方法?
I want to get as a result eg something like: 我想得到的结果例如:
X_values = [0.002, 0.004, 0.006, 0.008]
X_weights = [0.110, 0.480, 0.300, 0.110]
I want to avoid coding the calculation of the marginal distribution myself. 我想避免自己编写边际分布的计算代码。
I assume there's already some Python library method for that. 我认为已经有了一些Python库方法。
What is it and how can I call it given the data I have? 这是什么,给定我的数据怎么称呼它?
You could use margins : 您可以使用margin :
import numpy as np
from scipy.stats.contingency import margins
join_probability_X_Y = np.array([
[0.01, 0.02, 0.04, 0.04],
[0.03, 0.24, 0.15, 0.06],
[0.04, 0.10, 0.08, 0.08],
[0.02, 0.04, 0.03, 0.02]
])
x, y = margins(join_probability_X_Y)
print(x.T)
Output 输出量
[[0.11 0.48 0.3 0.11]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.