Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ docs/site/
Manifest.toml

# script for testing
script.jl
script.jl

.vscode/
.DS_Store
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantileMatching"
uuid = "c8fc808f-91d0-4910-90d7-c7fb3b64f671"
authors = ["Gabriel Gobeil <gabriel.gobeil@polymtl.ca>", "Jonathan Jalbert <jonathan.jalbert@polymtl.ca>"]
version = "0.1.0"
version = "0.2.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
9 changes: 9 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Release Notes

## 0.2.0

- Refactored `match()` for scalar matching.
The function can now be broadcast to match a vector.

- Added an exception to parametric matching when the actual value to match is outside the support of the actual distribution.
This situation can occur when the actual value lies outside the support of the actual distribution while the target distribution is unbounded. In such cases, the returned matched value is `Inf`.
Binary file modified docs/.DS_Store
Binary file not shown.
8 changes: 4 additions & 4 deletions docs/src/StationaryQuantileMatching.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ qmm = EmpiricalQuantileMatchingModel(y⁺, x⁺)

Quantile matching of the non-zero simulated precipitations:
```@example stationary
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

println("") # hide
```
Expand Down Expand Up @@ -212,7 +212,7 @@ qmm = ParametricQuantileMatchingModel(fd_Y, fd_X)

Quantile matching of the non-zero simulated precipitations:
```@example stationary
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

println("") # hide
```
Expand Down Expand Up @@ -304,7 +304,7 @@ qmm = ParametricQuantileMatchingModel(fd_Y, fd_X)

Quantile matching of the non-zero simulated precipitations:
```@example stationary
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

println("") # hide
```
Expand Down Expand Up @@ -369,7 +369,7 @@ qmm = ParametricQuantileMatchingModel(fd_Y, fd_X)

Quantile matching of the non-zero simulated precipitations:
```@example stationary
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

println("") # hide
```
Expand Down
2 changes: 2 additions & 0 deletions src/AbstractQuantileMatchingModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract type AbstractQuantileMatchingModel end
abstract type Stationary end
abstract type NonStationary end

Base.Broadcast.broadcastable(obj::AbstractQuantileMatchingModel) = Ref(obj)

"""
match(qmm::AbstractQuantileMappingModel, x::Vector{<:Real})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function showEmpiricalQuantileMatchingModel(io::IO, obj::EmpiricalQuantileMatchi
println(io, prefix, " ", "extrapolation: ",get_extrapolation(obj))
end

function match(eqmm::EmpiricalQuantileMatchingModel{Stationary}, x::Vector{<:Real})
function match(eqmm::EmpiricalQuantileMatchingModel{Stationary}, x::Real)

nbins = get_nbins(eqmm)

Expand All @@ -123,7 +123,7 @@ function match(eqmm::EmpiricalQuantileMatchingModel{Stationary}, x::Vector{<:Rea
x̃ = itp(x)
end

function match(eqmm::EmpiricalQuantileMatchingModel{NonStationary}, x::Vector{<:Real})
function match(eqmm::EmpiricalQuantileMatchingModel{NonStationary}, x::Real)

nbins = get_nbins(eqmm)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,43 @@ function showParametricQuantileMatchingModel(io::IO, obj::ParametricQuantileMatc
end


function match(pqm::ParametricQuantileMatchingModel{Stationary}, x::Vector{<:Real})
"""
match(pqm::ParametricQuantileMatchingModel{Stationary}, x::Real)

Parametric quantile matching of the actual value `x` according to the parametric quantile matching model `pqm`.

### Note

