我需要对向量(每次采样)进行混洗,以作为对数似然函数的输入传递。有没有办法在PyMC3中而不是在函数本身中做到这一点?我希望采样能够在内部调整为随机播放。
# define a theano Op for our likelihood function
class LogLike_ThetaFromData(tt.Op):
"""
Specify what type of object will be passed and returned to the Op when it is
called. In our case we will be passing it a vector of values (the parameters
that define our model) and returning a single "scalar" value (the
log-likelihood)
"""
itypes = [tt.dscalar] # expects a vector of parameter values when called
# This is the parameter that is going to be found
# therefore in this case, it is the alpha
otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)
def __init__(self, loglike, vectorToBeShuffled):
"""
Initialise the Op with various things that our log-likelihood function
requires. Below are the things that are needed in this particular
example.
Parameters
----------
loglike:
The log-likelihood (or whatever) function we've defined
vectorToBeShuffled:
The "observed" data that our log-likelihood function takes in
theta:
The dependent variable (aka 'theta') that our model requires
"""
# add inputs as class attributes
self.likelihood = loglike
self.vectorToBeShuffled = vectorToBeShuffled # Constants to be sent
def perform(self, node, inputs, outputs):
# the method that is used when calling the Op
theta, = inputs # this will contain my variables
# call the log-likelihood function
# self.observedData is the constant == data
logl =\
self.likelihood(theta, self.vectorToBeShuffled)
outputs[0][0] = np.array(logl) # output the log-likelihood