-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from biaslab/add-gamma
Add closed form expectation for Gamma
- Loading branch information
Showing
14 changed files
with
313 additions
and
87 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
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 StaticArrays | ||
import SpecialFunctions: trigamma, digamma, polygamma | ||
import Distributions: Gamma, shape, rate | ||
import LogExpFunctions: xlogx | ||
|
||
function mean(::ClosedFormExpectation, ::typeof(log), q::Gamma) | ||
return digamma(shape(q)) + log(scale(q)) | ||
end | ||
|
||
function mean(::ClosedWilliamsProduct, ::typeof(log), q::Gamma) | ||
return @SVector [ | ||
polygamma(1, shape(q)), | ||
1/scale(q) | ||
] | ||
end | ||
|
||
function mean(::ClosedFormExpectation, ::typeof(xlogx), q::Gamma) | ||
scaler = shape(q)/rate(q) | ||
return scaler * (digamma(shape(q)+1) - log(rate(q))) | ||
end | ||
|
||
function mean(::ClosedFormExpectation, ::typeof(xlog2x), q::Gamma) | ||
scaler = shape(q)/rate(q) | ||
return scaler * ((digamma(shape(q)+1) - log(rate(q)))^2 + trigamma(shape(q)+1)) | ||
end | ||
|
||
function mean(::ClosedFormExpectation, ::ComposedFunction{Square, typeof(log)}, q::Gamma) | ||
return trigamma(shape(q)) + (digamma(shape(q)) - log(rate(q)))^2 | ||
end | ||
|
||
function mean(::ClosedWilliamsProduct, ::ComposedFunction{Square, typeof(log)}, q::Gamma) | ||
return @SVector [ | ||
polygamma(2, shape(q)) + 2 * (digamma(shape(q)) - log(rate(q)))*trigamma(shape(q)), | ||
2 * (digamma(shape(q)) - log(rate(q))) * rate(q) | ||
] | ||
end | ||
|
||
function mean(strategy::ClosedFormExpectation, ::ComposedFunction{Power{Val{3}}, typeof(log)}, q::Gamma) | ||
Elogx = mean(strategy, log, q) | ||
Elog2x = mean(strategy, Square() ∘ log, q) | ||
return polygamma(2, shape(q)) + 3*Elogx * Elog2x - 2*Elogx^3 | ||
end | ||
|
||
function mean(strategy::ClosedFormExpectation, f::ComposedFunction{typeof(log), ExpLogSquare{T}}, q::Gamma) where {T} | ||
μ, σ = f.inner.μ, f.inner.σ | ||
Elogx = mean(strategy, log, q) | ||
Elog2x = mean(strategy, Square() ∘ log, q) | ||
return -1/(2*σ^2)*(μ^2 - 2*μ*Elogx + Elog2x) | ||
end | ||
|
||
function mean(strategy::ClosedFormExpectation, f::Logpdf{LogNormal{T}}, q::Gamma) where {T} | ||
μ, σ = f.dist.μ, f.dist.σ | ||
E_logexplogsquare = mean(strategy, log ∘ ExpLogSquare(μ, σ), q) | ||
E_logx = mean(strategy, log, q) | ||
return E_logexplogsquare - E_logx - log(σ) - 0.5*log(2pi) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export Power | ||
|
||
""" | ||
Power | ||
Power is a type that represents the x^N function. | ||
""" | ||
struct Power{T} <: Expression | ||
n::T | ||
end | ||
|
||
function (f::Power{Val{N}})(x) where {N} | ||
return x^N | ||
end | ||
|
||
function Base.log(::Power{Val{N}}, x) where {N} | ||
return N * log(x) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export Square | ||
|
||
""" | ||
Square | ||
Square is a type that represents the x^2 function. | ||
""" | ||
struct Square <: Expression end | ||
|
||
function (::Square)(x) | ||
return x^2 | ||
end | ||
|
||
function Base.log(::Square, x) | ||
return 2 * log(x) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export xlog2x | ||
|
||
""" | ||
Return `x * log(x)^2` for `x ≥ 0`, handling ``x = 0`` by taking the downward limit. | ||
```jldoctest | ||
julia> xlog2x(0) | ||
0.0 | ||
``` | ||
""" | ||
function xlog2x(x::Number) | ||
result = x * (log(x))^2 | ||
return iszero(x) ? zero(result) : result | ||
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
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
Oops, something went wrong.