本文介绍了正态分布的Scipy MLE拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试采用此线程来确定简单正态分布的参数。即使修改很小(基于维基百科),结果还是不错的。有什么建议哪里出错了吗?I was trying to adopt this solution proposed in this thread to determine the parameters of a simple normal distribution. Even though the modifications are minor (based on wikipedia), the result is pretty off. Any suggestion where it goes wrong?import mathimport numpy as npfrom scipy.optimize import minimizeimport matplotlib.pyplot as pltdef gaussian(x, mu, sig): return 1./(math.sqrt(2.*math.pi)*sig)*np.exp(-np.power((x - mu)/sig, 2.)/2)def lik(parameters): mu = parameters[0] sigma = parameters[1] n = len(x) L = n/2.0 * np.log(2 * np.pi) + n/2.0 * math.log(sigma **2 ) + 1/(2*sigma**2) * sum([(x_ - mu)**2 for x_ in x ]) return Lmu0 = 10sigma0 = 2x = np.arange(1,20, 0.1)y = gaussian(x, mu0, sigma0)lik_model = minimize(lik, np.array([5,5]), method='L-BFGS-B')mu = lik_model['x'][0]sigma = lik_model['x'][1]print lik_modelplt.plot(x, gaussian(x, mu, sigma), label = 'fit')plt.plot(x, y, label = 'data')plt.legend() Outpu适合的t:Output of the fit: jac:array([2.27373675e-05,2.27373675e-05])jac: array([2.27373675e-05, 2.27373675e-05])消息:'收敛性:REL_REDUCTION_OF_F_< = _ FACTR * EPSMCH'message: 'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'成功:真 x:array( [10.45000245,5.48475283])x: array([10.45000245, 5.48475283])推荐答案最大似然法是将分布的参数拟合为一组值,这些值据称是来自该分布的随机样本。在 lik 函数中,使用 x 来保存样本,但使用 x 是一个全局变量,已设置为 x = np.arange(1,20,0.1)。绝对不是来自正态分布的随机样本。The maximum likelihood method is for fitting the parameters of a distribution to a set of values that are purportedly a random sample from that distribution. In your lik function, you use x to hold the sample, but x is a global variable that you have set to x = np.arange(1,20, 0.1). That is definitely not a random sample from a normal distribution.由于您使用的是正态分布,因此可以对最大似然估计使用已知公式来检查计算:mu是样本均值,sigma是样本标准差:Because you are using the normal distribution, you can use the known formulas for the maximum likelihood estimate to check your computation: mu is the sample mean, and sigma is the sample standard deviation:In [17]: x.mean()Out[17]: 10.450000000000006In [18]: x.std()Out[18]: 5.484751589634671这些值与您调用 minimize 的结果非常接近,因此看起来您的代码正在运行Those value matches the result of your call to minimize pretty closely, so it looks like your code is working.要修改代码以按预期方式使用MLE, x 应该是一个集合的值据称是来自正态分布的随机样本。请注意,数组 y 不是这样的示例。它是网格上的概率密度函数(PDF)的值。如果将分布适合PDF样本是您的实际目标,则可以使用曲线拟合功能,例如 scipy.optimize.curve_fit 。 实际上,如果要使正态分布参数适合随机样本,那么要测试代码,则应使用输入,该输入是来自具有已知参数的分布的相当大的样本。在这种情况下,您可以To modify your code to use MLE in the way you expected it to work, x should be a collection of values that are purportedly a random sample from a normal distribution. Note that your array y is not such a sample. It is the value of the probability density function (PDF) on a grid. If fitting the distribution to a sample of the PDF is your actual goal, you can use an curve-fitting function such as scipy.optimize.curve_fit.If fitting the normal distribution parameters to a random sample is, in fact, what you want to do, then to test your code, you should use an input that is a reasonably large sample from a distribution with known parameters. In this case, you can dox = np.random.normal(loc=mu0, scale=sigma0, size=20)当我在其中使用这样的 x 您的代码,我得到了When I use such an x in your code, I getIn [20]: lik_model.xOut[20]: array([ 9.5760996 , 2.01946582])正如预期的那样,解决方案中的值大约为10和2。As expected, the values in the solution are approximately 10 and 2.(如果像我一样使用 x 作为样本,则必须更改绘图代码(If you use x for your sample as I did, you'll have to change yourplotting code accordingly.) 这篇关于正态分布的Scipy MLE拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 10:57