|
1 | 1 | using Test |
2 | 2 | using KernelDensity |
3 | 3 |
|
4 | | -X = randn(100) |
5 | | -Y = randn(100) |
6 | | - |
7 | | -k = kde(X) |
8 | | -@test pdf(k, k.x) ≈ k.density |
9 | | - |
10 | | -k = kde((X,Y)) |
11 | | -@test pdf(k, k.x, k.y) ≈ k.density |
12 | | - |
13 | | -# Try to evaluate the KDE outside the interpolation domain |
14 | | -# The KDE is allowed to be zero, but it should not be greater than the exact solution |
15 | | -k = kde([0.0], bandwidth=1.0) |
16 | | -@test pdf(k, k.x) ≈ k.density |
17 | | -@test pdf(k, -10.0) ≤ pdf(Normal(), -10.0) |
18 | | -@test pdf(k, +10.0) ≤ pdf(Normal(), +10.0) |
19 | | - |
20 | | -k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) |
21 | | -@test pdf(k, k.x, k.y) ≈ k.density |
22 | | -@test pdf(k, -10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [-10.0, 0.0]) |
23 | | -@test pdf(k, +10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [+10.0, 0.0]) |
24 | | -@test pdf(k, 0.0, -10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, -10.0]) |
25 | | -@test pdf(k, 0.0, +10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, +10.0]) |
| 4 | +@testset "interpolation computation" begin |
| 5 | + X = randn(100) |
| 6 | + Y = randn(100) |
| 7 | + |
| 8 | + k = kde(X) |
| 9 | + @test pdf.(k, k.x) ≈ k.density |
| 10 | + |
| 11 | + k = kde((X,Y)) |
| 12 | + @test pdf(k, k.x, k.y) ≈ k.density |
| 13 | + |
| 14 | + # Try to evaluate the KDE outside the interpolation domain |
| 15 | + # The KDE is allowed to be zero, but it should not be greater than the exact solution |
| 16 | + k = kde([0.0], bandwidth=1.0) |
| 17 | + @test pdf.(k, k.x) ≈ k.density |
| 18 | + @test pdf(k, -10.0) ≤ pdf(Normal(), -10.0) |
| 19 | + @test pdf(k, +10.0) ≤ pdf(Normal(), +10.0) |
| 20 | + |
| 21 | + k = kde(([0.0],[0.0]), bandwidth=(1.0, 1.0)) |
| 22 | + @test pdf(k, k.x, k.y) ≈ k.density |
| 23 | + @test pdf(k, -10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [-10.0, 0.0]) |
| 24 | + @test pdf(k, +10.0, 0.0) ≤ pdf(MvNormal(2, 1.0), [+10.0, 0.0]) |
| 25 | + @test pdf(k, 0.0, -10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, -10.0]) |
| 26 | + @test pdf(k, 0.0, +10.0) ≤ pdf(MvNormal(2, 1.0), [0.0, +10.0]) |
| 27 | +end |
| 28 | + |
| 29 | +@testset "pdf method interface" begin |
| 30 | + k = kde([-1., 1.]) |
| 31 | + ik = InterpKDE(k) |
| 32 | + |
| 33 | + @test pdf.(k, [0., 1.]) ≈ [pdf(k, 0.), pdf(k, 1.)] ≈ pdf.(k,[0., 1.]) ≈ [pdf(ik, 0.), pdf(ik, 1.)] |
| 34 | + @test all( pdf.(k, (0., 1.)) .≈ (pdf(k, 0.), pdf(k, 1.)) ) |
| 35 | + @test pdf.(k, [0. 1.; 2. -1.]) ≈ [pdf(k, 0.) pdf(k, 1.); pdf(k, 2.) pdf(k, -1.)] |
| 36 | + |
| 37 | + k2d = kde(([-1., 1.], [0., 1.])) |
| 38 | + ik2d = InterpKDE(k2d) |
| 39 | + |
| 40 | + @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) |
| 41 | + @test pdf(k2d, [0.5 1.; 0.1 1.]) ≈ [pdf(ik2d, 0.5, 0.1), pdf(ik2d, 1., 1.)] |
| 42 | + @test pdf(k2d, [0.5; 1. ;;; 0.1; 1.]) ≈ [pdf(ik2d, 0.5, 1.) pdf(ik2d, 0.1, 1.)] |
| 43 | +end |
0 commit comments