As of December 1, 2020, Focal Point is retired and repurposed as a reference repository. We value the wealth of knowledge that's been shared here over the years. You'll continue to have access to this treasure trove of knowledge, for search purposes only. Moving forward, myibi is our community platform to learn, share, and collaborate. We have the same Focal Point forum categories in myibi, so you can continue to have all new conversations there. If you need access to myibi, contact us at myibi@ibi.com and provide your corporate email address, company, and name.

Stephen , This is an old problem for WF. I just calculated standard deviation yesterday with WF 5.2.1 on AIX (self service application) Unfortunately you have to write the formula using the prefix ASQ. AVE. and then SQRT. it would be better for you to use "Sorting with multiple Display Command" (see Manual) Paolo

The formula for Standard Deviation in an example.. TABLE FILE CAR SUM ASQ.WEIGHT AVE.WEIGHT CNT.WEIGHT COMPUTE STD=((ASQ.WEIGHT-AVE.WEIGHT *AVE.WEIGHT) **.5) /CNT.WEIGHT ; END

The reason its not a function in WebFocus is that often the denominator has to be (N-1) and not N as in this example.

The formula for MEDIAN is shown in this example.. TABLE FILE CAR PRINT WEIGHT NOPRINT COMPUTE MEDIAN=MEDIAN +1 ; WHERE TOTAL MEDIAN EQ 9 ; BY WEIGHT

END

Note that the number of values has to be known in advance (9). But you can find this out automatically , eg TABLE FILE CAR RRINT WEIGHT AND HOLD END -SET &COUNT = &LINES ; TABLE FILE CAR PRINT WEIGHT NOPRINT COMPUTE MEDIAN=MEDIAN +1 ; WHERE TOTAL MEDIAN EQ &LINES ; BY WEIGHT

Slight error in median. TABLE FILE CAR RRINT WEIGHT AND HOLD END -SET &COUNT = &LINES ; TABLE FILE CAR PRINT WEIGHT NOPRINT COMPUTE MEDIAN=MEDIAN +1 ; WHERE TOTAL MEDIAN EQ &COUNT ; BY WEIGHT

END

if the &COUNT is even then often it and the next value is used and averaged.

Stephen, these need to be manual calculations, which imho is a good thing, bcause you can calc stats at levels within the data set, and not just for the entire data set. You can also check the shape of your distribution so that you know if your SD means anything at all,and youcancalc your own confidence limits, while you're at it. So it takes a few passes at the data to do this. Here's what works for me: For SD: you'll need your sample size N and you can get this as an &var ,if you want,by counting lines when you prepare your dataset, and you'll need to be able to exponentiate. Remember that in focus X**Y will perform whatever exponentiation you need, and X**.5 for example will take the square root. You can use one of the SD formulae and the Confidence Limit formulae that you'll find in Excel Help, to remind you how to set it all up. For Median: you'll need to order your data set, then use the LIST verb to number them, take the max of that LIST column, cut it in half, and then mark your data set where the values change from below that mark to above it, MAKING YOUR OWN decision about what to do if your median falls between two values, and proceed accordingly. I like to do these things manually, because I can then compute weighted medians, which is most common, or rank the groups of my entire data set inorder of , say, Confidence, Or present a report of member variance compared to group variance, and show the bad guys at the top of the list. Bored yet?

Posts: 3811 | Location: Manhattan | Registered: October 28, 2003

While Gerry's answer is (of course) correct, it's fairly simple, and uses TWO passes. The MEDIAN can be calculated with a single pass, and within sort fields, but it's a bit more complex. Below is annotated code, to allow this. It uses multi-verb, as well as COMPUTEs, and WHERE TOTAL tests.

