Standard Deviation, Root Mean Square and the Central Moments (Statistics in Erlang part 3)
August 10, 2008 Erlang, Math, Programming, Statistics, Tools and Libraries 4 Comments[digg-reddit-me]These are standard statistical tools for measuring differences, drift and error within sets, as well as Kth moments about the mean. Central moments are also the building blocks of skewness and kurtosis, which are covered in a later post. Root mean square is particularly useful as a measure of set error. Standard deviation is great for figuring out how much spread/differentiation there is within a set.
This code requires the arithmetic mean stuff from Statistics in Erlang part 1. There’s interesting, unrelated stuff in Part 2.
As usual, this code is part of the ScUtil library. ScUtil is free and MIT license, because the GPL is evil.
This implements issue 128. This implements issue 138. This finishes implementing issue 119.
std_deviation(Values) when is_list(Values) ->
Mean = arithmetic_mean(Values),
math:sqrt(arithmetic_mean([ (Val-Mean)*(Val-Mean) || Val <- Values ])).
root_mean_square(List) when is_list(List) ->
math:sqrt(arithmetic_mean([ Val*Val || Val <- List ])).
central_moments(Items) ->
Cnt = length(Items),
Mean = lists:sum(Items) / Cnt,
TFun = fun(X) ->
Base = X-Mean, B2=Base*Base, B3=B2*Base, B4=B3*Base, {B2,B3,B4}
end,
collapse_central_moments(Cnt, [ TFun(I) || I <- Items ], {0,0,0}).
collapse_central_moments(N, [], {WM2, WM3, WM4}) ->
{ WM2/N, WM3/N, WM4/N };
collapse_central_moments(N, [{I2,I3,I4}|Rem], {WM2, WM3, WM4}) ->
collapse_central_moments(N, Rem, {I2+WM2, I3+WM3, I4+WM4}).