If `x` is outside the support of the actual distribution and the target distribution is unbounded, the function returns an infinite value.
"""
function match(pqm::ParametricQuantileMatchingModel{Stationary}, x::Real)

targetdist = get_targetdist(pqm)
actualdist = get_actualdist(pqm)

p = cdf.(actualdist, x)

x̃ = quantile.(targetdist, p)
p = cdf(actualdist, x)

if p ≈ 0.
if isfinite(minimum(targetdist))
x̃ = minimum(targetdist)
else
x̃ = -Inf
end
elseif p ≈ 1.
if isfinite(maximum(targetdist))
x̃ = maximum(targetdist)
else
x̃ = Inf
end
else
x̃ = quantile.(targetdist, p)
end

return x̃

end

function match(nspqm::ParametricQuantileMatchingModel{NonStationary}, x::Vector{<:Real})
function match(nspqm::ParametricQuantileMatchingModel{NonStationary}, x::Real)

targetdist = get_targetdist(nspqm)
actualdist = get_actualdist(nspqm)
Expand Down
4 changes: 2 additions & 2 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function eqm(y::Vector{<:Real}, x::Vector{<:Real})
qmm = EmpiricalQuantileMatchingModel(y⁺, x⁺)

# Quantile matching of non-zero values
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

# Replace the non-zero values in the frequency adjusted series.
x̃[x̃ .> 0] = x̃⁺
Expand Down Expand Up @@ -89,7 +89,7 @@ function pqm(pd::Type{<:ContinuousUnivariateDistribution}, y::AbstractVector{<:R
qmm = ParametricQuantileMatchingModel(fd_Y, fd_X)

# Quantile matching of non-zero values
x̃⁺ = match(qmm, x⁺)
x̃⁺ = match.(qmm, x⁺)

# Replace the non-zero values in the frequency adjusted series.
x̃[x̃ .> 0] = x̃⁺
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ end

qmm = EmpiricalQuantileMatchingModel(targetsample, actualsample)

@test match(qmm, [2.])[] ≈ 0.48401845735265503
@test match(qmm, 2.) ≈ 0.48401845735265503

end

@testset "non-stationary" begin

qmm = EmpiricalQuantileMatchingModel(targetsample, actualsample, projsample)

@test match(qmm, [2.])[] ≈ 0.8349460253709341
@test match(qmm, 2.) ≈ 0.8349460253709341

end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,56 @@ end

@testset "match(::ParametricQuantileMatchingModel)" begin

targetdist = Exponential(.5)
actualdist = Exponential(2)
projdist = Exponential(3)

@testset "stationary" begin

targetdist = Exponential(.5)
actualdist = Exponential(2)

qmm = ParametricQuantileMatchingModel(targetdist, actualdist)

x = [4]
x̃ = rate(actualdist)/rate(targetdist)*[4]
x = 4
x̃ = rate(actualdist)/rate(targetdist)*4

@test match(qmm, x) ≈ x̃

end

@testset "non-stationary" begin

targetdist = Exponential(.5)
actualdist = Exponential(2)
projdist = Exponential(3)

qmm = ParametricQuantileMatchingModel(targetdist, actualdist, projdist)

λ = rate(targetdist)*rate(projdist)/rate(actualdist)

x = [4.]
x = 4.
x̃ = quantile(Exponential(1/λ), cdf(projdist, x))

@test match(qmm, x) ≈ x̃

end

@testset "outside the support" begin

targetdist = Exponential(1)
actualdist = Beta(1,1)

qmm = ParametricQuantileMatchingModel(targetdist, actualdist)

@test isfinite(match(qmm, -1.))
@test !isfinite(match(qmm, 1.2))

targetdist = Beta(2,2)
qmm = ParametricQuantileMatchingModel(targetdist, actualdist)
@test isfinite(match(qmm, 1.2))

targetdist = Normal(0,1)
qmm = ParametricQuantileMatchingModel(targetdist, actualdist)
@test !isfinite(match(qmm, -1.))

end

end

Expand Down
4 changes: 2 additions & 2 deletions test/functions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
actualsample = [0.21713363327549823, 1.502901744984021, 2.046092118287037, 2.2249912757196464, 2.6629189261859207]

qmm = EmpiricalQuantileMatchingModel(targetsample, actualsample)
x̃ = match(qmm, actualsample)
x̃ = match.(qmm, actualsample)

@test all(QuantileMatching.eqm(targetsample, actualsample) .≈ x̃)

Expand All @@ -40,7 +40,7 @@

qmm = ParametricQuantileMatchingModel(fd_Y, fd_X)

x̃ = match(qmm, actualsample)
x̃ = match.(qmm, actualsample)

@test all(pqm(Gamma, targetsample, actualsample) .≈ x̃)

Expand Down
Loading