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

This module implements the PartitionMCMC class, which is a specialized version of the MCMC algorithm for Bayesian network structure learning.
 
The PartitionMCMC class provides functionality for sampling Directed Acyclic Graphs (DAGs) from partitions, scoring partitions, and performing MCMC iterations to explore the space of possible network structures. It supports various configurations, including custom scoring methods, proposal objects, and search spaces.
 
Key Features:
- Initialization with custom data, initial states, and scoring objects.
- Sampling DAGs from partitions using categorical distributions.
- Scoring partitions based on parent sets and constraints.
- Performing MCMC iterations with acceptance criteria.
- Configurable search spaces, blacklists, whitelists, and neighborhood settings.
 
Dependencies:
- numpy
- pandas
- networkx
- torch
 
Classes:
PartitionMCMC: Implements the Partition MCMC algorithm.

 
Modules
       
numpy
networkx
pandas
random
time
torch

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

 
class PartitionMCMC(structure_learning.approximators.mcmc.MCMC)
    PartitionMCMC(data: pandas.core.frame.DataFrame = None, initial_state: Union[structure_learning.data_structures.partition.OrderedPartition, numpy.ndarray] = None, max_iter: int = 30000, proposal_object: structure_learning.proposals.proposal.StructureLearningProposal = None, score_object: Union[str, structure_learning.scores.score.Score] = None, blacklist=None, whitelist=None, searchspace=None, plus1: bool = False, seed: int = None, pc_significance_level=0.01, pc_ci_test='pearsonr', result_type='iterations', graph_type='dag', concise=True, burn_in: float = 0.1)
 
Implementation of Partition MCMC.
 
 
Method resolution order:
PartitionMCMC
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: Union[structure_learning.data_structures.partition.OrderedPartition, numpy.ndarray] = None, max_iter: int = 30000, proposal_object: structure_learning.proposals.proposal.StructureLearningProposal = None, score_object: Union[str, structure_learning.scores.score.Score] = None, blacklist=None, whitelist=None, searchspace=None, plus1: bool = False, seed: int = None, pc_significance_level=0.01, pc_ci_test='pearsonr', result_type='iterations', graph_type='dag', concise=True, burn_in: float = 0.1)
Initilialise Partition MCMC instance.
 
Parameters:
    data (pd.DataFrame):                                    Dataset. Optional if score_object is given.
    initial_state (numpy.ndarray | OrderedPartition):       Initial graph/partition 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.
    blacklist (numpy.ndarray):                              Mask for edges to ignore in the proposal
    whitelist (numpy.ndarray):                              Mask for edges to include in the proposal
    searchspace (str | numpy.ndarray):                      Graph search space. "FULL" | "PC" | np.ndarray | None. If none, full search space is used.
    plus1 (bool):                                           Use plus1 neighborhood
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