In [267]:
import readline
import rpy2.robjects
import code.independence_test as it
import code.additive_noise as an
import numpy as np
import numpy.random as rn
import code.hsic as hs
import pandas as pd

In [268]:
n=250
cause = rn.uniform(0,10,n)
indep = rn.uniform(0,10,n)

n_Y = np.random.uniform(-1,1,n)
n_L = np.random.uniform(-1,1,n)
n_C = np.random.uniform(-1,1,n)

In [269]:
Y = np.log(cause) + n_Y 
confounded = np.log(cause) + n_C
leaked = Y**2 + n_L

In [270]:
X = np.stack([cause, indep, confounded, leaked],axis=1)
X = pd.DataFrame(X, columns=['cause', 'indep', 'confounded', 'leaked'])

In [271]:
Y[0:10]


Out[271]:
array([-1.2285647 ,  0.94596792,  2.13165018,  1.49199779, -0.66854162,
        2.56994782,  1.15495431,  2.63818878,  0.33733907,  2.5795691 ])

In [272]:
X.head()


Out[272]:
cause indep confounded leaked
0 0.219043 1.567766 -1.499400 2.130497
1 3.411820 0.944898 0.737476 0.450120
2 9.513069 3.038724 3.132322 4.678155
3 2.386470 1.142832 1.189391 3.099337
4 0.822754 0.301655 -0.995328 0.406533

In [273]:
e = an.ANM_algorithm_with_test(X,Y, 'py', test='mi-g-sh')


Number of line searches 40
Number of line searches 40
Number of line searches 40
Number of line searches 40
Number of line searches 40
Number of line searches 40
['cause'] 2.02022978633e-10
['indep'] 1.05073264159e-34
['leaked'] 9.37036151922e-39
['cause', 'indep'] 3.16233015318e-10
['cause', 'leaked'] 7.39449057411e-16
['indep', 'leaked'] 6.63048328346e-32
['cause', 'indep', 'leaked'] 3.7867038201e-15
Number of line searches 40
Number of line searches 40
['cause'] 2.22298081076e-06
['indep'] 6.22865689541e-21
['confounded'] 5.54478650181e-22
['cause', 'indep'] 2.11216349294e-06
['cause', 'confounded'] 6.86366186717e-12
['indep', 'confounded'] 2.80908201314e-18
['cause', 'indep', 'confounded'] 2.41792446901e-11

In [274]:
e


Out[274]:
Direction Rank P_value Independence X and Y
indep Independent 0.00342176 0.148439
leaked Leakage 0.00230293 1.6922e-79
confounded Leakage 0.000538001 2.65347e-18
cause No Leakage -0.0132915 4.82163e-36

In [141]:
e.ix['confounded'].Direction


Out[141]:
"Column: confounded is independent of target given ['cause']"

In [ ]: