我正在尝试emcee
使用预定义的似然函数在 Python 中实现 MCMC 采样,以找到两个数据群之间的最佳边界。
参见emcee
:http ://dfm.io/emcee/current/user/line/
似然函数计算真阳性和真阴性分类,给定一些线性边界线,并用于最小化两个值之间的差异,同时最大化它们的总和。
这样,您可以想象 TP 和 TN 比率分别为 1 将给出似然值 1,而 TP 和 TN 比率为 0 将返回似然值 0。
但是,当我尝试对边界线的m
和b
、梯度和偏移(或偏差)的参数空间进行采样时,我得到了一些非常大和/或非常小的步行值。
我在下面放置了一个示例代码,它生成了一些很好划分的种群,然后围绕参数值的初始猜测生成了 MCMC。我不确定为什么 MCMC 链在这里不能很好地收敛到适当的值,所以任何帮助都将不胜感激。
以下代码应该开箱即用。
import emcee
import numpy as np
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
#generate some test x and y data
folded_xy_train = np.random.uniform(0,1,10000) #test x data
folded_z_train = np.random.uniform(0,1,10000) #test y data
#define the true gradient and offset for the boundary line
m_true, b_true = 5,-2.5
#generate labels for the test data
rounded_labels_train = np.ones(len(folded_z_train))
model = (m_true*folded_xy_train) + b_true
difference = model - folded_z_train
rounded_labels_train[difference<0] = 0
#show the test data
plt.figure()
plt.scatter(folded_xy_train,folded_z_train,c=rounded_labels_train,s=1.0)
#define a likelihood function for the boundary line
def lnlike(theta, x, y, labels):
m, b = theta
model = (m*x) + b
difference = model - y
classifications = np.ones(len(y))
classifications[difference<0]=0
cfm = confusion_matrix(labels,classifications)
cm = cfm.astype('float') / cfm.sum(axis=1)[:, np.newaxis]
tn, fp, fn, tp = cm.ravel()
likelihood_val = (0.5*(tp+tn))/(1+np.abs(tp-tn))
ln_like = -np.log(likelihood_val)
return ln_like
#define a wide flat prior
def lnprior(theta):
m, b, = theta
if 0 < m < 10 and -20 < b < 5:
return 0.0
return -np.inf
#define the posterior
def lnprob(p, x, y, labels):
lp = lnprior(p)
if not np.isfinite(lp):
return 0
return lp + lnlike(p, x, y, labels)
#setup the MCMC sampling
nwalkers = 4
ndim = 2
p0 = np.array([4.2,-2]) + [np.random.rand(ndim) for i in range(nwalkers)]
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=(folded_xy_train, folded_z_train, rounded_labels_train))
sampler.run_mcmc(p0, 500)
#extract the MCMC paramater value chains
samples = sampler.chain[:, 50:, :].reshape((-1, ndim))
#view the parameter chains
plt.figure()
plt.subplot(211)
plt.plot(samples[:,0])
plt.subplot(212)
plt.plot(samples[:,1])
初始测试数据,显示给定 xy 数据的明显边界线(由二进制类标签着色):
样本行走,显示梯度参数(顶部)和偏移参数(底部)的奇怪采样。x 轴表示 MCMC 步行步数,y 轴表示给定步的 MCMC 参数值: