scipy truncnorm拟合返回什么参数?

k97glaaz  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(185)

当我使用scipy的truncnorm fit时,即truncnorm.fit(my_array),我得到一个元组形式的答案,例如:
(1.5380758023189927,3.6988860591528123,-0.05604604029264389,0.03643906250110476)的相关系数分别为0.999999、0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
我以为这会是类似于min/max/mean/stddev的东西,但是这些数字实际上没有一个对应于这个数组的任何东西。我搞不清楚truncnorm拟合到底给了我什么。Scipy文档对此似乎不太清楚,至于拟合方法,它似乎把我引向https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_continuous.fit.html,它是所有类型分布的通用方法,并没有详细说明个别的分布。
有人能告诉我truncnorm拟合返回的参数/值是什么吗?

r6vfmomb

r6vfmomb1#

fit方法估计的是将分布参数化所需的值,而不是关于分布的统计量。实际上,rv_continuous的文档说明:
参数元组:浮点元组。任何形状参数的估计值(如果适用),后跟位置和比例的估计值...
truncnorm分配有两个shape参数:ab。因此,您收到的元组为:(fitted_a, fitted_b, fitted_loc, fitted_scale)。这些值是首先创建截断正态分布所需的值。truncnorm文档清楚地说明了ab是什么。下面演示了从查看PDF、从该分布生成随机变量到估计该分布的参数的往返过程。

import numpy as np
from scipy.stats import truncnorm
import matplotlib.pyplot as plt

myclip_a = 0.5
myclip_b = 2.5
loc = 1.3
scale = 1.1
a, b = (myclip_a - loc) / scale, (myclip_b - loc) / scale

x = np.linspace(-2, 4, 100)

# create a graph of how a, b, loc, scale are used to parameterise the truncnorm PDF.

plt.plot(x, truncnorm.pdf(x, a, b, loc, scale),
         'r-', lw=5, alpha=0.6, label='truncnorm pdf')


# create some random variates that obey that truncated normal distribution

noise = truncnorm.rvs(a, b, loc, scale, size=1000000)

# histogram the random variates to visualise the truncated normal distribution.

plt.hist(noise, bins=100);


# now estimate what the values of a, b, loc, scale are from the random variates

fit_a, fit_b, fit_loc, fit_scale = truncnorm.fit(noise, -1,1.2, loc=1, scale=1)

fit_myclip_a = fit_a*fit_scale + fit_loc
fit_myclip_b = fit_b*fit_scale + fit_loc

print(f"Original myclip_a: {myclip_a}, fit_myclip_a: {fit_myclip_a}")
print(f"Original myclip_b: {myclip_b}, fit_myclip_b: {fit_myclip_b}")
print(f"Original loc: {loc}, fit_loc: {fit_loc}")
print(f"Original scale: {scale}, fit_scale: {fit_scale}")

其输出为:

Original myclip_a: 0.5, fit_myclip_a: 0.5000021691860824
Original myclip_b: 2.5, fit_myclip_b: 2.499998152872373
Original loc: 1.3, fit_loc: 1.243679897703037
Original scale: 1.1, fit_scale: 0.9391439098572698

相关问题