Skip to content

Commit

Permalink
Merge pull request #361 from jakobrunge/developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
jakobrunge authored Aug 31, 2023
2 parents 9b6b931 + 60fbf03 commit eda6ceb
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 5 deletions.
98 changes: 98 additions & 0 deletions tests/test_PairwiseMultCI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
Tests for independence_tests.py.
"""
from __future__ import print_function
import numpy as np
import pytest
from scipy import stats

from tigramite.independence_tests.parcorr import ParCorr
from tigramite.independence_tests.robust_parcorr import RobustParCorr
from tigramite.independence_tests.pairwise_CI import PairwiseMultCI
from tigramite.independence_tests.cmiknn import CMIknn
from tigramite.independence_tests.regressionCI import RegressionCI
# PairwiseMultCI TESTING ##################################################
@pytest.fixture(params=[
# Generate PairwiseMultCI test instances
# test PairwiseMultCI for different combinations of:
# significance (of the univariate tests), alpha_pre, pre_step_sample_fraction, cond_ind_test, cond_ind_test_thres, cond_ind_test_thres_pre
('fixed_thres', 0.5, 0.2, ParCorr, 1, 0.5),
('fixed_thres', 0.5, 0.5, ParCorr, 1, 0.5),
('fixed_thres', 0.8, 0.2, ParCorr, 1, 0.5),
('fixed_thres', 0.8, 0.5, ParCorr, 1, 0.5),
('analytic', 0.5, 0.2, ParCorr, None, None),
('shuffle_test', 0.5, 0.2, ParCorr, None, None),
('analytic', 0.5, 0.5, ParCorr, None, None),
('shuffle_test', 0.5, 0.5, ParCorr, None, None),
('analytic', 0.8, 0.2, ParCorr, None, None),
('shuffle_test', 0.8, 0.2, ParCorr, None, None),
('analytic', 0.8, 0.5, ParCorr, None, None),
('shuffle_test', 0.8, 0.5, ParCorr, None, None),
('analytic', 0.5, 0.2, RobustParCorr, None, None),
('shuffle_test', 0.5, 0.2, RobustParCorr, None, None),
('analytic', 0.5, 0.5, RobustParCorr, None, None),
('shuffle_test', 0.5, 0.5, RobustParCorr, None, None),
('analytic', 0.8, 0.2, RobustParCorr, None, None),
('shuffle_test', 0.8, 0.2, RobustParCorr, None, None),
('analytic', 0.8, 0.5, RobustParCorr, None, None),
('shuffle_test', 0.8, 0.5, RobustParCorr, None, None),
('shuffle_test', 0.5, 0.2, CMIknn, None, None),
('shuffle_test', 0.5, 0.5, CMIknn, None, None),
('shuffle_test', 0.8, 0.2, CMIknn, None, None),
('shuffle_test', 0.8, 0.5, CMIknn, None, None),
])

def pairwise_mult_ci(request):
# Unpack the parameters
sig, alpha_pre, pre_step_sample_fraction, cond_ind_test, cond_ind_test_thres, cond_ind_test_thres_pre = request.param
# Generate the par_corr_wls independence test
if sig != "fixed_thres":
return PairwiseMultCI(cond_ind_test = cond_ind_test(significance = sig),
alpha_pre = alpha_pre,
pre_step_sample_fraction = pre_step_sample_fraction)
else:
return PairwiseMultCI(cond_ind_test = cond_ind_test(significance = sig),
alpha_pre = None,
pre_step_sample_fraction=pre_step_sample_fraction,
significance= sig,
fixed_thres_pre = 2
)

@pytest.fixture(params=[
# Generate PairwiseMultCI test instances
# test PairwiseMultCI for different combinations of:
# seed, true_dep, T (=sample size)
(123, 0, 100),
(123, 0, 1000),
(123, 0.2, 100),
(123, 0.2, 1000),
(123, 0.5, 100),
(123, 0, 1000),
(46, 0, 100),
(46, 0, 1000),
(46, 0.2, 100),
(46, 0.2, 1000),
(46, 0.5, 100),
(46, 0, 1000),
])

def data_sample_c_cc_c(request):
# Set the random seed
seed, true_dep, T = request.param
np.random.seed(seed)
x = np.random.normal(0, 1, T).reshape(T, 1)
y1 = np.random.normal(0, 1, T).reshape(T, 1)
y2 = true_dep * x + y1 + 0.3 * np.random.normal(0, 1, T).reshape(T, 1)
y = np.hstack((y1,y2))
z = np.random.normal(0, 1, T).reshape(T, 1)
# Return data xyz
return x, y, z

def test_pairwise_mult_ci(pairwise_mult_ci, data_sample_c_cc_c):
# Get the data sample values
x, y, z = data_sample_c_cc_c
# Get the analytic significance
test_result = pairwise_mult_ci.run_test_raw(x = x, y = y, z = z, alpha_or_thres=1)
val = test_result[0]
pval = test_result[1]
np.testing.assert_allclose(pval, 0.5, atol=0.5)
3 changes: 3 additions & 0 deletions tigramite/independence_tests/gpdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class GaussProcReg():
a \*.npz file containing the null distribution for different sample sizes.
This file can then be supplied as null_dist_filename.
Assumes one-dimensional X, Y. But can be combined with PairwiseMultCI to
obtain a test for multivariate X, Y.
Parameters
----------
null_samples : int
Expand Down
3 changes: 3 additions & 0 deletions tigramite/independence_tests/gpdc_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class GaussProcRegTorch():
a \*.npz file containing the null distribution for different sample sizes.
This file can then be supplied as null_dist_filename.
Assumes one-dimensional X, Y. But can be combined with PairwiseMultCI to
obtain a test for multivariate X, Y.
Parameters
----------
null_samples : int
Expand Down
3 changes: 2 additions & 1 deletion tigramite/independence_tests/gsquared.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Gsquared(CondIndTest):
above 1000-2000 samples (depends on data). For smaller sample sizes use the
CMIsymb class which includes a local permutation test.
Assumes one-dimensional X, Y.
Assumes one-dimensional X, Y. But can be combined with PairwiseMultCI to
obtain a test for multivariate X, Y.
This method requires the scipy.stats package.
Expand Down
Loading

0 comments on commit eda6ceb

Please sign in to comment.