From 15d0cbc7868a9f06b46fea6dfe63efeb7f25bdca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Tue, 6 Feb 2024 18:41:17 +0100 Subject: [PATCH 01/17] rem mutable + add Ref(x) --- .gitignore | 4 +++- src/KernelDensity.jl | 2 ++ src/interp.jl | 3 +-- src/univariate.jl | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ae3a70e3..291b930a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ examples/.ipynb_checkpoints/* -Manifest.toml \ No newline at end of file +Manifest.toml +.vscode/settings.json +tmp.jl diff --git a/src/KernelDensity.jl b/src/KernelDensity.jl index 722e118d..b6201c8a 100644 --- a/src/KernelDensity.jl +++ b/src/KernelDensity.jl @@ -12,6 +12,8 @@ export kde, kde_lscv, UnivariateKDE, BivariateKDE, InterpKDE, pdf abstract type AbstractKDE end +Base.broadcastable(x::AbstractKDE) = Ref(x) + include("univariate.jl") include("bivariate.jl") include("interp.jl") diff --git a/src/interp.jl b/src/interp.jl index 05e648e6..4503e43d 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -1,12 +1,11 @@ import Interpolations: interpolate, scale -mutable struct InterpKDE{K,I} <: AbstractKDE +struct InterpKDE{K,I} <: AbstractKDE kde::K itp::I InterpKDE{K,I}(kde::K, itp::I) where {K,I} = new{K,I}(kde, itp) end - function InterpKDE(kde::UnivariateKDE, opts...) itp_u = interpolate(kde.density, opts...) itp = scale(itp_u, kde.x) diff --git a/src/univariate.jl b/src/univariate.jl index 5f71a809..993c4339 100644 --- a/src/univariate.jl +++ b/src/univariate.jl @@ -13,7 +13,7 @@ sum(density) * step(x) ≈ 1 $(FIELDS) """ -mutable struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE +struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE "Gridpoints for evaluating the density." x::R "Kernel density at corresponding gridpoints `x`." From cd7723ea3a238b4153781f076f3b63cc1303a263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 01:09:09 +0100 Subject: [PATCH 02/17] broadcast --- .gitignore | 1 + Project.toml | 5 +++++ src/interp.jl | 5 +++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 291b930a..d66bf271 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ examples/.ipynb_checkpoints/* Manifest.toml .vscode/settings.json tmp.jl +Project.toml diff --git a/Project.toml b/Project.toml index e0df42fe..7a9ff6df 100644 --- a/Project.toml +++ b/Project.toml @@ -4,10 +4,15 @@ authors = ["Simon Byrne and various contributors"] version = "0.6.8" [deps] +BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" +Loess = "4345ca2d-374a-55d4-8d30-97f9976e7612" +LsqFit = "2fda8390-95c7-5789-9bda-21331edee243" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] diff --git a/src/interp.jl b/src/interp.jl index 4503e43d..cb125f20 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -23,9 +23,10 @@ function InterpKDE(kde::BivariateKDE, opts...) end InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Line(OnGrid())))) +pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE,xs::AbstractVector) = [ik.itp(x) for x in xs] -pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = [ik.itp(x,y) for x in xs, y in ys] +Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = InterpKDE(k).itp.(xs) -pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) +pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = [ik.itp(x,y) for x in xs, y in ys] From eee2128baa87705113c893fb3d1a4b5360581e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 01:39:55 +0100 Subject: [PATCH 03/17] Update interp.jl --- src/interp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interp.jl b/src/interp.jl index cb125f20..435a9acc 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -26,7 +26,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE,xs::AbstractVector) = [ik.itp(x) for x in xs] -Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = InterpKDE(k).itp.(xs) +Base.broadcast(::typeof(pdf),k::UnivariateKDE,xs) = InterpKDE(k).itp.(xs) pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = [ik.itp(x,y) for x in xs, y in ys] From 976c63088369cdc97fd249989725a0cf403fa2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 14:53:33 +0100 Subject: [PATCH 04/17] interface tests --- src/interp.jl | 17 ++++++++++++----- src/univariate.jl | 2 +- test/interp.jl | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/interp.jl b/src/interp.jl index cb125f20..e8e1afc9 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -1,6 +1,6 @@ import Interpolations: interpolate, scale -struct InterpKDE{K,I} <: AbstractKDE +mutable struct InterpKDE{K,I} <: AbstractKDE kde::K itp::I InterpKDE{K,I}(kde::K, itp::I) where {K,I} = new{K,I}(kde, itp) @@ -23,10 +23,17 @@ function InterpKDE(kde::BivariateKDE, opts...) end InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Line(OnGrid())))) -pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) -pdf(ik::InterpKDE,xs::AbstractVector) = [ik.itp(x) for x in xs] -Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = InterpKDE(k).itp.(xs) + +# interface implementation +# it should be consistent with Distributions.pdf + +pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) +Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE(k).itp, xs) +pdf(ik::InterpKDE,xs::AbstractVector) = pdf.(ik, xs) pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) -pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = [ik.itp(x,y) for x in xs, y in ys] +pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = ik.itp.(xs,ys') +pdf(k::BivariateKDE, M) = pdf(InterpKDE(k),M) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, 1}) = ik.itp(M[1],M[2]) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) diff --git a/src/univariate.jl b/src/univariate.jl index 993c4339..5f71a809 100644 --- a/src/univariate.jl +++ b/src/univariate.jl @@ -13,7 +13,7 @@ sum(density) * step(x) ≈ 1 $(FIELDS) """ -struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE +mutable struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE "Gridpoints for evaluating the density." x::R "Kernel density at corresponding gridpoints `x`." diff --git a/test/interp.jl b/test/interp.jl index ed93d914..1a917145 100644 --- a/test/interp.jl +++ b/test/interp.jl @@ -23,3 +23,19 @@ k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) @test pdf(k, +10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [+10.0, 0.0]) @test pdf(k, 0.0, -10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, -10.0]) @test pdf(k, 0.0, +10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, +10.0]) + +@testset "pdf method interface" begin + k = kde([-1., 1.]) + ik = InterpKDE(k) + + @test pdf.(k, [0., 1.]) ≈ [pdf(k, 0.), pdf(k, 1.)] ≈ pdf.(k,[0., 1.]) ≈ [pdf(ik, 0.), pdf(ik, 1.)] + @test all( pdf.(k, (0., 1.)) .≈ (pdf(k, 0.), pdf(k, 1.)) ) + @test pdf.(k, [0. 1.; 2. -1.]) ≈ [pdf(k, 0.) pdf(k, 1.); pdf(k, 2.) pdf(k, -1.)] + + k2d = kde(([-1., 1.], [0., 1.])) + ik2d = InterpKDE(k2d) + + @test pdf(k2d, [0.5, 0.1]) ≈ pdf(k2d, 0.5, 0.1) ≈ pdf(ik2d, 0.5, 0.1) + @test pdf(k2d, [0.5 1.; 0.1 1.]) ≈ [pdf(ik2d, 0.5, 0.1), pdf(ik2d, 1., 1.)] + @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] +end \ No newline at end of file From e00c4ccd5c48ad3a5ffc7e26e9d5a1a1b9202882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:12:44 +0100 Subject: [PATCH 05/17] Update interp.jl --- src/interp.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/interp.jl b/src/interp.jl index e8e1afc9..b294827f 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -30,10 +30,9 @@ pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE(k).itp, xs) -pdf(ik::InterpKDE,xs::AbstractVector) = pdf.(ik, xs) pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = ik.itp.(xs,ys') pdf(k::BivariateKDE, M) = pdf(InterpKDE(k),M) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, 1}) = ik.itp(M[1],M[2]) +pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V[1],V[2]) pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) From 6ba8bad9e553e27a6ffca65462c9f6240427d2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:06:20 +0100 Subject: [PATCH 06/17] comments --- src/interp.jl | 8 ++++++-- test/interp.jl | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/interp.jl b/src/interp.jl index b294827f..ed5c0aa6 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -25,14 +25,18 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) -# interface implementation +# pdf interface implementation # it should be consistent with Distributions.pdf +# 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE(k).itp, xs) +# 2 dimensions pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = ik.itp.(xs,ys') pdf(k::BivariateKDE, M) = pdf(InterpKDE(k),M) -pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V[1],V[2]) pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) + +# any dimension +pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) diff --git a/test/interp.jl b/test/interp.jl index 1a917145..d2f7c4a6 100644 --- a/test/interp.jl +++ b/test/interp.jl @@ -35,7 +35,7 @@ k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) k2d = kde(([-1., 1.], [0., 1.])) ik2d = InterpKDE(k2d) - @test pdf(k2d, [0.5, 0.1]) ≈ pdf(k2d, 0.5, 0.1) ≈ pdf(ik2d, 0.5, 0.1) + @test pdf(k2d, [0.5, 0.1]) ≈ pdf(k2d, [0.5; 0.1]) ≈ pdf(k2d, 0.5, 0.1) ≈ pdf(ik2d, 0.5, 0.1) @test pdf(k2d, [0.5 1.; 0.1 1.]) ≈ [pdf(ik2d, 0.5, 0.1), pdf(ik2d, 1., 1.)] @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] end \ No newline at end of file From a06b49a71ba0343f063c5d14b07b48924546a3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:16:55 +0100 Subject: [PATCH 07/17] Update interp.jl --- src/interp.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/interp.jl b/src/interp.jl index ed5c0aa6..b92525b4 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -23,11 +23,15 @@ function InterpKDE(kde::BivariateKDE, opts...) end InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Line(OnGrid())))) -pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) # pdf interface implementation # it should be consistent with Distributions.pdf +# any dimension +pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) +pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) + # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE(k).itp, xs) @@ -36,7 +40,3 @@ Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) pdf(ik::InterpKDE,xs::AbstractVector,ys::AbstractVector) = ik.itp.(xs,ys') pdf(k::BivariateKDE, M) = pdf(InterpKDE(k),M) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) - -# any dimension -pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) From 9fd648a0462e69c99f6a6ba607651b457ec429b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:33:13 +0100 Subject: [PATCH 08/17] Update Project.toml --- Project.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Project.toml b/Project.toml index 7a9ff6df..e0df42fe 100644 --- a/Project.toml +++ b/Project.toml @@ -4,15 +4,10 @@ authors = ["Simon Byrne and various contributors"] version = "0.6.8" [deps] -BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" -Loess = "4345ca2d-374a-55d4-8d30-97f9976e7612" -LsqFit = "2fda8390-95c7-5789-9bda-21331edee243" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] From b30a95025b9c1bc5f3536d54b77685b75f034a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:43:18 +0100 Subject: [PATCH 09/17] Update interp.jl --- test/interp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/interp.jl b/test/interp.jl index d2f7c4a6..cbdeb7c5 100644 --- a/test/interp.jl +++ b/test/interp.jl @@ -13,7 +13,7 @@ k = kde((X,Y)) # Try to evaluate the KDE outside the interpolation domain # The KDE is allowed to be zero, but it should not be greater than the exact solution k = kde([0.0], bandwidth=1.0) -@test pdf(k, k.x) ≈ k.density +@test pdf.(k, k.x) ≈ k.density @test pdf(k, -10.0) ≤ pdf(Normal(), -10.0) @test pdf(k, +10.0) ≤ pdf(Normal(), +10.0) From 9974b8c935156c1d622284d1b8012f4bb3b7a035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:47:56 +0100 Subject: [PATCH 10/17] tests update --- test/interp.jl | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/test/interp.jl b/test/interp.jl index cbdeb7c5..3b02de07 100644 --- a/test/interp.jl +++ b/test/interp.jl @@ -1,28 +1,30 @@ using Test using KernelDensity -X = randn(100) -Y = randn(100) - -k = kde(X) -@test pdf(k, k.x) ≈ k.density - -k = kde((X,Y)) -@test pdf(k, k.x, k.y) ≈ k.density - -# Try to evaluate the KDE outside the interpolation domain -# The KDE is allowed to be zero, but it should not be greater than the exact solution -k = kde([0.0], bandwidth=1.0) -@test pdf.(k, k.x) ≈ k.density -@test pdf(k, -10.0) ≤ pdf(Normal(), -10.0) -@test pdf(k, +10.0) ≤ pdf(Normal(), +10.0) - -k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) -@test pdf(k, k.x, k.y) ≈ k.density -@test pdf(k, -10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [-10.0, 0.0]) -@test pdf(k, +10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [+10.0, 0.0]) -@test pdf(k, 0.0, -10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, -10.0]) -@test pdf(k, 0.0, +10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, +10.0]) +@testset "interpolation computation" begin + X = randn(100) + Y = randn(100) + + k = kde(X) + @test pdf.(k, k.x) ≈ k.density + + k = kde((X,Y)) + @test pdf(k, k.x, k.y) ≈ k.density + + # Try to evaluate the KDE outside the interpolation domain + # The KDE is allowed to be zero, but it should not be greater than the exact solution + k = kde([0.0], bandwidth=1.0) + @test pdf.(k, k.x) ≈ k.density + @test pdf(k, -10.0) ≤ pdf(Normal(), -10.0) + @test pdf(k, +10.0) ≤ pdf(Normal(), +10.0) + + k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) + @test pdf(k, k.x, k.y) ≈ k.density + @test pdf(k, -10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [-10.0, 0.0]) + @test pdf(k, +10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [+10.0, 0.0]) + @test pdf(k, 0.0, -10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, -10.0]) + @test pdf(k, 0.0, +10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, +10.0]) +end @testset "pdf method interface" begin k = kde([-1., 1.]) @@ -38,4 +40,4 @@ k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) @test pdf(k2d, [0.5, 0.1]) ≈ pdf(k2d, [0.5; 0.1]) ≈ pdf(k2d, 0.5, 0.1) ≈ pdf(ik2d, 0.5, 0.1) @test pdf(k2d, [0.5 1.; 0.1 1.]) ≈ [pdf(ik2d, 0.5, 0.1), pdf(ik2d, 1., 1.)] @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] -end \ No newline at end of file +end From eac3b90ca3840977407d3527d8d25980eb323074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 20:02:14 +0100 Subject: [PATCH 11/17] Update interp.jl --- src/interp.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interp.jl b/src/interp.jl index b92525b4..3f2e726a 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -34,7 +34,7 @@ pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, d # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) -Base.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.broadcasted(InterpKDE(k).itp, xs) +Base.Broadcast.broadcasted(::typeof(pdf),k::UnivariateKDE,xs) = Base.Broadcast.broadcasted(InterpKDE(k).itp, xs) # 2 dimensions pdf(k::BivariateKDE,x,y) = pdf(InterpKDE(k),x,y) From d3ade85b48fa4ac7a7c959ea86a49535f13e0ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Wed, 7 Feb 2024 23:49:47 +0100 Subject: [PATCH 12/17] trying Compat for eachslice in Julia 1.0 --- Project.toml | 1 + src/KernelDensity.jl | 1 + src/interp.jl | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e0df42fe..e9ed4a9d 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] +Compat = "2.2.0" Distributions = "0.23, 0.24, 0.25" DocStringExtensions = "0.8, 0.9" FFTW = "1" diff --git a/src/KernelDensity.jl b/src/KernelDensity.jl index b6201c8a..61cde468 100644 --- a/src/KernelDensity.jl +++ b/src/KernelDensity.jl @@ -4,6 +4,7 @@ using DocStringExtensions: TYPEDEF, FIELDS using StatsBase using Distributions using Interpolations +using Compat import Distributions: twoπ, pdf import FFTW: rfft, irfft diff --git a/src/interp.jl b/src/interp.jl index 3f2e726a..a404ec7d 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -30,7 +30,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li # any dimension pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) +@compat pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) From 8d44f99d30d570338b1bd9948a249f69a952076d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Thu, 8 Feb 2024 00:02:56 +0100 Subject: [PATCH 13/17] remove Compat --- Project.toml | 1 - src/KernelDensity.jl | 1 - src/interp.jl | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index e9ed4a9d..e0df42fe 100644 --- a/Project.toml +++ b/Project.toml @@ -11,7 +11,6 @@ Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] -Compat = "2.2.0" Distributions = "0.23, 0.24, 0.25" DocStringExtensions = "0.8, 0.9" FFTW = "1" diff --git a/src/KernelDensity.jl b/src/KernelDensity.jl index 61cde468..b6201c8a 100644 --- a/src/KernelDensity.jl +++ b/src/KernelDensity.jl @@ -4,7 +4,6 @@ using DocStringExtensions: TYPEDEF, FIELDS using StatsBase using Distributions using Interpolations -using Compat import Distributions: twoπ, pdf import FFTW: rfft, irfft diff --git a/src/interp.jl b/src/interp.jl index a404ec7d..3f2e726a 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -30,7 +30,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li # any dimension pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) -@compat pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) From 84eaa9689b7272e5da9ccbab765dae88bd7bd3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:33:52 +0100 Subject: [PATCH 14/17] adding Compat --- Project.toml | 2 ++ src/interp.jl | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index e0df42fe..e3bd9594 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Simon Byrne and various contributors"] version = "0.6.8" [deps] +Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" @@ -17,6 +18,7 @@ FFTW = "1" Interpolations = "0.9, 0.10, 0.11, 0.12, 0.13, 0.14, 0.15" StatsBase = "0.33, 0.34" julia = "1" +Compat = "3.22, 4" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/interp.jl b/src/interp.jl index 3f2e726a..ea56df1b 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -1,5 +1,7 @@ +import Compat: @compat import Interpolations: interpolate, scale + mutable struct InterpKDE{K,I} <: AbstractKDE kde::K itp::I @@ -30,7 +32,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li # any dimension pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = @compat pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) From 60db7b0a93687f9a586d7086883383623b118601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:58:20 +0100 Subject: [PATCH 15/17] Compat ver --- Project.toml | 2 +- src/interp.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index e3bd9594..c43890d0 100644 --- a/Project.toml +++ b/Project.toml @@ -18,7 +18,7 @@ FFTW = "1" Interpolations = "0.9, 0.10, 0.11, 0.12, 0.13, 0.14, 0.15" StatsBase = "0.33, 0.34" julia = "1" -Compat = "3.22, 4" +Compat = "2.2.0, 4" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/interp.jl b/src/interp.jl index ea56df1b..c4498242 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -1,4 +1,4 @@ -import Compat: @compat +import Compat: eachslice import Interpolations: interpolate, scale @@ -32,7 +32,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li # any dimension pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = @compat pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) From 8b0126c67426cf1ed8761e0edf8730032d717e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Mon, 11 Mar 2024 23:19:52 +0100 Subject: [PATCH 16/17] rem Compat, eachslice to maplices + reshape --- Project.toml | 2 -- src/interp.jl | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index c43890d0..e0df42fe 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,6 @@ authors = ["Simon Byrne and various contributors"] version = "0.6.8" [deps] -Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" @@ -18,7 +17,6 @@ FFTW = "1" Interpolations = "0.9, 0.10, 0.11, 0.12, 0.13, 0.14, 0.15" StatsBase = "0.33, 0.34" julia = "1" -Compat = "2.2.0, 4" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/interp.jl b/src/interp.jl index c4498242..748dbc90 100644 --- a/src/interp.jl +++ b/src/interp.jl @@ -1,4 +1,3 @@ -import Compat: eachslice import Interpolations: interpolate, scale @@ -32,7 +31,7 @@ InterpKDE(kde::BivariateKDE) = InterpKDE(kde::BivariateKDE, BSpline(Quadratic(Li # any dimension pdf(ik::InterpKDE,x::Real...) = ik.itp(x...) pdf(ik::InterpKDE, V::AbstractVector) = ik.itp(V...) -pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = pdf.(ik,eachslice(M, dims=ntuple(i->i+1, N-1)) ) +pdf(ik::InterpKDE, M::AbstractArray{<:Real, N}) where N = reshape(mapslices(v->pdf(ik, v), M, dims = 1), size(M)[2:end]) # 1 dimension pdf(k::UnivariateKDE,x) = pdf(InterpKDE(k),x) From 8cd5a1c6c1ae1bae7620948319900d92a79f556a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=9Al=C4=99zak?= <128084860+jaksle@users.noreply.github.com> Date: Mon, 11 Mar 2024 23:36:05 +0100 Subject: [PATCH 17/17] alternative to ;;; --- test/interp.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/interp.jl b/test/interp.jl index 3b02de07..b2ea5411 100644 --- a/test/interp.jl +++ b/test/interp.jl @@ -39,5 +39,12 @@ end @test pdf(k2d, [0.5, 0.1]) ≈ pdf(k2d, [0.5; 0.1]) ≈ pdf(k2d, 0.5, 0.1) ≈ pdf(ik2d, 0.5, 0.1) @test pdf(k2d, [0.5 1.; 0.1 1.]) ≈ [pdf(ik2d, 0.5, 0.1), pdf(ik2d, 1., 1.)] - @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] + @static if VERSION >= v"1.1" + @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] + else + M = zeros(2, 1, 2) + M[:,1,1] .= [0.5, 1] + M[:,1,2] .= [0.1, 1] + @test pdf(k2d, M) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] + end end