JuliaDiff / FiniteDifferences.jl

High accuracy derivatives, estimated via numerical finite differences (formerly FDM.jl)
MIT License
298 stars 26 forks source link

Positive Semidefinite FDM #52

Open Roger-luo opened 4 years ago

Roger-luo commented 4 years ago

central_fdm will cause problem when one wants the input always being positive semidefinite, e.g the real value logdet function. It cause the gradcheck of logdet in Zygote fails occasionally, any idea how to preserve this property while doing FDM?

Update: I workaround it by doing

gradcheck(x->logdet(x * x'), rand(4, 4))
willtebbutt commented 4 years ago

Ah, yes, this is generally problematic. I always do something similar to what you've done (I'll usually add the identity to x * x' to ensure it's reasonably well conditioned though), but it's not a particularly satisfying solution.

wesselb commented 3 years ago

Perhaps a bit of an old issue, but just wanted to remark that there is also the solution of using forward_fdm instead of central_fdm:

julia> x = randn(2, 2); x = x'x;

julia> v = randn(2, 2); v = v'v;

julia> forward_fdm(5, 1)(ε -> logdet(x + ε .* v), 0)
38.547460846816136