Class Kci

java.lang.Object
edu.cmu.tetrad.search.test.Kci
All Implemented Interfaces:
IndependenceTest, RowsSettable

public class Kci extends Object implements IndependenceTest, RowsSettable
Gives an implementation of the Kernel Independence Test (KCI) by Kun Zhang, which is a general test of conditional independence. The reference is here:

Zhang, K., Peters, J., Janzing, D., and Schölkopf, B. (2012). Kernel-based conditional independence test and application in causal discovery. arXiv preprint arXiv:1202.3775.

Please see that paper, especially Theorem 4 and Proposition 5.

Using optimal kernel bandwidths suggested by Silverman:

Version:
$Id: $Id
Author:
kunzhang, Vineet Raghu on 7/3/2016, josephramsey refactoring 7/4/2018, 12/6/2024
  • Constructor Details

    • Kci

      public Kci(DataSet data, double alpha)
      Constructor.
      Parameters:
      data - The dataset to analyze. Must be continuous.
      alpha - The alpha value of the test.
  • Method Details

    • standardizeData

      public static org.ejml.simple.SimpleMatrix standardizeData(org.ejml.simple.SimpleMatrix data)
      Standardizes the data by centering (subtracting the mean) and scaling (dividing by the standard deviation) each column of the input matrix.
      Parameters:
      data - the input matrix where standardization will be applied column-wise
      Returns:
      a new matrix with the standardized data
    • indTestSubset

      public IndependenceTest indTestSubset(List<Node> vars)
      Description copied from interface: IndependenceTest
      Returns an Independence test for a sublist of the variables.
      Specified by:
      indTestSubset in interface IndependenceTest
      Parameters:
      vars - The sublist of variables.
      Returns:
      a IndependenceTest object
      Throws:
      UnsupportedOperationException - since not implemented.
    • checkIndependence

      public IndependenceResult checkIndependence(Node x, Node y, Set<Node> z) throws InterruptedException
      Checks the independence between two nodes given a set of conditioning variables.
      Specified by:
      checkIndependence in interface IndependenceTest
      Parameters:
      x - The first node.
      y - The second node.
      z - The set of conditioning variables.
      Returns:
      The result of the independence test.
      Throws:
      InterruptedException - if any
    • getVariables

      public List<Node> getVariables()
      Returns the list of variables over which this independence checker is capable of determinining independence relations.
      Specified by:
      getVariables in interface IndependenceTest
      Returns:
      This list.
    • getVariable

      public Node getVariable(String name)
      Returns the variable of the given name.
      Specified by:
      getVariable in interface IndependenceTest
      Parameters:
      name - a String object representing the name of the variable to retrieve
      Returns:
      the Node object representing the variable with the given name
    • getAlpha

      public double getAlpha()
      Returns the significance level of the independence test.
      Specified by:
      getAlpha in interface IndependenceTest
      Returns:
      This alpha.
    • setAlpha

      public void setAlpha(double alpha)
      Sets the alpha level for the test.
      Specified by:
      setAlpha in interface IndependenceTest
      Parameters:
      alpha - The alpha level to be set.
    • toString

      public String toString()
      Returns a string representation of this test.
      Specified by:
      toString in interface IndependenceTest
      Overrides:
      toString in class Object
      Returns:
      This string.
    • getData

      public DataModel getData()
      Returns The data model for the independence test.
      Specified by:
      getData in interface IndependenceTest
      Returns:
      This data.
    • getCov

      public ICovarianceMatrix getCov()
      Returns the covariance matrix.
      Specified by:
      getCov in interface IndependenceTest
      Returns:
      The covariance matrix.
    • getDataSets

      public List<DataSet> getDataSets()
      Returns a list consisting of the dataset for this test.
      Specified by:
      getDataSets in interface IndependenceTest
      Returns:
      This dataset in a list.
    • getSampleSize

      public int getSampleSize()
      Returns the sample size.
      Specified by:
      getSampleSize in interface IndependenceTest
      Returns:
      This size.
    • getScore

      public double getScore(IndependenceResult result)
      Returns alpha - p.
      Parameters:
      result - a IndependenceResult object
      Returns:
      This number.
    • setApproximate

      public void setApproximate(boolean approximate)
      Sets whether the approximate algorithm should be used.
      Parameters:
      approximate - True, if so.
    • setScalingFactor

      public void setScalingFactor(double scalingFactor)
      Sets the width multiplier.
      Parameters:
      scalingFactor - This multipler.
    • setNumBootstraps

      public void setNumBootstraps(int numBootstraps)
      Sets the number of bootstraps to do.
      Parameters:
      numBootstraps - This number.
    • setThreshold

      public void setThreshold(double threshold)
      Sets the threshold.
      Parameters:
      threshold - This number.
    • isVerbose

      public boolean isVerbose()
      Returns the value of the verbose flag.
      Specified by:
      isVerbose in interface IndependenceTest
      Returns:
      The value of the verbose flag.
    • setVerbose

      public void setVerbose(boolean verbose)
      Sets the verbosity of the method.
      Specified by:
      setVerbose in interface IndependenceTest
      Parameters:
      verbose - True if verbosity is enabled, false otherwise.
    • setKernelType

      public void setKernelType(Kci.KernelType kernelType)
      Sets the type of kernel to be used in computations.
      Parameters:
      kernelType - the KernelType to set
    • setPolyDegree

      public void setPolyDegree(double polyDegree)
      Sets the degree of the polynomial kernel, if used
      Parameters:
      polyDegree - the degree of the polynomial kernel to set
    • setPolyConst

      public void setPolyConst(double polyConst)
      Sets the constant of the polynomial kernel, if used
      Parameters:
      polyConst - the constant of the polynomial kernel to set
    • getRows

      public List<Integer> getRows()
      Returns the rows used in the test.
      Specified by:
      getRows in interface RowsSettable
      Returns:
      The rows used in the test.
    • setRows

      public void setRows(List<Integer> rows)
      Allows the user to set which rows are used in the test. Otherwise, all rows are used, except those with missing values.
      Specified by:
      setRows in interface RowsSettable
      Parameters:
      rows - The rows to use for the test. Can be null.