Class PythonKciIndependenceTest

java.lang.Object
edu.cmu.tetrad.search.test.PythonKciIndependenceTest
All Implemented Interfaces:
IndependenceTest, Closeable, AutoCloseable

public final class PythonKciIndependenceTest extends Object implements IndependenceTest, Closeable
The PythonKciIndependenceTest class implements an independence test using the kernel conditional independence (KCI) test provided through a Python backend, such as causallearn. It facilitates independence determination between variables of a given dataset and manages interaction with the associated Python service. This class is immutable except for certain configurable properties (e.g., alpha, verbosity). It initializes resources on the Python side upon instantiation and provides mechanisms to perform independence testing, adjust parameters, and release resources when no longer needed. Thread Safety: - The class is thread-safe for concurrent read/query operations. - Care is required when modifying properties (alpha, verbosity, or KCI parameters), as intended thread-safety for setters is only provided via `volatile` or synchronized service updates. - The service interactions must ensure proper thread coordination. Responsibilities: - Maps dataset variables to indices for Python integration. - Executes the KCI test by delegating the computation to the Python service. - Manages test parameters such as significance level (alpha) and verbosity. - Facilitates resource cleanup by implementing the Closeable interface.
  • Constructor Details

    • PythonKciIndependenceTest

      public PythonKciIndependenceTest(DataSet data, PythonCiService service)
      Constructs a PythonKciIndependenceTest object for performing conditional independence tests using a Python backend via the provided PythonCiService. The method initializes required fields and prepares the object for subsequent operations.
      Parameters:
      data - the dataset containing the variables and relationships to analyze; must not be null
      service - the PythonCiService used to execute independence tests in a Python environment; must not be null
  • Method Details

    • checkIndependence

      public IndependenceResult checkIndependence(Node x, Node y, Set<Node> z) throws InterruptedException
      Tests the conditional independence between two variables given a conditioning set, using a Python backend for statistical computations. Computes the p-value for the hypothesis that the variables are conditionally independent, and determines whether to reject the null hypothesis based on a specified significance level (alpha).
      Specified by:
      checkIndependence in interface IndependenceTest
      Parameters:
      x - the first variable (Node) for testing conditional independence; must not be null
      y - the second variable (Node) for testing conditional independence; must not be null
      z - the conditioning set (Set of Node) on which the independence hypothesis is conditioned; can be null or empty
      Returns:
      an IndependenceResult containing the outcome of the test, including the computed p-value, whether the null hypothesis was rejected, and a related score
      Throws:
      InterruptedException - if the thread running this method is interrupted
    • getVariables

      public List<Node> getVariables()
      Retrieves the list of variables involved in the analysis.
      Specified by:
      getVariables in interface IndependenceTest
      Returns:
      a list of Node objects representing the variables in the dataset.
    • getData

      public DataModel getData()
      Retrieves the data model associated with this instance.
      Specified by:
      getData in interface IndependenceTest
      Returns:
      the DataModel object representing the dataset used for analysis.
    • isVerbose

      public boolean isVerbose()
      Indicates whether verbose output is enabled for the PythonKciIndependenceTest.
      Specified by:
      isVerbose in interface IndependenceTest
      Returns:
      true if verbose output is enabled, false otherwise
    • setVerbose

      public void setVerbose(boolean verbose)
      Sets whether verbose output is enabled for this instance of PythonKciIndependenceTest. When verbosity is enabled, additional details about the operations performed by the Python backend may be provided in the output.
      Specified by:
      setVerbose in interface IndependenceTest
      Parameters:
      verbose - a boolean value indicating whether verbose output should be enabled (true) or disabled (false)
    • getAlpha

      public double getAlpha()
      Retrieves the significance level (alpha) used in conditional independence tests. The alpha value represents the threshold for deciding whether to reject the null hypothesis of conditional independence.
      Specified by:
      getAlpha in interface IndependenceTest
      Returns:
      the alpha value as a double, representing the significance level for the tests
    • setAlpha

      public void setAlpha(double alpha)
      Sets the significance level (alpha) used in conditional independence tests. The alpha value determines the threshold for rejecting the null hypothesis of conditional independence. A lower alpha value indicates a stricter criterion for concluding that two variables are not conditionally independent.
      Specified by:
      setAlpha in interface IndependenceTest
      Parameters:
      alpha - the significance level to be set; must be a double value between 0 and 1 (exclusive) representing the desired confidence level
    • close

      public void close()
      Closes the underlying PythonCiService associated with this instance, releasing any resources and terminating any persistent connections. This method ensures that the external Python backend for performing conditional independence tests is properly shut down. If an IOException occurs during the closing process, it will be wrapped and propagated as a RuntimeException.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      RuntimeException - if an IOException is encountered while closing the service
    • toString

      public String toString()
      Returns a string representation of the PythonKciIndependenceTest instance, providing a concise description intended to identify the use of the KCI method for causal independence tests integrated with a Python backend.
      Specified by:
      toString in interface IndependenceTest
      Overrides:
      toString in class Object
      Returns:
      a string describing the PythonKciIndependenceTest implementation.
    • setKciParam

      public void setKciParam(String key, Object value)
      Sets a KCI-specific parameter for the PythonKciIndependenceTest instance. This method allows users to modify or add configuration parameters specific to the Kernel-based Conditional Independence method. Once a parameter is updated, the changes are propagated to the underlying Python service responsible for performing conditional independence tests.
      Parameters:
      key - the name of the parameter to set; must not be null
      value - the value to assign to the specified parameter; can be any object representing the desired parameter value, depending on the expected type of the parameter