python - Decorators in PyMC -


i have 3 question regarding decorators not able find answer :

q1)what arguments decorators in pymc (@deterministic, @stochastic) denote ?

q2)

@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):         numpy.random import random         return np.round( (t_l - t_h) * random() ) + t_l 

1)print switchpoint.logp #prints log-probability expected

2)print switchpoint.random #does not generate random number

3)print switchpoint.random() # generates random number

4)print switchpoint.logp() #error

if 2 did not work , 3 worked 1 should not have worked , instaed 4 should have worked (which opposite of observed). can explain going on ?

q3)

@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) 

here not specified logp still if type switchpoint.logp, piece of code executed ?

q1) meaning of arguments stochastic documented here. arguments deterministic same, plus additional ones documented here.

q2) difference in behavior there magic inside pymc executes switchpoint.logp function , turns python property, while switchpoint.random doesn't treatment, , kept function.

if you're curious what's going on, here's of relevant source:

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 cast float'      if logp != logp:         raise valueerror, self.__name__ + ': computed log-probability nan'      # check if value smaller 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 set.'  logp = property(fget = get_logp, fset=set_logp, doc="self's log-probability value conditional on parents.") 

there's other stuff going on there, during logp function called lazyfunction, that's basic idea.

q3) stochastic decorator has (more) magic in uses code introspection determine if random , logp sub functions defined inside switchpoint. if are, uses logp sub-function compute logp, if not, uses switchpoint itself. source code here:

# gets used stochastic check long-format logp , random: if probe:     # define global tracing function (i assume debugging??)     # no, it's out logp , 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)) k in keys))             sys.settrace(none)         return probefunc      sys.settrace(probefunc)      # functions logp , random (complete interface).     # disable special methods prevent formation of 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()  key in keys:     if not kwds.has_key(key):         kwds[key] = none  key in ['logp', 'eval']:     if key in keys:         if kwds[key] none:             kwds[key] = __func__ 

again, there's more stuff going on, , it's complicated, that's basic idea.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -