Last active
August 29, 2015 13:56
-
-
Save ToJans/8966856 to your computer and use it in GitHub Desktop.
Statistics usable for data series
This file contains hidden or 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
Stream.repeatedly(&:random.uniform/0) | |
|> Enum.take(10000) | |
|> Stats.all | |
|> IO.inspect |
This file contains hidden or 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
#run using `elixirc stats.ex && elixir example.ex` | |
[len: 10000, avg: 0.5000783694558532, | |
range: {2.4128016570301725e-4, 0.9999365376549191}, stdev: 0.29015552976626224, | |
uniq: 10000, | |
pct: [{99, 0.9892887006250969}, {98, 0.9803346036167921}, | |
{95, 0.9518418120922166}, {90, 0.9028639280861167}, {80, 0.8028527301428907}, | |
{60, 0.6020177938775699}, {50, 0.4995763856900868}]] |
This file contains hidden or 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
defmodule Stats do | |
def all(data) do | |
len = length(data) | |
avg = sum(data)/len | |
[len: len, | |
avg: avg, | |
range: {Enum.min(data),Enum.max(data)}, | |
stdev: stdev(data,len,avg), | |
uniq: length(Enum.uniq(data)), | |
pct: percentiles(data, len) | |
] | |
end | |
def stdev(data,len,avg) do | |
total = data | |
|> Enum.map(&(&1-avg)) | |
|> Enum.map(&(&1*&1)) | |
|> sum() | |
:math.sqrt(total / len) | |
end | |
def sum(data) do | |
Enum.reduce(data,0,&(&1+&2)) | |
end | |
def percentiles(data, len) do | |
sorted = Enum.sort(data) | |
[99,98,95,90,80,60,50] | |
|> Enum.map( &({&1,Enum.at(sorted,round(&1*len/100))})) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment