| 1 | #!/bin/bash |
| 2 | # sd.sh: Standard Deviation |
| 3 | |
| 4 | # The Standard Deviation indicates how consistent a set of data is. |
| 5 | # It shows to what extent the individual data points deviate from the |
| 6 | #+ arithmetic mean, i.e., how much they "bounce around" (or cluster). |
| 7 | # It is essentially the average deviation-distance of the |
| 8 | #+ data points from the mean. |
| 9 | |
| 10 | # =========================================================== # |
| 11 | # To calculate the Standard Deviation: |
| 12 | # |
| 13 | # 1 Find the arithmetic mean (average) of all the data points. |
| 14 | # 2 Subtract each data point from the arithmetic mean, |
| 15 | # and square that difference. |
| 16 | # 3 Add all of the individual difference-squares in # 2. |
| 17 | # 4 Divide the sum in # 3 by the number of data points. |
| 18 | # This is known as the "variance." |
| 19 | # 5 The square root of # 4 gives the Standard Deviation. |
| 20 | # =========================================================== # |
| 21 | |
| 22 | count=0 # Number of data points; global. |
| 23 | SC=9 # Scale to be used by bc. Nine decimal places. |
| 24 | E_DATAFILE=90 # Data file error. |
| 25 | |
| 26 | # ----------------- Set data file --------------------- |
| 27 | if [ ! -z "$1" ] # Specify filename as cmd-line arg? |
| 28 | then |
| 29 | datafile="$1" # ASCII text file, |
| 30 | else #+ one (numerical) data point per line! |
| 31 | datafile=sample.dat |
| 32 | fi # See example data file, below. |
| 33 | |
| 34 | if [ ! -e "$datafile" ] |
| 35 | then |
| 36 | echo "\""$datafile"\" does not exist!" |
| 37 | exit $E_DATAFILE |
| 38 | fi |
| 39 | # ----------------------------------------------------- |
| 40 | |
| 41 | |
| 42 | arith_mean () |
| 43 | { |
| 44 | local rt=0 # Running total. |
| 45 | local am=0 # Arithmetic mean. |
| 46 | local ct=0 # Number of data points. |
| 47 | |
| 48 | while read value # Read one data point at a time. |
| 49 | do |
| 50 | rt=$(echo "scale=$SC; $rt + $value" | bc) |
| 51 | (( ct++ )) |
| 52 | done |
| 53 | |
| 54 | am=$(echo "scale=$SC; $rt / $ct" | bc) |
| 55 | |
| 56 | echo $am; return $ct # This function "returns" TWO values! |
| 57 | # Caution: This little trick will not work if $ct > 255! |
| 58 | # To handle a larger number of data points, |
| 59 | #+ simply comment out the "return $ct" above. |
| 60 | } <"$datafile" # Feed in data file. |
| 61 | |
| 62 | sd () |
| 63 | { |
| 64 | mean1=$1 # Arithmetic mean (passed to function). |
| 65 | n=$2 # How many data points. |
| 66 | sum2=0 # Sum of squared differences ("variance"). |
| 67 | avg2=0 # Average of $sum2. |
| 68 | sdev=0 # Standard Deviation. |
| 69 | |
| 70 | while read value # Read one line at a time. |
| 71 | do |
| 72 | diff=$(echo "scale=$SC; $mean1 - $value" | bc) |
| 73 | # Difference between arith. mean and data point. |
| 74 | dif2=$(echo "scale=$SC; $diff * $diff" | bc) # Squared. |
| 75 | sum2=$(echo "scale=$SC; $sum2 + $dif2" | bc) # Sum of squares. |
| 76 | done |
| 77 | |
| 78 | avg2=$(echo "scale=$SC; $sum2 / $n" | bc) # Avg. of sum of squares. |
| 79 | sdev=$(echo "scale=$SC; sqrt($avg2)" | bc) # Square root = |
| 80 | echo $sdev # Standard Deviation. |
| 81 | |
| 82 | } <"$datafile" # Rewinds data file. |
| 83 | |
| 84 | |
| 85 | # ======================================================= # |
| 86 | mean=$(arith_mean); count=$? # Two returns from function! |
| 87 | std_dev=$(sd $mean $count) |
| 88 | |
| 89 | echo |
| 90 | echo "Number of data points in \""$datafile"\" = $count" |
| 91 | echo "Arithmetic mean (average) = $mean" |
| 92 | echo "Standard Deviation = $std_dev" |
| 93 | echo |
| 94 | # ======================================================= # |
| 95 | |
| 96 | exit |
| 97 | |
| 98 | # This script could stand some drastic streamlining, |
| 99 | #+ but not at the cost of reduced legibility, please. |
| 100 | |
| 101 | |
| 102 | # ++++++++++++++++++++++++++++++++++++++++ # |
| 103 | # A sample data file (sample1.dat): |
| 104 | |
| 105 | # 18.35 |
| 106 | # 19.0 |
| 107 | # 18.88 |
| 108 | # 18.91 |
| 109 | # 18.64 |
| 110 | |
| 111 | |
| 112 | # $ sh sd.sh sample1.dat |
| 113 | |
| 114 | # Number of data points in "sample1.dat" = 5 |
| 115 | # Arithmetic mean (average) = 18.756000000 |
| 116 | # Standard Deviation = .235338054 |
| 117 | # ++++++++++++++++++++++++++++++++++++++++ # |