class_docstr: Class for sampling methods specific to Bayesian Models
  
  Parameters
  ----------
  model: instance of BayesianNetwork
      model on which inference queries will be computed
class_docstr_tok: ["Class","for","sampling","methods","specific","to","Bayesian","Models","Parameters","--","--","--","--","--","model",":","instance","of","BayesianNetwork","model","on","which","inference","queries","will","be","computed"]
docstr: Generates sample(s) from joint distribution of the Bayesian Network,
  given the evidence.
  
  Parameters
  ----------
  evidence: list of `pgmpy.factor.State` namedtuples
      None if no evidence
  
  size: int
      size of sample to be generated
  
  include_latents: boolean
      Whether to include the latent variable values in the generated samples.
  
  seed: int (default: None)
      If a value is provided, sets the seed for numpy.random.
  
  show_progress: boolean
      Whether to show a progress bar of samples getting generated.
  
  partial_samples: pandas.DataFrame
      A pandas dataframe specifying samples on some of the variables in the model. If
      specified, the sampling procedure uses these sample values, instead of generating them.
  
  Returns
  -------
  sampled: pandas.DataFrame
      The generated samples
  
  Examples
  --------
  >>> from pgmpy.models import BayesianNetwork
  >>> from pgmpy.factors.discrete import TabularCPD
  >>> from pgmpy.factors.discrete import State
  >>> from pgmpy.sampling import BayesianModelSampling
  >>> student = BayesianNetwork([('diff', 'grade'), ('intel', 'grade')])
  >>> cpd_d = TabularCPD('diff', 2, [[0.6], [0.4]])
  >>> cpd_i = TabularCPD('intel', 2, [[0.7], [0.3]])
  >>> cpd_g = TabularCPD('grade', 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,
  ...                0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],
  ...                ['intel', 'diff'], [2, 2])
  >>> student.add_cpds(cpd_d, cpd_i, cpd_g)
  >>> inference = BayesianModelSampling(student)
  >>> evidence = [State(var='diff', state=0)]
  >>> inference.rejection_sample(evidence=evidence, size=2, return_type='dataframe')
          intel       diff       grade
  0         0          0          1
  1         0          0          1
