def pyfgs(d): ''' fast greedy equivalence search from pycausal https://github.com/bd2kccd/py-causal Interpretation reasoning is provided in paper linked in repo README ''' pc.start_vm() n = d.shape[0] d = np.asarray(np.r_[d[:, :-1], d[:, 1:]]) d = pd.DataFrame(d) d = d.T d.columns = [str(x + 1) + '*' for x in range(n)] + [str(x + 1) for x in range(n)] fgs = search.fges(d, penaltydiscount=2, faithfulnessAssumed=True, verbose=True) edg = fgs.edges print(edg) g2 = {} for i in np.arange(1, n + 1): g2[str(i)] = {} for i in edg: if re.match(r'[0-9]+\s.*[0-9]+\Z', i): y = re.findall(r'[0-9]+', i) if y[1] in g2[y[0]].keys(): g2[y[0]][y[1]].add((2, 0)) else: g2[y[0]][y[1]] = set([(2, 0)]) if y[0] in g2[y[1]].keys(): g2[y[1]][y[0]].add((2, 0)) else: g2[y[1]][y[0]] = set([(2, 0)]) elif re.match(r'[0-9]+\*.*[0-9]+\Z', i): y = re.findall(r'[0-9]+', i) if y[1] in g2[y[0]].keys(): g2[y[0]][y[1]].add((0, 1)) else: g2[y[0]][y[1]] = set([(0, 1)]) elif re.match(r'[0-9]+\s.*[0-9]+\*', i): y = re.findall(r'[0-9]+', i) if y[0] in g2[y[1]].keys(): g2[y[1]][y[0]].add((0, 1)) else: g2[y[1]][y[0]] = set([(0, 1)]) return conv.dict_format_converter(g2)
#!/usr/local/bin/python import os import pandas as pd import pydot from IPython.display import SVG data_dir = os.path.join(os.getcwd(), 'data', 'audiology.txt') df = pd.read_table(data_dir, sep="\t") from pycausal import pycausal as pc pc.start_vm(java_max_heap_size = '100M') from pycausal import search as s fges = s.fgesDiscrete(df,structurePrior = 1.0, samplePrior = 1.0, maxDegree = 3, faithfulnessAssumed = True, numOfThreads = 2, verbose = True) fges.getNodes() fges.getEdges() dot = fges.getDot() svg_str = dot.create_svg(prog='dot') f = open('fges-discrete.dot','w') f.write(svg_str) f.close()
#!/usr/local/bin/python import os import pandas as pd import pydot from IPython.display import SVG data_dir = os.path.join(os.getcwd(), 'data', 'charity.txt') df = pd.read_table(data_dir, sep="\t") from pycausal import pycausal as pc pc.start_vm() from pycausal import search as s fges = s.fges(df, penaltydiscount=2, maxDegree=-1, faithfulnessAssumed=True, verbose=True) fges.getNodes() fges.getEdges() dot = fges.getDot() svg_str = dot.create_svg(prog='dot') f = open('fges-continuous.dot', 'w') f.write(svg_str) f.close()
#!/usr/local/bin/python import os import pandas as pd import pydot from IPython.display import SVG data_dir = os.path.join(os.getcwd(), 'data', 'audiology.txt') df = pd.read_table(data_dir, sep="\t") from pycausal import pycausal as pc pc.start_vm(java_max_heap_size='100M') from pycausal import search as s fges = s.fgesDiscrete(df, structurePrior=1.0, samplePrior=1.0, maxDegree=3, faithfulnessAssumed=True, verbose=True) fges.getNodes() fges.getEdges() dot = fges.getDot() svg_str = dot.create_svg(prog='dot') f = open('fges-discrete.dot', 'w') f.write(svg_str)
#!/usr/local/bin/python import os import pandas as pd import pydot from IPython.display import SVG data_dir = os.path.join(os.getcwd(), 'data', 'charity.txt') df = pd.read_table(data_dir, sep="\t") from pycausal import pycausal as pc pc.start_vm() from pycausal import search as s fges = s.fges(df,penaltydiscount = 2, maxDegree = -1, faithfulnessAssumed = True, numofthreads = 2, verbose = True) fges.getNodes() fges.getEdges() dot = fges.getDot() svg_str = dot.create_svg(prog='dot') f = open('fges-continuous.dot','w') f.write(svg_str) f.close() pc.stop_vm()