-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from mschauer/incscore
Incrementally compute score + test
- Loading branch information
Showing
4 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Random, CausalInference, Statistics, Test, Graphs | ||
@testset "Zig-Zag" begin | ||
Random.seed!(1) | ||
|
||
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) | ||
iterations = 5_000 | ||
n = length(df) # vertices | ||
κ = n - 1 # max degree | ||
penalty = 2.0 # increase to get more edges in truth | ||
Random.seed!(101) | ||
C = cor(CausalInference.Tables.matrix(df)) | ||
score = GaussianScore(C, N, penalty) | ||
gs = @time causalzigzag(n; score, κ, iterations) | ||
graphs, graph_pairs, hs, τs, ws, ts, scores = CausalInference.unzipgs(gs) | ||
posterior = sort(keyedreduce(+, graph_pairs, ws); byvalue=true, rev=true) | ||
|
||
# maximum aposteriori estimate | ||
@test first(posterior).first == [1=>2, 1=>3, 2=>1, 2=>4, 3=>1, 3=>4, 4=>5] | ||
# score of last sample | ||
@test score_dag(pdag2dag!(copy(graphs[end])), score) ≈ scores[end] + score_dag(DiGraph(n), score) | ||
|
||
end #testset |