Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Add sensitivities for pinv #174

Open
wants to merge 1 commit into
base: master
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
7 changes: 7 additions & 0 deletions src/sensitivities/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,10 @@ function ∇(::typeof(exp), ::Type{Arg{1}}, p, Y, Ȳ, X::AbstractMatrix)
F = factorize(Uᵀ)
return real(F \ (Uᵀ * Ȳ / F .* Z) * Uᵀ)
end

# Ported from autograd, which uses https://mathoverflow.net/questions/25778/analytical-...
# formula-for-numerical-derivative-of-the-matrix-pseudo-inverse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the academic reference here as well:
Golub, Gene H., and Victor Pereyra. "The differentiation of pseudo-inverses and nonlinear least squares problems whose variables separate." SIAM Journal on numerical analysis 10.2 (1973): 413-432.

import LinearAlgebra: pinv
@explicit_intercepts pinv Tuple{AbstractMatrix{<:∇Scalar}}
∇(::typeof(pinv), ::Type{Arg{1}}, p, Y, Ȳ, A::AbstractMatrix) =
(-Y*Ȳ'*Y + Y*Y'*Ȳ*(I - A*Y) + (I - Y*A)*Ȳ*Y'*Y)'
10 changes: 10 additions & 0 deletions test/sensitivities/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@
VA = randn(rng, n, n)
@test_throws ArgumentError check_errs(exp, randn(rng, n, n), A, VA)
end

@testset "pinv" begin
rng = MersenneTwister(12345)
for n in 3:5, m in 3:5
X = randn(rng, n, m)
VX = randn(rng, n, m)
Ȳ = randn(rng, m, n)
@test check_errs(pinv, Ȳ, X, VX)
end
end
end