Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ranking code #589

Merged
merged 7 commits into from
Feb 1, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions src/ranking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ function _rank(f!, x::AbstractArray{>: Missing}, R::Type=Int; sortkwargs...)
inds = findall(!ismissing, vec(x))
isempty(inds) && return missings(R, size(x))
T = nonmissingtype(eltype(x))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed that T is not used anymore, will remove this line

xv = Vector{T}(undef, length(inds))
@inbounds for (i, ind) in enumerate(inds)
xv[i] = x[ind]
end
rks = missings(R, size(x))
xv = disallowmissing(view(vec(x), inds))
ordv = sortperm(xv; sortkwargs...)
rks = missings(R, size(x))
f!(view(rks, inds), xv, ordv)
return rks
end
Expand All @@ -45,12 +42,11 @@ end


"""
ordinalrank(x; sortkwargs...)
ordinalrank(x; lt=isless, by=identity, rev::Bool=false, ...)

Return the [ordinal ranking](https://en.wikipedia.org/wiki/Ranking#Ordinal_ranking_.28.221234.22_ranking.29)
("1234" ranking) of an array. The `lt` keyword allows providing a custom "less
than" function; use `rev=true` to reverse the sorting order.
All items in `x` are given distinct, successive ranks based on their
("1234" ranking) of an array. Supports the same keyword arguments as `sort(x; sortkwargs...)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sortkwargs isn't defined now. Same below.

Suggested change
("1234" ranking) of an array. Supports the same keyword arguments as `sort(x; sortkwargs...)`
("1234" ranking) of an array. Supports the same keyword arguments as the `sort`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, in L48 I sort-of introduce the sortkwargs, because otherwise later in the docstring it would not be entirely clear that the keyword args are passed through to sort to define the element order. I think from the context (supports the same keyword args as ...) it should be clear that sortkwargs refers to the keyword arguments of ordinalranks(). But if you have a better suggestion, I'm eager to fix it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I would just have said "in the sorted vector" in the next sentence.

As you prefer, but if you keep this then please replace sort(x) with just sort below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds good. I also simplified a bit the language in the other ranking docstrings.

function. All items in `x` are given distinct, successive ranks based on their
position in `sort(x; sortkwargs...)`.
Missing values are assigned rank `missing`.
"""
Expand All @@ -67,17 +63,14 @@ function _competerank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
v = x[p1]
rks[p1] = k = 1

i = 2
while i <= n
for i in 2:n
pi = p[i]
xi = x[pi]
if xi == v
rks[pi] = k
else
rks[pi] = k = i
if xi != v
v = xi
k = i
end
i += 1
rks[pi] = k
end
end

Expand All @@ -86,11 +79,10 @@ end


"""
competerank(x; sortkwargs...)
competerank(x; lt=isless, by=identity, rev::Bool=false, ...)

Return the [standard competition ranking](http://en.wikipedia.org/wiki/Ranking#Standard_competition_ranking_.28.221224.22_ranking.29)
("1224" ranking) of an array. The `lt` keyword allows providing a custom "less
than" function; use `rev=true` to reverse the sorting order.
("1224" ranking) of an array. Supports the same keyword arguments as `sort(x)` function.
Items that compare equal are given the same rank, then a gap is left
in the rankings the size of the number of tied items - 1.
Missing values are assigned rank `missing`.
Expand All @@ -108,17 +100,14 @@ function _denserank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
v = x[p1]
rks[p1] = k = 1

i = 2
while i <= n
for i in 2:n
pi = p[i]
xi = x[pi]
if xi == v
rks[pi] = k
else
rks[pi] = (k += 1)
if xi != v
v = xi
k += 1
end
i += 1
rks[pi] = k
end
end

Expand All @@ -127,12 +116,11 @@ end


"""
denserank(x; sortkwargs...)
denserank(x; lt=isless, by=identity, rev::Bool=false, ...)

Return the [dense ranking](http://en.wikipedia.org/wiki/Ranking#Dense_ranking_.28.221223.22_ranking.29)
("1223" ranking) of an array. The `lt` keyword allows providing a custom "less
than" function; use `rev=true` to reverse the sorting order. Items that
compare equal receive the same ranking, and the next subsequent rank is
("1223" ranking) of an array. Supports the same keyword arguments as `sort(x)` function.
Items that compare equal receive the same ranking, and the next subsequent rank is
assigned with no gap.
Missing values are assigned rank `missing`.
"""
Expand All @@ -148,8 +136,7 @@ function _tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
v = x[p[1]]

s = 1 # starting index of current range
e = 2 # pass-by-end index of current range
while e <= n
for e in 2:n # e is pass-by-end index of current range
cx = x[p[e]]
if cx != v
# fill average rank to s : e-1
Expand All @@ -161,10 +148,9 @@ function _tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray)
s = e
v = cx
end
e += 1
end

# the last range (e == n+1)
# the last range
ar = (s + n) / 2
for i = s : n
rks[p[i]] = ar
Expand All @@ -176,12 +162,11 @@ end

# order (aka. rank), resolving ties using the mean rank
"""
tiedrank(x; sortkwargs...)
tiedrank(x; lt=isless, by=identity, rev::Bool=false, ...)

Return the [tied ranking](http://en.wikipedia.org/wiki/Ranking#Fractional_ranking_.28.221_2.5_2.5_4.22_ranking.29),
also called fractional or "1 2.5 2.5 4" ranking,
of an array. The `lt` keyword allows providing a custom "less
than" function; use `rev=true` to reverse the sorting order.
of an array. Supports the same keyword arguments as `sort(x)` function.
Items that compare equal receive the mean of the
rankings they would have been assigned under ordinal ranking.
Missing values are assigned rank `missing`.
Expand Down