繁体   English   中英

帕累托分布:R 与 Python - 不同的结果

[英]Pareto distribution: R vs Python - different results

我正在尝试使用scipy.stats在 Python 中复制 R 的 fitdist() 结果(参考,无法修改 R 代码)。 结果完全不同。 有谁知道为什么? 如何在 Python 中复制 R 的结果?

data = [2457.145, 1399.034, 20000.0, 476743.9, 24059.6, 28862.8]

R 代码:

library(fitdistrplus)
library(actuar)

fitdist(data, 'pareto', "mle")$estimate

R 结果:

       shape        scale 
    0.760164 10066.274196

Python 代码

st.pareto.fit(data, floc=0, scale=1)

Python 结果

(0.4019785013487883, 0, 1399.0339889072732)

差异主要是由于pdf不同。

Python

在 python st.pareto.fit()使用通过此 pdf 定义的帕累托分布:

在此处输入图像描述

import scipy.stats as st
data = [2457.145, 1399.034, 20000.0, 476743.9, 24059.6, 28862.8]
print(st.pareto.fit(data, floc = 0, scale = 1))

# (0.4019785013487883, 0, 1399.0339889072732)

R

而您的 R 代码使用 Pareto 与此 pdf:

在此处输入图像描述

library(fitdistrplus)
library(actuar)
data <- c(2457.145, 1399.034, 20000.0, 476743.9, 24059.6, 28862.8)
fitdist(data, 'pareto', "mle")$estimate

#    shape        scale 
#    0.760164 10066.274196 

制作 R 镜像 Python

要使 R 使用与st.pareto.fit()相同的分布,请使用actuar::dpareto1()

library(fitdistrplus)
library(actuar)
data <- c(2457.145, 1399.034, 20000.0, 476743.9, 24059.6, 28862.8)
fitdist(data, 'pareto1', "mle")$estimate

#     shape          min 
#   0.4028921 1399.0284977

制作 Python 镜像 R

这是一种近似 Python 中的 R 代码的方法:

import numpy as np
from scipy.optimize import minimize

def dpareto(x, shape, scale):
    return shape * scale**shape / (x + scale)**(shape + 1)

def negloglik(x):
    data = [2457.145, 1399.034, 20000.0, 476743.9, 24059.6, 28862.8]
    return -np.sum([np.log(dpareto(i, x[0], x[1])) for i in data])

res = minimize(negloglik, (1, 1), method='Nelder-Mead', tol=2.220446e-16)
print(res.x)

# [7.60082820e-01 1.00691719e+04]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM