2 -export([sum/1,mean/1,r_value/1,frequency/1,frequencies_to_scatterplot/2]).
5 sum([Head|Tail]) -> Head + sum(Tail).
7 mean(List) -> sum(List) / length(List).
9 covariance(Points, MeanX, MeanY) ->
10 sum(lists:map(fun({X,Y}) -> (X - MeanX) * (Y - MeanY) end, Points)).
12 standard_deviation(Samples, Mean) ->
13 math:sqrt(sum(lists:map(
14 fun(Item) -> math:pow(Item - Mean, 2) end,
18 Xs = lists:map(fun({X,_}) -> X end, Points),
19 Ys = lists:map(fun({_,Y}) -> Y end, Points),
22 Covariance = covariance(Points, MeanX, MeanY),
23 StandardDeviationX = standard_deviation(Xs, MeanX),
24 StandardDeviationY = standard_deviation(Ys, MeanY),
25 Covariance / StandardDeviationX / StandardDeviationY.
27 frequency([],Result) -> Result;
28 frequency([Head|Tail],Result) ->
29 frequency(Tail,dict:update_counter(Head,1,Result)).
31 frequency(Sample) -> frequency(Sample, dict:new()).
33 fetch_values(Dict) -> lists:map(
34 fun({_,Value}) -> Value end,
37 frequencies_to_scatterplot(Fx,Fy) ->
38 FxPoints = dict:map(fun(_,Value) -> {Value,0} end, Fx),
39 FyPoints = dict:map(fun(_,Value) -> {0,Value} end, Fy),
40 KeysToPoints = dict:merge(
41 fun(_,{X,_},{_,Y}) -> {X,Y} end,
44 fetch_values(KeysToPoints).