本文介绍了从Python中的联合分布计算边际分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个数组/矩阵,它们代表2个离散随机变量X和Y的联合分布.我以这种格式表示它们是因为我想使用numpy.cov函数,而该格式似乎是cov要求.

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.

https://docs.scipy .org/doc/numpy-1.15.0/reference/generation/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
            ]

如何根据给定的X和Y的联合分布来计算X(以及Y)的边际分布?我的意思是...有什么可以调用的库方法吗?

How do I calculate the marginal distribution of X (and also of Y) from the so given joint distribution of X and Y? I mean... is there any library method which I can call?

我想得到例如像这样:

 X_values = [0.002, 0.004, 0.006, 0.008]
 X_weights = [0.110, 0.480, 0.300, 0.110]

我想避免自己对边际分布的计算进行编码.
我认为已经有了一些Python库方法.
这是什么,给定我的数据怎么称呼它?

I want to avoid coding the calculation of the marginal distribution myself.
I assume there's already some Python library method for that.
What is it and how can I call it given the data I have?

推荐答案

您可以使用边距:

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)

输出

[[0.11 0.48 0.3  0.11]]

这篇关于从Python中的联合分布计算边际分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:22