### Install cdt from source Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_sources/index.rst.txt Clone the repository, install requirements, and set up the package for development. ```bash $ git clone https://github.com/FenTechSolutions/CausalDiscoveryToolbox.git # Download the package $ cd CausalDiscoveryToolbox $ pip install -r requirements.txt # Install the requirements $ python setup.py install develop --user ``` -------------------------------- ### HSICLasso usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Shows how to instantiate HSICLasso and use predict_features and predict on the Boston housing dataset to get feature scores and the graph skeleton. ```python >>> from cdt.independence.graph import HSICLasso >>> from sklearn.datasets import load_boston >>> boston = load_boston() >>> df_features = pd.DataFrame(boston['data']) >>> df_target = pd.DataFrame(boston['target']) >>> obj = HSICLasso() >>> output = obj.predict_features(df_features, df_target) >>> ugraph = obj.predict(df_features) # Predict skeleton ``` -------------------------------- ### LiNGAM example with sachs dataset Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/LiNGAM.html Demonstrates the LiNGAM algorithm from the cdt package on the "sachs" dataset. Note that LiNGAM does not support starting with a graph and requires the continuous, non-Gaussian data and R package pcalg. ```python import networkx as nx from cdt.causality.graph import LiNGAM from cdt.data import load_dataset data, graph = load_dataset("sachs") obj = LiNGAM() output = obj.predict(data) ``` -------------------------------- ### Install cdt from PyPI Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/index.html Installs the CausalDiscoveryToolbox package from PyPI. ```bash pip install cdt ``` -------------------------------- ### SAMv1 usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Shows how to instantiate SAMv1, load a dataset, and run predictions with and without a graph. The example also demonstrates how to visualize the resulting graph. ```python >>> import networkx as nx >>> from cdt.causality.graph import SAMv1 >>> from cdt.data import load_dataset >>> data, graph = load_dataset("sachs") >>> obj = SAMv1() >>> #The predict() method works without a graph, or with a >>> #directed or undirected graph provided as an input >>> output = obj.predict(data) #No graph provided as an argument >>> >>> output = obj.predict(data, nx.Graph(graph)) #With an undirected graph >>> >>> output = obj.predict(data, graph) #With a directed graph >>> >>> #To view the graph created, run the below commands: >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### SAM usage example with load_dataset and predict Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/SAM.html Shows how to instantiate SAM and call predict with no graph, an undirected graph, or a directed graph. The example also demonstrates how to visualize the resulting graph with networkx and matplotlib. ```python >>> import networkx as nx >>> from cdt.causality.graph import SAM >>> from cdt.data import load_dataset >>> data, graph = load_dataset("sachs") >>> obj = SAM() >>> #The predict() method works without a graph, or with a >>> #directed or undirected graph provided as an input >>> output = obj.predict(data) #No graph provided as an argument >>> >>> output = obj.predict(data, nx.Graph(graph)) #With an undirected graph >>> >>> output = obj.predict(data, graph) #With a directed graph >>> >>> #To view the graph created, run the below commands: >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### Install R packages for cdt on Ubuntu 20.04 Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_sources/index.rst.txt Install R and all required R packages for additional algorithms (bnlearn, kpcalg, pcalg, etc.) on Ubuntu 20.04. ```sh apt-get -qq update DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata apt-get -qq install dialog apt-utils -y apt-get install apt-transport-https -y apt-get install -qq software-properties-common -y apt-get -qq update apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' -y apt-get -qq update apt-get -qq install r-base -y apt-get -qq install libssl-dev -y apt-get -qq install libgmp3-dev -y apt-get -qq install git -y apt-get -qq install build-essential -y apt-get -qq install libv8-dev -y apt-get -qq install libcurl4-openssl-dev -y apt-get -qq install libgsl-dev -y Rscript -e 'install.packages(c("V8"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages(c("sfsmisc"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages(c("clue"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages("https://cran.r-project.org/src/contrib/Archive/randomForest/randomForest_4.6-14.tar.gz", repos=NULL, type="source")' Rscript -e 'install.packages(c("lattice"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages(c("devtools"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages(c("MASS"),repos="http://cran.us.r-project.org", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages("BiocManager")' Rscript -e 'BiocManager::install(c("igraph"))' Rscript -e 'install.packages("https://cran.r-project.org/src/contrib/Archive/fastICA/fastICA_1.2-2.tar.gz", repos=NULL, type="source")' Rscript -e 'BiocManager::install(c("SID", "bnlearn", "pcalg", "kpcalg", "glmnet", "mboost"))' Rscript -e 'install.packages("https://cran.r-project.org/src/contrib/Archive/CAM/CAM_1.0.tar.gz", repos=NULL, type="source")' Rscript -e 'install.packages("https://cran.r-project.org/src/contrib/sparsebnUtils_0.0.8.tar.gz", repos=NULL, type="source")' Rscript -e 'BiocManager::install(c("ccdrAlgorithm", "discretecdAlgorithm"))' apt-get -qq install libxml2-dev -y Rscript -e 'install.packages("devtools")' Rscript -e 'library(devtools); install_github("cran/CAM"); install_github("cran/momentchi2"); install_github("Diviyan-Kalainathan/RCIT", quiet=TRUE, verbose=FALSE)' Rscript -e 'install.packages("https://cran.r-project.org/src/contrib/Archive/sparsebn/sparsebn_0.1.2.tar.gz", repos=NULL, type="source")' ``` -------------------------------- ### Install cdt via pip Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_sources/index.rst.txt Install the Causal Discovery Toolbox package using pip. Requires Python >=3.5 and dependencies listed in the requirements file. ```bash pip install cdt ``` -------------------------------- ### ChannelBatchNorm1d usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/utils.html Shows how to instantiate and use nn.BatchNorm1d with and without learnable affine parameters. The example creates a batch norm layer, generates random input, and applies the layer to produce output. ```python >>> # With Learnable Parameters >>> m = nn.BatchNorm1d(100) >>> # Without Learnable Parameters >>> m = nn.BatchNorm1d(100, affine=False) >>> input = torch.randn(20, 100) >>> output = m(input) ``` -------------------------------- ### LiNGAM algorithm usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Example of using the LiNGAM algorithm from cdt.causality.graph. It loads the 'sachs' dataset, creates a LiNGAM object, and predicts the causal graph. Note that this implementation does not support starting with a graph. ```python >>> import networkx as nx >>> from cdt.causality.graph import LiNGAM >>> from cdt.data import load_dataset >>> data, graph = load_dataset("sachs") >>> obj = LiNGAM() >>> output = obj.predict(data) ``` -------------------------------- ### LiNGAM.__init__ Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/LiNGAM.html Initializes the LiNGAM model, checking for the required R package 'pcalg' and setting up algorithm arguments and verbosity. ```APIDOC ## LiNGAM.__init__ ### Description Initializes the LiNGAM model. It verifies that the R package 'pcalg' is available, sets up internal arguments for the R script, and configures verbosity based on the provided parameter or the global settings. ### Method Constructor ### Signature `__init__(self, verbose=False)` ### Parameters - **verbose** (bool) - Optional - Sets the verbosity of the algorithm. Defaults to `cdt.SETTINGS.verbose`. ### Raises - **ImportError** - If the R package 'pcalg' is not available. ### Example ```python from cdt.causality.graph import LiNGAM obj = LiNGAM(verbose=True) ``` ``` -------------------------------- ### MMDloss usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/utils.html Computes the Maximum Mean Discrepancy (MMD) between two empirical distributions using the RBF kernel. The example creates two random tensors of shape (100, 10) and instantiates MMDloss with a batch size of 100, then calls it on the tensors to get a scalar loss value. ```python >>> from cdt.utils.loss import MMDloss >>> import torch as th >>> x, y = th.randn(100,10), th.randn(100, 10) >>> mmd = MMDloss(100) # 100 is the batch size >>> mmd(x, y) 0.0766 ``` -------------------------------- ### CDS usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Shows how to instantiate CDS, predict on the Tuebingen dataset, and orient a graph on the Sachs dataset. Includes plotting the resulting directed graph. ```python >>> from cdt.causality.pairwise import CDS >>> import networkx as nx >>> import matplotlib.pyplot as plt >>> from cdt.data import load_dataset >>> data, labels = load_dataset('tuebingen') >>> obj = CDS() >>> >>> # This example uses the predict() method >>> output = obj.predict(data) >>> >>> # This example uses the orient_graph() method. The dataset used >>> # can be loaded using the cdt.data module >>> data, graph = load_dataset("sachs") >>> output = obj.orient_graph(data, nx.Graph(graph)) >>> >>> #To view the directed graph run the following command >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### Set up development environment with conda and git Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_sources/developer.rst.txt Commands to create a conda environment, activate it, clone the forked repository, checkout the dev branch, and install the package in developer mode. Requires Python 3 and a forked repository. ```bash $ conda create --name cdt_dev python=3.6 numpy scipy scikit-learn $ source activate cdt_dev $ git clone git@github.com:YourLogin/CausalDiscoveryToolbox.git $ cd CausalDiscoveryToolbox $ git checkout dev $ python setup.py install develop ``` -------------------------------- ### LinearSVRL2 usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example of using LinearSVRL2 to predict features and skeleton from the Boston dataset. ```python >>> from cdt.independence.graph import LinearSVRL2 >>> from sklearn.datasets import load_boston >>> boston = load_boston() >>> df_features = pd.DataFrame(boston['data']) >>> df_target = pd.DataFrame(boston['target']) >>> obj = LinearSVRL2() >>> output = obj.predict_features(df_features, df_target) >>> ugraph = obj.predict(df_features) # Predict skeleton ``` -------------------------------- ### ARD example usage Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/independence/graph/FSRegression.html Example from the ARD class docstring showing how to use the class to predict features and the skeleton. ```python >>> from cdt.independence.graph import ARD >>> from sklearn.datasets import load_boston >>> boston = load_boston() >>> df_features = pd.DataFrame(boston['data']) >>> df_target = pd.DataFrame(boston['target']) >>> obj = ARD() >>> output = obj.predict_features(df_features, df_target) >>> ugraph = obj.predict(df_features) # Predict skeleton ``` -------------------------------- ### SAMv1 __init__ method Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/SAMv1.html Initializes the SAMv1 model with hyperparameters for learning rate, layer sizes, regularization, training epochs, batch size, number of runs, and parallelization settings. Uses SETTINGS.get_default for njobs, gpus, and verbose. ```python def __init__(self, lr=0.1, dlr=0.1, l1=0.1, nh=50, dnh=200, train_epochs=1000, test_epochs=1000, batch_size=-1, nruns=6, njobs=None, gpus=None, verbose=None): """Init and parametrize the SAM model. """ super(SAMv1, self).__init__() self.lr = lr self.dlr = dlr self.l1 = l1 self.nh = nh self.dnh = dnh self.train = train_epochs self.test = test_epochs self.batch_size = batch_size self.nruns = nruns self.njobs = SETTINGS.get_default(njobs=njobs) self.gpus = SETTINGS.get_default(gpu=gpus) self.verbose = SETTINGS.get_default(verbose=verbose) ``` -------------------------------- ### DecisionTreeRegression example usage Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/independence/graph/FSRegression.html Example from the DecisionTreeRegression class docstring showing how to use the class to predict features and the skeleton. ```python >>> from cdt.independence.graph import DecisionTreeRegression >>> from sklearn.datasets import load_boston >>> boston = load_boston() >>> df_features = pd.DataFrame(boston['data']) >>> df_target = pd.DataFrame(boston['target']) >>> obj = DecisionTreeRegression() >>> output = obj.predict_features(df_features, df_target) >>> ugraph = obj.predict(df_features) # Predict skeleton ``` -------------------------------- ### GNN usage example with predict and orient_graph Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Example usage of the GNN class. It loads the 'tuebingen' dataset for pairwise prediction and the 'sachs' dataset for graph orientation. The example shows how to use the predict() and orient_graph() methods, and how to visualize the resulting directed graph with networkx and matplotlib. ```python >>> from cdt.causality.pairwise import GNN >>> import networkx as nx >>> import matplotlib.pyplot as plt >>> from cdt.data import load_dataset >>> data, labels = load_dataset('tuebingen') >>> obj = GNN() >>> >>> # This example uses the predict() method >>> output = obj.predict(data) >>> >>> # This example uses the orient_graph() method. The dataset used >>> # can be loaded using the cdt.data module >>> data, graph = load_dataset("sachs") >>> output = obj.orient_graph(data, nx.Graph(graph)) >>> >>> #To view the directed graph run the following command >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### MIRegression independence test example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example usage of the MIRegression class to compute a test statistic based on mutual information regression. ```python >>> from cdt.independence.stats import MIRegression >>> obj = MIRegression() >>> a = np.array([1, 2, 1, 5]) >>> b = np.array([1, 3, 0, 6]) >>> obj.predict(a, b) ``` -------------------------------- ### Jarfo model usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Shows how to instantiate the Jarfo model, split the 'tuebingen' dataset into training and test sets, fit the model, and use the predict() method. Also demonstrates loading the 'sachs' dataset and using orient_graph() to orient a graph, followed by drawing the result with networkx and matplotlib. ```python >>> from cdt.causality.pairwise import Jarfo >>> import networkx as nx >>> import matplotlib.pyplot as plt >>> from cdt.data import load_dataset >>> from sklearn.model_selection import train_test_split >>> data, labels = load_dataset('tuebingen') >>> X_tr, X_te, y_tr, y_te = train_test_split(data, labels, train_size=.5) >>> >>> obj = Jarfo() >>> obj.fit(X_tr, y_tr) >>> # This example uses the predict() method >>> output = obj.predict(X_te) >>> >>> # This example uses the orient_graph() method. The dataset used >>> # can be loaded using the cdt.data module >>> data, graph = load_dataset("sachs") >>> output = obj.orient_graph(data, nx.Graph(graph)) >>> >>> #To view the directed graph run the following command >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### aracne example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/utils.html Example usage of cdt.utils.graph.aracne. It generates a sample graph using AcyclicGraphGenerator, converts it to an adjacency matrix, and applies the ARACNE algorithm. ```python >>> from cdt.utils.graph import aracne >>> import networkx as nx >>> # Generate sample data >>> from cdt.data import AcyclicGraphGenerator >>> graph = AcyclicGraphGenerator(linear).generate()[1] >>> adj_mat = nx.adjacency_matrix(graph).todense() >>> output = aracne(adj_mat) ``` -------------------------------- ### SAM __init__ method with hyperparameters Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/SAM.html Initializes the SAM model with configurable hyperparameters for learning rate, loss type, DAG penalty, and network architecture. Sets default values for unspecified parameters using SETTINGS. ```python def __init__(self, lr=0.01, dlr=0.001, mixed_data=False, lambda1=10, lambda2=0.001, nh=20, dnh=200, train_epochs=3000, test_epochs=1000, batch_size=-1, losstype="fgan", dagloss=True, dagstart=0.5, dagpenalization=0, dagpenalization_increase=0.01, functional_complexity='l2_norm', hlayers=2, dhlayers=2, sampling_type='sigmoidproba', linear=False, nruns=8, njobs=None, gpus=None, verbose=None): """Init and parametrize the SAM model.""" super(SAM, self).__init__() self.lr = lr self.dlr = dlr self.mixed_data = mixed_data self.lambda1 = lambda1 self.lambda2 = lambda2 self.nh = nh self.dnh = dnh self.train = train_epochs self.test = test_epochs self.batch_size = batch_size self.dagstart = dagstart self.dagloss = dagloss self.dagpenalization = dagpenalization self.dagpenalization_increase = dagpenalization_increase self.losstype = losstype self.functionalComplexity = functional_complexity self.sampletype = sampling_type self.linear = linear self.numberHiddenLayersG = hlayers self.numberHiddenLayersD = dhlayers self.njobs = SETTINGS.get_default(njobs=njobs) self.gpus = SETTINGS.get_default(gpu=gpus) self.verbose = SETTINGS.get_default(verbose=verbose) self.nruns = nruns ``` -------------------------------- ### NormalizedHSIC independence test example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example usage of the NormalizedHSIC class to compute a kernel-based independence test statistic using an RBF kernel. ```python >>> from cdt.independence.stats import NormalizedHSIC >>> obj = NormalizedHSIC() >>> a = np.array([1, 2, 1, 5]) >>> b = np.array([1, 3, 0, 6]) >>> obj.predict(a, b) ``` -------------------------------- ### FSGNN usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Shows how to instantiate FSGNN and use predict_features and predict to find feature scores and the graph skeleton. Uses the Boston housing dataset. ```python >>> from cdt.independence.graph import FSGNN >>> from sklearn.datasets import load_boston >>> boston = load_boston() >>> df_features = pd.DataFrame(boston['data']) >>> df_target = pd.DataFrame(boston['target']) >>> obj = FSGNN() >>> output = obj.predict_features(df_features, df_target) >>> ugraph = obj.predict(df_features) # Predict skeleton ``` -------------------------------- ### KendallTau independence test example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example usage of the KendallTau class to compute Kendall's Tau test statistic between two arrays. ```python >>> from cdt.independence.stats import KendallTau >>> obj = KendallTau() >>> a = np.array([1, 2, 1, 5]) >>> b = np.array([1, 3, 0, 6]) >>> obj.predict(a, b) ``` -------------------------------- ### Glasso usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Shows how to instantiate Glasso and call predict on a random DataFrame to obtain the graph skeleton. ```python >>> from cdt.independence.graph import Glasso >>> df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) >>> obj = Glasso() >>> output = obj.predict(df) ``` -------------------------------- ### MMDloss.__init__ Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/utils/loss.html Initializes the MMD loss model with configurable bandwidths for the RBF kernel. ```APIDOC ## MMDloss.__init__ ### Description Initializes the MMD loss model with configurable bandwidths for the RBF kernel. ### Method Constructor ### Signature `__init__(self, input_size, bandwidths=None)` ### Parameters - **input_size** (int) - Required - The size of the input features. - **bandwidths** (torch.Tensor, optional) - Optional - The bandwidths for the RBF kernel. Defaults to `torch.Tensor([0.01, 0.1, 1, 10, 100])`. ### Returns None ``` -------------------------------- ### read_adjacency_matrix example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/utils.html Reads a file containing an adjacency matrix and converts it into a networkx graph. The example reads 'graph_file.csv' with directed=False, returning an undirected graph. ```python >>> from cdt.utils import read_adjacency_matrix >>> data = read_causal_pairs('graph_file.csv', directed=False) ``` -------------------------------- ### Linear3D usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/utils.html Demonstrates creating a Linear3D layer (using nn.Linear) and applying it to a random input tensor, then printing the output size. ```python >>> m = nn.Linear(3, 20, 30) >>> input = torch.randn(128, 20) >>> output = m(input) >>> print(output.size()) ``` -------------------------------- ### BivariateFit usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/pairwise/Bivariate_fit.html Example showing usage of BivariateFit with predict(), orient_graph(), and drawing the resulting directed graph. Requires cdt, networkx, and matplotlib. ```python from cdt.causality.pairwise import BivariateFit >>> import networkx as nx >>> import matplotlib.pyplot as plt >>> from cdt.data import load_dataset >>> data, labels = load_dataset('tuebingen') >>> obj = BivariateFit() >>> >>> # This example uses the predict() method >>> output = obj.predict(data) >>> >>> # This example uses the orient_graph() method. The dataset used >>> # can be loaded using the cdt.data module >>> data, graph = load_dataset("sachs") >>> output = obj.orient_graph(data, nx.Graph(graph)) >>> >>> #To view the directed graph run the following command >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` -------------------------------- ### SAMv1 constructor Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/causality.html Initializes the SAMv1 algorithm with hyperparameters for learning rates, network sizes, training epochs, batch size, number of runs, parallel jobs, GPUs, and verbosity. ```APIDOC ## SAMv1(lr=0.1, dlr=0.1, l1=0.1, nh=50, dnh=200, train_epochs=1000, test_epochs=1000, batch_size=-1, nruns=6, njobs=None, gpus=None, verbose=None) ### Description Initializes the SAMv1 algorithm with hyperparameters for learning rates, network sizes, training epochs, batch size, number of runs, parallel jobs, GPUs, and verbosity. ### Parameters - **lr** (float) - Optional - Learning rate of the generators. Default: 0.1 - **dlr** (float) - Optional - Learning rate of the discriminator. Default: 0.1 - **l1** (float) - Optional - L1 penalization on the causal filters. Default: 0.1 - **nh** (int) - Optional - Number of hidden units in the generators' hidden layers. Default: 50 - **dnh** (int) - Optional - Number of hidden units in the discriminator's hidden layer. Default: 200 - **train_epochs** (int) - Optional - Number of training epochs. Default: 1000 - **test_epochs** (int) - Optional - Number of test epochs (saving and averaging the causal filters). Default: 1000 - **batch_size** (int) - Optional - Size of the batches to be fed to the SAM model. Default: -1 - **nruns** (int) - Optional - Number of runs to be made for causal estimation. Recommended: >=12 for optimal performance. Default: 6 - **njobs** (int) - Optional - Numbers of jobs to be run in Parallel. Recommended: 1 if no GPU available, 2*number of GPUs else. Default: None - **gpus** (int) - Optional - Number of available GPUs for the algorithm. Default: None - **verbose** (bool) - Optional - Verbose mode. Default: None ### Example ```python >>> import networkx as nx >>> from cdt.causality.graph import SAMv1 >>> from cdt.data import load_dataset >>> data, graph = load_dataset("sachs") >>> obj = SAMv1() >>> #The predict() method works without a graph, or with a >>> #directed or undirected graph provided as an input >>> output = obj.predict(data) #No graph provided as an argument >>> >>> output = obj.predict(data, nx.Graph(graph)) #With an undirected graph >>> >>> output = obj.predict(data, graph) #With a directed graph >>> >>> #To view the graph created, run the below commands: >>> nx.draw_networkx(output, font_size=8) >>> plt.show() ``` ``` -------------------------------- ### PairwiseDataset Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/utils/io.html Dataset class for pairwise methods. Can be overridden for more specific dataloaders. Provides methods to get length, get items, and produce a copy on a device. ```APIDOC ## PairwiseDataset ### Description Dataset class for pairwise methods. Can be overridden for more specific dataloaders. ### Constructor Parameters - **a** (array-like) - Required - Variable 1. - **b** (array-like) - Required - Variable 2. - **device** (str) - Optional - Device on which the data has to be sent. Data must be of type `torch.Tensor` if `device` is specified. - **flip** (bool) - Optional - Return the data in the reversed order. ### Methods #### __len__ Returns the length of the dataset. #### __getitem__ Returns the pair (a[index], b[index]) or (b[index], a[index]) if flip is True. #### to Produce a copy of the dataset on a device. ##### Parameters - **device** (str) - Required - Device on which the data has to be sent. - **flip** (bool) - Optional - Return the data in the reversed order. ##### Returns - **cdt.utils.io.PairwiseDataset** - The new dataset on device. ``` -------------------------------- ### GNN.__init__ Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/pairwise/GNN.html Initializes the GNN model with hyperparameters for the neural network, training, and execution. ```APIDOC ## GNN.__init__ ### Description Initializes the GNN model with specified hyperparameters. ### Method Constructor ### Parameters - **nh** (int) - Optional - Number of hidden units in the neural network. Default: 20. - **lr** (float) - Optional - Learning rate of the optimizer. Default: 0.01. - **nruns** (int) - Optional - Number of runs to execute per batch (before testing for significance with t-test). Default: 6. - **njobs** (int) - Optional - Number of runs to execute in parallel. Defaults to `cdt.SETTINGS.NJOBS`. - **gpus** (bool) - Optional - Number of available GPUs. Defaults to `cdt.SETTINGS.GPU`. - **verbose** (bool) - Optional - Verbosity. Defaults to `cdt.SETTINGS.verbose`. - **batch_size** (int) - Optional - Batch size, defaults to full-batch. Default: -1. - **train_epochs** (int) - Optional - Number of epochs used for training. Default: 1000. - **test_epochs** (int) - Optional - Number of epochs used for evaluation. Default: 1000. - **dataloader_workers** (int) - Optional - How many subprocesses to use for data loading. 0 means data loaded in main process. Default: 0. ### Returns None ``` -------------------------------- ### MomentMatchingLoss.__init__ Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/utils/loss.html Initializes the MomentMatchingLoss model with the number of moments to compute. ```APIDOC ## MomentMatchingLoss.__init__ ### Description Initializes the MomentMatchingLoss model with the number of moments to compute. ### Method Constructor ### Signature `__init__(self, n_moments=1)` ### Parameters - **n_moments** (int, optional) - Optional - Number of moments to compute. Defaults to 1. ### Returns None ``` -------------------------------- ### SID example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/metrics.html Computes the Structural Intervention Distance between two DAGs. The example creates upper triangular random matrices to ensure DAGs and calls SID. ```python >>> from cdt.metrics import SID >>> from numpy.random import randint >>> tar = np.triu(randint(2, size=(10, 10))) >>> pred = np.triu(randint(2, size=(10, 10))) >>> SID(tar, pred) ``` -------------------------------- ### SAM training loop setup and execution Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/SAM.html This code sets up the SAM model components (discriminator, graph sampler, optimizers) and runs the adversarial training loop. It handles different loss types (mse, gan, fgan), sample types (sigmoid, sigmoidproba, gumbleproba), and functional complexity measures (n_hidden_units, l2_norm). The loop trains the discriminator and generators, computes losses, and applies regularization. ```python if losstype != "mse": discriminator = SAM_discriminator(cols, dnh, numberHiddenLayersD, mask=sam.categorical_matrix,).to(device) discriminator.reset_parameters() d_optimizer = th.optim.Adam(discriminator.parameters(), lr=lr_disc) criterion = th.nn.BCEWithLogitsLoss() else: criterion = th.nn.MSELoss() disc_loss = th.zeros(1) if sampletype == "sigmoid": graph_sampler = SimpleMatrixConnection(len(list_nodes), mask=skeleton).to(device) elif sampletype == "sigmoidproba": graph_sampler = MatrixSampler(len(list_nodes), mask=skeleton, gumble=False).to(device) elif sampletype == "gumbleproba": graph_sampler = MatrixSampler(len(list_nodes), mask=skeleton, gumble=True).to(device) else: raise ValueError('Unknown Graph sampler') graph_sampler.weights.data.fill_(2) graph_optimizer = th.optim.Adam(graph_sampler.parameters(), lr=lr_gen) if not linear and functionalComplexity=="n_hidden_units": neuron_sampler = MatrixSampler((nh, len(list_nodes)), mask=False, gumble=True).to(device) neuron_optimizer = th.optim.Adam(list(neuron_sampler.parameters()),lr=lr_gen) _true = th.ones(1).to(device) _false = th.zeros(1).to(device) output = th.zeros(len(list_nodes), len(list_nodes)).to(device) data_iterator = DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True) # RUN if verbose: pbar = tqdm(range(train + test)) else: pbar = range(train+test) for epoch in pbar: for i_batch, batch in enumerate(data_iterator): if losstype != "mse": d_optimizer.zero_grad() # Train the discriminator drawn_graph = graph_sampler() if not linear and functionalComplexity=="n_hidden_units": drawn_neurons = neuron_sampler() if linear or functionalComplexity!="n_hidden_units": generated_variables = sam(batch, drawn_graph) else: generated_variables = sam(batch, drawn_graph, drawn_neurons) if losstype != "mse": disc_vars_d = discriminator(generated_variables.detach(), batch) true_vars_disc = discriminator(batch) if losstype == "gan": disc_loss = sum([criterion(gen, _false.expand_as(gen)) for gen in disc_vars_d]) / nb_var \ + criterion(true_vars_disc, _true.expand_as(true_vars_disc)) # Gen Losses per generator: multiply py the number of channels elif losstype == "fgan": disc_loss = th.mean(th.exp(disc_vars_d - 1), [0, 2]).sum() / nb_var - th.mean(true_vars_disc) disc_loss.backward() d_optimizer.step() ### OPTIMIZING THE GENERATORS g_optimizer.zero_grad() graph_optimizer.zero_grad() if not linear and functionalComplexity=="n_hidden_units": neuron_optimizer.zero_grad() if losstype == "mse": gen_loss = criterion(generated_variables, batch) else: disc_vars_g = discriminator(generated_variables, batch) if losstype == "gan": # Gen Losses per generator: multiply py the number of channels gen_loss = sum([criterion(gen, _true.expand_as(gen)) for gen in disc_vars_g]) elif losstype == "fgan": gen_loss = -th.mean(th.exp(disc_vars_g - 1), [0, 2]).sum() filters = graph_sampler.get_proba() struc_loss = lambda1*drawn_graph.sum() if linear : func_loss = 0 else : if functionalComplexity=="n_hidden_units": func_loss = lambda2*drawn_neurons.sum() elif functionalComplexity=="l2_norm": l2_reg = th.Tensor([0.]).to(device) for param in sam.parameters(): l2_reg += th.norm(param) func_loss = lambda2*l2_reg regul_loss = struc_loss + func_loss # Optional: prune edges and sam parameters before dag search if dagloss and epoch > train * dagstart: dag_constraint = notears_constr(filters*filters) #dag_constraint = notears_constr(drawn_graph) ``` -------------------------------- ### SpearmanCorrelation usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example of using SpearmanCorrelation to compute the Spearman correlation coefficient between two arrays. The predict method returns the test statistic as a float. ```python >>> from cdt.independence.stats import SpearmanCorrelation >>> obj = SpearmanCorrelation() >>> a = np.array([1, 2, 1, 5]) >>> b = np.array([1, 3, 0, 6]) >>> obj.predict(a, b) ``` -------------------------------- ### SAMv1.__init__ Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/_modules/cdt/causality/graph/SAMv1.html Initializes the SAMv1 model with hyperparameters for the generative and discriminative networks, regularization, training epochs, batch size, number of runs, and parallelization settings. ```APIDOC ## SAMv1.__init__ ### Description Initializes the SAMv1 model with hyperparameters for the generative and discriminative networks, regularization, training epochs, batch size, number of runs, and parallelization settings. ### Method Constructor ### Signature `__init__(self, lr=0.1, dlr=0.1, l1=0.1, nh=50, dnh=200, train_epochs=1000, test_epochs=1000, batch_size=-1, nruns=6, njobs=None, gpus=None, verbose=None)` ### Parameters - **lr** (float) - Optional - Learning rate for the generator. - **dlr** (float) - Optional - Learning rate for the discriminator. - **l1** (float) - Optional - L1 regularization parameter. - **nh** (int) - Optional - Number of hidden units in the generator. - **dnh** (int) - Optional - Number of hidden units in the discriminator. - **train_epochs** (int) - Optional - Number of training epochs. - **test_epochs** (int) - Optional - Number of test epochs. - **batch_size** (int) - Optional - Batch size; -1 indicates full batch. - **nruns** (int) - Optional - Number of runs for the algorithm. - **njobs** (int or None) - Optional - Number of parallel jobs; defaults to settings. - **gpus** (int or None) - Optional - Number of GPUs to use; defaults to settings. - **verbose** (bool or None) - Optional - Verbosity flag; defaults to settings. ### Returns None ``` -------------------------------- ### PearsonCorrelation usage example Source: https://fentechsolutions.github.io/CausalDiscoveryToolbox/html/independence.html Example of using PearsonCorrelation to compute the Pearson correlation coefficient between two arrays. The predict method returns the test statistic as a float. ```python >>> from cdt.independence.stats import PearsonCorrelation >>> obj = PearsonCorrelation() >>> a = np.array([1, 2, 1, 5]) >>> b = np.array([1, 3, 0, 6]) >>> obj.predict(a, b) ```