Skip to content

Commit c9ffa63

Browse files
authored
Merge pull request #23 from JuliaAI/dev
For a 0.1.5 release
2 parents 40bad35 + 00d937f commit c9ffa63

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StatisticalMeasures"
22
uuid = "a19d573c-0a75-4610-95b3-7071388c7541"
33
authors = ["Anthony D. Blaom <anthony.blaom@gmail.com>"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"

src/confusion_matrices.jl

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ ordered confusion matrices, see [`$CM.confmat`](@ref).
185185
186186
"""
187187
struct ConfusionMatrix{N,O,L}
188-
mat::Matrix{Int}
189-
index_given_level::LittleDict{L, Int, NTuple{N,L}, NTuple{N,Int}}
188+
mat::Matrix{<:Integer}
189+
index_given_level::LittleDict{L, I, NTuple{N,L}, NTuple{N,I}} where I<:Integer
190190
end
191191

192192
"""
@@ -204,14 +204,13 @@ See also [`$CM.confmat`](@ref).
204204
"""
205205
function ConfusionMatrix(
206206
m,
207-
dic::AbstractDict{L,I};
207+
dic::LittleDict{L, I, NTuple{N,L}, NTuple{N,I}};
208208
checks=true,
209209
ordered=false,
210-
) where {L,I<:Integer}
211-
s = size(m)
212-
N = s[1]
210+
) where {L,I<:Integer,N}
213211
if checks
214-
N == s[2] || throw(ArgumentError("Expected a square matrix."))
212+
s = size(m)
213+
N == s[1] == s[2] || throw(ArgumentError("Expected a square matrix."))
215214
N > 1 || throw(ArgumentError("Expected a matrix of size ≥ 2x2."))
216215
length(unique(keys(dic))) == N || throw(ArgumentError(
217216
"Expected dictionary with $N unique keys (levels) as "*
@@ -222,8 +221,15 @@ function ConfusionMatrix(
222221
"to be integers from 1 to $N. "
223222
))
224223
end
225-
index_given_level = freeze(dic)
226-
ConfusionMatrix{N,ordered,L}(m, index_given_level)
224+
ConfusionMatrix{N,ordered,L}(m, dic)
225+
end
226+
function ConfusionMatrix(
227+
m,
228+
dic::AbstractDict;
229+
checks=true,
230+
ordered=false,
231+
)
232+
ConfusionMatrix(m, freeze(dic); checks, ordered)
227233
end
228234

229235
"""
@@ -251,7 +257,7 @@ function ConfusionMatrix(m, levels::AbstractVector{L}; ordered=false, checks=tru
251257
))
252258
end
253259
index_given_level =
254-
LittleDict{L, Int, Vector{L}, Vector{Int}}(levels, eachindex(levels)) |> freeze
260+
LittleDict{L, Int, Vector{L}, Vector{Int}}(levels, eachindex(levels))
255261
ConfusionMatrix(m, index_given_level; ordered, checks=false)
256262
end
257263

@@ -489,8 +495,8 @@ function confmat(ŷ, y, _levels, _perm, rev)
489495
perm = permutation(_perm, rev, levels)
490496

491497
levels = apply(perm, levels)
492-
indexer = LittleDict(levels[i] => i for i in eachindex(levels)) |> freeze
493-
498+
L = eltype(levels)
499+
indexer = LittleDict{L, Int, Vector{L}, Vector{Int}}(levels, eachindex(levels)) |> freeze
494500
_confmat(ŷ, y, indexer, levels, ordered)
495501
end
496502

@@ -533,18 +539,26 @@ end
533539

534540

535541
# ## Final method to do the computation
536-
537-
function _confmat(ŷ, y, indexer, levels, ordered)
542+
function _confmat(ŷ, y, indexer::F, levels, ordered) where F
538543
nc = length(levels)
539544
cmat = zeros(Int, nc, nc)
540545
@inbounds for i in eachindex(y)
541546
(ismissing(y[i]) || ismissing(ŷ[i])) && continue
542547
cmat[get(indexer, ŷ[i]), get(indexer, y[i])] += 1
543548
end
544-
index_given_level = LittleDict(c => get(indexer, c) for c in levels) |> freeze
545549
return ConfusionMatrix(cmat, levels; ordered, checks=false)
546550
end
547551

