Sponsored Content
Top Forums Shell Programming and Scripting Script for finding standard deviation Post 302235196 by joeyg on Thursday 11th of September 2008 10:38:25 AM
Old 09-11-2008
Tools Perhaps way out in left field, but

Depending on the accuracy required, you might consider
(a) For each of your values, multiplying by 100 or 1000 prior to beginning any math. Then know that you have to remove the extra digits and they are after the decimal point. For example 3/2 = 1 in integer, but 300/2 = 150 or adjusted 1.50
(b) An approximation for square root can be done in two parts. First off, add up all odd numbers until you are greater than the starting number. For example, sqrt of 10 would give you 1+3+5+7 and those four pieces are greater than the 10 you started with, so sqrt=3 (one less) as integer. Perhaps easier to see in the following [to get the integer part]
1 sqrt = 1+3 (more), so one digit is 1
2 sqrt = 1+3 (more), so 1
3 sqrt = 1+3 (more), so 1
4 sqrt = 1+3+5 (more), so 2 (again think one less)
5 sqrt = 1+3+5 (more), so 2
...
9 sqrt = 1+3+5+7 (more), so 3
To get to the decimal part there is another strange methodology involving looking at remaindors. In short the sqrt of 5 starts off with a 2 as seen above. Adding 1+3+5=9 and that is 4 too many (9-5). My last number in the 1+3+5 was a 5 and if I have 4 too many, I only needed a 1 (5-4=1). Take the 1 and the 5 and do 1/5 = .2
Add the first 2 to the .2 and you get 2.2 vs. actual of 2.23

For 8, start with the 2 as the integer. That is 1 too many (9-8). My last number was 5 again (in 1+3+5), so I only needed 4. Take that 4 and 5 to get to 4/5 = .8
Add the first 2 to this .8 and you get 2.8 vs actual 2.82


This is normally within a couple hundredths of the pure answer.

***
And I knew by the time I could write all that up, someone would have a program solution. But what the heck, if you can follow the logic of what I wrote for approximating sqrt, then you might agree it to be a cool function!Smilie

Last edited by joeyg; 09-11-2008 at 11:44 AM.. Reason: added comment at end
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mean and Standard deviation

Hi all, I am new to shell scripting and wanna calculate the mean and standard deviation using shell programming. I have a file with letters that are repeating and their corresponding duration a 0.32 a 0.89 aa 0.34 aa 0.23 au 0.012 au 0.26... (4 Replies)
Discussion started by: lakshmikanth.pg
4 Replies

2. UNIX for Dummies Questions & Answers

Calculating the Standard Deviation for a column

Hi all, I want to calculate the standard deviation for a column (happens to be column 3). Does any know of simple awk script to do this? Thanks (1 Reply)
Discussion started by: kylle345
1 Replies

3. Shell Programming and Scripting

using awk to print average and standard deviation into a file

Hi I want to use awk to print avg and st deviation but it does not go into a file for column 1 only. I can do average and # of records but i cannot get st deviation. awk '{sum+=$1} END { print "Average = ",sum/NR}' thanks (1 Reply)
Discussion started by: phil_heath
1 Replies

4. Shell Programming and Scripting

Standard deviation in awk

Hi all, I need to find the standard deviation of each column of a dataset below for each hour. The data is given in 5 second intervals as shown below DATE TIME FRAC_DAYS_SINCE_JAN1 FRAC_HRS_SINCE_JAN1 EPOCH_TIME ... (11 Replies)
Discussion started by: gd9629
11 Replies

5. Shell Programming and Scripting

AWK script for standard deviation / root mean square deviation

I have a file with say 50 columns, each containing a whole lot of data. Each column contains data from a separate simulation, but each simulation is related to the data in the last (REFERENCE) column $50 I need to calculate the RMS deviation for each data line, i.e. column 1 relative to... (12 Replies)
Discussion started by: chrisjorg
12 Replies

6. Shell Programming and Scripting

Finding standard deviation for all columns in a data file

Hi All, I want someone to modify the below script from this forum so that it can be used for all columns in the file( instead of only printing column 3 mean and standard deviation values). I don't know how to loop around all the columns. ... (3 Replies)
Discussion started by: ks_reddy
3 Replies

7. Shell Programming and Scripting

calculating row-wise standard deviation using awk

Hi, I have a file containing 100,000 rows-by-120 columns and I need to compute for the standard deviation for each row. Any idea on how to calculate row-wise standard deviation using awk? My sample data looks like this: input data: 23 35 12 25 16 17 18 19 29 12 12 26 15 14 15 23 12 12... (2 Replies)
Discussion started by: ida1215
2 Replies

8. Shell Programming and Scripting

Computing average and standard deviation from multiple text files

Hello there, I found an elegant solution to computing average values from multiple text files awk '{for (i=1;i<=NF;i++){if ($i!~"n/a"){a+=$i}else{b++}}}END{for (i=1;i<=FNR;i++){for (j=1;j<=NF;j++){printf (a/(3-b))((b>0)?"~"b" ":" ")};printf "\n"}}' file1 file2 file3 I tried to modify... (2 Replies)
Discussion started by: charmmilein
2 Replies

9. Shell Programming and Scripting

Output mean and standard deviation of a row

I have a file that looks that this: 820 890 530 1650 1600 1800 1850 1900 2270 1640 2300 1670 2080 2200 2350 1150 1630 2210 I would like to output the mean and standard deviation of each row so that my final output would look like this 820 890 530 746.667 155.849 1650 1600 1800... (5 Replies)
Discussion started by: kayak
5 Replies

10. Shell Programming and Scripting

SMA (Single Moving Average) and Standard Deviation

Hello Team, I am using the following awk script to calculate the SMA (Single Moving Average) for an specific period but now I would like to include the standard deviation output. Could you please help me to modify this awk shell script awk -F, -v points=5 ' { a = $2; ... (4 Replies)
Discussion started by: csierra
4 Replies
sqrt(3M)						  Mathematical Library Functions						  sqrt(3M)

NAME
sqrt, sqrtf, sqrtl - square root function SYNOPSIS
cc [ flag... ] file... -lm [ library... ] #include <math.h> double sqrt(double x); float sqrtf(float x); long double sqrtl(long double x); DESCRIPTION
These functions compute the square root of their argument x. RETURN VALUES
Upon successful completion, these functions return the square root of x. For finite values of x < -0, a domain error occurs and either a NaN (if supported) or an implementation-defined value is returned. If x is NaN, a NaN is returned. If x is +-0 or +Inf, x is returned. If x is -Inf, a domain error occurs and a NaN is returned. ERRORS
These functions will fail if: Domain Error The finite value of x is < -0 or x is -Inf. If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, the invalid floating-point exception is raised. The sqrt() function sets errno to EDOM if the value of x is negative. USAGE
An application wanting to check for exceptions should call feclearexcept(FE_ALL_EXCEPT) before calling these functions. On return, if fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an exception has been raised. An application should either examine the return value or check the floating point exception flags to detect exceptions. An application can also set errno to 0 before calling sqrt(). On return, if errno is non-zero, an error has occurred. The sqrtf() and sqrtl() functions do not set errno. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
feclearexcept(3M), fetestexcept(3M), isnan(3M), math.h(3HEAD), attributes(5), standards(5) SunOS 5.10 1 Nov 2003 sqrt(3M)
All times are GMT -4. The time now is 11:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy