Kbase P16039: How to calculate the Standard Deviation using 4GL?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  3/11/2003 |
|
Status: Unverified
GOAL:
A 4GL Standard Deviation FUNCTION?
FIX:
The following procedure includes a definition of a 4GL FUNCTION to calculate the Standard Deviation. The Main Block demonstrates two sample calls for this function. The first call passes the whole population of the Customer.Balance values while the second call passes only a random sample population of the Customer Credit-Limit values:
/*-----------------------------------------------------------------------------*/
FUNCTION StandardDeviation RETURNS DECIMAL
(INPUT pcValuesList AS CHARACTER,
INPUT lSample AS LOGICAL).
/*------------------------------------------------------------------------------
Purpose: Calculate the Standard Deviation of a sequqnce of decimal values.
Parameters:
pcValuesList: A comma seperated list of all the the values strings.
lSample : TRUE if this is a Sample data population
FALSE if this is the Whole data population.
------------------------------------------------------------------------------*/
DEFINE VARIABLE iNumElements AS INTEGER NO-UNDO.
DEFINE VARIABLE iCounter AS INTEGER NO-UNDO.
DEFINE VARIABLE dTotal AS DECIMAL NO-UNDO.
DEFINE VARIABLE dMean AS DECIMAL NO-UNDO.
DEFINE VARIABLE dVariance AS DECIMAL NO-UNDO.
/* Calculate the total of the array elements */
ASSIGN
iNumElements = NUM-ENTRIES(pcValuesList)
dTotal = 0.
DO iCounter = 1 TO iNumElements:
dTotal = dTotal + DECIMAL(ENTRY(iCounter, pcValuesList)).
END.
/* Calculate the Average (Mean) of the data */
ASSIGN
dMean = dTotal / iNumElements.
/* Calculate the total squared deviation */
ASSIGN
dTotal = 0.
DO iCounter = 1 TO iNumElements:
dTotal = dTotal + EXP(DECIMAL(ENTRY(iCounter, pcValuesList)) - dMean , 2).
END.
/* Calculate the Variance */
IF lSample THEN
ASSIGN
/* If only a sample population is used: */
dVariance = dTotal / (iNumElements - 1).
ELSE
/* If the whole population is used: */
dVariance = dTotal / iNumElements.
/* Calculate the Standard Deviation */
RETURN SQRT(dVariance).
END FUNCTION.
/*----------------------------- Main Block -----------------------------------*/
DEFINE VARIABLE cBlanaceWholePopulation AS CHARACTER NO-UNDO.
DEFINE VARIABLE cCreditLimitSample AS CHARACTER NO-UNDO.
FOR EACH customer NO-LOCK:
cBlanaceWholePopulation = cBlanaceWholePopulation + "," + STRING(Balance).
IF RANDOM(1,2) = 1 THEN
cCreditLimitSample = cCreditLimitSample + "," + STRING(Credit-Limit).
END.
MESSAGE "Balance WHOLE population Standard Deviation: " "~t" StandardDeviation(INPUT cBlanaceWholePopulation, NO) "~n"
"Credit-Limit SAMPLE Standard Deviation: " "~t" StandardDeviation(INPUT cCreditLimitSample, NO)
VIEW-AS ALERT-BOX INFO BUTTONS OK.