我试图使分布符合某些值。这是我的密码
from __future__ import print_function
import pandas as pd
import numpy as np
import scipy as sp
import scipy.optimize as opt
import scipy.stats
import matplotlib.pyplot as plt
values = np.random.pareto(1.5, 10000)
loc = values.min()
scale = 1
def cost_function(alpha):
cost = -sp.stats.pareto(alpha, loc=loc, scale=scale).pdf(values)
return cost.sum()
opt_res = opt.fmin(cost_function, 1.5)
alpha_fit_v = sp.stats.pareto.fit(values, floc=loc, fscale=scale)
print('opt_res = ', opt_res,
' alpha_fit_v = ', alpha_fit_v)
我原以为alpha_fit_v
等同于opt_res
,但事实并非如此。怎么了?。在
np.random.pareto
的分布与sp.stats.pareto
不同1。成本函数错误
求逆概率之和是没有意义的。您需要使用对数:
2。
不同np.random.pareto
的分布与sp.stats.pareto
这是一个棘手的问题,但是您可能已经注意到即使
^{pr2}$sp.stats.pareto.fit
也没有返回正确的结果。这是因为scipy的Pareto分布无法拟合numpy生成的数据。在也就是说,这将按预期工作:
根据文献,^{} 并不完全来自于帕累托分布:
因此,如果使用numpy生成数据,您有两种选择:
loc=-1
。在values = np.random.pareto(1.5, 10000) + 1
并设置loc=0
。在相关问题 更多 >
编程相关推荐