552+
function _confmat(ŷ, y, indexer::AbstractDict{L,I}, levels, ordered) where {L,I<:Integer}
553+
nc = length(levels)
554+
cmat = zeros(Int, nc, nc)
555+
@inbounds for i in eachindex(y)
556+
(ismissing(y[i]) || ismissing(ŷ[i])) && continue
557+
cmat[get(indexer, ŷ[i]), get(indexer, y[i])] += 1
558+
end
559+
return ConfusionMatrix(cmat, indexer; ordered, checks=false)
560+
end
561+
548562

549563
# DISPLAY
550564

src/continuous.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ const RootMeanSquaredProportionalErrorDoc = docstring(
351351
scitype=DOC_INFINITE,
352352
body=
353353
"""
354-
Specifically, compute the mean of ``(ŷ_i-y_i \\over y_i)^2}`` over all pairs of
355-
observations ``(ŷ_i, y_i)`` in `(ŷ, y)`, and return the square root of the result. More
354+
Specifically, compute the mean of `((ŷᵢ-yᵢ)/yᵢ)^2}` over all pairs of
355+
observations `(ŷᵢ, yᵢ)` in `(ŷ, y)`, and return the square root of the result. More
356356
generally, pre-multiply the values averaged by the specified weights. Terms for which
357-
``|y_i|``<`tol` are dropped in the summation, but counts still
357+
`abs(yᵢ) < tol` are dropped in the summation, but counts still
358358
contribute to the mean normalization factor.
359359
""",
360360
)

src/finite.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,13 @@ const BalancedAccuracyType = API.FussyMeasure{
293293
human_name="balanced accuracy",
294294
)
295295

296-
register(BalancedAccuracy, "balanced_accuracy", "bacc", "bac")
296+
register(
297+
BalancedAccuracy,
298+
"balanced_accuracy",
299+
"bacc",
300+
"bac",
301+
"Ppprobability_of_correct_classification",
302+
)
297303

298304
const BalancedAccuracyDoc = docstring(
299305
"BalancedAccuracy(; adjusted=false)",

src/probabilistic.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ const LogScoreDoc = docstring(
178178
body=
179179
"""
180180
The score is a mean of observational scores. More generally, observational scores are
181-
pre-multiplied by the specified weights before avaraging. See below for the form that
181+
pre-multiplied by the specified weights before averaging. See below for the form that
182182
probabilistic predictions `ŷ` should take. Raw probabilities are clamped away from `0` and
183183
`1`. Specifically, if `p` is the probability mass/density function evaluated at given
184184
observed ground truth observation `η`, then the score for that example is defined as
@@ -228,6 +228,7 @@ const LogLossType = API.FussyMeasure{<:API.RobustMeasure{<:_LogLossType}}
228228
@trait(
229229
_LogLossType,
230230
consumes_multiple_observations=true,
231+
can_report_unaggregated=true,
231232
kind_of_proxy=LearnAPI.Distribution(),
232233
# observation_scitype depends on distribution type
233234
observation_scitype = Union{Missing,Finite,Infinite},
@@ -328,7 +329,7 @@ const BrierScoreDoc = docstring(
328329
body=
329330
"""
330331
The score is a mean of observational scores. More generally, observational scores are
331-
pre-multiplied by the specified weights before avaraging. See below for the form that
332+
pre-multiplied by the specified weights before averaging. See below for the form that
332333
probabilistic predictions `ŷ` should take.
333334
334335
Convention as in $PROPER_SCORING_RULES
@@ -398,6 +399,7 @@ const BrierLossType = API.FussyMeasure{<:API.RobustMeasure{<:_BrierLossType}}
398399
@trait(
399400
_BrierLossType,
400401
consumes_multiple_observations=true,
402+
can_report_unaggregated=true,
401403
kind_of_proxy=LearnAPI.Distribution(),
402404
# observation_scitype depends on distribution type
403405
observation_scitype = Union{Missing,Finite,Infinite},
@@ -513,7 +515,7 @@ const SphericalScoreDoc = docstring(
513515
body=
514516
"""
515517
The score is a mean of observational scores. More generally, observational scores are
516-
pre-multiplied by the specified weights before avaraging. See below for the form that
518+
pre-multiplied by the specified weights before averaging. See below for the form that
517519
probabilistic predictions `ŷ` should take.
518520
519521
Convention as in $PROPER_SCORING_RULES: If `y` takes on a finite

0 commit comments

Comments
 (0)