python - 使用pymc用MCMC拟合两个正态分布(直方图)?

标签 python statistics pymc

我正在尝试拟合用 CCD 上的光谱仪检测到的线轮廓。为了便于考虑,我提供了一个演示,如果解决了,它与我实际上想要解决的问题非常相似。

我看过这个: https://stats.stackexchange.com/questions/46626/fitting-model-for-two-normal-distributions-in-pymc 以及其他各种问题和答案,但他们所做的事情与我想做的事情根本不同。

import pymc as mc
import numpy as np
import pylab as pl
def GaussFunc(x, amplitude, centroid, sigma):
    return amplitude * np.exp(-0.5 * ((x - centroid) / sigma)**2)

wavelength = np.arange(5000, 5050, 0.02)

# Profile 1
centroid_one = 5025.0
sigma_one = 2.2
height_one = 0.8
profile1 = GaussFunc(wavelength, height_one, centroid_one, sigma_one, )

# Profile 2
centroid_two = 5027.0
sigma_two = 1.2
height_two = 0.5
profile2 = GaussFunc(wavelength, height_two, centroid_two, sigma_two, )

# Measured values
noise = np.random.normal(0.0, 0.02, len(wavelength))
combined = profile1 + profile2 + noise

# If you want to plot what this looks like
pl.plot(wavelength, combined, label="Measured")
pl.plot(wavelength, profile1, color='red', linestyle='dashed', label="1")
pl.plot(wavelength, profile2, color='green', linestyle='dashed', label="2")
pl.title("Feature One and Two")
pl.legend()

Plot of true values and measured values

我的问题:PyMC(或某些变体)能否为我提供上面使用的两个组件的平均值、振幅和西格玛?

请注意,我实际适合我的实际问题的函数不是高斯函数——因此请提供使用通用函数(如我示例中的 GaussFunc)的示例,而不是“内置”pymc.Normal () 类型函数。

此外,我知道模型选择是另一个问题:因此对于当前的噪音,1 个组件(配置文件)可能是统计上合理的。但我想看看 1、2、3 等组件的最佳解决方案是什么。

我也不认同使用 PyMC 的想法——如果 scikit-learn、astroML 或其他一些包看起来很完美,请告诉我!

编辑:

我失败了很多方法,但我认为正确的事情之一是:

sigma_mc_one = mc.Uniform('sig', 0.01, 6.5)
height_mc_one = mc.Uniform('height', 0.1, 2.5)
centroid_mc_one = mc.Uniform('cen', 5015., 5040.)

但我无法构建有效的 mc.model。

最佳答案

这不是最简洁的 PyMC 代码,但我做出这个决定是为了帮助读者。这应该会运行,并给出(真正)准确的结果。

我决定使用具有自由范围的统一先验,因为我真的不知道我们在建模什么。但可能有人对质心位置有所了解,并且可以在那里使用更好的先验。

### Suggested one runs the above code first.
### Unknowns we are interested in


est_centroid_one = mc.Uniform("est_centroid_one", 5000, 5050 )
est_centroid_two = mc.Uniform("est_centroid_two", 5000, 5050 )

est_sigma_one = mc.Uniform( "est_sigma_one", 0, 5 )
est_sigma_two = mc.Uniform( "est_sigma_two", 0, 5 )

est_height_one = mc.Uniform( "est_height_one", 0, 5 ) 
est_height_two = mc.Uniform( "est_height_two", 0, 5 ) 

#std deviation of the noise, converted to precision by tau = 1/sigma**2
precision= 1./mc.Uniform("std", 0, 1)**2

#Set up the model's relationships.

@mc.deterministic( trace = False) 
def est_profile_1(x = wavelength, centroid = est_centroid_one, sigma = est_sigma_one, height= est_height_one):
    return GaussFunc( x, height, centroid, sigma )


@mc.deterministic( trace = False) 
def est_profile_2(x = wavelength, centroid = est_centroid_two, sigma = est_sigma_two, height= est_height_two):
    return GaussFunc( x, height, centroid, sigma )


@mc.deterministic( trace = False )
def mean( profile_1 = est_profile_1, profile_2 = est_profile_2 ):
    return profile_1 + profile_2


observations = mc.Normal("obs", mean, precision, value = combined, observed = True)


model = mc.Model([est_centroid_one, 
              est_centroid_two, 
                est_height_one,
                est_height_two,
                est_sigma_one,
                est_sigma_two,
                precision])

#always a good idea to MAP it prior to MCMC, so as to start with good initial values
map_ = mc.MAP( model )
map_.fit()

mcmc = mc.MCMC( model )
mcmc.sample( 50000,40000 ) #try running for longer if not happy with convergence.

重要

请记住,该算法与标记无关,因此结果可能显示 profile1 具有 profile2 的所有特征,反之亦然。

关于python - 使用pymc用MCMC拟合两个正态分布(直方图)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15184293/

相关文章:

python - 如何使用 mapPartitions 在 RDD 的分区上运行 python 用户定义函数?

r - 非参数逆(累积)分布函数

python - statsmodels 与 pymc 中的对数似然

python - PyMC 多元线性回归

python - pymc3:如何在多级线性回归中建模相关截距和斜率

python - 如何使用颜色在直方图上表示类别?

python - 如何展平 pandas DataFrame 中的分层列索引?

python - 使用某些函数后对 python 结果感到困惑

php - 如何从 PHP 中的 double 组计算第 n 个百分位数?

r - 在 R 中绘制多个函数