Created
January 26, 2015 08:52
-
-
Save alexanderscott/584813450db328fc17ab to your computer and use it in GitHub Desktop.
Redis Lua script to return the standard deviation of ZSET scores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- @desc Returns the std deviation of a ZSET key | |
-- @usage redis-cli EVAL "$(cat ZSTDDEV.lua)" 1 <zsetKey> | |
-- @return string representation of float value | |
local function ZSTDDEV(key) | |
local mean, sum, size, sumsqrs = 0, 0, 0, 0 | |
local memberScores = redis.call("ZRANGE", key, 0, -1, "WITHSCORES") | |
local scores = {} | |
for idx,val in ipairs(memberScores) do | |
if(idx % 2 == 0) then | |
sum = sum + val | |
table.insert(scores, val) | |
end | |
end | |
size = #scores | |
mean = sum / size | |
for _,val in ipairs(scores) do | |
sumsqrs = sumsqrs + math.pow((val - mean), 2) | |
end | |
return math.sqrt(sumsqrs/(size)) | |
end | |
--[[ @TEST | |
redis.call("DEL", "test_zstddev") | |
redis.call("ZADD", "test_zstddev", 2, "one", 4, "two", 4, "three", 4, | |
"four", 5, "five", 5, "six", 7, "seven", 9, "eight") | |
local zstddev = ZSTDDEV("test_zstddev") | |
assert(zstddev == 2) | |
--]] | |
return tostring(ZSTDDEV(KEYS[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment