Module Index : Topics : Keywords
 
 
structure_learning.approximators.structure_mcmc
index
/Users/165421/Documents/code/structure_learning/src/structure_learning/approximators/structure_mcmc.py

This module implements the StructureMCMC class, which is a specialized Markov Chain Monte Carlo (MCMC) sampler for structure learning in graphical models.
 
The StructureMCMC class extends the base MCMC class and provides functionality for sampling graph structures based on a given dataset, scoring function, and proposal mechanism. It supports initialization with a predefined graph or a graph generated using the PC algorithm, and allows for the inclusion of edge constraints through blacklists and whitelists.
 
Classes:
    StructureMCMC: A class for performing Structure MCMC simulations.
 
Dependencies:
    - numpy
    - pandas
    - structure_learning.proposals
    - structure_learning.data_structures
    - structure_learning.scores
    - structure_learning.samplers

 
Modules
       
numpy
pandas
time

 
Classes
       
structure_learning.approximators.mcmc.MCMC(structure_learning.approximators.approximator.Approximator)
StructureMCMC

 
class StructureMCMC(structure_learning.approximators.mcmc.MCMC)
    StructureMCMC(data: pandas.core.frame.DataFrame = None, initial_state: numpy.ndarray = None, max_iter: int = 30000, score_object: Union[str, structure_learning.scores.score.Score] = None, proposal_object: structure_learning.proposals.proposal.StructureLearningProposal = None, prior: structure_learning.priors.prior.Prior = None, pc_init=True, pc_significance_level=0.01, pc_ci_test='pearsonr', blacklist: numpy.ndarray = None, whitelist: numpy.ndarray = None, seed: int = None, sparse=True, result_type: str = 'distribution', graph_type='dag', burn_in: float = 0.1)
 
Implementation of Structure MCMC.
 
 
Method resolution order:
StructureMCMC
structure_learning.approximators.mcmc.MCMC
structure_learning.approximators.approximator.Approximator
abc.ABC
builtins.object

Methods defined here:
__init__(self, data: pandas.core.frame.DataFrame = None, initial_state: numpy.ndarray = None, max_iter: int = 30000, score_object: Union[str, structure_learning.scores.score.Score] = None, proposal_object: structure_learning.proposals.proposal.StructureLearningProposal = None, prior: structure_learning.priors.prior.Prior = None, pc_init=True, pc_significance_level=0.01, pc_ci_test='pearsonr', blacklist: numpy.ndarray = None, whitelist: numpy.ndarray = None, seed: int = None, sparse=True, result_type: str = 'distribution', graph_type='dag', burn_in: float = 0.1)
Initilialise Structure MCMC instance.
 
Parameters:
    data (pd.DataFrame):                            Dataset. Optional if score_object is given.
    initial_state (numpy.ndarray | DAG | None):           Initial graph for the MCMC simulation.
                                                    If None, simulation starts with a random graph or a graph
                                                    constructed from PC algorithm.
    max_iter (int):                                 The number of MCMC iterations to run. Default: 30000.
    score_object (Score):                           A score object implementing compute(). If None, BGeScore is used
                                                    (data must be provided). Default: None.
    proposal_object (StructureLearningProposal):    A proposal object. If None, a GraphProposal instance is used.
                                                    Default: None.
    pc_init (bool):                                 If True and initial_graph is not given, PC algorithm will be used
                                                    to generate initial graph.
    blacklist (numpy.ndarray):                      Mask for edges to ignore in the proposal
    whitelist (numpy.ndarray):                      Mask for edges to include in the proposal
step(self)
Perform one MCMC iteration
 
Returns:
    (dict): information on one MCMC iteration

Data and other attributes defined here:
__abstractmethods__ = frozenset()

Methods inherited from structure_learning.approximators.mcmc.MCMC:
__str__(self)
Return str(self).
config(self)
Get the configuration of the MCMC instance.
 
Returns:
    dict: Configuration dictionary.
get_chain_info(self, results, key='graph')
Extract chain information from the MCMC results.
 
Parameters:
    results (dict): Results of the MCMC simulation.
    key (str): Key to extract information for. Default is 'graph'.
 
Returns:
    list: Chain information.
get_graphs(self, results)
Retrieve a list of sampled graphs from the MCMC results.
 
Parameters:
    results (dict): Results of the MCMC simulation.
 
Returns:
    list: Sampled graphs.
run(self, intervals=-1) -> Tuple[dict, float]
Execute the MCMC simulation.
 
Returns:
    Tuple[dict, float]: Results of the simulation and acceptance ratio.
to_cpdag_distribution(self)
to_distribution(self)
Convert the MCMC results to a distribution.
 
Returns:
    MCMCDistribution: Distribution object.
to_opad(self, plus=False)
Convert the MCMC results to an OPAD object.
 
Parameters:
    plus (bool): Whether to use the plus1 neighborhood. Default is False.
 
Returns:
    OPAD: OPAD object.
traceplot(self, ax=None)
Generate a trace plot of the MCMC simulation.
 
Parameters:
    ax (matplotlib.axes.Axes): Matplotlib axis to plot on. Default is None.
 
Returns:
    list: Plot object.
update_results(self, iteration, info)
Update the results of the MCMC simulation with information from the current iteration.
 
Parameters:
    iteration (int): Current iteration number.
    info (dict): Information about the current iteration.

Readonly properties inherited from structure_learning.approximators.mcmc.MCMC:
trace

Data and other attributes inherited from structure_learning.approximators.mcmc.MCMC:
RESULT_TYPE_DIST = 'distribution'
RESULT_TYPE_ITER = 'iterates'
RESULT_TYPE_OPAD = 'opad'
RESULT_TYPE_OPAD_PLUS = 'opad+'

Methods inherited from structure_learning.approximators.approximator.Approximator:
save(self, filename: str, compression='gzip')
Saves the Graph object to a file.
 
Parameters:
    filename (str): Path to the output file.

Class methods inherited from structure_learning.approximators.approximator.Approximator:
load(filename: str, compression='gzip')
Loads a Graph object from a file.
 
Parameters:
    filename (str): Path to the input file.
 
Returns:
    Graph: Loaded Graph object.

Data descriptors inherited from structure_learning.approximators.approximator.Approximator:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
Data
        Union = typing.Union