Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyMC中的装饰器_Python_Decorator_Python Decorators_Pymc_Mcmc - Fatal编程技术网

Python PyMC中的装饰器

Python PyMC中的装饰器,python,decorator,python-decorators,pymc,mcmc,Python,Decorator,Python Decorators,Pymc,Mcmc,我有三个关于装饰师的问题,我无法找到答案: Q1)PyMC(@Deterministic,@randomic)中装饰器的参数表示什么 问题2) @pymc.randomic(dtype=int) def开关点(值=10,t_l=0,t_h=110): def logp(值、t_l、t_h): 如果值>t_h或值t_h或值

我有三个关于装饰师的问题,我无法找到答案:

Q1)PyMC(@Deterministic,@randomic)中装饰器的参数表示什么

问题2)

@pymc.randomic(dtype=int)
def开关点(值=10,t_l=0,t_h=110):
def logp(值、t_l、t_h):
如果值>t_h或值
1) print switchpoint.logp#按预期打印日志概率

2) print switchpoint.random#不生成随机数

3) 打印开关点。随机()#生成一个随机数

4) 打印开关点.logp()#错误

如果2个不起作用,3个起作用,那么1个不应该起作用,4个应该起作用(这与我观察到的相反)。有人能解释一下发生了什么吗?

问题3)

@pymc.randomic(dtype=int)
def开关点(值=1900,t_l=1851,t_h=1962):
如果值>t_h或值
这里没有指定如果我键入
开关点,它仍然是
logp
。logp
,则执行这段代码?

Q1)记录所有随机参数的含义。确定性的参数是相同的,加上记录的附加参数

问题2)行为上的区别在于PyMC内部有一些魔法,它实际执行
开关点.logp
函数并将其转换为Python,而
开关点.random
没有得到这种处理,而是作为一个函数保留

如果你对实际发生的事情感到好奇,以下是一些相关的信息:

再说一次,还有一些事情正在进行,这相当复杂,但这是基本的想法

@pymc.stochastic(dtype=int)
def switchpoint(value=10, t_l=0, t_h=110):
    def logp(value, t_l, t_h):
        if value > t_h or value < t_l:
            return -np.inf
        else:
            return -np.log(t_h - t_l + 1)
    def random(t_l, t_h):
        from numpy.random import random
        return np.round( (t_l - t_h) * random() ) + t_l
@pymc.stochastic(dtype=int)
def switchpoint(value=1900, t_l=1851, t_h=1962):
    if value > t_h or value < t_l:
        # Invalid values
        return -np.inf
    else:
        # Uniform log-likelihood
        return -np.log(t_h - t_l + 1)
def get_logp(self):
    if self.verbose > 1:
        print '\t' + self.__name__ + ': log-probability accessed.'
    logp = self._logp.get()
    if self.verbose > 1:
        print '\t' + self.__name__ + ': Returning log-probability ', logp

    try:
        logp = float(logp)
    except:
        raise TypeError, self.__name__ + ': computed log-probability ' + str(logp) + ' cannot be cast to float'

    if logp != logp:
        raise ValueError, self.__name__ + ': computed log-probability is NaN'

    # Check if the value is smaller than a double precision infinity:
    if logp <= d_neg_inf:
        if self.verbose > 0:
            raise ZeroProbability, self.errmsg + ": %s" %self._parents.value
        else:
            raise ZeroProbability, self.errmsg

    return logp

def set_logp(self,value):
    raise AttributeError, 'Potential '+self.__name__+'\'s log-probability cannot be set.'

logp = property(fget = get_logp, fset=set_logp, doc="Self's log-probability value conditional on parents.")
# This gets used by stochastic to check for long-format logp and random:
if probe:
    # Define global tracing function (I assume this is for debugging??)
    # No, it's to get out the logp and random functions, if they're in there.
    def probeFunc(frame, event, arg):
        if event == 'return':
            locals = frame.f_locals
            kwds.update(dict((k,locals.get(k)) for k in keys))
            sys.settrace(None)
        return probeFunc

    sys.settrace(probeFunc)

    # Get the functions logp and random (complete interface).
    # Disable special methods to prevent the formation of a hurricane of Deterministics
    cur_status = check_special_methods()
    disable_special_methods()
    try:
        __func__()
    except:
        if 'logp' in keys:
            kwds['logp']=__func__
        else:
            kwds['eval'] =__func__
    # Reenable special methods.
    if cur_status:
        enable_special_methods()

for key in keys:
    if not kwds.has_key(key):
        kwds[key] = None

for key in ['logp', 'eval']:
    if key in keys:
        if kwds[key] is None:
            kwds[key] = __func__