Help on class understanding in pymc code
Peter Otten
__peter__ at web.de
Mon Dec 14 03:56:39 EST 2015
Robert wrote:
> On Sunday, December 13, 2015 at 8:10:25 PM UTC-5, Peter Otten wrote:
>> Robert wrote:
>>
>> > Hi,
>> >
>> > I follow code example at link:
>> >
>> > https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html
>> >
>> >
>> > There is the following code line:
>> >
>> > sampler =
>> > pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true])
>> >
>> >
>> > I want to know the detail of pymc.MCMC, then I get help content of it
>> > with:
>> >
>> > /////////////
>> > help(pymc.MCMC)
>> > Help on class MCMC in module pymc.MCMC:
>> >
>> > class MCMC(pymc.Model.Sampler)
>> > | This class fits probability models using Markov Chain Monte Carlo.
>> > | Each stochastic variable is assigned a StepMethod object, which
>> > | makes it take a single MCMC step conditional on the rest of the
>> > | model. These step methods are called in turn.
>> > |
>> > | >>> A = MCMC(input, db, verbose=0)
>> > |
>> > \\\\\\\\\\\\\\\\\\
>> >
>> >
>> > help('pymc.Model.Sampler')
>> > no Python documentation found for 'pymc.Model.Sampler'
>> >
>> >
>> > help('pymc.Model')
>> > Help on class Model in pymc:
>> >
>> > pymc.Model = class Model(pymc.Container.ObjectContainer)
>> > | The base class for all objects that fit probability models. Model
>> > | is initialized with:
>> > |
>> > | >>> A = Model(input, verbose=0)
>> > |
>> > | :Parameters:
>> > | - input : module, list, tuple, dictionary, set, object or
>> > | nothing.
>> > | Model definition, in terms of Stochastics, Deterministics,
>> > | Potentials and Containers. If nothing, all nodes are
>> > | collected from the base namespace.
>> > |
>> > | Attributes:
>> > | - deterministics
>> > | - stochastics (with observed=False)
>> > | - data (stochastic variables with observed=True)
>> > | - variables
>> > | - potentials
>> > | - containers
>> > | - nodes
>> > | - all_objects
>> > | - status: Not useful for the Model base class, but may be used by
>> > | subclasses.
>> > |
>> > | The following attributes only exist after the appropriate method is
>> > | called:
>> > | - moral_neighbors: The edges of the moralized graph. A
>> > | dictionary, keyed by stochastic variable,
>> > | whose values are sets of stochastic variables. Edges exist
>> > | between the key variable and all variables in the value.
>> > | Created by method _moralize.
>> > | - extended_children: The extended children of self's stochastic
>> > | variables. See the docstring of
>> > | extend_children. This is a dictionary keyed by stochastic
>> > | variable.
>> > | - generations: A list of sets of stochastic variables. The
>> > | members of each element only have parents in
>> > | previous elements. Created by method find_generations.
>> > |
>> > | Methods:
>> > | - sample_model_likelihood(iter): Generate and return iter
>> > | samples of p(data and potentials|model).
>> > | Can be used to generate Bayes' factors.
>> > |
>> > | :SeeAlso: Sampler, MAP, NormalApproximation, weight, Container,
>> > | :graph.
>> > |
>> > | Method resolution order:
>> > | Model
>> > | pymc.Container.ObjectContainer
>> > | pymc.six.NewBase
>> > | pymc.Node.ContainerBase
>> > | __builtin__.object
>> > |
>> > | Methods defined here:
>> > |
>> > | __init__(self, input=None, name=None, verbose=-1)
>> > | Initialize a Model instance.
>> > |
>> > | :Parameters:
>> > | - input : module, list, tuple, dictionary, set, object or
>> > | nothing.
>> > | Model definition, in terms of Stochastics,
>> > | Deterministics, Potentials and Containers. If nothing,
>> > | all nodes are collected from the base namespace.
>> > |
>> > | draw_from_prior(self)
>> > | Sets all variables to random values drawn from joint 'prior',
>> > | meaning contributions of data and potentials to the joint
>> > | distribution are not considered.
>> > |
>> > | get_node(self, node_name)
>> > | Retrieve node with passed name
>> > |
>> > | seed(self)
>> > | Seed new initial values for the stochastics.
>> > |
>> > |
----------------------------------------------------------------------
>> > | Data descriptors defined here:
>> > |
>> > | generations
>> > |
>> > |
----------------------------------------------------------------------
>> > | Data and other attributes defined here:
>> > |
>> > | __slotnames__ = []
>> > |
>> > | register = False
>> > |
>> > |
----------------------------------------------------------------------
>> > | Methods inherited from pymc.Container.ObjectContainer:
>> > |
>> > | replace(self, item, new_container, key)
>> > |
>> > |
----------------------------------------------------------------------
>> > | Data descriptors inherited from pymc.Container.ObjectContainer:
>> > |
>> > | value
>> > | A copy of self, with all variables replaced by their values.
>> > |
>> > |
----------------------------------------------------------------------
>> > | Methods inherited from pymc.Node.ContainerBase:
>> > |
>> > | assimilate(self, new_container)
>> > |
>> > |
----------------------------------------------------------------------
>> > | Data descriptors inherited from pymc.Node.ContainerBase:
>> > |
>> > | __dict__
>> > | dictionary for instance variables (if defined)
>> > |
>> > | __weakref__
>> > | list of weak references to the object (if defined)
>> > |
>> > | logp
>> > | The summed log-probability of all stochastic variables (data
>> > | or otherwise) and factor potentials in self.
>> > |
>> > |
----------------------------------------------------------------------
>> > | Data and other attributes inherited from pymc.Node.ContainerBase:
>> > |
>> > | change_methods = []
>> > |
>> > | containing_classes = []
>> > ---------
>> >
>> >
>> > Now, I have puzzles on the class constructor input parameter:
>> > [alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true]
>> >
>> > 1. 'class MCMC(pymc.Model.Sampler)' says its inheritance is from
>> > 'pymc.Model.Sampler'
>> >
>> > 2. When I try to get help on 'pymc.Model.Sampler', it says:
>> > 'no Python documentation found for 'pymc.Model.Sampler'
>> >
>> > 3. When I continue to catch help on 'pymc.Model.Sampler', I don't see
>> > content mentions 'Sampler'. This complete help message is shown above.
>> >
>> > So, what is 'pymc.Model.Sampler'?
>>
>> Unfortunately there is a module pymc.Model and a class pymc.Model.Model,
>> and in pymc.__init__.py there is code that overwrites the module with the
>> class. Therefore when you write
>>
>> pymc.Model
>>
>> you get
>>
>> pymc.Model.Model
>>
>> as you can see when you type
>>
>> >>> import pymc
>> >>> pymc.Model
>> <class 'pymc.Model.Model'>
>>
>> To get around this bad naming use
>>
>> >>> from pymc.Model import Sampler
>> >>> help(Sampler)
>
> Thanks. Your answer does solve the problem, but I cannot follow your
> words. When you run below code, what is 'pymc.Model'?
>
>
>>>> import pymc
>>>> pymc.Model
> <class 'pymc.Model.Model'>
>
>
> When I run:
import pymc
This imports the pymc package. Technically this is achieved by executing
pymc/__init__.py
In __init__.py there is a line
from Model import *
This line puts all names in pymc.Model.__all__ into the current namespace
and is roughly equivalent to
import Model as _tmp
Model = _tmp.Model
Sampler = _tmp.Sampler
del _tmp
so that after the star import pymc.Model is the class pymc.Model.Model.
> aaa=pymc.Model
Now aaa is (a name for) that class, too.
> type(aaa)
The type() of a class is its "metaclass". The relationship
metaclass --> class
is the same as
class --> instance
i. e. a Python class is an instance of its metaclass like 42 is an instance
of int.
> Out[160]: pymc.Node.ContainerMeta
By default all classes derived from object are of type "type". So that's a
custom metaclass. You probably don't care about that at this time in your
career as a pythonista.
> type(pymc.Model)
> Out[161]: pymc.Node.ContainerMeta
>
> I see that it is not '<class 'pymc.Model.Model'>'.
Because pymc.Model is pymc.Model.Model already. The equivalent for
int would be to type
>>> type(int)
and expecting it to return int.
More information about the Python-list
mailing list