|
1 | 1 | @testset "unbiased.jl" begin |
2 | 2 | @testset "Unbiased: Two-dimensional example" begin |
| 3 | + # categorical distributions |
3 | 4 | skce = UnbiasedSKCE(SqExponentialKernel() ⊗ WhiteKernel()) |
4 | | - |
5 | | - # only two predictions, i.e., one term in the estimator |
6 | 5 | @test iszero(@inferred(skce([[1, 0], [0, 1]], [1, 2]))) |
7 | 6 | @test iszero(@inferred(skce([[1, 0], [0, 1]], [1, 1]))) |
8 | 7 | @test @inferred(skce([[1, 0], [0, 1]], [2, 1])) ≈ -2 * exp(-1) |
9 | 8 | @test iszero(@inferred(skce([[1, 0], [0, 1]], [2, 2]))) |
| 9 | + |
| 10 | + # probabilities |
| 11 | + skce = UnbiasedSKCE( |
| 12 | + (SqExponentialKernel() ∘ ScaleTransform(sqrt(2))) ⊗ WhiteKernel() |
| 13 | + ) |
| 14 | + @test iszero(@inferred(skce([1, 0], [true, false]))) |
| 15 | + @test iszero(@inferred(skce([1, 0], [true, true]))) |
| 16 | + @test @inferred(skce([1, 0], [false, true])) ≈ -2 * exp(-1) |
| 17 | + @test iszero(@inferred(skce([1, 0], [false, false]))) |
10 | 18 | end |
11 | 19 |
|
12 | 20 | @testset "Unbiased: Basic properties" begin |
13 | 21 | skce = UnbiasedSKCE((ExponentialKernel() ∘ ScaleTransform(0.1)) ⊗ WhiteKernel()) |
14 | 22 | estimates = Vector{Float64}(undef, 1_000) |
15 | 23 |
|
| 24 | + # categorical distributions |
16 | 25 | for nclasses in (2, 10, 100) |
17 | 26 | dist = Dirichlet(nclasses, 1.0) |
18 | 27 |
|
|
30 | 39 | @test any(x -> x < zero(x), estimates) |
31 | 40 | @test mean(estimates) ≈ 0 atol = 1e-3 |
32 | 41 | end |
| 42 | + |
| 43 | + # probabilities |
| 44 | + predictions = Vector{Float64}(undef, 20) |
| 45 | + targets = Vector{Bool}(undef, 20) |
| 46 | + for i in 1:length(estimates) |
| 47 | + rand!(predictions) |
| 48 | + map!(targets, predictions) do p |
| 49 | + rand() < p |
| 50 | + end |
| 51 | + estimates[i] = skce(predictions, targets) |
| 52 | + end |
| 53 | + |
| 54 | + @test any(x -> x > zero(x), estimates) |
| 55 | + @test any(x -> x < zero(x), estimates) |
| 56 | + @test mean(estimates) ≈ 0 atol = 1e-3 |
33 | 57 | end |
34 | 58 |
|
35 | 59 | @testset "Block: Two-dimensional example" begin |
36 | | - # Blocks of two samples |
| 60 | + # categorical distributions |
37 | 61 | skce = BlockUnbiasedSKCE(SqExponentialKernel() ⊗ WhiteKernel()) |
38 | | - |
39 | | - # only two predictions, i.e., one term in the estimator |
40 | 62 | @test iszero(@inferred(skce([[1, 0], [0, 1]], [1, 2]))) |
41 | 63 | @test iszero(@inferred(skce([[1, 0], [0, 1]], [1, 1]))) |
42 | 64 | @test @inferred(skce([[1, 0], [0, 1]], [2, 1])) ≈ -2 * exp(-1) |
|
48 | 70 | @test @inferred(skce(repeat([[1, 0], [0, 1]], 10), repeat([2, 1], 10))) ≈ |
49 | 71 | -2 * exp(-1) |
50 | 72 | @test iszero(@inferred(skce(repeat([[1, 0], [0, 1]], 10), repeat([2, 2], 10)))) |
| 73 | + |
| 74 | + # probabilities |
| 75 | + skce = BlockUnbiasedSKCE( |
| 76 | + (SqExponentialKernel() ∘ ScaleTransform(sqrt(2))) ⊗ WhiteKernel() |
| 77 | + ) |
| 78 | + @test iszero(@inferred(skce([1, 0], [true, false]))) |
| 79 | + @test iszero(@inferred(skce([1, 0], [true, true]))) |
| 80 | + @test @inferred(skce([1, 0], [false, true])) ≈ -2 * exp(-1) |
| 81 | + @test iszero(@inferred(skce([1, 0], [false, false]))) |
| 82 | + |
| 83 | + # two predictions, ten times replicated |
| 84 | + @test iszero(@inferred(skce(repeat([1, 0], 10), repeat([true, false], 10)))) |
| 85 | + @test iszero(@inferred(skce(repeat([1, 0], 10), repeat([true, true], 10)))) |
| 86 | + @test @inferred(skce(repeat([1, 0], 10), repeat([false, true], 10))) ≈ -2 * exp(-1) |
| 87 | + @test iszero(@inferred(skce(repeat([1, 0], 10), repeat([false, false], 10)))) |
51 | 88 | end |
52 | 89 |
|
53 | 90 | @testset "Block: Basic properties" begin |
|
58 | 95 | blockskce_all = BlockUnbiasedSKCE(kernel, nsamples) |
59 | 96 | estimates = Vector{Float64}(undef, 1_000) |
60 | 97 |
|
| 98 | + # categorical distributions |
61 | 99 | for nclasses in (2, 10, 100) |
62 | 100 | dist = Dirichlet(nclasses, 1.0) |
63 | 101 |
|
|
82 | 120 | @test any(x -> x < zero(x), estimates) |
83 | 121 | @test mean(estimates) ≈ 0 atol = 5e-3 |
84 | 122 | end |
| 123 | + |
| 124 | + # probabilities |
| 125 | + predictions = Vector{Float64}(undef, nsamples) |
| 126 | + targets = Vector{Bool}(undef, nsamples) |
| 127 | + |
| 128 | + for i in 1:length(estimates) |
| 129 | + rand!(predictions) |
| 130 | + map!(targets, predictions) do p |
| 131 | + return rand() < p |
| 132 | + end |
| 133 | + estimates[i] = blockskce(predictions, targets) |
| 134 | + |
| 135 | + # consistency checks |
| 136 | + @test estimates[i] ≈ mean( |
| 137 | + skce(predictions[(2 * i - 1):(2 * i)], targets[(2 * i - 1):(2 * i)]) for |
| 138 | + i in 1:(nsamples ÷ 2) |
| 139 | + ) |
| 140 | + @test skce(predictions, targets) == blockskce_all(predictions, targets) |
| 141 | + end |
| 142 | + |
| 143 | + @test any(x -> x > zero(x), estimates) |
| 144 | + @test any(x -> x < zero(x), estimates) |
| 145 | + @test mean(estimates) ≈ 0 atol = 5e-3 |
85 | 146 | end |
86 | 147 | end |
0 commit comments