Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to DifferentiationInterface #111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name = "TMLE"
uuid = "8afdd2fb-6e73-43df-8b62-b1650cd9c8cf"
authors = ["Olivier Labayle"]
version = "0.16.1"
version = "0.16.2"

[deps]
AbstractDifferentiation = "c29ec348-61ec-40c8-8164-b8c60e9d9f3d"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand Down Expand Up @@ -35,9 +35,9 @@ JSONExt = "JSON"
YAMLExt = "YAML"

[compat]
AbstractDifferentiation = "0.6.0"
CategoricalArrays = "0.10"
Combinatorics = "1.0.2"
DifferentiationInterface = "0.5"
Distributions = "0.25"
GLM = "1.8.2"
Graphs = "1.8"
Expand Down
2 changes: 1 addition & 1 deletion src/TMLE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using Zygote
using LogExpFunctions
using PrecompileTools
using Random
import AbstractDifferentiation as AD
import DifferentiationInterface as DI
using Graphs
using MetaGraphsNext
using Combinatorics
Expand Down
16 changes: 9 additions & 7 deletions src/estimators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ConditionalDistributionEstimator(train_validation_indices, model) =

Estimates all components of Ψ and then Ψ itself.
"""
function (estimator::Estimator)(Ψ::ComposedEstimand, dataset; cache=Dict(), verbosity=1, backend=AD.ZygoteBackend())
function (estimator::Estimator)(Ψ::ComposedEstimand, dataset; cache=Dict(), verbosity=1, backend=DI.AutoZygote())
estimates = map(Ψ.args) do estimand
estimate, _ = estimator(estimand, dataset; cache=cache, verbosity=verbosity)
estimate
Expand Down Expand Up @@ -157,23 +157,25 @@ f(x, y) = [x^2 - y, y - 3x]
compose(f, res₁, res₂)
```
"""
function compose(f, estimates...; backend=AD.ZygoteBackend())
function compose(f, estimates...; backend=DI.AutoZygote())
f₀, σ₀, n = _compose(f, estimates...; backend=backend)
estimand = ComposedEstimand(f, Tuple(e.estimand for e in estimates))
return ComposedEstimate(estimand, estimates, f₀, σ₀, n)
end

function _compose(f, estimates...; backend=AD.ZygoteBackend())
_make_vec(x::Number) = [x]
_make_vec(x::AbstractVector) = x

function _compose(f, estimates...; backend=DI.AutoZygote())
Σ = covariance_matrix(estimates...)
point_estimates = [r.estimate for r in estimates]
f₀, Js = AD.value_and_jacobian(backend, f, point_estimates...)
J = hcat(Js...)
f₀, J = DI.value_and_jacobian(_make_vec ∘ Base.splat(f), backend, point_estimates)
n = size(first(estimates).IC, 1)
σ₀ = J * Σ * J'
return collect(f₀), σ₀, n
return f₀, σ₀, n
end

function covariance_matrix(estimates...)
X = hcat([r.IC for r in estimates]...)
return cov(X, dims=1, corrected=true)
end
end
Loading