docstr_tok: ["Generates","sample","(","s",")","from","joint","distribution","of","the","Bayesian","Network",",","given","the","evidence",".","Parameters","--","--","--","--","--","evidence",":","list","of","`","pgmpy.factor.State","`","namedtuples","None","if","no","evidence","size",":","int","size","of","sample","to","be","generated","include_latents",":","boolean","Whether","to","include","the","latent","variable","values","in","the","generated","samples",".","seed",":","int","(","default",":","None",")","If","a","value","is","provided",",","sets","the","seed","for","numpy.random",".","show_progress",":","boolean","Whether","to","show","a","progress","bar","of","samples","getting","generated",".","partial_samples",":","pandas.DataFrame","A","pandas","dataframe","specifying","samples","on","some","of","the","variables","in","the","model",".","If","specified",",","the","sampling","procedure","uses","these","sample","values",",","instead","of","generating","them",".","Returns","--","--","--","-","sampled",":","pandas.DataFrame","The","generated","samples","Examples","--","--","--","--",">",">",">","from","pgmpy.models","import","BayesianNetwork",">",">",">","from","pgmpy.factors.discrete","import","TabularCPD",">",">",">","from","pgmpy.factors.discrete","import","State",">",">",">","from","pgmpy.sampling","import","BayesianModelSampling",">",">",">","student","=","BayesianNetwork","(","[","(","'diff","'",",","'grade","'",")",",","(","'intel","'",",","'grade","'",")","]",")",">",">",">","cpd_d","=","TabularCPD","(","'diff","'",",","2",",","[","[","0.6","]",",","[","0.4","]","]",")",">",">",">","cpd_i","=","TabularCPD","(","'intel","'",",","2",",","[","[","0.7","]",",","[","0.3","]","]",")",">",">",">","cpd_g","=","TabularCPD","(","'grade","'",",","3",",","[","[","0.3",",","0.05",",","0.9",",","0.5","]",",","[","0.4",",","0.25",",","...","0.08",",","0.3","]",",","[","0.3",",","0.7",",","0.02",",","0.2","]","]",",","...","[","'intel","'",",","'diff","'","]",",","[","2",",","2","]",")",">",">",">","student.add_cpds","(","cpd_d",",","cpd_i",",","cpd_g",")",">",">",">","inference","=","BayesianModelSampling","(","student",")",">",">",">","evidence","=","[","State","(","var='diff","'",",","state=0",")","]",">",">",">","inference.rejection_sample","(","evidence=evidence",",","size=2",",","return_type='dataframe","'",")","intel","diff","grade","0","0","0","1","1","0","0","1"]
code: def rejection_sample(
      self,
      evidence=[],
      size=1,
      include_latents=False,
      seed=None,
      show_progress=True,
      partial_samples=None,
  ):
      """
      Generates sample(s) from joint distribution of the Bayesian Network,
      given the evidence.
  
      Parameters
      ----------
      evidence: list of `pgmpy.factor.State` namedtuples
          None if no evidence
  
      size: int
          size of sample to be generated
  
      include_latents: boolean
          Whether to include the latent variable values in the generated samples.
  
      seed: int (default: None)
          If a value is provided, sets the seed for numpy.random.
  
      show_progress: boolean
          Whether to show a progress bar of samples getting generated.
  
      partial_samples: pandas.DataFrame
          A pandas dataframe specifying samples on some of the variables in the model. If
          specified, the sampling procedure uses these sample values, instead of generating them.
  
      Returns
      -------
      sampled: pandas.DataFrame
          The generated samples
  
      Examples
      --------
      >>> from pgmpy.models import BayesianNetwork
      >>> from pgmpy.factors.discrete import TabularCPD
      >>> from pgmpy.factors.discrete import State
      >>> from pgmpy.sampling import BayesianModelSampling
      >>> student = BayesianNetwork([('diff', 'grade'), ('intel', 'grade')])
      >>> cpd_d = TabularCPD('diff', 2, [[0.6], [0.4]])
      >>> cpd_i = TabularCPD('intel', 2, [[0.7], [0.3]])
      >>> cpd_g = TabularCPD('grade', 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,
      ...                0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],
      ...                ['intel', 'diff'], [2, 2])
      >>> student.add_cpds(cpd_d, cpd_i, cpd_g)
      >>> inference = BayesianModelSampling(student)
      >>> evidence = [State(var='diff', state=0)]
      >>> inference.rejection_sample(evidence=evidence, size=2, return_type='dataframe')
              intel       diff       grade
      0         0          0          1
      1         0          0          1
      """
  
      if seed is not None:
          np.random.seed(seed)
  
      # If no evidence is given, it is equivalent to forward sampling.
      if len(evidence) == 0:
          return self.forward_sample(
              size=size, include_latents=include_latents
          )
  
      # Setup array to be returned
      sampled = pd.DataFrame()
      prob = 1
      i = 0
  
      # Do the sampling by generating samples from forward sampling and rejecting the
      # samples which do not match our evidence. Keep doing until we have enough
      # samples.
      if show_progress and config.SHOW_PROGRESS:
          pbar = tqdm(total=size)
  
      while i < size:
          _size = int(((size - i) / prob) * 1.5)
  
          # If partial_samples is specified, can only generate < partial_samples.shape[0] number of samples
          # at a time. For simplicity, just generate the same size as partial_samples.shape[0].
          if partial_samples is not None:
              _size = partial_samples.shape[0]
  
          _sampled = self.forward_sample(
              size=_size,
              include_latents=True,
              show_progress=False,
              partial_samples=partial_samples,
          )
  
          for var, state in evidence:
              _sampled = _sampled[_sampled[var] == state]
  
          prob = max(len(_sampled) / _size, 0.01)
          sampled = pd.concat(
              [sampled, _sampled], axis=0, join="outer"
          ).iloc[:size, :]
          i += _sampled.shape[0]
  
          if show_progress and config.SHOW_PROGRESS:
              # Update at maximum to `size`
              comp = (
                  _sampled.shape[0]
                  if i < size
                  else size - (i - _sampled.shape[0])
              )
              pbar.update(comp)
  
      if show_progress and config.SHOW_PROGRESS:
          pbar.close()
  
      sampled = sampled.reset_index(drop=True)
      if not include_latents:
          sampled.drop(self.model.latents, axis=1, inplace=True)
      return sampled
  
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 3 for calling the BayesianModelSampling obj's underlying member method code with example usage: obj.rejection_sample(evidence, size, include_latents, seed, show_progress, partial_samples) and returns: sampled, self