LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-20-2011, 11:20 PM   #1
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Rep: Reputation: 0
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.
 
Old 04-21-2011, 12:33 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 04-21-2011, 12:57 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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 :}
 
Old 04-21-2011, 01:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
}
 
Old 04-21-2011, 02:33 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 04-21-2011, 04:55 AM   #6
vjramana
Member
 
Registered: Sep 2009
Posts: 89

Original Poster
Rep: Reputation: 0
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.
 
Old 04-21-2011, 09:36 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating average from Python Script stryker213 Programming 5 04-25-2010 04:58 PM
Calculating Average in PHP Array? your_shadow03 Programming 1 01-27-2010 04:19 AM
Need computationally fast approximation to standard deviation. jiml8 Programming 22 11-23-2008 11:41 PM
error when finding the standard deviation of a vector mshinska Programming 5 10-25-2005 11:03 PM
charting mean and standard deviation allelopath Linux - Software 2 02-04-2005 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration