DirectLiNGAM en Python

DirectLiNGAM (avec bootstrapping) en Python

Notes et mémorandum

table des matières

◆ Présentation ◆ Environnement ◆ Procédure ◆ 3 variables --Préparation

introduction

J'ai estimé les données de simulation en utilisant le package lingam implémenté la dernière fois.

LiNGAM en Python https://qiita.com/kumalpha/items/f05bd031cf9daac464a0

environnement

OS: Mojave (version; 10.14.6) Python: 3.7.6 JupyterLab: 1.2.6

procédure

  1. Préparation
  2. Génération de données
  3. Sangle de démarrage
  4. Confirmation de l'orientation
  5. Confirmation du DAG

3 variables

Préparation

# DirectLiNGAM
# Import and sets
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)
['1.18.1', '1.0.1', '0.13.2', '1.2.1']

Génération de données

# Create test data
x0 = np.random.uniform(size=10000)
x1 = 3.0*x0 + np.random.uniform(size=10000)
x2 = 5.0*x0 + 0.5*x1 + np.random.uniform(size=10000)
X = pd.DataFrame(np.array([x0, x1, x2]).T,columns=['x0', 'x1', 'x2'])
X.head()
	x0	x1	x2
0	0.758125	2.643633	5.420089
1	0.503319	1.721282	3.439239
2	0.177017	1.007955	2.377846
3	0.832537	2.579844	6.171695
4	0.516825	1.788134	4.235760
# Visualize the test data
m = np.array([[0.0, 0.0, 0.0],
			[3.0, 0.0, 0.0],
			[5.0, 0.5, 0.0]])
make_dot(m)

Cette relation causale sera estimée à l'aide de la méthode bootstrap.

Amorcer

model = lingam.DirectLiNGAM()
result = model.bootstrap(X, 3000) # Number of bootstrapping samples
cdc = result.get_causal_direction_counts(n_directions=10, min_causal_effect=0.1)

Confirmation d'orientation

from lingam.utils import print_causal_directions
print_causal_directions(cdc, 3000)
x1 <--- x0  (100.0%)
x2 <--- x0  (100.0%)
x2 <--- x1  (100.0%)

Puisqu'il s'agit de données simples, la relation causale était claire à ce stade.

Confirmation du DAG

À l'étape précédente, nous examinions simplement la relation univoque entre les variables. Cette fois, nous les intégrerons dans un DAG.

dagc = result.get_directed_acyclic_graph_counts(n_dags=5, min_causal_effect=0.1)
from lingam.utils import print_dagc
print_dagc(dagc, 3000)
DAG[0]: 100.0%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 

J'ai pu bien estimer l'orientation. Je l'avais configuré pour produire jusqu'à 5 candidats DAG, mais probablement parce qu'il était de 100,0%, le reste n'était pas sorti.

# Get the probability of bootstrapping.
prob = result.get_probabilities(min_causal_effect=0.1)
print(prob)
[[0. 0. 0.]
 [1. 0. 0.]
 [1. 1. 0.]]

La probabilité était également sortie comme 1.

7 variables

Cette relation causale a été estimée avec une certaine complexité.

Préparation

# DirectLiNGAM
# Import and sets
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)
['1.18.1', '1.0.1', '0.13.2', '1.2.1']

Génération de données

# Create test data
x0 = np.random.uniform(size=10000)
x6 = np.random.uniform(size=10000)
x1 = -5.0*x0 + np.random.uniform(size=10000)
x2 = -2.5*x0 + 3.0*x1 + np.random.uniform(size=10000)
x5 = 6.0*x6 + np.random.uniform(size=10000)
x3 = 4.0*x2 + 7.0*x5 + np.random.uniform(size=10000)
x4 = 1.0*x1 + 2.0*x2 + 8.0*x6 +np.random.uniform(size=10000)

X = pd.DataFrame(np.array([x0, x1, x2, x3, x4, x5, x6]).T,columns=['x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6'])
X.head()
	x0	x1	x2	x3	x4	x5	x6
0	0.548814	-2.351895	-7.669592	3.641327	-10.776980	4.858864	0.748268
1	0.715189	-3.534790	-11.889026	-38.446302	-24.968283	1.292542	0.180203
2	0.602763	-2.090516	-7.601441	-9.739672	-13.753596	2.811044	0.389023
3	0.544883	-2.318181	-7.484214	-27.062919	-16.475002	0.307835	0.037600
4	0.423655	-1.173992	-4.064288	-13.340880	-8.625065	0.308386	0.011788
# Visualize the test data
m = np.array([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
			[-5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
			[-2.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0],
			[0.0, 0.0, 4.0, 0.0, 0.0, 7.0, 0.0],
			[0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 8.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 6.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]])
make_dot(m)

Cette relation causale sera estimée à l'aide de la méthode bootstrap.

Amorcer

model = lingam.DirectLiNGAM()
result = model.bootstrap(X, 3000) # Number of bootstrapping samples
cdc = result.get_causal_direction_counts(n_directions=10, min_causal_effect=0.1)

Confirmation d'orientation

from lingam.utils import print_causal_directions
print_causal_directions(cdc, 3000)
x1 <--- x0  (100.0%)
x2 <--- x0  (100.0%)
x2 <--- x1  (100.0%)
x3 <--- x5  (100.0%)
x4 <--- x1  (100.0%)
x4 <--- x2  (100.0%)
x5 <--- x6  (100.0%)
x4 <--- x6  (97.4%)
x3 <--- x2  (96.1%)
x3 <--- x4  (4.8%)

Confirmation du DAG

dagc = result.get_directed_acyclic_graph_counts(n_dags=5, min_causal_effect=0.1)
from lingam.utils import print_dagc
print_dagc(dagc, 3000)
DAG[0]: 88.5%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 
	x3 <--- x2 
	x3 <--- x5 
	x4 <--- x1 
	x4 <--- x2 
	x4 <--- x6 
	x5 <--- x6 
DAG[1]: 3.9%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 
	x3 <--- x4 
	x3 <--- x5 
	x4 <--- x1 
	x4 <--- x2 
	x4 <--- x6 
	x5 <--- x6 
DAG[2]: 2.7%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 
	x3 <--- x2 
	x3 <--- x5 
	x4 <--- x0 
	x4 <--- x1 
	x4 <--- x2 
	x4 <--- x6 
	x5 <--- x6 
DAG[3]: 2.6%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 
	x3 <--- x2 
	x3 <--- x5 
	x4 <--- x1 
	x4 <--- x2 
	x4 <--- x3 
	x5 <--- x6 
DAG[4]: 0.9%
	x1 <--- x0 
	x2 <--- x0 
	x2 <--- x1 
	x3 <--- x2 
	x3 <--- x4 
	x3 <--- x5 
	x4 <--- x1 
	x4 <--- x2 
	x4 <--- x6 
	x5 <--- x6 

Une relation causale précise pourrait être estimée à 88,5%.

prob = result.get_probabilities(min_causal_effect=0.1)
print(prob)
[[0.    0.    0.    0.    0.    0.    0.   ]
 [1.    0.    0.    0.    0.    0.    0.   ]
 [1.    1.    0.    0.    0.    0.    0.   ]
 [0.006 0.    0.961 0.    0.048 1.    0.007]
 [0.027 1.    1.    0.026 0.    0.    0.974]
 [0.    0.    0.    0.    0.    0.    1.   ]
 [0.    0.    0.    0.    0.    0.    0.   ]]

référence

・ LiNGAM (version ICA) pour comprendre avec des formules mathématiques et Python https://qiita.com/k-kotera/items/6d7f5598464e18afaa7c ・ Inférence causale par modèle d'équation structurelle: développements récents dans la recherche de structure causale https://www.slideshare.net/sshimizu2006/bsj2012-tutorial-finalweb ・ LiNGAM en Python https://qiita.com/kumalpha/items/f05bd031cf9daac464a0 ・ Documents LiNGAM https://lingam.readthedocs.io/en/latest/index.html ・ Lingam GitHub (exemples) https://github.com/cdt15/lingam/tree/master/examples

Recommended Posts

DirectLiNGAM en Python
Quadtree en Python --2
Python en optimisation
CURL en Python
Géocodage en python
SendKeys en Python
Méta-analyse en Python
Unittest en Python
Époque en Python
Discord en Python
Allemand en Python
DCI en Python
tri rapide en python
nCr en python
N-Gram en Python
Programmation avec Python
Plink en Python
Constante en Python
FizzBuzz en Python
Sqlite en Python
Étape AIC en Python
LINE-Bot [0] en Python
CSV en Python
Réflexion en Python
Constante en Python
nCr en Python.
format en python
Scons en Python 3
Puyopuyo en python
python dans virtualenv
PPAP en Python
Quad-tree en Python
Réflexion en Python
Chimie avec Python
Hashable en Python
LiNGAM en Python
Aplatir en Python
Aplatir en python
Liste triée en Python
AtCoder # 36 quotidien avec Python
Texte de cluster en Python
AtCoder # 2 tous les jours avec Python
Daily AtCoder # 32 en Python
Daily AtCoder # 6 en Python
Daily AtCoder # 18 en Python
Modifier les polices en Python
Motif singleton en Python
Lire DXF avec python
Daily AtCoder # 53 en Python
Utilisez config.ini avec Python
Daily AtCoder # 33 en Python
Résoudre ABC168D en Python
Distribution logistique en Python
AtCoder # 7 tous les jours avec Python
Décomposition LU en Python
Une doublure en Python
GRPC simple en Python
AtCoder # 24 tous les jours avec Python
Résolvez ABC167-D avec Python
Daily AtCoder # 37 en Python