固定一些参数拟合双峰高斯分布

asc*_*ter 3 python statistics distribution scipy

问题:我想将经验数据拟合到双峰正态分布,从物理环境中我可以知道峰值的距离(固定),并且两个峰值必须具有相同的标准差。

我试图创建一个自己的发行版scipy.stats.rv_continous(参见下面的代码),但参数始终适合 1。有人了解发生了什么,或者可以向我指出解决问题的不同方法吗?

详细信息:我避免了locscale参数,并将它们作为ms直接实现到_pdf- 方法中,因为峰值距离delta不会受到 的影响scale。为了弥补这一点,我将它们固定在floc=0-方法fscale=1fit,并且实际上想要 的拟合参数m以及s峰值的权重w

x=-450我在样本数据中期望的是在和x=450(=> )周围有峰值的分布m=0。标准s差应约为 100 或 200,但不是 1.0,并且权重w应约为 1.0。0.5

from __future__ import division
from scipy.stats import rv_continuous
import numpy as np


class norm2_gen(rv_continuous):
    def _argcheck(self, *args):
        return True

    def _pdf(self, x, m, s, w, delta):
        return np.exp(-(x-m+delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2) * w + \
               np.exp(-(x-m-delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2) * (1 - w)


norm2 = norm2_gen(name='norm2')

data = [487.0, -325.5, -159.0, 326.5, 538.0, 552.0, 563.0, -156.0, 545.5, 341.0, 530.0, -156.0, 473.0, 328.0, -319.5, -287.0, -294.5, 153.5, -512.0, 386.0, -129.0, -432.5, -382.0, -346.5, 349.0, 391.0, 299.0, 364.0, -283.0, 562.5, -42.0, 214.0, -389.0, 42.5, 259.5, -302.5, 330.5, -338.0, 508.5, 319.5, -356.5, 421.5, 543.0]

m, s, w, delta, loc, scale = norm2.fit(data, fdelta=900, floc=0, fscale=1)
print m, s, w, delta, loc, scale

>>> 1.0 1.0 1.0 900 0 1
Run Code Online (Sandbox Code Playgroud)

War*_*ser 6

经过一些调整后,我能够让您的分布适合数据:

  • 按照w您的操作,您有一个隐含约束:0 <= w<= 1。该fit()方法使用的求解器不知道此约束,因此w最终可能会得到不合理的值。处理此类约束的一种方法是允许w为任意实数值,但在 PDF 公式中,使用 转换为 0 到 1 之间的w分数。phiphi = 0.5 + arctan(w)/pi
  • 通用fit()方法使用数值优化例程来查找最大似然估计。与大多数此类例程一样,它需要一个优化起点。默认的起始点是全 1,但这并不总是有效。fit()您可以通过在数据后面提供值作为位置参数来选择不同的起点。我在脚本中使用的值有效;我没有探究结果对这些起始值的敏感程度。

我做了两个估计。在第一个中,我将delta设为自由参数,在第二个中,我将其固定delta为 900。

下面的脚本生成以下图:

阴谋

这是脚本:

from __future__ import division
from scipy.stats import rv_continuous
import numpy as np
import matplotlib.pyplot as plt


class norm2_gen(rv_continuous):
    def _argcheck(self, *args):
        return True

    def _pdf(self, x, m, s, w, delta):
        phi = 0.5 + np.arctan(w)/np.pi
        return np.exp(-(x-m+delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2) * phi + \
               np.exp(-(x-m-delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2) * (1 - phi)

norm2 = norm2_gen(name='norm2')


data = [487.0, -325.5, -159.0, 326.5, 538.0, 552.0, 563.0, -156.0, 545.5,
        341.0, 530.0, -156.0, 473.0, 328.0, -319.5, -287.0, -294.5, 153.5,
        -512.0, 386.0, -129.0, -432.5, -382.0, -346.5, 349.0, 391.0, 299.0,
        364.0, -283.0, 562.5, -42.0, 214.0, -389.0, 42.5, 259.5, -302.5,
        330.5, -338.0, 508.5, 319.5, -356.5, 421.5, 543.0]

# In the fit method, the positional arguments after data are the initial
# guesses that are passed to the optimization routine that computes the MLE.
# First let's see what we get if delta is not fixed.
m, s, w, delta, loc, scale = norm2.fit(data, 1.0, 1.0, 0.0, 900.0, floc=0, fscale=1)

# Fit the disribution with delta fixed.
fdelta = 900
m1, s1, w1, delta1, loc, scale = norm2.fit(data, 1.0, 1.0, 0.0, fdelta=fdelta, floc=0, fscale=1)

plt.hist(data, bins=12, normed=True, color='c', alpha=0.65)
q = np.linspace(-800, 800, 1000)
p = norm2.pdf(q, m, s, w, delta)
p1 = norm2.pdf(q, m1, s1, w1, fdelta)
plt.plot(q, p, 'k', linewidth=2.5, label='delta=%6.2f (fit)' % delta)
plt.plot(q, p1, 'k--', linewidth=2.5, label='delta=%6.2f (fixed)' % fdelta)
plt.legend(loc='best')
plt.show()
Run Code Online (Sandbox Code Playgroud)