Skip to content

Commit

Permalink
OCE bugfix (#284)
Browse files Browse the repository at this point in the history
* Expose `verbose` keyword at top level

* Correct and clean up `backwards_eliminate!`

* Update changelog and version
  • Loading branch information
kahaaga authored Mar 22, 2023
1 parent a52eeff commit 736cfc8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "CausalityTools"
uuid = "5520caf5-2dd7-5c5d-bfcb-a00e56ac49f7"
authors = ["Kristian Agasøster Haaga <kahaaga@gmail.com>", "Tor Einar Møller <temolle@gmail.com>", "George Datseris <datseris.george@gmail.com>"]
repo = "https://github.com/kahaaga/CausalityTools.jl.git"
version = "2.2.0"
version = "2.2.1"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.2.1

- `infer_graph` now accepts the `verbose` keyword.
- Fixed a bug in backwards elimination step of `OCE` algorithm that was caused due
to an undefined variable.

## 2.2

- Added `MCR` and `RMCD` recurrence based association measures, along with
Expand Down
42 changes: 24 additions & 18 deletions src/causal_graphs/oce/OCE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Base.@kwdef struct OCE{U, C, T} <: GraphAlgorithm
α = 0.05
end

function infer_graph(alg::OCE, x)
parents = select_parents(alg, x)
function infer_graph(alg::OCE, x; verbose = true)
parents = select_parents(alg, x; verbose)
return parents
end

Expand Down Expand Up @@ -233,29 +233,35 @@ function backwards_eliminate!(parents, alg, xᵢ, k; verbose = false)
M = length(parents.parents)
P = parents.parents
Pj = P[k]
remaining = StateSpaceSet(P...)[:, setdiff(1:M, k)]
remaining_idxs = setdiff(1:M, k)
remaining = StateSpaceSet(P...)[:, remaining_idxs]
test = independence(alg.ctest, xᵢ, Pj, remaining)
τ, j = parents.parents_τs[k], parents.parents_js[k]
I = test.m

if verbose
τ, j = parents.parents_τs[k], parents.parents_js[k] # Variable currently considered
τs = parents.parents_τs
js = parents.parents_js
src_var = "x$j()"
targ_var = "x$(js[k])($(τs[k]))"
cond_var = join(["x$(js[i])($(τs[i]))" for i in remaining_idxs], ", ")

if test.pvalue >= alg.α
outcome_msg = "Removing x$(j)() from parent set"
println("\t$src_var$targ_var | $cond_var$outcome_msg")
else
outcome_msg = "Keeping x$(j)() in parent set"
println("\t$src_var !⫫ $targ_var | $cond_var$outcome_msg")
end
end

# If p-value >= α, then we can't reject the null, i.e. the statistic I is
# indistinguishable from zero, so we claim independence.
# indistinguishable from zero, so we claim independence and remove the variable.
if test.pvalue >= alg.α
τ = parents.parents_τs[k]
j = parents.parents_τs[j]
s = join(["x$(js[i])($(τs[i]))" for i in idxs], ", ")
r = "Removing x$(js[k])($(τs[k])) from parent set"
verbose && println("\tx$j() ⫫ x$(js[k])($(τs[k])) | $s$r")
deleteat!(parents.parents, k)
deleteat!(parents.parents_js, k)
deleteat!(parents.parents_τs, k)
return true # a variable was removed, so we decrement `k_remaining` in parent function
return true
else
idxs = setdiff(1:M, k)
τs = parents.parents_τs
js = parents.parents_js
s = join(["x$(js[i])($(τs[i]))" for i in idxs], ", ")
r = "Keeping x$(js[k])($(τs[k])) in parent set"
verbose && println("\tx$j() !⫫ x$(js[k])($(τs[k])) | $s$r")
return false
end
end

0 comments on commit 736cfc8

Please sign in to comment.