python 中的加权经验分布函数 (ECDF)

问题描述 投票:0回答:3

我正在尝试在 python 中生成加权经验 CDF。我知道

statsmodel.distributions.empirical_distribution
提供了一个
ECDF
功能,但它没有加权。有没有我可以使用的库,或者我该如何扩展它来编写一个函数来计算加权 ECDF (EWCDF),比如 R 中的ewcdf {spatstat}

python statistics statsmodels
3个回答
0
投票

Seaborn
库具有
ecdfplot
函数,它实现了
ECDF
的加权版本。我查看了
seaborn
如何计算它的代码。

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

sample = np.arange(100)
weights = np.random.randint(10, size=100)

estimator = sns.distributions.ECDF('proportion', complementary=True)
stat, vals = estimator(sample, weights=weights)
plt.plot(vals, stat)

0
投票

Seaborn 提供 ecdfplot 允许您绘制加权 CDF。见seaborn.ecdf。根据 deepAgrawal 的回答,我对其进行了一些调整,以便绘制的是 CDF 而不是 1-CDF。

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

sample = np.arange(15)
weights = np.random.randint(5, size=15)
df = pd.DataFrame(np.vstack((sample, weights)).T, columns = ['sample', 'weights'])
sns.ecdfplot(data = df, x = 'sample', weights = 'weights', stat = 'proportion', legend = True)

0
投票
def ecdf(x):
    Sorted = np.sort(x)
    Length = len(x)
    ecdf = np.zeros(Length)
    for i in range(Length):
        ecdf[i] = sum(Sorted <= x[i])/Length
    return ecdf

x = np.array([1, 2, 5, 4, 3, 6, 7, 8, 9, 10])
ecdf(x)
© www.soinside.com 2019 - 2024. All rights reserved.