Skip to content

Commit

Permalink
relax qqbuild signature (#1206)
Browse files Browse the repository at this point in the history
  • Loading branch information
piever authored Nov 2, 2020
1 parent 2907bf2 commit 50ce715
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/qq.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
struct QQPair
qx::Vector{Float64}
qy::Vector{Float64}
struct QQPair{U<:AbstractVector, V<:AbstractVector}
qx::U
qy::V
end

function qqbuild(x::Vector, y::Vector)
function qqbuild(x::AbstractVector, y::AbstractVector)
n = min(length(x), length(y))
grid = [0.0:(1 / (n - 1)):1.0;]
qx = quantile(x, grid)
qy = quantile(y, grid)
return QQPair(qx, qy)
end

function qqbuild(x::Vector, d::UnivariateDistribution)
function qqbuild(x::AbstractVector, d::UnivariateDistribution)
n = length(x)
grid = [(1 / (n - 1)):(1 / (n - 1)):(1.0 - (1 / (n - 1)));]
qx = quantile(x, grid)
qd = quantile.(Ref(d), grid)
return QQPair(qx, qd)
end

function qqbuild(d::UnivariateDistribution, x::Vector)
function qqbuild(d::UnivariateDistribution, x::AbstractVector)
n = length(x)
grid = [(1 / (n - 1)):(1 / (n - 1)):(1.0 - (1 / (n - 1)));]
qd = quantile.(Ref(d), grid)
Expand Down
18 changes: 12 additions & 6 deletions test/qq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ using Distributions
using Test

a = qqbuild(collect(1:10), collect(1:10))
@test a.qx collect(1.0:10)
@test a.qy collect(1.0:10)
b = qqbuild(1:10, 1:10)
c = qqbuild(view(collect(1:20), 1:10), view(collect(1:20), 1:10))
@test a.qx b.qx c.qx collect(1.0:10)
@test a.qy b.qy c.qy collect(1.0:10)

a = qqbuild(collect(1:10), Uniform(1,10))
@test a.qx collect(2.0:9)
@test a.qy collect(2.0:9)
b = qqbuild(1:10, Uniform(1,10))
c = qqbuild(view(collect(1:20), 1:10), Uniform(1,10))
@test a.qx b.qx c.qx collect(2.0:9)
@test a.qy b.qy c.qy collect(2.0:9)

a = qqbuild(Uniform(1,10), collect(1:10))
@test a.qx collect(2.0:9)
@test a.qy collect(2.0:9)
b = qqbuild(Uniform(1,10), 1:10)
c = qqbuild(Uniform(1,10), view(collect(1:20), 1:10))
@test a.qx b.qx c.qx collect(2.0:9)
@test a.qy b.qy c.qy collect(2.0:9)

0 comments on commit 50ce715

Please sign in to comment.