class_docstr: Class for performing Gibbs sampling.
  
  Parameters
  ----------
  model: BayesianNetwork or MarkovNetwork
      Model from which variables are inherited and transition probabilities computed.
  
  Examples
  --------
  Initialization from a BayesianNetwork object:
  
  >>> from pgmpy.factors.discrete import TabularCPD
  >>> from pgmpy.models import BayesianNetwork
  >>> 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 = BayesianNetwork()
  >>> 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
class_docstr_tok: ["Class","for","performing","Gibbs","sampling",".","Parameters","--","--","--","--","--","model",":","BayesianNetwork","or","MarkovNetwork","Model","from","which","variables","are","inherited","and","transition","probabilities","computed",".","Examples","--","--","--","--","Initialization","from","a","BayesianNetwork","object",":",">",">",">","from","pgmpy.factors.discrete","import","TabularCPD",">",">",">","from","pgmpy.models","import","BayesianNetwork",">",">",">","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","=","BayesianNetwork","(",")",">",">",">","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"]
docstr: 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 MarkovNetwork
  >>> model = MarkovNetwork([('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
docstr_tok: ["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","MarkovNetwork",">",">",">","model","=","MarkovNetwork","(","[","(","'","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"]
code: def sample(
      self, start_state=None, size=1, seed=None, include_latents=False
  ):
      """
      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 MarkovNetwork
      >>> model = MarkovNetwork([('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
      """
      if start_state is None and self.state is None:
          self.state = self.random_state()
      elif start_state is not None:
          self.set_start_state(start_state)
  
      if seed is not None:
          np.random.seed(seed)
  
      types = [(str(var_name), "int") for var_name in self.variables]
      sampled = np.zeros(size, dtype=types).view(np.recarray)
      sampled[0] = tuple(st for var, st in self.state)
      for i in tqdm(range(size - 1)):
          for j, (var, st) in enumerate(self.state):
              other_st = tuple(st for v, st in self.state if var != v)
              next_st = sample_discrete(
                  list(range(self.cardinalities[var])),
                  self.transition_models[var][other_st],
              )[0]
              self.state[j] = State(var, next_st)
          sampled[i + 1] = tuple(st for var, st in self.state)
  
      samples_df = _return_samples(sampled)
      if not include_latents:
          samples_df.drop(self.latents, axis=1, inplace=True)
      return samples_df
  
imports: import itertools
  from collections import namedtuple
  import networkx
  import numpy
  import pandas
  import torch
  from joblib import Parallel, delayed
  from tqdm.auto import tqdm
  from pgmpy import config
  from pgmpy.factors import factor_product
  from pgmpy.models import BayesianNetwork, MarkovChain, MarkovNetwork
  from pgmpy.sampling import BayesianModelInference, _return_samples
  from pgmpy.utils.mathext import sample_discrete, sample_discrete_maps
label_desc: Use image node_id 4 for calling the GibbsSampling obj's underlying member method code with example usage: obj.sample(start_state, size, seed, include_latents) and returns: samples_df