View the Most Wanted LQ Wiki articles.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [SOLVED] awk syntax for calculating average and standard deviation
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-20-2011, 11:20 PM #1 vjramana Member   Registered: Sep 2009 Posts: 88 Rep: awk syntax for calculating average and standard deviation I have a data in a column. Code: ```2 4 7 3 5``` I want to calculate average and standard deviation. As first step I want to calculate average for the data than calculate (del=data - avg) for all the data. I suppose get Code: ```avg=4.2 x x-avg(x) 2 -2.2 4 -0.2 7 2.8 3 -1.2 5 0.8``` For this I use AWK and the code goes like this Code: ```#!/usr/bin/awk -f {col=1} {sum+=\$col; avg=sum/NR;} {delX=(\$col-avg)} {print \$col, delX}``` But I get different answers. Code: ```x x-avg(x) 2 0 4 1 7 2.66667 3 -1 5 0.8``` Can anyone explain why the answers are so different? since this is wrong I can not continue calculating the standard deviation. Many thanks in advance.
 04-21-2011, 12:33 AM #2 David the H. Bash Guru   Registered: Jun 2004 Location: Osaka, Japan Distribution: Debian sid + kde 3.5 & 4.4 Posts: 6,823 Rep: I don't really understand the math that you want to do, but here's the math that you actually did. Code: ```\$ cat file.txt 2 4 7 3 5 \$ cat script.sh #! /usr/bin/awk -f { col=1; } { sum+=\$col; } { avg=(sum/NR); } { delX=(\$col-avg); } { print "col="\$col,"sum="sum,"NR="NR,"avg="avg,"delX="delX; } \$ ./script.sh file.txt col=2 sum=2 NR=1 avg=2 delX=0 col=4 sum=6 NR=2 avg=3 delX=1 col=7 sum=13 NR=3 avg=4.33333 delX=2.66667 col=3 sum=16 NR=4 avg=4 delX=-1 col=5 sum=21 NR=5 avg=4.2 delX=0.8``` The numbers appear to be coming out exactly as you told the script to calculate them. (e.g. line one: "delX" = "\$col-avg" = "2-2" = "0"). Last edited by David the H.; 04-21-2011 at 12:42 AM. Reason: slight reformatting for clarity and consistency
 04-21-2011, 12:57 AM #3 Tinkster Moderator   Registered: Apr 2002 Location: in a fallen world Distribution: slackware by choice, others too :} ... android. Posts: 23,019 Blog Entries: 11 Rep: As far as I'm concerned it doesn't make too much sense to use an average that's a moving target (changes w/ each row) to calculate anything :}
 04-21-2011, 01:12 AM #4 grail Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 7,834 Rep: I agree with the others that what you require mathematically is not clear, but this seems to give the results you want: Code: ```#!/usr/bin/awk -f { sum += num[NR] = \$0 } END{ avg = sum / NR print "avg = "avg print "x (x - avg)" for(i = 1; i <= NR; i++) print num[i], num[i] - avg }```
 04-21-2011, 02:33 AM #5 David the H. Bash Guru   Registered: Jun 2004 Location: Osaka, Japan Distribution: Debian sid + kde 3.5 & 4.4 Posts: 6,823 Rep: I also think it's risky to rely on NR for the calculation, if there's a possibility of blank lines or non-numeric data occurring in the input. It would be safer to test for proper input and keep a running tab of the number of lines processed. Code: ```#! /usr/bin/awk -f { if ( \$1 ~ /^[[:digit:]]+\$/ ) { sum += num[++n] = \$1 } } END{ avg=( sum / n ) print "avg = "avg print "x ( x - avg )" for ( i=1 ; i<=n ; i++ ) print num[i] , ( num[i] - avg ) }``` grail will probably come back and show me how to write it in half the space, of course. Edit: if there could be decimal or negative numbers, you'll have to widen the test. Something like this: Code: `if ( \$1 ~ /^-?[[:digit:]]+(\.[[:digit:]]+)?\$/ )` Edit2: I just found an easier way to determine if a field is numeric. Code: `if ( \$1 + 0.0 == \$1 )` Last edited by David the H.; 04-21-2011 at 03:25 AM. Reason: as stated
04-21-2011, 04:55 AM   #6
vjramana
Member

Registered: Sep 2009
Posts: 88

Original Poster
Rep:
Thank you for the reply. Basically I want to calculate average and standard deviation from data as below:
Quote:
 O11 0.115 0.096 0.138 0.106 0.001 0.009 0.000 0.000 O12 0.298 0.290 0.282 0.240 0.012 0.003 0.000 0.000 O13 0.450 0.451 0.418 0.362 0.052 0.038 0.000 0.000 O14 0.078 0.082 0.068 0.063 0.003 0.006 0.000 0.000 O15 0.138 0.136 0.182 0.201 0.001 0.000 0.000 0.000 O16 0.717 0.832 0.766 0.759 0.029 0.018 0.000 0.000 O22 0.390 0.373 0.381 0.373 0.159 0.129 0.000 0.000 O23 0.195 0.312 0.184 0.307 0.305 0.372 0.000 0.000 O24 0.358 0.472 0.465 0.497 0.146 0.175 0.000 0.000 O25 0.298 0.274 0.251 0.249 0.074 0.104 0.000 0.000 O26 0.777 0.771 0.775 0.697 0.296 0.262 0.000 0.000
I want to calculate average and standard deviation for each column starting from column 2 until 5 separately.
After that I want to calculate the average and standard deviation line by line starting from column 2 until column 5 from each line.

For achieve this I tried to work out from model data for calculating average and standard deviation. I just wonder how to write awk code for doing this.

 04-21-2011, 09:36 AM #7 grail Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 7,834 Rep: Well I had a bit of a play ... you can do what you will but this should get you going in some sort of direction Code: ```#!/usr/bin/awk -f { row[NR] = \$1 for(i=2;i<=NF;i++){ sum[NR]+=num[NR,i]=\$i col[i]+=\$i } avg[NR] = sum[NR] / (NF - 1) } END{ printf "Column details:\n%-7s","RowID" for(x=2;x<=NF;x++){ col_avg[x] = col[x] / NR printf "%-8s%s-(%2.8f) ","Value","Dev", col_avg[x] } for(a=1;a<=NR;a++){ printf "\n%-6s ", row[a] for(b=2;b<=NF;b++){ d = num[a,b] >= col_avg[b]?8:7 printf "%1.5f (%2.8f)%"d"s", num[a,b],num[a,b] - col_avg[b], " " } } printf "\n\nRow details:\n%-7s%-16s","RowID","(Dev)" for(x=2;x<=NF;x++) printf "%-8s%-16s","Value","Dev" for(a=1;a<=NR;a++){ printf "\n%-7s(%2.8f)%3s", row[a], avg[a]," " for(b=2;b<=NF;b++){ d = num[a,b] >= avg[b]?3:2 printf " %1.5f (%2.8f)%"d"s", num[a,b],num[a,b] - avg[b], " " } } print "" }``` You can also implement David's test for digits if you require. @David - looks good, only suggestion for shorter would be to just implement the 'if' as the test to sum: Code: `\$1+0==\$1{sum+= num[++n] = \$1 }`