<jupyter_start><jupyter_text>pcalg G2検定・G2検定はデータを離散化する必要がある  ・データの離散化にはpandasのcutが使える  ・離散化する際のカテゴリ数をデータ点数から決定する「スタージェスの公式」<jupyter_code>data_size = 100
cpu_usage = np.random.uniform(0, 100, data_size)
df = pd.DataFrame({"cpu-usage": cpu_usage})
df.head()
# スタージェスの公式でビン数を決定し，離散化する
n_bins = int(np.log2(data_size) + 1)
print("Number of bins: {}".format(n_bins))
discrete_data = pd.cut(df["cpu-usage"], n_bins, labels=np.arange(0, n_bins)) #retbins=True)
discrete_data
df["discrete-cpu-usage"] = discrete_data
df.head()
# pcalgを使ってみる
import pcalg
import networkx as nx
import numpy as np
from gsq.ci_tests import ci_test_dis
from gsq.gsq_testdata import dis_data

dm = np.array(dis_data).reshape((10000, 5))
(g, sep_set) = pcalg.estimate_skeleton(indep_test_func=ci_test_dis,
                                 data_matrix=dm,
                                 alpha=0.01,
                                 levels=[3,2,3,4,2])
g = pcalg.estimate_cpdag(skel_graph=g, sep_set=sep_set)
g_answer = nx.DiGraph()
g_answer.add_nodes_from([0, 1, 2, 3, 4])
g_answer.add_edges_from([(0, 2), (1, 2), (1, 3), (4, 3)])
print('Edges are:', g.edges(), end='')
if nx.is_isomorphic(g, g_answer):
    print(' => GOOD')
else:
    print(' => WRONG')
    print('True edges should be:', g_answer.edges())<jupyter_output>Edges are: [(0, 2), (1, 2), (1, 3), (4, 3)] => GOOD<jupyter_text>Fisher-Z検定<jupyter_code>from citestfz.ci_tests import ci_test_gauss
cm = np.corrcoef(dm.T)

(g, sep_set) = pcalg.estimate_skeleton(indep_test_func=ci_test_gauss,
                                 data_matrix=dm,
                                 alpha=0.01,
                                 corr_matrix=cm)
g = pcalg.estimate_cpdag(skel_graph=g, sep_set=sep_set)
g_answer = nx.DiGraph()
g_answer.add_nodes_from([0, 1, 2, 3, 4])
g_answer.add_edges_from([(0, 2), (1, 2), (1, 3), (4, 3)])
print('Edges are:', g.edges(), end='')
if nx.is_isomorphic(g, g_answer):
    print(' => GOOD')
else:
    print(' => WRONG')
    print('True edges should be:', g_answer.edges())<jupyter_output>Edges are: [(0, 2), (1, 2), (1, 3), (4, 3)] => GOOD<jupyter_text>事前知識を入れる<jupyter_code>import pcalg
import networkx as nx
from gsq.ci_tests import ci_test_dis
from gsq.gsq_testdata import dis_data

dm = np.array(dis_data).reshape((10000, 5))
node_ids = range(dm.shape[1])
node_ids
from itertools import combinations

init_g = nx.Graph()
init_g.add_nodes_from(node_ids)
for (i, j) in combinations(node_ids, 2):
    init_g.add_edge(i, j)

no_paths=[[4, 1], [1, 2]]
for no_path in no_paths:
    init_g.remove_edge(no_path[0], no_path[1])
# nx.nx_agraph.view_pygraphviz(g, prog='fdp')
nx.draw(init_g, with_labels=True)
(g, sep_set) = pcalg.estimate_skeleton(indep_test_func=ci_test_dis,
                                 data_matrix=dm,
                                 alpha=0.01,
                                 levels=[3,2,3,4,2],
                                 init_graph=init_g)
g = pcalg.estimate_cpdag(skel_graph=g, sep_set=sep_set)
g_answer = nx.DiGraph()
g_answer.add_nodes_from([0, 1, 2, 3, 4])
g_answer.add_edges_from([(0, 2), (1, 2), (1, 3), (4, 3)])
print('Edges are:', g.edges(), end='')
if nx.is_isomorphic(g, g_answer):
    print(' => GOOD')
else:
    print(' => WRONG')
    print('True edges should be:', g_answer.edges())
nx.draw(g, with_labels=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15885885885885886, 'fraction_numerical': 0.03183183183183183, 'mean_word_length': 3.588154269972452, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '585363', 'license': 'MIT', 'n_tokens_mistral': 1489, 'n_tokens_neox': 1423, 'n_words': 252, 'path': 'pc-algorithm.ipynb', 'repo_name': 'tsurubee/causal-discovery', 'size': '3330'}