我有两组随机变量。我为它们生成了两个 CDF。以图形方式绘制了两个 CDF。我需要找出两个 CDF 分布的差异。我已经了解了一种叫做分布卷积的方法,它给出了总和的分布。如果我们加上要减去的一个分布的否定,我们得到分布的差异(例如, A - B = A + (-B) ),这正是我所需要的。
谁能给我一些软件工具,比如 python 中的 scipy,我可以用它来找到我的问题的解决方案?
我有两组随机变量。我为它们生成了两个 CDF。以图形方式绘制了两个 CDF。我需要找出两个 CDF 分布的差异。我已经了解了一种叫做分布卷积的方法,它给出了总和的分布。如果我们加上要减去的一个分布的否定,我们得到分布的差异(例如, A - B = A + (-B) ),这正是我所需要的。
谁能给我一些软件工具,比如 python 中的 scipy,我可以用它来找到我的问题的解决方案?
是两个随机变量和应该是独立的?如果是这样,很容易证明的分布函数由卷积给出
我认为您不需要特殊的软件包来执行此操作;普通的numpy就足够了。我在下面附加了示例代码及其输出。请注意,(AB) 的 cdf 看起来与 A 和 B 的 cdf 非常相似,但实际上并非如此。您可以看到大约 +/- 2 或 3 sigma 的细微差别。(AB) 的 cdf 比 A 和 B 的单独 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()