From 382214cbec58c32c2c1a1d0d7ea7cef90bdb817d Mon Sep 17 00:00:00 2001 From: Oskar Laverny Date: Sat, 20 Apr 2024 20:28:03 +0200 Subject: [PATCH] [Bug] Force elliptical cdf to behave by clamping them (#197) * Try to clamp the cdf's Fixes #196 * last clamping --- src/EllipticalCopula.jl | 4 ++-- src/SklarDist.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EllipticalCopula.jl b/src/EllipticalCopula.jl index b60b2f35..703bbbbc 100644 --- a/src/EllipticalCopula.jl +++ b/src/EllipticalCopula.jl @@ -49,7 +49,7 @@ abstract type EllipticalCopula{d,MT} <: Copula{d} end Base.eltype(C::CT) where CT<:EllipticalCopula = Base.eltype(N(CT)(C.Σ)) function Distributions._rand!(rng::Distributions.AbstractRNG, C::CT, x::AbstractVector{T}) where {T<:Real, CT <: EllipticalCopula} Random.rand!(rng,N(CT)(C.Σ),x) - x .= Distributions.cdf.(U(CT),x) + x .= clamp.(Distributions.cdf.(U(CT),x),0,1) return x end function Distributions._rand!(rng::Distributions.AbstractRNG, C::CT, A::DenseMatrix{T}) where {T<:Real, CT<:EllipticalCopula} @@ -57,7 +57,7 @@ function Distributions._rand!(rng::Distributions.AbstractRNG, C::CT, A::DenseMat n = N(CT)(C.Σ) u = U(CT) Random.rand!(rng,n,A) - A .= Distributions.cdf.(u,A) + A .= clamp.(Distributions.cdf.(u,A),0,1) return A end function Distributions._logpdf(C::CT, u) where {CT <: EllipticalCopula} diff --git a/src/SklarDist.jl b/src/SklarDist.jl index 1cd85e89..be155ae3 100644 --- a/src/SklarDist.jl +++ b/src/SklarDist.jl @@ -59,7 +59,7 @@ function Distributions._rand!(rng::Distributions.AbstractRNG, S::SklarDist{CT,Tp x .= Distributions.quantile.(S.m,x) end function Distributions._logpdf(S::SklarDist{CT,TplMargins},u) where {CT,TplMargins} - sum(Distributions.logpdf(S.m[i],u[i]) for i in 1:length(u)) + Distributions.logpdf(S.C,Distributions.cdf.(S.m,u)) + sum(Distributions.logpdf(S.m[i],u[i]) for i in 1:length(u)) + Distributions.logpdf(S.C,clamp.(Distributions.cdf.(S.m,u),0,1)) end