Package edu.cmu.tetrad.search.test
Class Kci
java.lang.Object
edu.cmu.tetrad.search.test.Kci
- All Implemented Interfaces:
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 Bowman and Azzalini (1997):
Bowman, A. W., and Azzalini, A. (1997). Applied smoothing techniques for data analysis: the kernel approach with S-Plus illustrations (Vol. 18). OUP Oxford.
- Version:
- $Id: $Id
- Author:
- kunzhang, Vineet Raghu on 7/3/2016, josephramsey refactoring 7/4/2018, 12/6/2024
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
A record representing the result of an eigenvalue decomposition.static enum
Represents the type of kernel to be used in a computation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncheckIndependence
(Node x, Node y, Set<Node> z) Checks the independence between two nodes given a set of conditioning variables.static double[]
convertTo1DArray
(org.ejml.simple.SimpleMatrix matrix) Converts a SimpleMatrix to a 1D array.double
getAlpha()
Returns the significance level of the independence test.getCov()
Returns the covariance matrix.getData()
Returns The data model for the independence test.Returns a list consisting of the dataset for this test.getRows()
Returns the rows used in the test.int
Returns the sample size.double
getScore
(IndependenceResult result) Returns alpha - p.getVariable
(String name) Returns the variable of the given name.Returns the list of variables over which this independence checker is capable of determinining independence relations.indTestSubset
(List<Node> vars) Returns an Independence test for a sublist of the variables.boolean
Returns the value of the verbose flag.void
setAlpha
(double alpha) Sets the alpha level for the test.void
setApproximate
(boolean approximate) Sets whether the approximate algorithm should be used.void
setKernelType
(Kci.KernelType kernelType) Sets the type of kernel to be used in computations.void
setNumBootstraps
(int numBootstraps) Sets the number of bootstraps to do.void
setPolyConst
(double polyConst) Sets the constant of the polynomial kernel, if usedvoid
setPolyDegree
(double polyDegree) Sets the degree of the polynomial kernel, if usedvoid
Allows the user to set which rows are used in the test.void
setScalingFactor
(double scalingFactor) Sets the width multiplier.void
setThreshold
(double threshold) Sets the threshold.void
setVerbose
(boolean verbose) Sets the verbosity of the method.static org.ejml.simple.SimpleMatrix
standardizeData
(org.ejml.simple.SimpleMatrix data) toString()
Returns a string representation of this test.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface edu.cmu.tetrad.search.IndependenceTest
checkIndependence, determines, getVariableNames
-
Constructor Details
-
Kci
Constructor.- Parameters:
data
- The dataset to analyze. Must be continuous.alpha
- The alpha value of the test.
-
-
Method Details
-
convertTo1DArray
public static double[] convertTo1DArray(org.ejml.simple.SimpleMatrix matrix) Converts a SimpleMatrix to a 1D array.- Parameters:
matrix
- The matrix to convert.- Returns:
- The 1D array.
-
standardizeData
public static org.ejml.simple.SimpleMatrix standardizeData(org.ejml.simple.SimpleMatrix data) -
indTestSubset
Description copied from interface:IndependenceTest
Returns an Independence test for a sublist of the variables.- Specified by:
indTestSubset
in interfaceIndependenceTest
- 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 interfaceIndependenceTest
- Parameters:
x
- The first node.y
- The second node.z
- The set of conditioning variables.- Returns:
- The result of the independence test.
- Throws:
InterruptedException
- See Also:
-
getVariables
Returns the list of variables over which this independence checker is capable of determinining independence relations.- Specified by:
getVariables
in interfaceIndependenceTest
- Returns:
- This list.
-
getVariable
Returns the variable of the given name.- Specified by:
getVariable
in interfaceIndependenceTest
- Parameters:
name
- aString
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 interfaceIndependenceTest
- Returns:
- This alpha.
-
setAlpha
public void setAlpha(double alpha) Sets the alpha level for the test.- Specified by:
setAlpha
in interfaceIndependenceTest
- Parameters:
alpha
- The alpha level to be set.
-
toString
Returns a string representation of this test.- Specified by:
toString
in interfaceIndependenceTest
- Overrides:
toString
in classObject
- Returns:
- This string.
-
getData
Returns The data model for the independence test.- Specified by:
getData
in interfaceIndependenceTest
- Returns:
- This data.
- See Also:
-
getCov
Returns the covariance matrix.- Specified by:
getCov
in interfaceIndependenceTest
- Returns:
- The covariance matrix.
-
getDataSets
Returns a list consisting of the dataset for this test.- Specified by:
getDataSets
in interfaceIndependenceTest
- Returns:
- This dataset in a list.
-
getSampleSize
public int getSampleSize()Returns the sample size.- Specified by:
getSampleSize
in interfaceIndependenceTest
- Returns:
- This size.
-
getScore
Returns alpha - p.- Parameters:
result
- aIndependenceResult
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 interfaceIndependenceTest
- Returns:
- The value of the verbose flag.
-
setVerbose
public void setVerbose(boolean verbose) Sets the verbosity of the method.- Specified by:
setVerbose
in interfaceIndependenceTest
- Parameters:
verbose
- True if verbosity is enabled, false otherwise.
-
setKernelType
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
Returns the rows used in the test.- Specified by:
getRows
in interfaceRowsSettable
- Returns:
- The rows used in the test.
-
setRows
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 interfaceRowsSettable
- Parameters:
rows
- The rows to use for the test. Can be null.
-