* 
* REMEDIAN MYDATA  NBEG NEND  
*
* MYDATA = THE DATA VECTOR WHOSE MEDIAN IS TO BE ESTIMATED
* NBEG = THE FIRST OBSERVATION IN MYDATA
* NEND = THE LAST OBSERVATION IN MYDATA
*
* REMEDIAN.SRC COMPUTES THE REMEDIAN, A MEDIAN FOR
* LARGE DATA SETS. THE REMEDIAN, WHICH USES A
* SMALL AMOUNT OF COMPUTER STORAGE, WAS PROPOSED BY:
*
*   PETER J. ROUSSEEUW AND GILBERT W. BASSETT, JR.
*   (1990), "THE REMEDIAN: A ROBUST AVERAGING
*   METHOD FOR LARGE DATA SETS," JOURNAL OF THE
*   AMERICAN STATISTICAL ASSOCIATION, 85, 97-104.
*
*  THE CURRENT VERSION OF THIS PROGRAM CAN HANDLE
*  AS MANY AS 17^5 = 1,419,857 OBSERVATIONS.
*  
*  NOTE THAT REMEDIAN.SRC EXPECTS THE DATA IN A VECTOR, NOT
*  A SERIES; IF NECESSARY, THE PROGRAM THAT CALLS REMEDIAN.SRC 
*  SHOULD STORE THE SERIES IN A VECTOR. IN ADDITION, THE
*  PROGRAM CALLING REMEDIAN.SRC SHOULD ALLOCATE AT LEAST
*  85 UNITS FOR THE SERIES Z AND ZZ IN THE PROCEDURE (E. G.,
*  ALL 85). 
*
*  REMEDIAN.SRC IS NOT RECOMMENDED FOR DATA SETS SMALLER THAN
*  A FEW THOUSAND OBSERVATIONS, FOR WHICH THE MEDIAN CAN BE
*  COMPUTED FASTER AND MORE ACCURATELY USING THE STATISTICS
*  COMMAND WITH THE FRACTILES OPTION. 
* 
PROCEDURE REMEDIAN MYDATA NBEG NEND
*
TYPE INTEGER NBEG NEND
TYPE VECTOR MYDATA
LOCAL INTEGER I J K L M N NOBS JW KW LW MW NW II NTOT
LOCAL REAL SUMW WCAP
LOCAL VECTOR[REAL] A1 A2 A3 A4 A5 W V  
LOCAL SERIES[REAL]  Z ZZ
DIM A1(17) A2(17) A3(17) A4(17) A5(17) W(85) V(85)
COMPUTE NOBS = NEND-NBEG+1 
COMPUTE I = 0
COMPUTE J = 1
COMPUTE K = 1
COMPUTE L = 1
COMPUTE M = 1
COMPUTE N = 1
DO N = 1,17
DO M = 1,17
DO L = 1,17
DO K = 1,17
DO J = 1,17
COMPUTE I = I + 1
IF I > NOBS 
BEGIN
BRANCH 10
END IF
COMPUTE A1(J) = MYDATA(I)
END DO J
SET Z 1 17 = A1(T)
ORDER Z 1 17
COMPUTE A2(K) = Z(9)
END DO K
SET Z 1 17 = A2(T)
ORDER Z 1 17
COMPUTE A3(L) = Z(9)
END DO L
SET Z 1 17 = A3(T)
ORDER Z 1 17
COMPUTE A4(M) = Z(9)
END DO M
SET Z 1 17 = A4(T)
ORDER Z 1 17
COMPUTE A5(N) = Z(9)
END DO N
:10 COMPUTE JW = J-1
COMPUTE KW = K-1
COMPUTE LW = L-1
COMPUTE MW = M-1
COMPUTE NW = N-1
COMPUTE II=0
DO I = 1,JW
COMPUTE II=II+1
COMPUTE V(II) = A1(I)
COMPUTE W(II) = 1.0
END DO I
DO I = 1,KW
COMPUTE II=II+1
COMPUTE V(II) = A2(I)
COMPUTE W(II) = 17.0
END DO I
DO I = 1,LW
COMPUTE II=II+1
COMPUTE V(II) = A3(I)
COMPUTE W(II) = 289.0
END DO I
DO I = 1,MW
COMPUTE II=II+1
COMPUTE V(II) = A4(I)
COMPUTE W(II) = 4913.0
END DO I
DO I = 1,NW
COMPUTE II=II+1
COMPUTE V(II) = A5(I)
COMPUTE W(II) = 83521.0
END DO I
COMPUTE NTOT = JW+KW+LW+MW+NW
SET Z 1 85 = V(T)
SET ZZ 1 85 = W(T)
ORDER Z 1 85 ZZ
DO I = 1,85
COMPUTE V(I) = Z(I)
COMPUTE W(I) = ZZ(I)
END DO I 
COMPUTE SUMW = 0.0
COMPUTE WCAP = (NOBS+1)/2
DO I = 1,NTOT
IF SUMW > WCAP
BEGIN
BRANCH 20
END IF
COMPUTE SUMW = SUMW + W(I)
END DO I
:20 DISPLAY 'REMEDIAN =  ' V(I-1)
END REMEDIAN

 