Skip to content
New issue

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

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

Already on GitHub? # to your account

[API choices] fit_mle for insteances, _tau #27

Open
lrnv opened this issue Mar 23, 2023 · 1 comment
Open

[API choices] fit_mle for insteances, _tau #27

lrnv opened this issue Mar 23, 2023 · 1 comment

Comments

@lrnv
Copy link
Owner

lrnv commented Mar 23, 2023

function Distributions.fit(D::SklarDist, x)
    # The first thing to do is to fit the marginals : 
    @assert length(D) == size(x, 1)
    # One could put fit but EM works for with fit_mle only
    m = Tuple(Distributions.fit_mle(D.m[i], x[i, :]) for i in axes(x, 1))
    u = pseudos(x)

    # τ⁻¹(FrankCopula, τ) works but not τ⁻¹(FrankCopula{3, Float64}, τ)
    # I found https://discourse.julialang.org/t/extract-type-name-only-from-parametric-type/14188/20
    # to just extract the typename.
    # Again defining fit(C::ArchimedeanCopula, u) = fit(::Type{CT},u) where {CT <: ArchimedeanCopula} would simplify the writting
    C = Distributions.fit(Base.typename(typeof(D.C)).wrapper, u)
    return SklarDist(C, m)
end

D̂mix = fit(D, simu)

Note that I had to define the weighted fit_mle(LogNormal, x, w) (only fit_mle(LogNormal, x) is in Distributions.jl).
I believe this does the job

using StatsBase
function Distributions.fit_mle(::Type{<:LogNormal}, x::AbstractArray{T}, w::AbstractArray) where T<:Real
    lx = log.(x)
    μ, σ = mean_and_std(lx, weights(w))
    LogNormal(μ, σ)
end

(Note that StochasticEM() version only use unweighted version of fit_mle.)

Hence, IMO, it seems that for compatibility between EM.jl and Copula.jl, it is Copula.jl that needs to simply add Distributions.fit(D::SklarDist, x) as shown here. I believe it should not have any consequences on your existing functions.

Originally posted by @dmetivie in dmetivie/ExpectationMaximization.jl#8 (comment)

@lrnv
Copy link
Owner Author

lrnv commented Mar 23, 2023

Maybe τ⁻¹(FrankCopula{3, Float64}, τ) should work and not only τ⁻¹(FrankCopula, τ) ? Dunno yet, have to check.

@lrnv lrnv changed the title fit_mle for insteances, _tau [API choices] fit_mle for insteances, _tau Feb 7, 2024
@lrnv lrnv added the enhancement New feature or request label Feb 7, 2024
@lrnv lrnv removed the enhancement New feature or request label May 9, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant