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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-20-2011, 11:20 PM
|
#1
|
Member
Registered: Sep 2009
Posts: 89
Rep:
|
awk syntax for calculating average and standard deviation
I have a data in a column.
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
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
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
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
Member
Registered: Sep 2009
Posts: 89
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.
thank you in advance.
|
|
|
04-21-2011, 09:36 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
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 }
|
|
|
All times are GMT -5. The time now is 10:30 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|