Skip to content

Commit

Permalink
Minor fix in Quantile transform
Browse files Browse the repository at this point in the history
  • Loading branch information
juliohm committed Sep 16, 2024
1 parent 56917f6 commit 5b387cf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ EmpiricalDistribution(values) = EmpiricalDistribution{eltype(values)}(values)

quantile(d::EmpiricalDistribution, p::Real) = quantile(d.values, p, sorted=true)

function cdf(d::EmpiricalDistribution{T}, x::T) where {T}
function cdf(d::EmpiricalDistribution, x)
v = d.values
n = length(v)

Expand All @@ -37,9 +37,9 @@ function cdf(d::EmpiricalDistribution{T}, x::T) where {T}
l, u = v[head], v[tail]

if x < l
return T(0)
return 0.0
elseif x > u
return T(1)
return 1.0
else
if l == u
return tail / n
Expand Down
5 changes: 2 additions & 3 deletions src/transforms/quantile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ end
# transform samples from original to target distribution
function qtransform(samples, origin, target)
# avoid evaluating the quantile at 0 or 1
T = eltype(samples)
pmin = T(0) + T(1e-3)
pmax = T(1) - T(1e-3)
pmin = 0.0 + 1e-3
pmax = 1.0 - 1e-3
map(samples) do sample
prob = cdf(origin, sample)
quantile(target, clamp(prob, pmin, pmax))
Expand Down

0 comments on commit 5b387cf

Please sign in to comment.