GibbsSampling#
- class pgmpy.sampling.GibbsSampling(model=None)[source]#
Bases:
MarkovChainClass for performing Gibbs sampling.
- Parameters:
- model: DiscreteBayesianNetwork or DiscreteMarkovNetwork
Model from which variables are inherited and transition probabilities computed.
Examples
Initialization from a DiscreteBayesianNetwork object:
>>> from pgmpy.factors.discrete import TabularCPD >>> from pgmpy.models import DiscreteBayesianNetwork >>> intel_cpd = TabularCPD("intel", 2, [[0.7], [0.3]]) >>> sat_cpd = TabularCPD( ... "sat", 2, [[0.95, 0.2], [0.05, 0.8]], evidence=["intel"], evidence_card=[2] ... ) >>> student = DiscreteBayesianNetwork() >>> student.add_nodes_from(["intel", "sat"]) >>> student.add_edge("intel", "sat") >>> student.add_cpds(intel_cpd, sat_cpd) >>> from pgmpy.sampling import GibbsSampling >>> gibbs_chain = GibbsSampling(student) >>> gibbs_chain.sample(size=3) intel sat 0 0 0 1 0 0 2 1 1
- generate_sample(start_state=None, size=1, include_latents=False, seed=None)[source]#
Generator version of self.sample
- Returns:
- List of State namedtuples, representing the assignment to all variables of the model.
Examples
>>> from pgmpy.factors.discrete import DiscreteFactor >>> from pgmpy.sampling import GibbsSampling >>> from pgmpy.models import DiscreteMarkovNetwork >>> model = DiscreteMarkovNetwork([("A", "B"), ("C", "B")]) >>> factor_ab = DiscreteFactor(["A", "B"], [2, 2], [1, 2, 3, 4]) >>> factor_cb = DiscreteFactor(["C", "B"], [2, 2], [5, 6, 7, 8]) >>> model.add_factors(factor_ab, factor_cb) >>> gibbs = GibbsSampling(model) >>> gen = gibbs.generate_sample(size=2) >>> [sample for sample in gen] [[State(var='C', state=1), State(var='B', state=1), State(var='A', state=0)], [State(var='C', state=0), State(var='B', state=1), State(var='A', state=1)]]
- sample(start_state=None, size=1, seed=None, include_latents=False)[source]#
Sample from the Markov Chain.
- Parameters:
- start_state: dict or array-like iterable
Representing the starting states of the variables. If None is passed, a random start_state is chosen.
- size: int
Number of samples to be generated.
- seed: int (default: None)
If a value is provided, sets the seed for numpy.random.
- include_latents: boolean
Whether to include the latent variable values in the generated samples.
- Returns:
- sampled: pandas.DataFrame
The generated samples
Examples
>>> from pgmpy.factors.discrete import DiscreteFactor >>> from pgmpy.sampling import GibbsSampling >>> from pgmpy.models import DiscreteMarkovNetwork >>> model = DiscreteMarkovNetwork([("A", "B"), ("C", "B")]) >>> factor_ab = DiscreteFactor(["A", "B"], [2, 2], [1, 2, 3, 4]) >>> factor_cb = DiscreteFactor(["C", "B"], [2, 2], [5, 6, 7, 8]) >>> model.add_factors(factor_ab, factor_cb) >>> gibbs = GibbsSampling(model) >>> gibbs.sample(size=4, return_tupe="dataframe") A B C 0 0 1 1 1 1 0 0 2 1 1 0 3 1 1 1