docstr: Marginalize the distribution with respect to the given variables.
  
  Parameters
  ----------
  variables: list, array-like
      List of variables to be removed from the marginalized distribution.
  
  inplace: boolean
      If inplace=True it will modify the factor itself, else would return
      a new CustomDistribution instance.
  
  Returns
  -------
  Marginalized distribution or None:
          if inplace=True (default) returns None
          if inplace=False returns a new CustomDistribution instance.
  
  Examples
  --------
  >>> from pgmpy.factors.distributions import CustomDistribution
  >>> from scipy.stats import multivariate_normal
  >>> normal_pdf = lambda x1, x2: multivariate_normal.pdf(
  ...                x=[x1, x2], mean=[0, 0], cov=[[1, 0], [0, 1]])
  >>> normal_dist = CustomDistribution(variables=['x1', 'x2'],
  ...                                  distribution=normal_pdf)
  >>> normal_dist.variables
  ['x1', 'x2']
  >>> normal_dist.assignment(1, 1)
  0.058549831524319168
  >>> normal_dist.marginalize(['x2'])
  >>> normal_dist.variables
  ['x1']
  >>> normal_dist.assignment(1)
  0.24197072451914328
docstr_tok: ["Marginalize","the","distribution","with","respect","to","the","given","variables",".","Parameters","--","--","--","--","--","variables",":","list",",","array-like","List","of","variables","to","be","removed","from","the","marginalized","distribution",".","inplace",":","boolean","If","inplace=True","it","will","modify","the","factor","itself",",","else","would","return","a","new","CustomDistribution","instance",".","Returns","--","--","--","-","Marginalized","distribution","or","None",":","if","inplace=True","(","default",")","returns","None","if","inplace=False","returns","a","new","CustomDistribution","instance",".","Examples","--","--","--","--",">",">",">","from","pgmpy.factors.distributions","import","CustomDistribution",">",">",">","from","scipy.stats","import","multivariate_normal",">",">",">","normal_pdf","=","lambda","x1",",","x2",":","multivariate_normal.pdf","(","...","x=","[","x1",",","x2","]",",","mean=","[","0",",","0","]",",","cov=","[","[","1",",","0","]",",","[","0",",","1","]","]",")",">",">",">","normal_dist","=","CustomDistribution","(","variables=","[","'x1","'",",","'x2","'","]",",","...","distribution=normal_pdf",")",">",">",">","normal_dist.variables","[","'x1","'",",","'x2","'","]",">",">",">","normal_dist.assignment","(","1",",","1",")","0.058549831524319168",">",">",">","normal_dist.marginalize","(","[","'x2","'","]",")",">",">",">","normal_dist.variables","[","'x1","'","]",">",">",">","normal_dist.assignment","(","1",")","0.24197072451914328"]
code: def marginalize(self, variables, inplace=True):
      """
      Marginalize the distribution with respect to the given variables.
  
      Parameters
      ----------
      variables: list, array-like
          List of variables to be removed from the marginalized distribution.
  
      inplace: boolean
          If inplace=True it will modify the factor itself, else would return
          a new CustomDistribution instance.
  
      Returns
      -------
      Marginalized distribution or None:
              if inplace=True (default) returns None
              if inplace=False returns a new CustomDistribution instance.
  
      Examples
      --------
      >>> from pgmpy.factors.distributions import CustomDistribution
      >>> from scipy.stats import multivariate_normal
      >>> normal_pdf = lambda x1, x2: multivariate_normal.pdf(
      ...                x=[x1, x2], mean=[0, 0], cov=[[1, 0], [0, 1]])
      >>> normal_dist = CustomDistribution(variables=['x1', 'x2'],
      ...                                  distribution=normal_pdf)
      >>> normal_dist.variables
      ['x1', 'x2']
      >>> normal_dist.assignment(1, 1)
      0.058549831524319168
      >>> normal_dist.marginalize(['x2'])
      >>> normal_dist.variables
      ['x1']
      >>> normal_dist.assignment(1)
      0.24197072451914328
      """
      if len(variables) == 0:
          raise ValueError(
              "Shouldn't be calling marginalize over no variable."
          )
  
      if not isinstance(variables, (list, tuple, np.ndarray)):
          raise TypeError(
              f"variables: Expected type iterable, got: {type(variables)}"
          )
  
      for var in variables:
          if var not in self.variables:
              raise ValueError(f"{var} not in scope.")
  
      phi = self if inplace else self.copy()
  
      all_var = [var for var in self.variables]
      var_to_keep = [
          var for var in self.variables if var not in variables
      ]
      reordered_var_index = [
          all_var.index(var) for var in variables + var_to_keep
      ]
      pdf = phi._pdf
  
      # The arguments need to be reordered because integrate.nquad
      # integrates the first n-arguments of the function passed.
  
      def reordered_pdf(*args):
          # ordered_args restores the original order as it was in self.variables
          ordered_args = [
              args[reordered_var_index.index(index_id)]
              for index_id in range(len(all_var))
          ]
          return pdf(*ordered_args)
  
      def marginalized_pdf(*args):
          return integrate.nquad(
              reordered_pdf,
              [[-np.inf, np.inf] for i in range(len(variables))],
              args=args,
          )[0]
  
      phi._pdf = marginalized_pdf
      phi.variables = var_to_keep
  
      if not inplace:
          return phi
  
imports: import numpy
  from scipy import integrate
  from pgmpy.factors.distributions import BaseDistribution
label_desc: Use image node_id 10 for calling the CustomDistribution obj's underlying member method code with example usage: obj.marginalize(variables, inplace) and returns: pdf, integrate, phi