【问题标题】:How can I manipulate the CDF of a sample such that it matches that of a different sample?如何操纵样本的 CDF 使其与不同样本的 CDF 匹配?
【发布时间】:2015-10-21 02:19:51
【问题描述】:

我想使用 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')

谢谢大家!

【问题讨论】:

    标签: python numpy statistics scipy weather


    【解决方案1】:

    您所需要的只是一个逼近 A 的 CDF 的函数,以及一个逼近 B 的逆 CDF(或 PPF)的函数。然后您只需计算 qcorrected = PPFB(CDFA(q)).

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

    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)
    

    【讨论】:

    • 工作出色,再次感谢。我创建了一个iPython notebook 来记录方法。
    • Here's iPython notebook 的正确链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2020-02-22
    • 2021-03-12
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多