-* median is the 'middle' value (if odd number of values), or the -* average of the two middle values (if even number) -* when all the values are sorted TABLE FILE &FILE -* use cnt. to get number of instances WRITE CNT.&FIELD NOPRINT COMPUTE -* use this if even number of instances M1/I4=INT((CNT.&FIELD + 1)/2); NOPRINT COMPUTE -* use this too for even. if odd number of instances - same as m1 M2/I4=INT((CNT.&FIELD + 2)/2); NOPRINT -* sort field(s) within which to calculate median BY &BYFIELD -* get details in sort fields and 'median'ed field order PRINT COMPUTE -* concat all by fields to compute the xlist (for when to reset) BYFIELDS/A10=&BYFIELD; NOPRINT -* use this compute to order the field within sorts COMPUTE XLIST/I5=IF BYFIELDS EQ LAST BYFIELDS THEN XLIST + 1 ELSE 1; NOPRINT -* get cumulative values for median calculation -* if count is odd, M1 = M2, and only one value. If even, need average COMPUTE C&FIELD/&FMT = IF M1 NE M2 THEN (LAST &FIELD + &FIELD)/2 ELSE &FIELD; AS 'MEDIAN,&FIELD' BY &BYFIELD BY &FIELD -* only look at possible median values (m1 = m2 if odd, only m2 if even) WHERE TOTAL XLIST EQ M2 ENDThis message has been edited. Last edited by: <Mabel>,

Posts: 25 | Location: 2 Penn Plaza 28 fl | Registered: March 27, 2003

Gerald , the formula is wrong. A wonderful old IBI Manual "Statistical Analyse" says that for computational puprposes is used: TABLE FILE CAR SUM ASQ.WEIGHT AVE.WEIGHT CNT.WEIGHT COMPUTE STD=( (ASQ.WEIGHT-CNT.WEIGHT*AVE.WEIGHT*AVE.WEIGHT) **.5) /(CNT.WEIGHT -1); END

Paulo, Nope, your formula would give a negative number. Actually you can take my formula and multiple by cnt.x/(cnt.x -1) to adjust for the degrees of freedom.

Since we're talking FOCUS, you can get both standard deviation and variance with a LET, as follows:

LET -* Population variance PVAR = COMPUTE PVAR.<1>=(ASQ.<1>-(AVE.<1>*AVE.<1>));; -* sample variance ( population variance time N/(N-1) SVAR = CNT.<1> NOPRINT COMPUTE # SVAR2 <1>; SVAR2= SVAR.<1>=(ASQ.<1>-(AVE.<1>*AVE.<1>)) * (CNT.<1>/(CNT.<1>-1)) ;; -* population standard deviation PSDEV = PVAR <1> # NOPRINT COMPUTE PSDEV.<1>=SQRT(PVAR.<1>);; -* sample standard deviation SSDEV = SVAR <1> # NOPRINT COMPUTE SSDEV.<1>=SQRT(SVAR.<1>);; END So, to get the POPULATION variance of WEIGHT, you'd use:

PVAR WEIGHT

to get the SAMPLE standard deviation, you'd use:

SSDEV WEIGHTThis message has been edited. Last edited by: <Mabel>,

Posts: 25 | Location: 2 Penn Plaza 28 fl | Registered: March 27, 2003

I'm not sure what your ENTIRE request was, but I took your data, loaded it into a file, and ran your code, as follows. I had to remove the final right paren (one too many), and got the following results:

STDEVA=(ASQ.DATA-(CNT.DATA*(AVE.DATA)**2))/(CNT.DATA-1); STDEV=SQRT(STDEVA); END Now, my Stats book defines VARIANCE (St. Dev. squared) as:

(n * SUM(x**2) - (SUM(x))**2)/n**2

which can be re-arranged as:

(n * SUM(x**2))/n**2 - (SUM(x)**2)/n**2

which becomes:

SUM(x**2)/n - (SUM(x)/n)**2

The first term is the definition of ASQ; the second is the average squared. The last part is giving sample values (uses n-1 instead of n instances). This is the calculation given.This message has been edited. Last edited by: <Mabel>,

Posts: 25 | Location: 2 Penn Plaza 28 fl | Registered: March 27, 2003

The first term is the definition of ASQ; the second is the average squared. The last part is giving sample values (uses n-1 instead of n instances). This is the calculation given.

I was looking for a standard deviation function myself. The above definition is exactly what wikipedia gives as:

WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :

This is an example of the standard deviation code I have used for years.

-* File Standard_Deviation.fex
LET
-* Population variance
PVAR = COMPUTE PVAR.<1>=(ASQ.<1>-(AVE.<1>*AVE.<1>));;
-* sample variance ( population variance time N/(N-1)
SVAR = CNT.<1> NOPRINT COMPUTE # SVAR2 <1>;
SVAR2 = SVAR.<1>=(ASQ.<1>-(AVE.<1>*AVE.<1>)) * (CNT.<1>/(CNT.<1>-1)) ;;
-* population standard deviation
PSDEV = PVAR <1> # NOPRINT COMPUTE PSDEV.<1>=SQRT(PVAR.<1>);;
-* sample standard deviation
SSDEV = SVAR <1> # NOPRINT COMPUTE SSDEV.<1>=SQRT(SVAR.<1>);;
END
SET ASNAMES=ON
TABLE FILE CAR
SUM
AVE.MPG AS AVE_MPG
MIN.MPG AS MIN_MPG
MAX.MPG AS MAX_MPG
SSDEV MPG AS SD_MPG
CNT.MPG AS N
BY COUNTRY
ON TABLE HOLD AS HLD_STATS
END
DEFINE FILE HLD_STATS
SE_MPG/D16.6 = SD_MPG/SQRT(N);
END
TABLE FILE HLD_STATS
SUM
AVE_MPG
MIN_MPG
MAX_MPG
SD_MPG
N
SE_MPG
BY COUNTRY
END

Posts: 60 | Location: Ellensburg Washington | Registered: May 22, 2009

But it still works, if not maintained. The difficult point is that the Masters created by Analyse are not correct and Focus can't read the Data created by Analyse. I manually correct the master and I can have Analyse at Work. Of course, it's not very professional, but some old cow-boys like to spit into a copper spitoon and drink an old scotch with old Focusians friends. Focusely and Cordially

Focus Mainframe 7.6.11 Dev Studio 7.6.11 and !!! PC Focus, Focus for OS/2, FFW Six, MSO

The first term is the definition of ASQ; the second is the average squared. The last part is giving sample values (uses n-1 instead of n instances). This is the calculation given.

I was looking for a standard deviation function myself. The above definition is exactly what wikipedia gives as:

The definition of the formula (according to wikipedia) contains SUM((x - AVE.x) **2), which is not equivalent to SUM(x **2) - SUM(AVE.x **2)!

Equivalent would be (if I remember my high-school math correctly):

s = SUM(x**2 - 2*x*AVE.x + AVE.x**2)
<=> s = SUM(x**2) - 2*SUM(AVE.x)*SUM(x) + ASQ.x

I think this is a correct implementation:

TABLE FILE EXAMPLE
SUM AVE.X AS X_AVE
BY GROUP
ON TABLE HOLD AS EXAMPLE_AVG FORMAT FOCUS INDEX GROUP
END
JOIN GROUP IN EXAMPLE TO GROUP IN EXAMPLE_AVG AS J0
DEFINE FILE EXAMPLE
DEV/D20.2 = (X - X_AVG) **2;
END
TABLE FILE EXAMPLE
COMPUTE DEV_AVG_SMP/D20.2 = DEV / (CNT.X -1); NOPRINT
COMPUTE STDDEV_POP/D20.2 = AVE.DEV ** 0.5;
COMPUTE STDDEV_SMP/D20.2 = DEV_AVG_SMP ** 0.5;
BY GROUP
END

The complexity of this issue raises the question why there is no standard implementation in WebFOCUS! Do we have an ETA for that? It's dearly needed.This message has been edited. Last edited by: Wep5622,

WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :

Easier yet, but not always an option: use SQL passthru and calculate stddev there.

That can be done more often than you'd think, because many databases support windowing aggregate functions these days, so you can do stuff like:

SELECT stddev_samp(X) over (PARTITION BY GROUP) AS X_STDDEV, X
FROM EXAMPLE
;

Which is quite a bit more flexible than the "old":

SELECT stddev_samp(X) AS X_STDDEV
FROM EXAMPLE
GROUP BY X
;

WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010 : Member of User Group Benelux :