有没有办法从scipy.stats.norm.fit中获取拟合参数的错误? [英] Is there a way to get the error in fitting parameters from scipy.stats.norm.fit?

查看:495
本文介绍了有没有办法从scipy.stats.norm.fit中获取拟合参数的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据已使用scipy.stats.normal对象拟合函数拟合为正态分布,如下所示:

 将numpy导入为np 
将matplotlib.pyplot导入为来自scipy.stats的plt
导入规范
将matplotlib.mlab导入为mlab

x = np.random.normal(size = 50000)

图,ax = plt.subplots()

nbins = 75
亩,sigma = norm.fit(x)
n,垃圾箱,补丁= ax.hist(x,nbins,normed = 1,facecolor ='灰色',alpha = 0.5,标签='之前');
y0 = mlab.normpdf(bins,mu,sigma)#最适合的行
ax.plot(bins,y0,'k-',linewidth = 2,label ='fit before')
ax.set_title('$ \mu $ = {},$ \sigma $ = {}'。format(mu,sigma))

plt.show()

I现在想提取拟合的mu和sigma值中的不确定性/误差。我该怎么办?

解决方案

您可以使用


I have some data which I have fitted a normal distribution to using the scipy.stats.normal objects fit function like so:

import numpy as np                                                                                                                                                                                                                       
import matplotlib.pyplot as plt                                                                                                                                                                                                          
from scipy.stats import norm                                                                                                                                                                                                             
import matplotlib.mlab as mlab                                                                                                                                                                                                           

x = np.random.normal(size=50000)                                                                                                                                                                                                         

fig, ax = plt.subplots()                                                                                                                                                                                                                 

nbins = 75                                                                                                                                                                                                                               
mu, sigma = norm.fit(x)                                                                                                                                                                                                                  
n, bins, patches = ax.hist(x,nbins,normed=1,facecolor = 'grey', alpha = 0.5, label='before');                                                                                                                                            
y0 = mlab.normpdf(bins, mu, sigma) # Line of best fit                                                                                                                                                                                    
ax.plot(bins,y0,'k--',linewidth = 2, label='fit before')                                                                                                                                                                                 
ax.set_title('$\mu$={}, $\sigma$={}'.format(mu, sigma))                                                                                                                                                                                  

plt.show()                                                                                                                                                                                                                               

I would now like to extract the uncertainty/error in the fitted mu and sigma values. How can I go about this?

解决方案

You can use scipy.optimize.curve_fit: This method does not only return the estimated optimal values of the parameters, but also the corresponding covariance matrix:

popt : array

Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized

pcov : 2d array

The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).

How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.

If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.

You can calculate the standard deviation errors of the parameters from the square roots of the diagonal elements of the covariance matrix as follows:

import numpy as np                                                                                                                                                                                                                       
import matplotlib.pyplot as plt                                                                                                                                                                                                          
from scipy.stats import norm                                                                                                                                                                                                                                                                                                                                                                                                                   
from scipy.optimize import curve_fit

x = np.random.normal(size=50000)
fig, ax = plt.subplots()                                                                                                                                                                                                                 
nbins = 75                                                                                                                                                                                                                                                                                                                                                                                                                                              
n, bins, patches = ax.hist(x,nbins, density=True, facecolor = 'grey', alpha = 0.5, label='before');       

centers = (0.5*(bins[1:]+bins[:-1]))
pars, cov = curve_fit(lambda x, mu, sig : norm.pdf(x, loc=mu, scale=sig), centers, n, p0=[0,1])  

ax.plot(centers, norm.pdf(centers,*pars), 'k--',linewidth = 2, label='fit before')                                                                                                                                                                                 
ax.set_title('$\mu={:.4f}\pm{:.4f}$, $\sigma={:.4f}\pm{:.4f}$'.format(pars[0],np.sqrt(cov[0,0]), pars[1], np.sqrt(cov[1,1 ])))                                                                                                                                                                              

plt.show()

This results in the following plot:

这篇关于有没有办法从scipy.stats.norm.fit中获取拟合参数的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