Skip to content

Commit

Permalink
Merge branch 'master' into new_official_master
Browse files Browse the repository at this point in the history
  • Loading branch information
Ewen Gillies authored Feb 20, 2019
2 parents b95b466 + 34178bf commit 3fadf67
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 881 deletions.
22 changes: 19 additions & 3 deletions docs/_modules/tigramite/plotting.html
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,17 @@ <h1>Source code for tigramite.plotting</h1><div class="highlight"><pre>
<span class="n">alpha</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_alpha&#39;</span><span class="p">]</span>
<span class="n">arrowstyle</span> <span class="o">=</span> <span class="s1">&#39;simple,head_length=0.0001&#39;</span>
<span class="n">link_edge</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_edge&#39;</span><span class="p">]</span>
<span class="n">linestyle</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_style&#39;</span><span class="p">]</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">linestyle</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_style&#39;</span><span class="p">]</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">linestyle</span> <span class="o">=</span> <span class="s1">&#39;solid&#39;</span>
<span class="n">linewidth</span> <span class="o">=</span> <span class="mf">0.</span>
<span class="k">if</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_attribute&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;spurious&#39;</span><span class="p">:</span>
<span class="n">facecolor</span> <span class="o">=</span> <span class="s1">&#39;grey&#39;</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">if</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;undirected_attribute&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;spurious&#39;</span><span class="p">:</span>
<span class="n">facecolor</span> <span class="o">=</span> <span class="s1">&#39;grey&#39;</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
<span class="c1"># facecolor = </span>
<span class="c1"># linestyle = &#39;dashed&#39;</span>

