Skip to content

Commit

Permalink
Add example ges
Browse files Browse the repository at this point in the history
  • Loading branch information
mschauer committed Jul 28, 2023
1 parent 6832f97 commit b92bdd0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/backdoor_example.md
Original file line number Diff line number Diff line change
@@ -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`:

Expand Down
48 changes: 48 additions & 0 deletions docs/src/examples/ges_basic_examples.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions src/ges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b92bdd0

Please sign in to comment.