import numpy as np
import pandas as pd
import graphviz
import lingam
from lingam.utils import make_dot

print([np.__version__, pd.__version__, graphviz.__version__, lingam.__version__])

np.set_printoptions(precision=3, suppress=True)
np.random.seed(0)

"""
Explanation: DirectLiNGAM by Kernel Method
Import and settings
In this example, we need to import numpy, pandas, and graphviz in addition to lingam.
End of explanation
"""


n = 1000
e = lambda n: np.random.laplace(0, 1, n)
x3 = e(n)
x2 = 0.3*x3 + e(n)
x1 = 0.3*x3 + 0.3*x2 + e(n)
x0 = 0.3*x2 + 0.3*x1 + e(n)
x4 = 0.3*x1 + 0.3*x0 + e(n)
X = pd.DataFrame(np.array([x0, x1, x2, x3, x4]).T ,columns=['x0', 'x1', 'x2', 'x3', 'x4'])
X.head()

m = np.array([[0.0, 0.3, 0.3, 0.0, 0.0],
              [0.0, 0.0, 0.3, 0.3, 0.0],
              [0.0, 0.0, 0.0, 0.3, 0.0],
              [0.0, 0.0, 0.0, 0.0, 0.0],
              [0.3, 0.3, 0.0, 0.0, 0.0]])

make_dot(m)

"""
Explanation: Test data
We create test data consisting of 6 variables.
End of explanation
"""


model = lingam.DirectLiNGAM(measure='kernel')
model.fit(X)

"""
Explanation: Causal Discovery
To run causal discovery, we create a DirectLiNGAM object by specifying 'kernel' in the measure parameter. Then, we call the fit method.
End of explanation
"""


model.causal_order_

"""
Explanation: Using the causal_order_ properties, we can see the causal ordering as a result of the causal discovery.
End of explanation
"""


model.adjacency_matrix_

"""
Explanation: Also, using the adjacency_matrix_ properties, we can see the adjacency matrix as a result of the causal discovery.
End of explanation
"""


make_dot(model.adjacency_matrix_)

"""
Explanation: We can draw a causal graph by utility funciton.
End of explanation
"""


