Q1)所有随机参数的含义都记录在这里。确定性的论点是相同的,加上此处记录的其他论点。
Q2) 行为上的区别在于 PyMC 内部有一些魔法,它实际执行switchpoint.logp
函数并将其转换为 Python property
,而switchpoint.random
没有得到这种处理,并保留为函数。
如果您对实际发生的事情感到好奇,这里有一些相关的来源:
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.")
那里还有一些其他的东西,比如在logp
函数中进入一个叫做 a 的东西LazyFunction
,但这是基本的想法。
Q3)stochastic
装饰器有一些(更多)魔法,它使用代码自省来确定是否定义了内部函数random
和logp
子函数switchpoint
。如果是,它使用logp
子函数来计算logp
,如果不是,它只使用switchpoint
它自己。源代码在这里:
# 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__
再一次,还有更多的事情发生,而且相当复杂,但这是基本的想法。