Home
Insert
Update snippet 'Moving Average'
Title
Passcode
Description
Given a period and a sequence of values, calculates the moving average of the input. E.g., given a period of 4 and a sequence of { 2.0, 8.0, 12.0, 10.0, 6.0, 4.0, 14.0 }, the result of this function is { 8.0, 9.0, 8.0, 8.5 }.
Source code
let movingAverage (period : int) (values : float seq) = Seq.zip values (Seq.skip period values) |> Seq.scan (fun last (prev, cur) -> last - prev + cur) (values |> Seq.take period |> Seq.sum) |> Seq.map (fun x -> x / float(period))
Tags
sequences
average
sequences
average
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update