|
6 | 6 | insupport = a <= x <= b, |
7 | 7 | diff = b - a, |
8 | 8 | c = insupport ? inv(diff) : inv(one(diff)), |
9 | | - z = insupport ? zero(x) : oftype(x, NaN), |
10 | 9 | ), |
11 | | - (c, -c, z), |
| 10 | + (c, -c, ZeroTangent()), |
12 | 11 | ) |
13 | | - |
14 | | -# StatsFuns: https://github.com/JuliaStats/StatsFuns.jl/pull/106 |
15 | | - |
16 | | -## Beta ## |
17 | | - |
18 | | -@scalar_rule( |
19 | | - betalogpdf(α::Real, β::Real, x::Number), |
20 | | - @setup(z = digamma(α + β)), |
21 | | - ( |
22 | | - log(x) + z - digamma(α), |
23 | | - log1p(-x) + z - digamma(β), |
24 | | - (α - 1) / x + (1 - β) / (1 - x), |
25 | | - ), |
26 | | -) |
27 | | - |
28 | | -## Gamma ## |
29 | | - |
30 | | -@scalar_rule( |
31 | | - gammalogpdf(k::Real, θ::Real, x::Number), |
32 | | - @setup( |
33 | | - invθ = inv(θ), |
34 | | - xoθ = invθ * x, |
35 | | - z = xoθ - k, |
36 | | - ), |
37 | | - ( |
38 | | - log(xoθ) - digamma(k), |
39 | | - invθ * z, |
40 | | - - (1 + z) / x, |
41 | | - ), |
42 | | -) |
43 | | - |
44 | | -## Chisq ## |
45 | | - |
46 | | -@scalar_rule( |
47 | | - chisqlogpdf(k::Real, x::Number), |
48 | | - @setup(hk = k / 2), |
49 | | - ( |
50 | | - (log(x) - logtwo - digamma(hk)) / 2, |
51 | | - (hk - 1) / x - one(hk) / 2, |
52 | | - ), |
53 | | -) |
54 | | - |
55 | | -## FDist ## |
56 | | - |
57 | | -@scalar_rule( |
58 | | - fdistlogpdf(ν1::Real, ν2::Real, x::Number), |
59 | | - @setup( |
60 | | - xν1 = x * ν1, |
61 | | - temp1 = xν1 + ν2, |
62 | | - a = (x - 1) / temp1, |
63 | | - νsum = ν1 + ν2, |
64 | | - di = digamma(νsum / 2), |
65 | | - ), |
66 | | - ( |
67 | | - (-log1p(ν2 / xν1) - ν2 * a + di - digamma(ν1 / 2)) / 2, |
68 | | - (-log1p(xν1 / ν2) + ν1 * a + di - digamma(ν2 / 2)) / 2, |
69 | | - ((ν1 - 2) / x - ν1 * νsum / temp1) / 2, |
70 | | - ), |
71 | | -) |
72 | | - |
73 | | -## TDist ## |
74 | | - |
75 | | -@scalar_rule( |
76 | | - tdistlogpdf(ν::Real, x::Number), |
77 | | - @setup( |
78 | | - νp1 = ν + 1, |
79 | | - xsq = x^2, |
80 | | - invν = inv(ν), |
81 | | - a = xsq * invν, |
82 | | - b = νp1 / (ν + xsq), |
83 | | - ), |
84 | | - ( |
85 | | - (digamma(νp1 / 2) - digamma(ν / 2) + a * b - log1p(a) - invν) / 2, |
86 | | - - x * b, |
87 | | - ), |
88 | | -) |
89 | | - |
90 | | -## Binomial ## |
91 | | - |
92 | | -@scalar_rule( |
93 | | - binomlogpdf(n::Real, p::Real, k::Real), |
94 | | - @setup(z = digamma(n - k + 1)), |
95 | | - ( |
96 | | - digamma(n + 2) - z + log1p(-p) - 1 / (1 + n), |
97 | | - (k / p - n) / (1 - p), |
98 | | - z - digamma(k + 1) + logit(p), |
99 | | - ), |
100 | | -) |
101 | | - |
102 | | -## Poisson ## |
103 | | - |
104 | | -@scalar_rule( |
105 | | - poislogpdf(λ::Number, x::Number), |
106 | | - ((iszero(x) && iszero(λ) ? zero(x / λ) : x / λ) - 1, log(λ) - digamma(x + 1)), |
107 | | -) |
108 | | - |
109 | | -## PoissonBinomial |
110 | | - |
111 | | -function ChainRulesCore.rrule( |
112 | | - ::typeof(Distributions.poissonbinomial_pdf_fft), p::AbstractVector{<:Real} |
113 | | -) |
114 | | - y = Distributions.poissonbinomial_pdf_fft(p) |
115 | | - A = poissonbinomial_partialderivatives(p) |
116 | | - function poissonbinomial_pdf_fft_pullback(Δy) |
117 | | - p̄ = InplaceableThunk( |
118 | | - @thunk(A * Δy), |
119 | | - Δ -> LinearAlgebra.mul!(Δ, A, Δy, true, true), |
120 | | - ) |
121 | | - return (NO_FIELDS, p̄) |
122 | | - end |
123 | | - return y, poissonbinomial_pdf_fft_pullback |
124 | | -end |
125 | | - |
126 | | -if isdefined(Distributions, :poissonbinomial_pdf) |
127 | | - function ChainRulesCore.rrule( |
128 | | - ::typeof(Distributions.poissonbinomial_pdf), p::AbstractVector{<:Real} |
129 | | - ) |
130 | | - y = Distributions.poissonbinomial_pdf(p) |
131 | | - A = poissonbinomial_partialderivatives(p) |
132 | | - function poissonbinomial_pdf_pullback(Δy) |
133 | | - p̄ = InplaceableThunk( |
134 | | - @thunk(A * Δy), |
135 | | - Δ -> LinearAlgebra.mul!(Δ, A, Δy, true, true), |
136 | | - ) |
137 | | - return (NO_FIELDS, p̄) |
138 | | - end |
139 | | - return y, poissonbinomial_pdf_pullback |
140 | | - end |
141 | | -end |
0 commit comments