From b92bdd0b354e887df38e0b790b1ac6772f155b06 Mon Sep 17 00:00:00 2001 From: mschauer Date: Fri, 28 Jul 2023 10:02:30 +0200 Subject: [PATCH] Add example ges --- docs/make.jl | 1 + docs/src/examples/backdoor_example.md | 4 +-- docs/src/examples/ges_basic_examples.md | 48 +++++++++++++++++++++++++ src/ges.jl | 2 ++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 docs/src/examples/ges_basic_examples.md diff --git a/docs/make.jl b/docs/make.jl index 5d909d1..f8d32b8 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -6,6 +6,7 @@ makedocs(modules = [CausalInference], authors = "Moritz Schauer and contributors", pages = Any["Home" => "index.md", "Examples" => Any["examples/pc_basic_examples.md", + "examples/ges_basic_examples.md", "examples/pc_cmi_examples.md", "examples/pc_real_example.md", "examples/backdoor_example.md"], diff --git a/docs/src/examples/backdoor_example.md b/docs/src/examples/backdoor_example.md index a75840f..90ee562 100644 --- a/docs/src/examples/backdoor_example.md +++ b/docs/src/examples/backdoor_example.md @@ -1,6 +1,6 @@ -# Reasoning about experiments +# Reasoning about experiments (Backdoors and adjustments) -The following examples is discussed as Example 3.4 in Chapter 3-4 (http://bayes.cs.ucla.edu/BOOK-2K/ch3-3.pdf). See also http://causality.cs.ucla.edu/blog/index.php/category/back-door-criterion/. +The following example is discussed as Example 3.4 in Chapter 3-4 (http://bayes.cs.ucla.edu/BOOK-2K/ch3-3.pdf). See also http://causality.cs.ucla.edu/blog/index.php/category/back-door-criterion/. The causal model we are going to study can be represented using the following DAG concerning a set of variables numbered `1` to `8`: diff --git a/docs/src/examples/ges_basic_examples.md b/docs/src/examples/ges_basic_examples.md new file mode 100644 index 0000000..a879eb8 --- /dev/null +++ b/docs/src/examples/ges_basic_examples.md @@ -0,0 +1,48 @@ +# GES algorithm: Basic examples + +Causal discovery with the GES algorithm goes along the same lines as the PC algorithm. We again take the examle in chapter 2 of Judea Pearl's book. The causal model we are going to study can be represented using the following DAG: + +![True example DAG](https://raw.githubusercontent.com/mschauer/CausalInference.jl/master/assets/true_graph.png) + +We can easily create some sample data that corresponds to the causal structure described by the DAG. For the sake of simplicity, let's create data from a simple linear model that follows the structure defined by the DAG shown above: + +```Julia +using CausalInference +using TikzGraphs +using Random +Random.seed!(1) + +# Generate some sample data to use with the GES algorithm + +N = 2000 # number of data points + +# define simple linear model with added noise +x = randn(N) +v = x + randn(N)*0.25 +w = x + randn(N)*0.25 +z = v + w + randn(N)*0.25 +s = z + randn(N)*0.25 + +df = (x=x, v=v, w=w, z=z, s=s) +``` + +With this data ready, we can now see to what extent we can back out the underlying causal structure from the data using the GES algorithm. Under the hood, GES uses a score to determine the causal relationships between different variables in a given data set. By default, `ges` uses a Gaussian BIC to score different causal models. + +```Julia +est_g, score = ges(df; penalty=1.0, parallel=true) +``` + +In order to investigate the output of the GES algorithm, the same plotting function applies. + +```Julia +tp = plot_pc_graph_tikz(est_g, [String(k) for k in keys(df)]) +``` + +![Example output of GES algorithm](https://raw.githubusercontent.com/mschauer/CausalInference.jl/master/assets/pc_graph_linear.png) + +We can compare with the output of the PC algorithm +``` +est_g2 = pcalg(df, 0.01, gausscitest) +est_g == est_g2 +``` +For the data using this seed, both agree. \ No newline at end of file diff --git a/src/ges.jl b/src/ges.jl index 1adc3fa..2df67e5 100644 --- a/src/ges.jl +++ b/src/ges.jl @@ -38,6 +38,8 @@ function ges(X::AbstractMatrix; method=:gaussian_bic, penalty=0.5, parallel=fals throw(ArgumentError("method=$method")) end end +ges(X; method=:gaussian_bic, penalty=0.5, parallel=false, verbose=false) = ges(Tables.matrix(X); method, penalty, parallel, verbose) + """ ges(n, local_score; score=0.0, parallel=false, verbose=false)