-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ChainRules adjoints * Move differentiation rules to a separate file * Update to new test syntax * `rand_tangent` is fixed upstream * Add support for ChainRulesCore 0.10 * Fix definition of chainrule for `poislogpdf` * Use ChainRulesCore 1 * Only support ChainRulesCore 1
- Loading branch information
Showing
8 changed files
with
146 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
ChainRulesCore.@scalar_rule( | ||
betalogpdf(α::Real, β::Real, x::Number), | ||
@setup(z = digamma(α + β)), | ||
( | ||
log(x) + z - digamma(α), | ||
log1p(-x) + z - digamma(β), | ||
(α - 1) / x + (1 - β) / (1 - x), | ||
), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
binomlogpdf(n::Real, p::Real, k::Real), | ||
@setup(z = digamma(n - k + 1)), | ||
( | ||
digamma(n + 2) - z + log1p(-p) - 1 / (1 + n), | ||
(k / p - n) / (1 - p), | ||
z - digamma(k + 1) + logit(p), | ||
), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
chisqlogpdf(k::Real, x::Number), | ||
@setup(hk = k / 2), | ||
( | ||
(log(x) - logtwo - digamma(hk)) / 2, | ||
(hk - 1) / x - one(hk) / 2, | ||
), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
fdistlogpdf(ν1::Real, ν2::Real, x::Number), | ||
@setup( | ||
xν1 = x * ν1, | ||
temp1 = xν1 + ν2, | ||
a = (x - 1) / temp1, | ||
νsum = ν1 + ν2, | ||
di = digamma(νsum / 2), | ||
), | ||
( | ||
(-log1p(ν2 / xν1) - ν2 * a + di - digamma(ν1 / 2)) / 2, | ||
(-log1p(xν1 / ν2) + ν1 * a + di - digamma(ν2 / 2)) / 2, | ||
((ν1 - 2) / x - ν1 * νsum / temp1) / 2, | ||
), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
gammalogpdf(k::Real, θ::Real, x::Number), | ||
@setup( | ||
invθ = inv(θ), | ||
xoθ = invθ * x, | ||
z = xoθ - k, | ||
), | ||
( | ||
log(xoθ) - digamma(k), | ||
invθ * z, | ||
- (1 + z) / x, | ||
), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
poislogpdf(λ::Number, x::Number), | ||
((iszero(x) && iszero(λ) ? zero(x / λ) : x / λ) - 1, log(λ) - digamma(x + 1)), | ||
) | ||
|
||
ChainRulesCore.@scalar_rule( | ||
tdistlogpdf(ν::Real, x::Number), | ||
@setup( | ||
νp1 = ν + 1, | ||
xsq = x^2, | ||
invν = inv(ν), | ||
a = xsq * invν, | ||
b = νp1 / (ν + xsq), | ||
), | ||
( | ||
(digamma(νp1 / 2) - digamma(ν / 2) + a * b - log1p(a) - invν) / 2, | ||
- x * b, | ||
), | ||
) |
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,56 @@ | ||
using StatsFuns, Test | ||
using ChainRulesCore | ||
using ChainRulesTestUtils | ||
using Random | ||
|
||
@testset "chainrules" begin | ||
x = exp(randn()) | ||
y = exp(randn()) | ||
z = logistic(randn()) | ||
test_frule(betalogpdf, x, y, z) | ||
test_rrule(betalogpdf, x, y, z) | ||
|
||
x = exp(randn()) | ||
y = exp(randn()) | ||
z = exp(randn()) | ||
test_frule(gammalogpdf, x, y, z) | ||
test_rrule(gammalogpdf, x, y, z) | ||
|
||
x = exp(randn()) | ||
y = exp(randn()) | ||
test_frule(chisqlogpdf, x, y) | ||
test_rrule(chisqlogpdf, x, y) | ||
|
||
x = exp(randn()) | ||
y = exp(randn()) | ||
z = exp(randn()) | ||
test_frule(fdistlogpdf, x, y, z) | ||
test_rrule(fdistlogpdf, x, y, z) | ||
|
||
x = exp(randn()) | ||
y = randn() | ||
test_frule(tdistlogpdf, x, y) | ||
test_rrule(tdistlogpdf, x, y) | ||
|
||
# use `BigFloat` to avoid Rmath implementation in finite differencing check | ||
# (returns `NaN` for non-integer values) | ||
n = rand(1:100) | ||
x = BigFloat(n) | ||
y = big(logistic(randn())) | ||
z = BigFloat(rand(1:n)) | ||
test_frule(binomlogpdf, x, y, z) | ||
test_rrule(binomlogpdf, x, y, z) | ||
|
||
x = big(exp(randn())) | ||
y = BigFloat(rand(1:100)) | ||
test_frule(poislogpdf, x, y) | ||
test_rrule(poislogpdf, x, y) | ||
|
||
# test special case λ = 0 | ||
_, pb = rrule(StatsFuns.poislogpdf, 0.0, 0.0) | ||
_, x̄1, _ = pb(1) | ||
@test x̄1 == -1 | ||
_, pb = rrule(StatsFuns.poislogpdf, 0.0, 1.0) | ||
_, x̄1, _ = pb(1) | ||
@test x̄1 == Inf | ||
end |
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