Display the standard deviation of a column of numbers with awk

awk '{delta = $1 - avg; avg += delta / NR; mean2 += delta * ($1 - avg); } END { print sqrt(mean2 / NR); }'
This will calculate a running standard deviation in one pass and should never have the possibility for overflow that can happen with other implementations. I suppose there is a potential for underflow in the corner case where the deltas are small or the values themselves are small.

By: ashawley
2009-09-11 04:46:01

2 Alternatives + Submit Alt

What Others Think

I was the one who commented on the alternate/older system which was failing. Not 100% on how your scheme works, but it appears to get the same result at the stuff I posted. Good job! -- grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{sum+=$1; array[NR]=$1} END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))**2);}print sqrt(sumsq/NR)}' 0.0111318 grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{sum+=$1; sumsq+=$1*$1} END {print sqrt(sumsq/NR - (sum/NR)**2)}' 0.0111299 grep -e '^#BEST' 00111260_Control.ASC | grep 'L1_INT' | cut -d ',' -f 14 | less | awk '{delta = $1 - avg; avg += delta / NR; mean2 += delta * ($1 - avg); } END { print sqrt(mean2 / NR); }' 0.0111318 --
mungewell · 458 weeks ago
Thanks. And thank you for testing it on your large data set.
ashawley · 458 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: