Skip to content

Instantly share code, notes, and snippets.

@akelleh
Created October 2, 2016 19:53
Show Gist options
  • Save akelleh/de5dc8eebec155cd0b55c3a46d2e35cd to your computer and use it in GitHub Desktop.
Save akelleh/de5dc8eebec155cd0b55c3a46d2e35cd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from causality.inference.search import IC\n",
"from causality.inference.independence_tests import RobustRegressionTest"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"SIZE = 2000\n",
"x1 = np.random.normal(size=SIZE)\n",
"x2 = x1 + np.random.normal(size=SIZE)\n",
"x3 = x1 + np.random.normal(size=SIZE)\n",
"x4 = x2 + x3 + np.random.normal(size=SIZE)\n",
"x5 = x4 + np.random.normal(size=SIZE)\n",
"\n",
"# load the data into a dataframe:\n",
"X = pd.DataFrame({'x1' : x1, 'x2' : x2, 'x3' : x3, 'x4' : x4, 'x5' : x5})\n",
"\n",
"# define the variable types: 'c' is 'continuous'. The variables defined here\n",
"# are the ones the search is performed over -- NOT all the variables defined\n",
"# in the data frame.\n",
"variable_types = {'x1' : 'c', 'x2' : 'c', 'x3' : 'c', 'x4' : 'c', 'x5' : 'c'}\n",
"\n",
"# run the search\n",
"ic_algorithm = IC(RobustRegressionTest, X, variable_types)\n",
"graph = ic_algorithm.search()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[('x2', 'x1', {'arrows': [], 'marked': False}),\n",
" ('x2', 'x4', {'arrows': ['x4'], 'marked': False}),\n",
" ('x3', 'x1', {'arrows': [], 'marked': False}),\n",
" ('x3', 'x4', {'arrows': ['x4'], 'marked': False}),\n",
" ('x4', 'x5', {'arrows': ['x5'], 'marked': True})]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph.edges(data=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"SIZE = 2000\n",
"x1 = np.random.normal(size=SIZE)\n",
"x2 = x1 + np.random.normal(size=SIZE)\n",
"x3 = x1 + np.random.normal(size=SIZE)\n",
"x6 = np.random.normal(size=SIZE)\n",
"x4 = x2 + x3 + x6 + np.random.normal(size=SIZE)\n",
"x5 = x6 + np.random.normal(size=SIZE)\n",
"\n",
"# load the data into a dataframe:\n",
"X = pd.DataFrame({'x1' : x1, 'x2' : x2, 'x3' : x3, 'x4' : x4, 'x5' : x5})\n",
"\n",
"# define the variable types: 'c' is 'continuous'. The variables defined here\n",
"# are the ones the search is performed over -- NOT all the variables defined\n",
"# in the data frame.\n",
"variable_types = {'x1' : 'c', 'x2' : 'c', 'x3' : 'c', 'x4' : 'c', 'x5' : 'c'}\n",
"\n",
"# run the search\n",
"ic_algorithm = IC(RobustRegressionTest, X, variable_types)\n",
"graph = ic_algorithm.search()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[('x2', 'x1', {'arrows': [], 'marked': False}),\n",
" ('x2', 'x4', {'arrows': ['x4', 'x4'], 'marked': False}),\n",
" ('x3', 'x1', {'arrows': [], 'marked': False}),\n",
" ('x3', 'x4', {'arrows': ['x4', 'x4'], 'marked': False}),\n",
" ('x4', 'x5', {'arrows': ['x4', 'x4'], 'marked': False})]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"graph.edges(data=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment