python - 如何操纵样本的 CDF 使其与不同样本的 CDF 相匹配?

标签 python numpy statistics scipy weather

我想使用 CDF 匹配来纠正降水的原始模型预测(但该应用程序相当通用)。

假设下面的 CDF B 是观测到的 CDF(我信任的 CDF),我想计算 CDF A 和 B 之间的差异,以便在给定的一天我可以获取降水预报并将其移动 A 之间的差异和B,使其更能代表B而不是A。

因此,对于每个 x 值,我需要获取 A 的 y 值,然后在 B 是相同值的情况下,我需要获取 x 值,从而给我 2 个 x 值来计算差异。

当然,这只会给出我知道修正值的离散 x 值,因此我想我需要做额外的工作来修正落在其他 2 个之间的 x 值。

这是我用来生成示例的 Python 代码:

import numpy.random
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

quantiles = [0, 1, 2, 3, 4, 5, 7, 10, 15, 20, 30, 40, 50, 60, 75, 100]

# Generate fake precip data
sample_size = 100000
A = numpy.random.gamma(0.7, scale=50, size=sample_size)
B = numpy.random.gamma(0.5, scale=70, size=sample_size)
ens = (40 - 20) * np.random.random_sample((21)) + 20

# Calculate histograms
A_pdf, edges = np.histogram(A, bins=quantiles)
A_pdf = A_pdf / sample_size
A_cdf = np.cumsum(A_pdf)
B_pdf, edges = np.histogram(B, bins=quantiles)
B_pdf = B_pdf / sample_size
B_cdf = np.cumsum(B_pdf)

# Plot CDFs
plt.figure()
plt.plot(quantiles[1:], A_cdf, 'x-', c='r', lw=3, ms=10, mew=2, label='A')
plt.plot(quantiles[1:], B_cdf, '+-', c='k', lw=3, ms=15, mew=2, label='B')
plt.xticks(quantiles[1:])
plt.legend(loc='upper left')

enter image description here

谢谢大家!

最佳答案

您所需要的只是一个近似 A 的 CDF 的函数,以及一个近似 B 的逆 CDF(或 PPF)的函数。然后您只需计算 q校正 = PPF< sub>B(CDFA(q))

对于您的示例数据,我们可以简单地使用 .cdf.ppf 方法来实现 scipy.stats.gamma frozen distributions使用适当的参数:

from scipy import stats

distA = stats.gamma(0.7, scale=50)
distB = stats.gamma(0.5, scale=70)

corrected_quantiles = distB.ppf(distA.cdf(quantiles[1:]))

当然,对于真实数据,您不太可能知道真正的基础分布的参数。如果您对它们的函数形式有很好的了解,您可以尝试对数据执行最大似然拟合以估计它们:

distA = stats.gamma(*stats.gamma.fit(A))
distB = stats.gamma(*stats.gamma.fit(B))

如果做不到这一点,您可以尝试从经验 CDF 中进行插值/外推,例如使用 scipy.interpolate.InterpolatedUnivariateSpline :

from scipy.interpolate import InterpolatedUnivariateSpline

# cubic spline interpolation
itp_A_cdf = InterpolatedUnivariateSpline(quantiles[1:], A_cdf, k=3)
# the PPF is the inverse of the CDF, so we simply reverse the order of the
# x & y arguments to InterpolatedUnivariateSpline
itp_B_ppf = InterpolatedUnivariateSpline(B_cdf, quantiles[1:], k=3)

itp_corrected_quantiles = itp_B_ppf(itp_A_cdf(quantiles[1:]))

fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.plot(quantiles[1:], A_cdf, '-r', lw=3, label='A')
ax.plot(quantiles[1:], B_cdf, '-k', lw=3, label='B')
ax.plot(corrected_quantiles, A_cdf, '--xr', lw=3, ms=10, mew=2, label='exact')
ax.plot(itp_corrected_quantiles, A_cdf, '--+b', lw=3, ms=10, mew=2,
        label='interpolated')
ax.legend(loc=5)

enter image description here

关于python - 如何操纵样本的 CDF 使其与不同样本的 CDF 相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711683/

相关文章:

代表 1/10 的 Python

python - Django 字段引用另一个字段

python - python 调试器不可用的局部变量

r - 所有变量的独立性卡方检验

r - 使用R?在VECM模型中的模量值(根)。

python - 如何在openCV中了解Orb输出中关键点和des的格式

python - TensorFlow 中张量值的条件赋值

python - pandas.concat 产生所有 NaN

python - numpy 中的行专业和列专业是什么?

python - 在 matplotlib 中绘制一个散点图,x 轴为日期,y 轴为值