defmodule SD do
import Enum, only: [map: 2, sum: 1]
import :math, only: [sqrt: 1, pow: 2]
def standard_deviation(data) do
m = mean(data)
data |> variance(m) |> mean |> sqrt
end
def mean(data) do
sum(data) / length(data)
end
def variance(data, mean) do
for n <- data, do: pow(n - mean, 2)
end
end
# usage
data = [1,2,3,4]
m = SD.mean(data) # => 2.5
sd = SD.standard_deviation(data) # => 1.118033988749895
var m = data.Average();
var s = CalculateStdDev(data);
float CalculateStdDev(IEnumerable<float> values)
{
double ret = 0;
if (values.Count() > 0)
{
double avg = values.Average();
double sum = values.Sum(d => Math.Pow(d - avg, 2));
ret = Math.Sqrt((sum) / values.Count()-1);
}
return (float)ret;
}