Easily Calculating Statistical Functions
December 6, 2006 Hey, Ted
The code associated with this article is available for download. Would you be interested in publishing a program that calculates statistical values, such as average, standard deviation, maximum value, and minimum value? I have such a program, which I use in to calculate average usage and standard deviation of the usage of the items we handle. –Victor Pisman Thanks to faithful reader Victor Pisman for an interesting program. When I think of calculating an average, I think in terms of accumulating an amount and counting before dividing. Victor took a different approach, and I like it. Victor’s RPG program, GETSTAT, has the following parameter list.
Call this program for each number in the list. That is, if you need the average of seven numbers, call the program seven times. On the first call for the group, pass a value of Y through the RESET parameter in order to zero out all the aggregate values. On subsequent calls for the group, pass a RESET value of N. * Example caller of GETSTAT * reset accumlators on first call for the group C move 'Y' @@reset * * loop to process all the numbers in the group C dow On each call, GETSTAT counts, accumulates, saves min and max, and recalculates average and standard deviation. ***** GETSTAT - calculate statistical values C *entry plist C parm @@parm 11 2 C parm @@count 5 0 C parm @@sum 11 2 C parm @@average 11 2 C parm @@max 11 2 C parm @@min 11 2 C parm @@deviation 11 2 C parm @@reset 1 * * Clear fields on first record of a group. C if @@reset='Y' C z-add *zero @@count C z-add *zero @@sum C z-add *zero @@average C z-add *zero @@max C z-add *zero @@min C z-add *zero @@deviation C endif * * Count and accumulate C eval @@count=@@count+1 C eval @@sum=@@sum+@@parm * * Recalculate average and standard deviation C eval(h) @@average=(@@sum/@@count) * C eval(h) @@deviation=((@@count-1)* C @@deviation*@@deviation+ C (@@parm-@@average)*(@@parm-svaverage))/ C @@count C sqrt(h) @@deviation @@deviation * * Track lowest and highest numbers of the group C if @@parm>@@max or @@count=1 C eval @@max=@@parm C endif * C if @@parm |