我可以找到如何在Python中使用MCMC估计这种类型的IRT贝叶斯模型的最佳示例是example。下面是我运行的代码的可重现版本。我的理解是,为了识别模型,能力参数theta
被约束为正态分布,均值为0,标准差为1,我认为这是在下面的代码中用这一行完成的:
# theta (proficiency params) are sampled from a normal distribution
theta = Normal("theta", mu=0, tau=1, value=theta_initial, observed= generating)
然而,当我运行代码时,theta的后验均值类似于1.85e18,标准偏差更大,即不是零和标准偏差1.为什么我会收到此错误,如何确保在每次迭代后,theta被归一化为0,sd 1?
#from pylab import * #Pylab will not install with pip so I just loaded numpy itself
from numpy import *
import numpy
from pymc import *
from pymc.Matplot import plot as mplot
import numpy as np
numquestions = 300 # number of test items being simulated
numpeople = 10 # number of participants
numthetas = 1 # number of latent proficiency variables
generating = 0
theta_initial = zeros((numthetas, numpeople))
correctness = np.random.randint(2, size= numquestions * numpeople) == 1 #Produces Error
#correctness = np.random.randint(2, size= numquestions * numpeople) == -1 #all False code runs fine
#correctness = np.random.randint(2, size= numquestions * numpeople) != -1 #all True code throws error message
correctness.shape = (numquestions, numpeople)
# theta (proficiency params) are sampled from a normal distribution
theta = Normal("theta", mu=0, tau=1, value=theta_initial, observed= generating)
# question-parameters (IRT params) are sampled from normal distributions (though others were tried)
a = Normal("a", mu=1, tau=1, value=[[0.0] * numthetas] * numquestions)
# a = Exponential("a", beta=0.01, value=[[0.0] * numthetas] * numquestions)
b = Normal("b", mu=0, tau=1, value=[0.0] * numquestions)
# take vectors theta/a/b, return a vector of probabilities of each person getting each question correct
@deterministic
def sigmoid(theta=theta, a=a, b=b):
bs = repeat(reshape(b, (len(b), 1)), numpeople, 1)
return np.exp(1.0 / (1.0 + np.exp(bs - dot(a, theta))))
# take the probabilities coming out of the sigmoid, and flip weighted coins
correct = Bernoulli('correct', p=sigmoid, value=correctness, observed=not generating)
# create a pymc simulation object, including all the above variables
m = MCMC([a,b,theta,sigmoid,correct])
# run an interactive MCMC sampling session
m.isample(iter=20000, burn=15000)
mydict = m.stats()
print(mydict['theta']['mean']) #Get ability parameters for each student
print(mydict['theta']['mean'].mean()) #Should be zero, but returns something link 1.85e18, i.e. an absurdly large value.
答案 0 :(得分:1)
我认为你的sigmoid函数有额外的n.exp
。根据{{3}},S(t) = 1 / (1 + exp(-t))
。我用这个替代版本替换了你的第34行:
return 1.0 / (1.0 + np.exp(bs - dot(a, theta)))
有了这个,我得到了0.08的theta的平均值。