Logo

Programming-Idioms

History of Idiom 203 > diff from v5 to v6

Edit summary for version 6 by Francob411:
New Elixir implementation by user [Francob411]

Version 5

2019-11-01, 15:03:49

Version 6

2019-11-02, 16:07:50

Idiom #203 Calculate mean and standarddeviation

Calculate the mean m and the standard deviation s of the list of floating point values data.

Idiom #203 Calculate mean and standarddeviation

Calculate the mean m and the standard deviation s of the list of floating point values data.

Extra Keywords
avg average variance
Extra Keywords
avg average variance
Code
data = [1,2,3,4]

defmodule SD do

  def calculate(data) do
    data
    |> mean
    |> variance(data)
    |> mean 
    |> :math.sqrt
  end

  def mean(data) do
    Enum.sum(data) / length(data)
  end

  def variance(mean, data) do
    data
    |> Enum.map(fn n -> :math.pow(n - mean, 2) end)
  end

end

SD.calculate(data) # 1.118033988749895

Doc URL
https://hex.pm/