Logo

Programming-Idioms

History of Idiom 203 > diff from v13 to v14

Edit summary for version 14 by setupminimal:
New Haskell implementation by user [setupminimal]

Version 13

2019-11-04, 18:52:07

Version 14

2020-04-29, 00:19:31

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
mean dat = sum dat / (fromIntegral $ length dat)

stddev dat = sqrt . mean $ map ((**2) . (m -)) dat
  where
    m = mean dat
Comments bubble
data is a keyword in Haskell, so I'm using dat instead.

This solution is not very memory efficient on long, lazily-computed lists. If you're dealing with one of those, you might want to write a recursive version instead.