<span class="c1"># print d[&#39;undirected_attribute&#39;]</span>
Expand Down Expand Up @@ -1464,6 +1471,9 @@ <h1>Source code for tigramite.plotting</h1><div class="highlight"><pre>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># Node color is max of average autodependency</span>
<span class="n">node_color</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="n">val_matrix</span><span class="p">[</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">][</span><span class="n">argmax</span><span class="p">]</span>
<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;undirected_attribute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_attribute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>


<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_edge&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_edgecolor&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
Expand Down Expand Up @@ -1699,6 +1709,8 @@ <h1>Source code for tigramite.plotting</h1><div class="highlight"><pre>
<span class="c1"># Add attributes, contemporaneous and directed links are handled separately</span>
<span class="k">for</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">dic</span><span class="p">)</span> <span class="ow">in</span> <span class="n">G</span><span class="o">.</span><span class="n">edges</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>

<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_attribute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

<span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>

<span class="k">if</span> <span class="n">u</span> <span class="o">%</span> <span class="n">max_lag</span> <span class="o">==</span> <span class="n">v</span> <span class="o">%</span> <span class="n">max_lag</span><span class="p">:</span>
Expand Down Expand Up @@ -1986,6 +1998,8 @@ <h1>Source code for tigramite.plotting</h1><div class="highlight"><pre>
<span class="c1"># Add attributes, contemporaneous and directed links are handled separately</span>
<span class="k">for</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">dic</span><span class="p">)</span> <span class="ow">in</span> <span class="n">G</span><span class="o">.</span><span class="n">edges</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>

<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_attribute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

<span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>

<span class="k">if</span> <span class="n">u</span> <span class="o">%</span> <span class="n">max_lag</span> <span class="o">==</span> <span class="n">v</span> <span class="o">%</span> <span class="n">max_lag</span><span class="p">:</span>
Expand Down Expand Up @@ -2279,6 +2293,8 @@ <h1>Source code for tigramite.plotting</h1><div class="highlight"><pre>
<span class="n">all_strengths</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Add attributes, contemporaneous and directed links are handled separately</span>
<span class="k">for</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">dic</span><span class="p">)</span> <span class="ow">in</span> <span class="n">G</span><span class="o">.</span><span class="n">edges</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
<span class="n">dic</span><span class="p">[</span><span class="s1">&#39;directed_attribute&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

<span class="c1"># average lagfunc for link u --&gt; v ANDOR u -- v</span>
<span class="k">if</span> <span class="n">tau_max</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="c1"># argmax of absolute maximum</span>
Expand Down
288 changes: 3 additions & 285 deletions tigramite/independence_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2421,176 +2421,8 @@ class RCOT(CondIndTest):
features. To get reproducable results, you should fix the seed (default).
This class requires the rpy package and the prior installation of ``rcit``
from https://github.com/ericstrobl/RCIT.
References
----------
.. [5] Eric V. Strobl, Kun Zhang, Shyam Visweswaran:
Approximate Kernel-based Conditional Independence Tests for Fast Non-
Parametric Causal Discovery.
https://arxiv.org/abs/1702.03877
Parameters
----------
num_f : int, optional
Number of random fourier features for conditioning set. More features
better approximate highly structured joint densities, but take more
computational time.
approx : str, optional
Which approximation of the partial cross-covariance matrix, options:
'lpd4' the Lindsay-Pilla-Basak method (default), 'gamma' for the
Satterthwaite-Welch method, 'hbe' for the Hall-Buckley-Eagleson method,
'chi2' for a normalized chi-squared statistic, 'perm' for permutation
testing (warning: this one is slow).
seed : int or None, optional
Which random fourier feature seed to use. If None, you won't get
reproducable results.
significance : str, optional (default: 'analytic')
Type of significance test to use.
**kwargs :
Arguments passed on to parent class CondIndTest.
"""
def __init__(self,
num_f=25,
approx="lpd4",
seed=42,
significance='analytic',
**kwargs):

self.num_f = num_f
self.approx = approx
self.seed = seed

self.measure = 'rcot'
self.two_sided = False
self.residual_based = False
self.recycle_residuals = False

CondIndTest.__init__(self, significance=significance, **kwargs)

if self.verbosity > 0:
print("num_f = %s" % self.num_f)
print("approx = %s" % self.approx)
print("")

def get_dependence_measure(self, array, xyz):
"""Returns RCOT estimate.
Parameters
----------
array : array-like
data array with X, Y, Z in rows and observations in columns
xyz : array of ints
XYZ identifier array of shape (dim,).
Returns
-------
val : float
RCOT estimate.
"""

dim, T = array.shape

x = array[0]
y = array[1]
z = numpy.fastCopyAndTranspose(array[2:])

rcot = numpy.asarray(rpy2.robjects.r['RCIT'](x, y, z,
corr=True,
num_f=self.num_f,
approx=self.approx,
seed=self.seed))

val = float(rcot[1])
self.pval = float(rcot[0])

return val


def get_analytic_significance(self, **args):
"""Returns analytic p-value from RCIT test statistic.
Returns
-------
pval : float or numpy.nan
P-value.
"""

return self.pval

def get_shuffle_significance(self, array, xyz, value,
return_null_dist=False):
"""Returns p-value for shuffle significance test.
For residual-based test statistics only the residuals are shuffled.
Parameters
----------
array : array-like
data array with X, Y, Z in rows and observations in columns
xyz : array of ints
XYZ identifier array of shape (dim,).
value : number
Value of test statistic for unshuffled estimate.
Returns
-------
pval : float
p-value
"""

null_dist = self._get_shuffle_dist(array, xyz,
self.get_dependence_measure,
sig_samples=self.sig_samples,
sig_blocklength=self.sig_blocklength,
verbosity=self.verbosity)

pval = (null_dist >= value).mean()

if return_null_dist:
return pval, null_dist
else:
return pval

def get_analytic_confidence(self, value, df, conf_lev):
"""Placeholder function, not available."""
raise ValueError("Analytic confidence not implemented for %s"
"" % self.measure)

def get_model_selection_criterion(self, j,
parents,
tau_max=0):
"""Placeholder function, not available."""
raise ValueError("Model selection not implemented for %s"
"" % self.measure)


if __name__ == '__main__':

# Quick test
import data_processing as pp
numpy.random.seed(44)
a = 0.
c = 0.6
T = 4000
# Each key refers to a variable and the incoming links are supplied as a
# list of format [((driver, lag), coeff), ...]
links_coeffs = {0: [((0, -1), a)],
1: [((1, -1), a), ((0, -1), c)],
2: [((2, -1), a), ((1, -1), c)],
}
=======

This class requires the rpy package and the prior installation of ``rcit``
from https://github.com/ericstrobl/RCIT.
>>>>>>> 4049fb9e43bfa8152ff5b93734b676e027695575
from https://github.com/ericstrobl/RCIT. This is provided with tigramite
as an external package.
References
----------
Expand Down Expand Up @@ -2727,118 +2559,4 @@ def get_shuffle_significance(self, array, xyz, value,

if return_null_dist:
return pval, null_dist
return pval


if __name__ == '__main__':

# import sys

# np.random.seed(30)
# T = 10
# shuffle_neighbors = 3
# neighbors = np.zeros((T, shuffle_neighbors))
# order = np.random.permutation(T).astype('int32')

# for i in range(T):
# neighbors[i] = np.random.permutation(T)[:shuffle_neighbors]
# neighbors = neighbors.astype('int32')

# print (order)
# print (neighbors)

# print(neighbors.shape)

# p = tigramite_cython_code._get_restricted_permutation_cython(
# T=T,
# shuffle_neighbors=shuffle_neighbors,
# neighbors=neighbors,
# order=order)
# print(p)

# sys.exit(0)

# Quick test
import data_processing as pp
# np.random.seed(44)
a = 0.
c = 0.1
d = 0.9
T = 500
# Each key refers to a variable and the incoming links are supplied as a
# list of format [((driver, lag), coeff), ...]
links_coeffs = {0: [((0, -1), a)],
1: [((1, -1), a), ((0, -1), d)],
2: [((2, -1), a), ((0, -2), d), ((1, -1), c)],
}

data, true_parents_neighbors = pp.var_process(links_coeffs, T=T)

data_mask = np.zeros(data.shape)

cond_ind_test = CMIknn(
significance='shuffle_test',
sig_samples=1000,
knn=.1,
transform='ranks',
shuffle_neighbors=5,
confidence=False, #'bootstrap',
conf_lev=0.9,
conf_samples=1000,
conf_blocklength=None,
verbosity=0)

# cond_ind_test = RCOT(significance='analytic')

data = pp.quantile_bin_array(data, bins=2) #.astype('int64')
# cond_ind_test = CMIsymb(sig_samples=1000)
# cond_ind_test = GPDC()

<<<<<<< HEAD

# cond_ind_test = RCOT(
# significance='analytic',
# num_f=25,
# confidence=False, #'bootstrap', #'bootstrap',
# conf_lev=0.9,
# conf_samples=100,
# conf_blocklength=None,

# use_mask=False,
# mask_type='y',
# recycle_residuals=False,
# verbosity=3,
# )

if cond_ind_test.measure == 'cmi_symb':
data = pp.quantile_bin_array(data, bins=6)
=======
# cond_ind_test = ParCorr()
>>>>>>> 4049fb9e43bfa8152ff5b93734b676e027695575

dataframe = pp.DataFrame(data)
cond_ind_test.set_dataframe(dataframe)

tau_max = 5
X = [(1, -1)]
Y = [(2, 0)]
<<<<<<< HEAD
Z = [(1, -1)] #(2, -1), (1, -1), (0, -3)] #[(1, -1)] #[(2, -1), (1, -1), (0, -3)] # [(2, -1), (1, -1), (2, -3)] [(1, -1)]

print cond_ind_test._get_shuffle_dist
=======
Z = [(0, -2)] #,(0, -3),(0, -4)] #(2, -1), (1, -1), (0, -3)] #[(1, -1)] #[(2, -1), (1, -1), (0, -3)] # [(2, -1), (1, -1), (2, -3)] [(1, -1)]

# print cond_ind_test._get_shuffle_dist
>>>>>>> 4049fb9e43bfa8152ff5b93734b676e027695575

val, pval = cond_ind_test.run_test(X, Y, Z, tau_max=tau_max)
conf_interval = cond_ind_test.get_confidence(X, Y, Z, tau_max=tau_max)

# print cond_ind_test.get_model_selection_criterion(2,
# [(0, -2)],
# tau_max=tau_max)

print ("I(X,Y|Z) = %.4f | p-value = %.3f " % (val, pval))
if conf_interval is not None:
print ("[%.2f, %.2f]" % conf_interval)
return pval
9 changes: 9 additions & 0 deletions tigramite/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,9 @@ def plot_graph(val_matrix,
else:
# Node color is max of average autodependency
node_color[u] = val_matrix[u, v][argmax]
dic['undirected_attribute'] = None
dic['directed_attribute'] = None


dic['directed_edge'] = False
dic['directed_edgecolor'] = None
Expand Down Expand Up @@ -1624,6 +1627,8 @@ def translate(row, lag):
# Add attributes, contemporaneous and directed links are handled separately
for (u, v, dic) in G.edges(data=True):

dic['directed_attribute'] = None

if u != v:

if u % max_lag == v % max_lag:
Expand Down Expand Up @@ -1911,6 +1916,8 @@ def translate(row, lag):
# Add attributes, contemporaneous and directed links are handled separately
for (u, v, dic) in G.edges(data=True):

dic['directed_attribute'] = None

if u != v:

if u % max_lag == v % max_lag:
Expand Down Expand Up @@ -2204,6 +2211,8 @@ def plot_mediation_graph(
all_strengths = []
# Add attributes, contemporaneous and directed links are handled separately
for (u, v, dic) in G.edges(data=True):
dic['directed_attribute'] = None

# average lagfunc for link u --> v ANDOR u -- v
if tau_max > 0:
# argmax of absolute maximum
Expand Down
Loading

0 comments on commit 3fadf67

Please sign in to comment.