PyMC3:洗牌输入D

2024-09-28 05:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要洗牌一个向量(每次采样)作为对数似然函数的输入。有没有办法在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

Tags: oftheselflogthatisourfunction

热门问题