-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGROUPFUN.for
More file actions
280 lines (270 loc) · 8.73 KB
/
GROUPFUN.for
File metadata and controls
280 lines (270 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
program groupfun
parameter(maxnum=160,maxbin=15000)
implicit double precision (a-h,o-z)
* maxnum is max. sample size
************************************************************************
* *
* This program does permutation 2-sample grouped t-test. *
* date 21/2/94 *
* *
************************************************************************
integer n,firstgroup(0:maxnum),binsreq,x(0:maxnum)
+,dope(0:maxnum)
double precision a(0:maxnum),p,cv,ptol ,b(0:maxbin)
* fractional error required on prob.
character*80 filename
ptol=0.05
* array to hold filename
call coua('Enter input file name: ')
read(*,'(a)')filename
* get file name
open(5,file=filename,status='old')
n=0
1 read(5,*,err=4,end=4)a(n),firstgroup(n)
write(*,2)n,a(n),firstgroup(n)
2 format('number',i5,' is ',f12.4,' group ',i5)
n=n+1
if(n.gt.maxnum)then
write(*,3)maxnum
3 format('sample size too large. Maximum is', i5)
stop 'failed'
endif
goto 1
4 continue
write(*,5)n
5 format('read in', i5,' numbers')
p=pitmanprob(a,firstgroup,n,ptol,cv,binsreq,x,b,maxbin,dope)
* find tail probability
if(p.lt.0.)then
write(*,6)binsreq
6 format(i5,' array elements requested, which is too many.')
stop 'failed'
else
write(*,7)binsreq,p,cv
7 format(i5,' array elements used'
+/'1-tailed probability is ',f12.4/
+'CV of estimated probability is ',f12.4)
endif
stop 'done'
end
double precision function pitmanprob( a, firstgroup,
+n, ptol,cv,binsreq,x,b,maxbin,dope)
implicit double precision (a-h,o-z)
* returns 1-tail probability from Pitman's 2-sample permutation test,
* the distribution-free analogue of the grouped t-test
integer numberone,numbertwo,binsreq,
+smallnum,k,j,smallgroup,ntop,kbot,
+bin,i,isum,s,n0,n0dash,ndash,m, dope(0:*), x(0:*),firstgroup(0:*)
double precision a(0:*),onesum,twosum,scale,sum,tail,term1,
+term2,
+minnum,maxnum,onemean,twomean,mu,sigsq,em,en,factor, b(0:*)
numberone=0
numbertwo=0
isum=0
onesum=0.d+00
twosum=0.d+00
binsreq=n
do 1 i=0,n-1
if(i .eq. 0)then
minnum=a(i)
* find max. and min. sample values
maxnum=a(i)
else
minnum=min(a(i),minnum)
maxnum=max(a(i),maxnum)
endif
if(firstgroup(i) .eq.1)then
numberone=numberone+1
* count numbers of in each group
onesum=onesum+a(i)
* and their sums
else
numbertwo=numbertwo+1
twosum=twosum+a(i)
endif
1 continue
onemean=onesum/numberone
* find means for each group
twomean=twosum/numbertwo
if(onemean.gt.twomean)then
smallgroup=2
else
smallgroup=1
endif
do 2 i=0,n-1
2 a(i)=a(i)-minnum
* translate sample values to be .gt.= 0
if(numberone.lt.numbertwo)then
smallnum=1
else
smallnum=2
endif
onesum=onesum-numberone*minnum
* and modify sums and maximum value
twosum=twosum-numbertwo*minnum
maxnum=maxnum-minnum
if(smallgroup .ne. smallnum)then
* if necessary, reflect values so smaller group has smaller mean
do 3 i=0,n-1
3 a(i)=maxnum-a(i)
onesum=numberone*maxnum-onesum
twosum=numbertwo*maxnum-twosum
endif
if(smallnum.eq.1)then
sum=onesum
else
sum=twosum
endif
m=min(numberone,numbertwo)
* number in smaller group
em=m
en=n
mu=0.d+00
sigsq=0.d+00
do 5 i=0,n-1
mu=mu+a(i)
sigsq=sigsq+a(i)*a(i)
do 4 j=i+1,n-1
4 sigsq=sigsq-2.*a(i)*a(j)/(en-1.)
5 continue
mu=(em/en)*mu
sigsq=(em/en)*(1.-(em/en))*sigsq
* permutation mean and variance
factor=sqrt((em/en)*(1.-(em/en))*(en+(mu-sum)*(mu-sum)*
+(en+1.)/(en*sigsq))/12.)
term1=mu*(mu-sum)/(ptol*sigsq)
term2=2./ptol
bin=factor*max(term1,term2)
* number of histogram bins needed
scale=bin/sum
* scale sample values so that sum of smaller sample
* is in bin number bin
do 6 i=0,n-1
x(i)=a(i)*scale+0.5
* rescaling sample values.
* Adding 0.5 rounds to nearest bin rather than truncating
if(firstgroup(i).eq.smallnum)isum=isum+x(i)
6 continue
bin=isum
* find exact histogram bin for start of reject H0 region
binsreq=(bin+2)*m
if(binsreq.gt.maxbin)then
write(*,7)maxbin,binsreq
7 format('increase MAXBIN from ',i5,' to',i6,' and recompile')
stop 'failed'
endif
do 666 i=0,binsreq
666 b(i)=0.d+00
call sort(x,n)
* sort sample
do 8 i=0,n-1
8 x(i)=min(x(i),bin+1)
* any sample values greater than the test statistic 'bin'
* will cause b(.gt.bin) to be occupied and are set
* to bin+1, the overflow bin NOW rather than later so as not to cause integer
* overflow
do 9 i=0,m-1
9 dope(i)=i*(bin+2)
* offsets to mimick 2 dim. array
b(dope(0)+x(0))=b(dope(0)+x(0))+1
* start histogram off
* for 2 dim. array code would be ++b(0)(x(0))
n0=x(0)
do 13 s=1,n-1
kbot=max(m+s-n,1)
n0dash=n0+x(s)
* translate each level by x(s)
do 12 k=min(s,m-1),kbot,-1
* only build up histograms
* as far as mth level, which is the one we want
if(n0dash.gt.bin)then
do 10 ndash=bin-x(s)+1,bin+1
* translate k-1 th level onto kth level...these terms go into overflow bin
10 b(dope(k)+bin+1)=b(dope(k)+bin+1)+b(dope(k-1)+ndash)
endif
* for 2 dim. array... b(k)(bin+1)+=b(k-1)(ndash)
ntop=min(bin,n0dash)
do 11 ndash=x(s),ntop
* these terms just translate along, but may go into overflow bin
b(dope(k)+ndash)=b(dope(k)+ndash)+b(dope(k-1)+ndash-x(s)
+)
* for 2 dim. array... b(k)(ndash)+=b(k-1)(ndash-x(s))
11 continue
12 continue
b(dope(0)+x(s))=b(dope(0)+x(s))+1
* add new term to lowest level
* for 2 dim. array... ++b(0)(x(s))
n0=n0dash
13 continue
tail=0.
* find tail probability
do 14 i=0,bin
14 tail=tail+b(dope(m-1)+i)
* for 2 dim. array... tail+=b(m-1)(i)
cv=100.*b(dope(m-1)+bin)*factor/tail
* find coefficient of variation
* for 2d array... *cv=100.*b(m-1)(bin)*factor/tail
pitmanprob=tail/(tail+b(dope(m-1)+bin+1))
end
SUBROUTINE SORT(A,N)
INTEGER A(N),X,W
DIMENSION LTT(20),RT(20)
INTEGER R,RT
************************************************************************
* *
* THIS ROUTINE SORTS THE INTEGER ARRAY ~A, WITH N ELEMENTS, *
* INTO ASCENDING ORDER. THE ~QUICKERSORT ALGORITHM, ALGORITHM 271, *
* COMM ~ACM 8, OF NOVEMBER 1965, P669, IS USED. *
* INPUT-OUTPUT *
* MODIFY A INTEGER ARRAY OF NUMBERS TO BE SORTED. *
* INPUT N, NUMBER OF ELEMENTS. *
* DATE 20/09/83 *
* *
************************************************************************
IF(N.EQ.0)RETURN
LEVEL=1
LTT(1)=1
RT(1)=N
1 L=LTT(LEVEL)
R=RT(LEVEL)
LEVEL=LEVEL-1
2 IF(R.LE.L)THEN
IF(LEVEL)8,8,1
ENDIF
C
C SUBDIVIDE THE INTERVAL L,R
C L : LOWER LIMIT OF THE INTERVAL (INPUT)
C R : UPPER LIMIT OF THE INTERVAL (INPUT)
C J : UPPER LIMIT OF LOWER SUB-INTERVAL (OUTPUT)
C I : LOWER LIMIT OF UPPER SUB-INTERVAL (OUTPUT)
C
I=L
J=R
M=(L+R)/2
X=A(M)
3 IF(A(I).GE.X)GOTO 4
I=I+1
GOTO 3
4 IF(A(J).LE.X)GOTO 5
J=J-1
GOTO 4
C
5 IF(I.GT.J)GOTO 6
W=A(I)
A(I)=A(J)
A(J)=W
I=I+1
J=J-1
IF(I.LE.J)GOTO 3
C
6 LEVEL=LEVEL+1
IF((R-I).GE.(J-L))GOTO 7
LTT(LEVEL)=L
RT(LEVEL)=J
L=I
GOTO 2
7 LTT(LEVEL)=I
RT(LEVEL)=R
R=J
GOTO 2
8 END