Debian Codegraph @

reports/gender_order.ll

raw
1 /*
2 * @progname gender_order.ll
3 * @version 4
4 * @author Jim Eggert
5 * @category
6 * @output Text
7 * @description
8
9 This program computes gender order statistics for children in all the
10 families in a database. Genders are considered to be ternary: male
11 (M), female (F), or unknown (U). Children in a family form a pattern
12 of genders by birth order, e.g. MFFM for a family consisting of a boy,
13 two girls, then a boy. The frequency of these patterns is calculated.
14 In addition to the complete gender pattern for a family, initial
15 gender patterns are computed. For the above example, the initial
16 patterns are . (no children), M, MF, MFF, and MFFM. The frequency of
17 these initial patterns can be used to answer questions such as how
18 many families with a boy then two girls go on to have another boy.
19
20 For example, suppose you want to know what fraction of families with a
21 child gender pattern P (e.g., P=MFFM) have no more children, have a
22 boy next (PM), have a girl next (PF), and have a child of unknown (to
23 the database!) gender next (PU). You can find these fractions as
24 #complete(P)/#initial(P), #initial(PM)/#initial(P),
25 #initial(PF)/#initial(P), and #initial(PU)/#initial(P), respectively.
26 Note that these fractions should add up to 1. Also note that the
27 pattern "." denotes no children at all. As a initial pattern it gives
28 the total number of families in the database, as a complete pattern the
29 number of childless families in the database.
30
31 You can use either of two compare functions to sort the results
32 differently. Rename the one you want to use as compare, the other one
33 something else (like compare1).
34
35 gender_order - a LifeLines gender order statistics program
36 by Jim Eggert ([email protected])
37 Version 1, 5 August 1993
38 listsort code by John Chandler ([email protected])
39 Version 2, 10 August 1993
40 added family examples, modified output format slightly
41 Version 3, 26 March 1995
42 changed listsort to quicksort
43 Version 4, 15 Jan 2000
44 quicksort bug fix
45
46 */
47
48 /* This compare procedure sorts purely alphabetically */
49 func compare1(astring,bstring) {
50 return(strcmp(astring,bstring))
51 }
52
53 /* This compare procedure sorts by length
54 and alphabetically within groups of equal length */
55 func compare(astring,bstring) {
56 set(alen,strlen(astring))
57 set(blen,strlen(bstring))
58 if (lt(alen,blen)) { return(neg(1)) }
59 if (gt(alen,blen)) { return(1) }
60 return(strcmp(astring,bstring))
61 }
62
63 /*
64 quicksort: Sort an input list by generating a permuted index list
65 Input: alist - list to be sorted
66 Output: ilist - list of index pointers into "alist" in sorted order
67 Needed: compare- external function of two arguments to return -1,0,+1
68 according to relative order of the two arguments
69 */
70 proc quicksort(alist,ilist) {
71 set(len,length(alist))
72 set(index,len)
73 while(index) {
74 setel(ilist,index,index)
75 decr(index)
76 }
77 if (ge(len,2)) { call qsort(alist,ilist,1,len) }
78 }
79
80 /* recursive core of quicksort */
81 proc qsort(alist,ilist,left,right) {
82 if(pcur,getpivot(alist,ilist,left,right)) {
83 set(pivot,getel(alist,getel(ilist,pcur)))
84 set(mid,partition(alist,ilist,left,right,pivot))
85 call qsort(alist,ilist,left,sub(mid,1))
86 call qsort(alist,ilist,mid,right)
87 }
88 }
89
90 /* partition around pivot */
91 func partition(alist,ilist,left,right,pivot) {
92 while(1) {
93 set(tmp,getel(ilist,left))
94 setel(ilist,left,getel(ilist,right))
95 setel(ilist,right,tmp)
96 while(lt(compare(getel(alist,getel(ilist,left)),pivot),0)) {
97 incr(left)
98 }
99 while(ge(compare(getel(alist,getel(ilist,right)),pivot),0)) {
100 decr(right)
101 }
102 if(gt(left,right)) { break() }
103 }
104 return(left)
105 }
106
107 /* choose pivot */
108 func getpivot(alist,ilist,left,right) {
109 set(pivot,getel(alist,getel(ilist,left)))
110 set(left0,left)
111 incr(left)
112 while(le(left,right)) {
113 set(rel,compare(getel(alist,getel(ilist,left)),pivot))
114 if (gt(rel,0)) { return(left) }
115 if (lt(rel,0)) { return(left0) }
116 incr(left)
117 }
118 return(0)
119 }
120
121 proc main() {
122 list(patterns)
123 list(initial_counts)
124 list(complete_counts)
125 table(indices)
126 list(sorted_indices)
127 list(complete_examples)
128
129 /* accumulate gender order statistics, even unknown genders */
130
131 enqueue(initial_counts,0)
132 enqueue(complete_counts,0)
133 enqueue(patterns,".")
134 enqueue(complete_examples,"no example")
135 insert(indices,".",1)
136 set(max_index,1)
137 set(max_nc,0)
138
139 set(nextfam,0)
140 print("Processing families ")
141 forfam(family,fnum) {
142 setel(initial_counts,1,fnum)
143 set(pattern,"")
144 set(index,1)
145 if (nc,nchildren(family)) {
146 if (gt(nc,max_nc)) { set(max_nc,nc) }
147 children(family,child,cnum) {
148 if (not(strcmp(sex(child),"F"))) {
149 set(pattern,save(concat(pattern,"F")))
150 }
151 elsif (not(strcmp(sex(child),"M"))) {
152 set(pattern,save(concat(pattern,"M")))
153 }
154 else {
155 set(pattern,save(concat(pattern,"U")))
156 }
157 set(index,lookup(indices,pattern))
158 if (index) {
159 setel(initial_counts,index,
160 add(getel(initial_counts,index),1))
161 }
162 else {
163 set(max_index,add(max_index,1))
164 set(index,max_index)
165 insert(indices,pattern,index)
166 enqueue(patterns,save(pattern))
167 enqueue(initial_counts,1)
168 enqueue(complete_counts,0)
169 }
170 }
171 }
172 else {
173 if(not(strcmp(getel(complete_examples,1),"no example"))) {
174 setel(complete_examples,1,save(key(family)))
175 }
176 }
177 if (not(getel(complete_examples,index))) {
178 setel(complete_examples,index,save(key(family)))
179 }
180 setel(complete_counts,index,add(getel(complete_counts,index),1))
181 if (ge(fnum,nextfam)) {
182 print(d(fnum)) print(" ")
183 set(nextfam,add(nextfam,100))
184 }
185 }
186
187 print("\nSorting results...")
188
189 call quicksort(patterns,sorted_indices)
190
191 /* print out gender order statistics sorted alphabetically */
192
193 print("done\nPrinting results...")
194
195 set(initialcol,add(max_nc,16))
196 set(completecol,add(initialcol,12))
197 set(examplecol,add(completecol,12))
198 "Gender pattern" col(sub(initialcol,7)) "initial"
199 col(sub(completecol,8)) "complete" col(examplecol) "example\n"
200
201 forlist(sorted_indices,index,inum) {
202 getel(patterns,index)
203 set(initial,getel(initial_counts,index))
204 col(sub(initialcol,strlen(d(initial)))) d(initial)
205 set(complete,getel(complete_counts,index))
206 col(sub(completecol,strlen(d(complete)))) d(complete)
207 col(examplecol) getel(complete_examples,index) "\n"
208 }
209 print("done")
210 }