Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
(added slate language) |
(F#) |
||
Line 286: | Line 286: | ||
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)</lang> |
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
The following computes the running mean using a tail-recursive approach. If we just sum all the values then divide by the number of values then we will suffer from overflow problems for large lists. See [http://en.wikipedia.org/wiki/Moving_average wikipedia] about the moving average computation. |
|||
let avg (a:float) (v:float) n = |
|||
a + (1. / ((float n) + 1.)) * (v - a) |
|||
let mean_series list = |
|||
let rec f a n list = |
|||
match list with |
|||
| [] -> a |
|||
| h :: t -> f (avg a (float h) n) (n + 1) t |
|||
f 0. 0 list |
|||
Checking this: |
|||
> mean_series [1; 8; 2; 8; 1; 7; 1; 8; 2; 7; 3; 6; 1; 8; 100] ;; |
|||
val it : float = 10.86666667 |
|||
> mean_series [] ;; |
|||
val it : float = 0.0 |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |