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[NR % points] = $2;    if(NR>=points) {  total_a = 0;    for (i = 0; i < points; ++i)       { total_a = total_a + a; }        print ($1, $2, total_a/points);  }  }' data.csv

Input Data

Row    Data
1    10.09
2    10.14
3    10.02
4    10.13
5    10.1
6    10.06
7    10.15
8    9.91
9    9.99
10    9.72
11    9.75
12    9.57
13    9.62
14    9.97
15    9.71
16    9.77
17    9.88
18    9.71
19    9.75
20    9.53
21    9.46
22    9.38
23    9.63
24    9.67
25    9.92
26    10.04

Expected Output:

Row    Data    SMA 5    Standard Deviation 5 Period
5    10.1    10.096    0.042237424
6    10.06    10.09    0.04472136
7    10.15    10.092    0.047074409
8    9.91    10.07    0.085556999
9    9.99    10.042    0.084237759
10    9.72    9.966    0.146232691
11    9.75    9.904    0.158442419
12    9.57    9.788    0.147837749
13    9.62    9.73    0.145464772
14    9.97    9.726    0.138361844
15    9.71    9.724    0.138506318
16    9.77    9.728    0.139484766
17    9.88    9.79    0.123450395
18    9.71    9.808    0.102058807
19    9.75    9.764    0.062481997
20    9.53    9.728    0.113912247
21    9.46    9.666    0.152131522
22    9.38    9.566    0.142632395
23    9.63    9.55    0.129460419
24    9.67    9.534    0.106695829
25    9.92    9.612    0.187339264
26    10.04    9.728    0.231637648

? The data.xls file included in the post already has the data combined. All that has to be done is delete rows 2 thru 5 (if needed) and save the file as tab delimited text.

Hello rdrtx1,

The attached .xls files are example of the output requested.
The purpose is to use this shell script with multiple files, different periods and more rows.

1 Like

Try

awk -v points=5 '

NR == 1 {next
        }

        {a[(NR-1) % points] = $2
         if ((NR-1)>=points)    {total_a = 0
                                 for (i = 0; i < points; ++i)   {total_a = total_a + a
#                                                                print i, a, total_a, total_a / 5 
                                                                }
                                 AVG = total_a/points
                                 ERR = 0
                                 for (i = 0; i < points; ++i)   {ERR += (AVG - a) * (AVG - a)
                                                                }
                                 print $1, $2, AVG, sqrt(ERR/points)
                                }  
        }
' OFS="	" file1
5	10.1	10.096	0.0422374
6	10.06	10.09	0.0447214
7	10.15	10.092	0.0470744
8	9.91	10.07	0.085557
9	9.99	10.042	0.0842378
10	9.72	9.966	0.146233
11	9.75	9.904	0.158442
12	9.57	9.788	0.147838
13	9.62	9.73	0.145465
14	9.97	9.726	0.138362
15	9.71	9.724	0.138506
16	9.77	9.728	0.139485
17	9.88	9.79	0.12345
18	9.71	9.808	0.102059
19	9.75	9.764	0.062482
20	9.53	9.728	0.113912
21	9.46	9.666	0.152132
22	9.38	9.566	0.142632
23	9.63	9.55	0.12946
24	9.67	9.534	0.106696
25	9.92	9.612	0.187339
26	10.04	9.728	0.231638
1 Like

RudiC, Thanks a lot for your support.