class_docstr: Class to represent a Markov Chain with multiple kernels for factored state space,
  along with methods to simulate a run.
  
  Examples
  --------
  
  Create an empty Markov Chain:
  
  >>> from pgmpy.models import MarkovChain as MC
  >>> model = MC()
  
  And then add variables to it
  
  >>> model.add_variables_from(['intel', 'diff'], [2, 3])
  
  Or directly create a Markov Chain from a list of variables and their cardinalities
  
  >>> model = MC(['intel', 'diff'], [2, 3])
  
  Add transition models
  
  >>> intel_tm = {0: {0: 0.25, 1: 0.75}, 1: {0: 0.5, 1: 0.5}}
  >>> model.add_transition_model('intel', intel_tm)
  >>> diff_tm = {0: {0: 0.1, 1: 0.5, 2: 0.4}, 1: {0: 0.2, 1: 0.2, 2: 0.6 }, 2: {0: 0.7, 1: 0.15, 2: 0.15}}
  >>> model.add_transition_model('diff', diff_tm)
  
  Set a start state
  
  >>> from pgmpy.factors.discrete import State
  >>> model.set_start_state([State('intel', 0), State('diff', 2)])
  
  Sample from it
  
  >>> model.sample(size=5)
     intel  diff
  0      0     2
  1      1     0
  2      0     1
  3      1     0
  4      0     2
class_docstr_tok: ["Class","to","represent","a","Markov","Chain","with","multiple","kernels","for","factored","state","space",",","along","with","methods","to","simulate","a","run",".","Examples","--","--","--","--","Create","an","empty","Markov","Chain",":",">",">",">","from","pgmpy.models","import","MarkovChain","as","MC",">",">",">","model","=","MC","(",")","And","then","add","variables","to","it",">",">",">","model.add_variables_from","(","[","'intel","'",",","'diff","'","]",",","[","2",",","3","]",")","Or","directly","create","a","Markov","Chain","from","a","list","of","variables","and","their","cardinalities",">",">",">","model","=","MC","(","[","'intel","'",",","'diff","'","]",",","[","2",",","3","]",")","Add","transition","models",">",">",">","intel_tm","=","{","0",":","{","0",":","0.25",",","1",":","0.75","}",",","1",":","{","0",":","0.5",",","1",":","0.5","}","}",">",">",">","model.add_transition_model","(","'intel","'",",","intel_tm",")",">",">",">","diff_tm","=","{","0",":","{","0",":","0.1",",","1",":","0.5",",","2",":","0.4","}",",","1",":","{","0",":","0.2",",","1",":","0.2",",","2",":","0.6","}",",","2",":","{","0",":","0.7",",","1",":","0.15",",","2",":","0.15","}","}",">",">",">","model.add_transition_model","(","'diff","'",",","diff_tm",")","Set","a","start","state",">",">",">","from","pgmpy.factors.discrete","import","State",">",">",">","model.set_start_state","(","[","State","(","'intel","'",",","0",")",",","State","(","'diff","'",",","2",")","]",")","Sample","from","it",">",">",">","model.sample","(","size=5",")","intel","diff","0","0","2","1","1","0","2","0","1","3","1","0","4","0","2"]
docstr: Adds a transition model for a particular variable.
  
  Parameters
  ----------
  variable: any hashable python object
      must be an existing variable of the model.
  
  transition_model: dict or 2d array
      dict representing valid transition probabilities defined for every possible state of the variable.
      array represent a square matrix where every row sums to 1,
      array[i,j] indicates the transition probalities from State i to State j
  
  Examples
  --------
  >>> from pgmpy.models import MarkovChain as MC
  >>> model = MC()
  >>> model.add_variable('grade', 3)
  >>> grade_tm = {0: {0: 0.1, 1: 0.5, 2: 0.4}, 1: {0: 0.2, 1: 0.2, 2: 0.6 }, 2: {0: 0.7, 1: 0.15, 2: 0.15}}
  >>> grade_tm_matrix = np.array([[0.1, 0.5, 0.4], [0.2, 0.2, 0.6], [0.7, 0.15, 0.15]])
  >>> model.add_transition_model('grade', grade_tm)
  >>> model.add_transition_model('grade', grade_tm_matrix)
docstr_tok: ["Adds","a","transition","model","for","a","particular","variable",".","Parameters","--","--","--","--","--","variable",":","any","hashable","python","object","must","be","an","existing","variable","of","the","model",".","transition_model",":","dict","or","2d","array","dict","representing","valid","transition","probabilities","defined","for","every","possible","state","of","the","variable",".","array","represent","a","square","matrix","where","every","row","sums","to","1",",","array","[","i",",","j","]","indicates","the","transition","probalities","from","State","i","to","State","j","Examples","--","--","--","--",">",">",">","from","pgmpy.models","import","MarkovChain","as","MC",">",">",">","model","=","MC","(",")",">",">",">","model.add_variable","(","'grade","'",",","3",")",">",">",">","grade_tm","=","{","0",":","{","0",":","0.1",",","1",":","0.5",",","2",":","0.4","}",",","1",":","{","0",":","0.2",",","1",":","0.2",",","2",":","0.6","}",",","2",":","{","0",":","0.7",",","1",":","0.15",",","2",":","0.15","}","}",">",">",">","grade_tm_matrix","=","np.array","(","[","[","0.1",",","0.5",",","0.4","]",",","[","0.2",",","0.2",",","0.6","]",",","[","0.7",",","0.15",",","0.15","]","]",")",">",">",">","model.add_transition_model","(","'grade","'",",","grade_tm",")",">",">",">","model.add_transition_model","(","'grade","'",",","grade_tm_matrix",")"]
code: def add_transition_model(self, variable, transition_model):
      """
      Adds a transition model for a particular variable.
  
      Parameters
      ----------
      variable: any hashable python object
          must be an existing variable of the model.
  
      transition_model: dict or 2d array
          dict representing valid transition probabilities defined for every possible state of the variable.
          array represent a square matrix where every row sums to 1,
          array[i,j] indicates the transition probalities from State i to State j
  
      Examples
      --------
      >>> from pgmpy.models import MarkovChain as MC
      >>> model = MC()
      >>> model.add_variable('grade', 3)
      >>> grade_tm = {0: {0: 0.1, 1: 0.5, 2: 0.4}, 1: {0: 0.2, 1: 0.2, 2: 0.6 }, 2: {0: 0.7, 1: 0.15, 2: 0.15}}
      >>> grade_tm_matrix = np.array([[0.1, 0.5, 0.4], [0.2, 0.2, 0.6], [0.7, 0.15, 0.15]])
      >>> model.add_transition_model('grade', grade_tm)
      >>> model.add_transition_model('grade', grade_tm_matrix)
      """
      if isinstance(transition_model, list):
          transition_model = np.array(transition_model, dtype=float)
  
      # check if the transition model is valid
      if not isinstance(transition_model, dict):
          if not isinstance(transition_model, np.ndarray):
              raise ValueError(
                  "Transition model must be a dict or numpy array"
              )
          elif len(transition_model.shape) != 2:
              raise ValueError(
                  f"Transition model must be 2d array.given {transition_model.shape}"
              )
          elif transition_model.shape[0] != transition_model.shape[1]:
              raise ValueError(
                  f"Dimension mismatch {transition_model.shape[0]}!={transition_model.shape[1]}"
              )
          else:
              # convert the matrix to dict
              size = transition_model.shape[0]
              transition_model = dict(
                  (
                      i,
                      dict(
                          (j, float(transition_model[i][j]))
                          for j in range(0, size)
                      ),
                  )
                  for i in range(0, size)
              )
  
      exp_states = set(range(self.cardinalities[variable]))
      tm_states = set(transition_model.keys())
      if not exp_states == tm_states:
          raise ValueError(
              f"Transitions must be defined for all states of variable {variable}. Expected states: {exp_states}, Got: {tm_states}."
          )
  
      for _, transition in transition_model.items():
          if not isinstance(transition, dict):
              raise ValueError("Each transition must be a dict.")
          prob_sum = 0
  
          for _, prob in transition.items():
              if prob < 0 or prob > 1:
                  raise ValueError(
                      "Transitions must represent valid probability weights."
                  )
              prob_sum += prob
  
          if not np.allclose(prob_sum, 1):
              raise ValueError(
                  "Transition probabilities must sum to 1."
              )
  
      self.transition_models[variable] = transition_model
  
imports: from collections import defaultdict
  import numpy
  from pandas import DataFrame
  from scipy.linalg import eig
  from pgmpy.factors.discrete import State
  from pgmpy.global_vars import logger
  from pgmpy.utils import sample_discrete
label_desc: Use image node_id 6 for calling the MarkovChain obj's underlying member method code with example usage: obj.add_transition_model(variable, transition_model) without return types