Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate mean, variance and range using Bash script

Tags:

bash

numeric

Given a file file.txt:

AAA 1 2 3 4 5 6 3 4 5 2 3 
BBB 3 2 3 34 56 1 
CCC 4 7 4 6 222 45 

Does any one have any ideas on how to calculate the mean, variance and range for each item, i.e. AAA, BBB, CCC respectively using Bash script? Thanks.

like image 325
Brian James Avatar asked Aug 13 '26 18:08

Brian James


2 Answers

Here's a solution with awk, which calculates:

  • minimum = smallest value on each line
  • maximum = largest value on each line
  • average = μ = sum of all values on each line, divided by the count of the numbers.
  • variance = 1/n × [(Σx)² - Σ(x²)] where
    n = number of values on the line = NF - 1 (in awk, NF = number of fields on the line)
    (Σx)² = square of the sum of the values on the line
    Σ(x²) = sum of the squares of the values on the line

 

awk '{
  min = max = sum = $2;       # Initialize to the first value (2nd field)
  sum2 = $2 * $2              # Running sum of squares
  for (n=3; n <= NF; n++) {   # Process each value on the line
    if ($n < min) min = $n    # Current minimum
    if ($n > max) max = $n    # Current maximum
    sum += $n;                # Running sum of values
    sum2 += $n * $n           # Running sum of squares
  }
  print $1 ": min=" min ", avg=" sum/(NF-1) ", max=" max \
    ", var=" (sum2 - (sum*sum)/(NF-1))/(NF-1);
}' filename

Output:

AAA: min=1, avg=3.45455, max=6, sum2=154, var=2.06612
BBB: min=1, avg=16.5, max=56, sum2=4315, var=446.917
CCC: min=4, avg=48, max=222, sum2=51426, var=6267

Note that you can save the awk script (everything between, but not including, the single-quotes) in a file, say called script, and execute it with awk -f script filename

like image 69
Adam Liss Avatar answered Aug 17 '26 00:08

Adam Liss


You can use python:

$ AAA() {  echo "$@" | python -c 'from sys import stdin; nums = [float(i) for i in stdin.read().split()]; print(sum(nums)/len(nums))'; }

$ AAA 1 2 3 4 5 6 3 4 5 2 3
3.45454545455
like image 21
kev Avatar answered Aug 16 '26 23:08

kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!