Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make suffstats and fit_mle type-generic for Normal{T} #1560

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
32 changes: 16 additions & 16 deletions src/univariate/continuous/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ rand(rng::AbstractRNG, d::Normal{T}) where {T} = d.μ + d.σ * randn(rng, float(

#### Fitting

struct NormalStats <: SufficientStats
s::Float64 # (weighted) sum of x
m::Float64 # (weighted) mean of x
s2::Float64 # (weighted) sum of (x - μ)^2
tw::Float64 # total sample weight
struct NormalStats{T} <: SufficientStats
s::T # (weighted) sum of x
m::T # (weighted) mean of x
s2::T # (weighted) sum of (x - μ)^2
tw::T # total sample weight
end
gdalle marked this conversation as resolved.
Show resolved Hide resolved

function suffstats(::Type{<:Normal}, x::AbstractArray{T}) where T<:Real
function suffstats(::Type{Normal{T}}, x::AbstractArray{<:Real}) where {T<:Real}
gdalle marked this conversation as resolved.
Show resolved Hide resolved
n = length(x)

# compute s
Expand All @@ -128,10 +128,10 @@ function suffstats(::Type{<:Normal}, x::AbstractArray{T}) where T<:Real
@inbounds s2 += abs2(x[i] - m)
end

NormalStats(s, m, s2, n)
NormalStats{T}(s, m, s2, n)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
end

function suffstats(::Type{<:Normal}, x::AbstractArray{T}, w::AbstractArray{Float64}) where T<:Real
function suffstats(::Type{Normal{T}}, x::AbstractArray{<:Real}, w::AbstractArray{<:Real}) where {T<:Real}
gdalle marked this conversation as resolved.
Show resolved Hide resolved
n = length(x)

# compute s
Expand All @@ -150,7 +150,7 @@ function suffstats(::Type{<:Normal}, x::AbstractArray{T}, w::AbstractArray{Float
@inbounds s2 += w[i] * abs2(x[i] - m)
end

NormalStats(s, m, s2, tw)
NormalStats{T}(s, m, s2, tw)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
end

# Cases where μ or σ is known
Expand Down Expand Up @@ -211,16 +211,16 @@ end

# fit_mle based on sufficient statistics

fit_mle(::Type{<:Normal}, ss::NormalStats) = Normal(ss.m, sqrt(ss.s2 / ss.tw))
fit_mle(::Type{D}, ss::NormalStats) where {D<:Normal} = D(ss.m, sqrt(ss.s2 / ss.tw))
fit_mle(g::NormalKnownMu, ss::NormalKnownMuStats) = Normal(g.μ, sqrt(ss.s2 / ss.tw))
fit_mle(g::NormalKnownSigma, ss::NormalKnownSigmaStats) = Normal(ss.sx / ss.tw, g.σ)
gdalle marked this conversation as resolved.
Show resolved Hide resolved

# generic fit_mle methods

function fit_mle(::Type{<:Normal}, x::AbstractArray{T}; mu::Float64=NaN, sigma::Float64=NaN) where T<:Real
function fit_mle(::Type{D}, x::AbstractArray{<:Real}; mu::Float64=NaN, sigma::Float64=NaN) where {D<:Normal}
gdalle marked this conversation as resolved.
Show resolved Hide resolved
if isnan(mu)
if isnan(sigma)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
fit_mle(Normal, suffstats(Normal, x))
fit_mle(D, suffstats(Normal, x))
else
g = NormalKnownSigma(sigma)
Copy link
Member

Choose a reason for hiding this comment

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

We should find a way to preserve/forward the type of D in these cases as well.

Copy link
Author

Choose a reason for hiding this comment

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

Which eltype should we pick for D though, the one of g or the one of the suffstats?

Copy link
Author

Choose a reason for hiding this comment

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

I added a conversion in my latest commit, what do you think?

fit_mle(g, suffstats(g, x))
Expand All @@ -230,15 +230,15 @@ function fit_mle(::Type{<:Normal}, x::AbstractArray{T}; mu::Float64=NaN, sigma::
g = NormalKnownMu(mu)
fit_mle(g, suffstats(g, x))
else
Normal(mu, sigma)
D(mu, sigma)
end
end
end

function fit_mle(::Type{<:Normal}, x::AbstractArray{T}, w::AbstractArray{Float64}; mu::Float64=NaN, sigma::Float64=NaN) where T<:Real
function fit_mle(::Type{D}, x::AbstractArray{<:Real}, w::AbstractArray{<:Real}; mu::Float64=NaN, sigma::Float64=NaN) where {D<:Normal}
gdalle marked this conversation as resolved.
Show resolved Hide resolved
if isnan(mu)
if isnan(sigma)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
fit_mle(Normal, suffstats(Normal, x, w))
fit_mle(D, suffstats(Normal, x, w))
else
g = NormalKnownSigma(sigma)
fit_mle(g, suffstats(g, x, w))
Expand All @@ -248,7 +248,7 @@ function fit_mle(::Type{<:Normal}, x::AbstractArray{T}, w::AbstractArray{Float64
g = NormalKnownMu(mu)
fit_mle(g, suffstats(g, x, w))
else
Normal(mu, sigma)
D(mu, sigma)
end
end
end