import pandas as pd
# for other distributions, you'll need to implement PMF
from scipy.stats import nbinom, poisson, geom
x = pd.Series(x)
mean = x.mean()
var = x.var()
likelihoods = {} # we'll use it later
# From the wikipedia page, we have:
# mean = pr / (1-p)
# var = pr / (1-p)**2
# without wiki, you could use MGF to get moments; too long to explain here
# Solving for p and r, we get:
p = 1 - mean / var # TODO: check for zero variance and limit p by [0, 1]
r = (1-p) * mean / p
# from Wikipedia,
# mean = variance = lambda. Nothing to solve here
lambda_ = mean
likelihoods['poisson'] = x.map(lambda val: poisson.pmf(val, lambda_)).prod()
您可以使用Method of Moments来适应任何特定的分布。你知道吗
基本思想:先得到经验的一阶矩、二阶矩等,然后由这些矩推导出分布参数。你知道吗
所以,在所有这些情况下,我们只需要两分钟。让我们得到他们:
注意:我用熊猫而不是小熊猫。这是因为numpy的
var()
和std()
不适用Bessel's correction,而pandas的则适用。如果你有100多个样本,应该没有太大的区别,但在较小的样本,这可能是重要的。你知道吗现在,让我们得到这些分布的参数。Negative binomial有两个参数:p,r。让我们估计它们并计算数据集的可能性:
UPD:Wikipedia和scipy使用了不同的p定义,一个将其视为成功概率,另一个视为失败概率。因此,为了与scipy的概念保持一致,请使用:
UPD结束
计算可能性:
与Poisson相同,只有一个参数:
与Geometric distribution相同:
最后,让我们获得最佳拟合:
如果你有任何问题,请告诉我
相关问题 更多 >
编程相关推荐