两个随机变量分布的差异

机器算法验证 Python 累积分布函数 卷积
2022-04-06 19:03:42

我有两组随机变量。我为它们生成了两个 CDF。以图形方式绘制了两个 CDF。在此处输入图像描述我需要找出两个 CDF 分布的差异。我已经了解了一种叫做分布卷积的方法,它给出了总和的分布。如果我们加上要减去的一个分布的否定,我们得到分布的差异(例如, A - B = A + (-B) ),这正是我所需要的。

谁能给我一些软件工具,比如 python 中的 scipy,我可以用它来找到我的问题的解决方案?

2个回答

是两个随机变量XY应该是独立的?如果是这样,很容易证明的分布函数Z=XY由卷积给出

FZ(z)=P(XYz)=FX(z+y)dFY(y).
因此,一种想法是计算经验分布函数F^m(x1,,xm), 和G^n(y1,,yn), 并使用
H^(z)=F^m(z+y)dG^n(y)=1mni=1nj=1mI[xj,)(z+yi)
作为估计FZ(z). 请注意,相应的估计量对于每个z.

我认为您不需要特殊的软件包来执行此操作;普通的numpy就足够了。我在下面附加了示例代码及其输出。请注意,(AB) 的 cdf 看起来与 A 和 B 的 cdf 非常相似,但实际上并非如此。您可以看到大约 +/- 2 或 3 sigma 的细微差别。(AB) 的 cdf 比 A 和 B 的单独 cdf 宽一点。

两个高斯 pdf 差异的示例 cdf

import numpy as np
import matplotlib.pyplot as plt

#!/usr/bin/env python

# Number of random draws to use
ndraws = 1000
# Set this distance (in sigmas) large enough to capture all of the outliers
plotrange = 5
# Number of bins to use for pdf/cdf
nbin = 100
# Get random draws from a Gaussian
A = np.random.randn(1,ndraws)
B = np.random.randn(1,ndraws)
dfAB = A - B

# Calculate cdfs of A and B
Apdf, edges = np.histogram(A, bins=nbin, range=(-plotrange, plotrange))
Bpdf, edges = np.histogram(B, bins=nbin, range=(-plotrange, plotrange))
dfABpdf, edges = np.histogram(dfAB, bins=nbin, range=(-plotrange, plotrange))
xrng = (edges[0:-1] + edges[1:]) / 2

Acdf = np.cumsum(map(float, Apdf)) / ndraws
Bcdf = np.cumsum(map(float, Bpdf)) / ndraws
dfABcdf = np.cumsum(map(float,dfABpdf)) / ndraws

# Plot cdfs and differences of cdfs
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1.plot(xrng, Acdf)
ax1.set_title("A cdf")

ax2 = fig.add_subplot(2,2,2)
ax2.plot(xrng, Bcdf)
ax2.set_title("B cdf")

ax3 = fig.add_subplot(2,2,3)
ax3.plot(xrng, dfABcdf)
ax3.set_title("(A-B) cdf")

ax4 = fig.add_subplot(2,2,4)
ax4.plot(xrng, Acdf - Bcdf)
ax4.set_title("(A cdf) - (B cdf)")

plt.show()