class_docstr: Class for constraint-based estimation of DAGs using the PC algorithm
  from a given data set.  Identifies (conditional) dependencies in data
  set using statistical independence tests and estimates a DAG pattern
  that satisfies the identified dependencies. The DAG pattern can then be
  completed to a faithful DAG, if possible.
  
  Parameters
  ----------
  data: pandas DataFrame object
      dataframe object where each column represents one variable.  (If some
      values in the data are missing the data cells should be set to
      `numpy.NaN`.  Note that pandas converts each column containing
      `numpy.NaN`s to dtype `float`.)
  
  References
  ----------
  [1] Koller & Friedman, Probabilistic Graphical Models - Principles and Techniques,
      2009, Section 18.2
  [2] Neapolitan, Learning Bayesian Networks, Section 10.1.2 for the PC algorithm (page 550), http://www.cs.technion.ac.il/~dang/books/Learning%20Bayesian%20Networks(Neapolitan,%20Richard).pdf
docstr: Estimates a DAG/PDAG from the given dataset using the PC algorithm which
  is a constraint-based structure learning algorithm[1]. The independencies
  in the dataset are identified by doing statistical independece test. This
  method returns a DAG/PDAG structure which is faithful to the independencies
  implied by the dataset.
  
  Parameters
  ----------
  variant: str (one of "orig", "stable", "parallel")
      The variant of PC algorithm to run.
      "orig": The original PC algorithm. Might not give the same
              results in different runs but does less independence
              tests compared to stable.
      "stable": Gives the same result in every run but does needs to
              do more statistical independence tests.
      "parallel": Parallel version of PC Stable. Can run on multiple
              cores with the same result on each run.
  
  ci_test: str or fun
      The statistical test to use for testing conditional independence in
      the dataset. If `str` values should be one of:
          "independence_match": If using this option, an additional parameter
                  `independencies` must be specified.
          "chi_square": Uses the Chi-Square independence test. This works
                  only for discrete datasets.
          "pearsonr": Uses the pertial correlation based on pearson
                  correlation coefficient to test independence. This works
                  only for continuous datasets.
          "g_sq": G-test. Works only for discrete datasets.
          "log_likelihood": Log-likelihood test. Works only for discrete dataset.
          "freeman_tuckey": Freeman Tuckey test. Works only for discrete dataset.
          "modified_log_likelihood": Modified Log Likelihood test. Works only for discrete variables.
          "neyman": Neyman test. Works only for discrete variables.
          "cressie_read": Cressie Read test. Works only for discrete variables.
  
  max_cond_vars: int
      The maximum number of conditional variables allowed to do the statistical
      test with.
  
  return_type: str (one of "dag", "cpdag", "pdag", "skeleton")
      The type of structure to return.
  
      If `return_type=pdag` or `return_type=cpdag`: a partially directed structure
          is returned.
      If `return_type=dag`, a fully directed structure is returned if it
          is possible to orient all the edges.
      If `return_type="skeleton", returns an undirected graph along
          with the separating sets.
  
  significance_level: float (default: 0.01)
      The statistical tests use this value to compare with the p-value of
      the test to decide whether the tested variables are independent or
      not. Different tests can treat this parameter differently:
          1. Chi-Square: If p-value > significance_level, it assumes that the
              independence condition satisfied in the data.
          2. pearsonr: If p-value > significance_level, it assumes that the
              independence condition satisfied in the data.
  
  Returns
  -------
  Estimated model: pgmpy.base.DAG, pgmpy.base.PDAG, or tuple(networkx.UndirectedGraph, dict)
          The estimated model structure, can be a partially directed graph (PDAG)
          or a fully directed graph (DAG), or (Undirected Graph, separating sets)
          depending on the value of `return_type` argument.
  
  References
  ----------
  [1] Original PC: P. Spirtes, C. Glymour, and R. Scheines, Causation,
              Prediction, and Search, 2nd ed. Cambridge, MA: MIT Press, 2000.
  [2] Stable PC:  D. Colombo and M. H. Maathuis, “A modification of the PC algorithm
              yielding order-independent skeletons,” ArXiv e-prints, Nov. 2012.
  [3] Parallel PC: Le, Thuc, et al. "A fast PC algorithm for high dimensional causal
              discovery with multi-core PCs." IEEE/ACM transactions on computational
              biology and bioinformatics (2016).
  
  Examples
  --------
  >>> from pgmpy.utils import get_example_model
  >>> from pgmpy.estimators import PC
  >>> model = get_example_model('alarm')
  >>> data = model.simulate(n_samples=1000)
  >>> est = PC(data)
  >>> model_chi = est.estimate(ci_test='chi_square')
  >>> print(len(model_chi.edges()))
  28
  >>> model_gsq, _ = est.estimate(ci_test='g_sq', return_type='skeleton')
  >>> print(len(model_gsq.edges()))
  33
docstr_tok: ["Estimates","a","DAG\/PDAG","from","the","given","dataset","using","the","PC","algorithm","which","is","a","constraint-based","structure","learning","algorithm","[","1","]",".","The","independencies","in","the","dataset","are","identified","by","doing","statistical","independece","test",".","This","method","returns","a","DAG\/PDAG","structure","which","is","faithful","to","the","independencies","implied","by","the","dataset",".","Parameters","--","--","--","--","--","variant",":","str","(","one","of","``","orig","''",",","``","stable","''",",","``","parallel","''",")","The","variant","of","PC","algorithm","to","run",".","``","orig","''",":","The","original","PC","algorithm",".","Might","not","give","the","same","results","in","different","runs","but","does","less","independence","tests","compared","to","stable",".","``","stable","''",":","Gives","the","same","result","in","every","run","but","does","needs","to","do","more","statistical","independence","tests",".","``","parallel","''",":","Parallel","version","of","PC","Stable",".","Can","run","on","multiple","cores","with","the","same","result","on","each","run",".","ci_test",":","str","or","fun","The","statistical","test","to","use","for","testing","conditional","independence","in","the","dataset",".","If","`","str","`","values","should","be","one","of",":","``","independence_match","''",":","If","using","this","option",",","an","additional","parameter","`","independencies","`","must","be","specified",".","``","chi_square","''",":","Uses","the","Chi-Square","independence","test",".","This","works","only","for","discrete","datasets",".","``","pearsonr","''",":","Uses","the","pertial","correlation","based","on","pearson","correlation","coefficient","to","test","independence",".","This","works","only","for","continuous","datasets",".","``","g_sq","''",":","G-test",".","Works","only","for","discrete","datasets",".","``","log_likelihood","''",":","Log-likelihood","test",".","Works","only","for","discrete","dataset",".","``","freeman_tuckey","''",":","Freeman","Tuckey","test",".","Works","only","for","discrete","dataset",".","``","modified_log_likelihood","''",":","Modified","Log","Likelihood","test",".","Works","only","for","discrete","variables",".","``","neyman","''",":","Neyman","test",".","Works","only","for","discrete","variables",".","``","cressie_read","''",":","Cressie","Read","test",".","Works","only","for","discrete","variables",".","max_cond_vars",":","int","The","maximum","number","of","conditional","variables","allowed","to","do","the","statistical","test","with",".","return_type",":","str","(","one","of","``","dag","''",",","``","cpdag","''",",","``","pdag","''",",","``","skeleton","''",")","The","type","of","structure","to","return",".","If","`","return_type=pdag","`","or","`","return_type=cpdag","`",":","a","partially","directed","structure","is","returned",".","If","`","return_type=dag","`",",","a","fully","directed","structure","is","returned","if","it","is","possible","to","orient","all","the","edges",".","If","`","return_type=","''","skeleton","''",",","returns","an","undirected","graph","along","with","the","separating","sets",".","significance_level",":","float","(","default",":","0.01",")","The","statistical","tests","use","this","value","to","compare","with","the","p-value","of","the","test","to","decide","whether","the","tested","variables","are","independent","or","not",".","Different","tests","can","treat","this","parameter","differently",":","1",".","Chi-Square",":","If","p-value",">","significance_level",",","it","assumes","that","the","independence","condition","satisfied","in","the","data",".","2.","pearsonr",":","If","p-value",">","significance_level",",","it","assumes","that","the","independence","condition","satisfied","in","the","data",".","Returns","--","--","--","-","Estimated","model",":","pgmpy.base.DAG",",","pgmpy.base.PDAG",",","or","tuple","(","networkx.UndirectedGraph",",","dict",")","The","estimated","model","structure",",","can","be","a","partially","directed","graph","(","PDAG",")","or","a","fully","directed","graph","(","DAG",")",",","or","(","Undirected","Graph",",","separating","sets",")","depending","on","the","value","of","`","return_type","`","argument",".","References","--","--","--","--","--","[","1","]","Original","PC",":","P.","Spirtes",",","C.","Glymour",",","and","R.","Scheines",",","Causation",",","Prediction",",","and","Search",",","2nd","ed",".","Cambridge",",","MA",":","MIT","Press",",","2000",".","[","2","]","Stable","PC",":","D.","Colombo","and","M.","H.","Maathuis",",","\u201c","A","modification","of","the","PC","algorithm","yielding","order-independent","skeletons",",","\u201d","ArXiv","e-prints",",","Nov.","2012",".","[","3","]","Parallel","PC",":","Le",",","Thuc",",","et","al",".","``","A","fast","PC","algorithm","for","high","dimensional","causal","discovery","with","multi-core","PCs",".","''","IEEE\/ACM","transactions","on","computational","biology","and","bioinformatics","(","2016",")",".","Examples","--","--","--","--",">",">",">","from","pgmpy.utils","import","get_example_model",">",">",">","from","pgmpy.estimators","import","PC",">",">",">","model","=","get_example_model","(","'alarm","'",")",">",">",">","data","=","model.simulate","(","n_samples=1000",")",">",">",">","est","=","PC","(","data",")",">",">",">","model_chi","=","est.estimate","(","ci_test='chi_square","'",")",">",">",">","print","(","len","(","model_chi.edges","(",")",")",")","28",">",">",">","model_gsq",",","_","=","est.estimate","(","ci_test='g_sq","'",",","return_type='skeleton","'",")",">",">",">","print","(","len","(","model_gsq.edges","(",")",")",")","33"]
code: def estimate(
      self,
      variant="stable",
      ci_test="chi_square",
      max_cond_vars=5,
      return_type="dag",
      significance_level=0.01,
      n_jobs=-1,
      show_progress=True,
      **kwargs,
  ):
      """
      Estimates a DAG/PDAG from the given dataset using the PC algorithm which
      is a constraint-based structure learning algorithm[1]. The independencies
      in the dataset are identified by doing statistical independece test. This
      method returns a DAG/PDAG structure which is faithful to the independencies
      implied by the dataset.
  
      Parameters
      ----------
      variant: str (one of "orig", "stable", "parallel")
          The variant of PC algorithm to run.
          "orig": The original PC algorithm. Might not give the same
                  results in different runs but does less independence
                  tests compared to stable.
          "stable": Gives the same result in every run but does needs to
                  do more statistical independence tests.
          "parallel": Parallel version of PC Stable. Can run on multiple
                  cores with the same result on each run.
  
      ci_test: str or fun
          The statistical test to use for testing conditional independence in
          the dataset. If `str` values should be one of:
              "independence_match": If using this option, an additional parameter
                      `independencies` must be specified.
              "chi_square": Uses the Chi-Square independence test. This works
                      only for discrete datasets.
              "pearsonr": Uses the pertial correlation based on pearson
                      correlation coefficient to test independence. This works
                      only for continuous datasets.
              "g_sq": G-test. Works only for discrete datasets.
              "log_likelihood": Log-likelihood test. Works only for discrete dataset.
              "freeman_tuckey": Freeman Tuckey test. Works only for discrete dataset.
              "modified_log_likelihood": Modified Log Likelihood test. Works only for discrete variables.
              "neyman": Neyman test. Works only for discrete variables.
              "cressie_read": Cressie Read test. Works only for discrete variables.
  
      max_cond_vars: int
          The maximum number of conditional variables allowed to do the statistical
          test with.
  
      return_type: str (one of "dag", "cpdag", "pdag", "skeleton")
          The type of structure to return.
  
          If `return_type=pdag` or `return_type=cpdag`: a partially directed structure
              is returned.
          If `return_type=dag`, a fully directed structure is returned if it
              is possible to orient all the edges.
          If `return_type="skeleton", returns an undirected graph along
              with the separating sets.
  
      significance_level: float (default: 0.01)
          The statistical tests use this value to compare with the p-value of
          the test to decide whether the tested variables are independent or
          not. Different tests can treat this parameter differently:
              1. Chi-Square: If p-value > significance_level, it assumes that the
                  independence condition satisfied in the data.
              2. pearsonr: If p-value > significance_level, it assumes that the
                  independence condition satisfied in the data.
  
      Returns
      -------
      Estimated model: pgmpy.base.DAG, pgmpy.base.PDAG, or tuple(networkx.UndirectedGraph, dict)
              The estimated model structure, can be a partially directed graph (PDAG)
              or a fully directed graph (DAG), or (Undirected Graph, separating sets)
              depending on the value of `return_type` argument.
  
      References
      ----------
      [1] Original PC: P. Spirtes, C. Glymour, and R. Scheines, Causation,
                  Prediction, and Search, 2nd ed. Cambridge, MA: MIT Press, 2000.
      [2] Stable PC:  D. Colombo and M. H. Maathuis, “A modification of the PC algorithm
                  yielding order-independent skeletons,” ArXiv e-prints, Nov. 2012.
      [3] Parallel PC: Le, Thuc, et al. "A fast PC algorithm for high dimensional causal
                  discovery with multi-core PCs." IEEE/ACM transactions on computational
                  biology and bioinformatics (2016).
  
      Examples
      --------
      >>> from pgmpy.utils import get_example_model
      >>> from pgmpy.estimators import PC
      >>> model = get_example_model('alarm')
      >>> data = model.simulate(n_samples=1000)
      >>> est = PC(data)
      >>> model_chi = est.estimate(ci_test='chi_square')
      >>> print(len(model_chi.edges()))
      28
      >>> model_gsq, _ = est.estimate(ci_test='g_sq', return_type='skeleton')
      >>> print(len(model_gsq.edges()))
      33
      """
      # Step 0: Do checks that the specified parameters are correct, else throw meaningful error.
      if variant not in ("orig", "stable", "parallel"):
          raise ValueError(
              f"variant must be one of: orig, stable, or parallel. Got: {variant}"
          )
      elif (not callable(ci_test)) and (
          ci_test.lower()
          not in (list(CI_TESTS.keys()) + ["independence_match"])
      ):
          raise ValueError(
              "ci_test must be a callable or one of the tests defined in CITests.py"
          )
  
      if (ci_test == "independence_match") and (
          self.independencies is None
      ):
          raise ValueError(
              "For using independence_match, independencies argument must be specified"
          )
      elif (
          ci_test in set(CI_TESTS.keys()) - set(["independence_match"])
      ) and (self.data is None):
          raise ValueError(
              "For using Chi Square or Pearsonr, data argument must be specified"
          )
  
      # Step 1: Run the PC algorithm to build the skeleton and get the separating sets.
      skel, separating_sets = self.build_skeleton(
          ci_test=ci_test,
          max_cond_vars=max_cond_vars,
          significance_level=significance_level,
          variant=variant,
          n_jobs=n_jobs,
          show_progress=show_progress,
          **kwargs,
      )
  
      if return_type.lower() == "skeleton":
          return skel, separating_sets
  
      # Step 2: Orient the edges based on build the PDAG/CPDAG.
      pdag = self.skeleton_to_pdag(skel, separating_sets)
  
      # Step 3: Either return the CPDAG or fully orient the edges to build a DAG.
      if return_type.lower() in ("pdag", "cpdag"):
          return pdag
      elif return_type.lower() == "dag":
          return pdag.to_dag()
      else:
          raise ValueError(
              f"return_type must be one of: dag, pdag, cpdag, or skeleton. Got: {return_type}"
          )
  
file_path: pgmpy/pgmpy/estimators/PC.py
imports: from itertools import chain, combinations, permutations
  import networkx
  from joblib import Parallel, delayed
  from tqdm.auto import tqdm
  from pgmpy import config
  from pgmpy.base import PDAG
  from pgmpy.estimators import StructureEstimator
  from pgmpy.estimators.CITests import *
  from pgmpy.global_vars import logger
label_desc: Use image node_id 2 for calling the PC obj's underlying member method code with example usage: obj.estimate(variant, ci_test, max_cond_vars, return_type, significance_level, n_jobs, show_progress) and returns: skel,  separating_sets, pdag, pdag