Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Created January 26, 2015 08:52
Show Gist options
  • Save alexanderscott/584813450db328fc17ab to your computer and use it in GitHub Desktop.
Save alexanderscott/584813450db328fc17ab to your computer and use it in GitHub Desktop.
Redis Lua script to return the standard deviation of ZSET scores
-- @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