Debian Codegraph @

reports/bias.ll

raw
1 /*
2 * @progname bias.ll
3 * @version 1.4
4 * @author Chandler
5 * @category
6 * @output Text
7 * @description
8
9 Ever notice that certain families seem to have all boys or all girls?
10 Sometimes five or six in a row of all the same sex? Is this a mere
11 statistical fluctuation, or is something special happening?
12 This program gives statistics for male vs female births.
13
14 Compute sex bias based on previous births
15
16 Version 1.3 - 1993 Aug 23 - John F. Chandler
17 Version 1.4 - 1994 Jul 19 (requires LL 3.0 or higher)
18
19 Ever notice that certain families seem to have all boys or all girls?
20 Sometimes five or six in a row of all the same sex? Is this a mere
21 statistical fluctuation, or is something special happening?
22
23 This program gives statistics for male vs female births. First, it
24 tabulates the number of males and females next born after each possible
25 proportion of previous births in the same family. In particular, it
26 gives the sex tally of first-borns (where the proportion of previous
27 births is 0 males and 0 females), then the tally for second-borns where
28 the first child was a female (0+1), and so on. Any combination that
29 doesn't actually occur in the database is skipped in the report (for
30 example, if no family is found with more than 3 sons, the tallies for
31 3+0, 3+1, and so on would all show a total of 0 males, and there would
32 be no tallies listed for 4+0, 4+1, and so on).
33
34 Children of unknown sex are not included in these statistics.
35
36 The program next prints out the relative excess of male births
37 (typically a positive value) over the nominally expected 50%. For many
38 files, there is a tendency to include incomplete families with only one
39 known child; for this reason, "only" children are excluded from these
40 statistics. Also, the male excess is computed for two different subsets
41 of the children: (A) the set of all children not born last, and (B) the
42 set of all children not born first. For both of these, there is also a
43 measure of the variability of the sex ratio to put the percentages in
44 perspective. In addition, the program prints out the correlation
45 between the sex ratio for children already born into a family and the
46 likelihood of getting a male (or female) as the *next* child. If the
47 sample is unbiased, and if the sex of each child is truly random, this
48 correlation should be 0.
49
50 It also tallies the fraction of births matching the sex of the previous
51 birth in the same family (again, excluding any children of unknown sex).
52 These results are printed out for a succession of increasingly restricted
53 cases: first, for all births of non-first-borns; then, for births preceded
54 by two-in-a-row of the same sex; then, for three-in-a-row; and so on.
55
56 Bug: combinations with more than 9 sons or more than 9 daughters are not
57 listed properly.
58
59 This program works only with LifeLines.
60
61 */
62
63 global(maxcount) /* maximum attained runcount */
64 global(nextsex) /* sex of next offspring in family */
65 global(prevsex) /* sex of previous offspring in family */
66 global(runcount) /* number of offspring so far in family */
67
68 /* Square Root function. */
69 func sqrt(x) {
70 set(sqrtval,0)
71 if(gt(x,0)) {
72 set(sqrtval,1)
73 set(approx,1)
74 set(y,4096)
75 while(le(y,x)) { /* coarse grid */
76 set(approx,y)
77 set(sqrtval,mul(sqrtval,64))
78 set(y,mul(y,4096))
79 }
80 set(y,mul(approx,4))
81 while(le(y,x)) { /* fine grid */
82 set(approx,y)
83 set(sqrtval,mul(sqrtval,2))
84 set(y,mul(y,4))
85 }
86 set(count,0)
87 while(and(ne(y,sqrtval),lt(count,9))) {
88 set(y,div(x,sqrtval))
89 set(sqrtval,div(add(y,sqrtval),2))
90 set(count,add(1,count))
91 }
92 }
93 return(sqrtval)
94 }
95
96 proc accstep(list) {
97 set(x,1)
98 while(le(x,runcount)) {
99 setel(list,x,add(1,getel(list,x)))
100 set(x,add(1,x))
101 }
102 }
103
104 proc accum(samsex,difsex) {
105 if(gt(runcount,0)) {
106 if(strcmp(nextsex,prevsex)) {
107 call accstep(difsex)
108 set(runcount,0)
109 } else { call accstep(samsex) }
110 }
111 set(prevsex,nextsex)
112 set(runcount,add(1,runcount))
113 if(gt(runcount,maxcount)) {set(maxcount,runcount)}
114 }
115
116 proc main ()
117 {
118
119 list(males)
120 list(fems)
121 list(samsex)
122 list(difsex)
123
124 set(totmales,0)
125 set(totfems,0)
126 set(onlymales,0)
127 set(onlyfems,0)
128
129 forfam (family, num) {
130 set(count,0)
131 set(runcount,0)
132 children(family,child,fnum) {
133 set(nextsex,sex(child))
134 if(not(strcmp(nextsex,"M"))) {
135 call accum(samsex,difsex)
136 if(gt(count,0)) {
137 set(totmales,add(1,totmales))
138 setel(males,count,add(1,getel(males,count)))
139 } else {set(onlymales,add(1,onlymales))}
140 set(count,add(count,10))
141 }
142 elsif(not(strcmp(nextsex,"F"))) {
143 call accum(samsex,difsex)
144 if(gt(count,0)) {
145 set(totfems,add(1,totfems))
146 setel(fems,count,add(1,getel(fems,count)))
147 } else {set(onlyfems,add(1,onlyfems))}
148 if(gt(9,mod(count,10))) {set(count,add(count,1))}
149 else { print("More than 9 daughters\n") }
150 }
151 }
152 }
153
154 /* Initialize statistics */
155 set(tot,add(totmales,totfems))
156 set(count,1)
157 set(nsample,0)
158 set(sumnfract,0)
159 set(sumpfract,0)
160 set(sumsqnfract,0)
161 set(sumsqpfract,0)
162 set(prodfract,0)
163 set(nrecs,0)
164
165 "Previous\nbirth Next\nrecord birth\nMF M F\n"
166 "00" col(sub(13,strlen(d(onlymales)))) d(onlymales)
167 col(sub(20,strlen(d(onlyfems)))) d(onlyfems) " (excluded from statistics)\n\n"
168
169 while(lt(count,100)) {
170 set(nmales,getel(males,count))
171 set(nfems,getel(fems,count))
172 if(or(nmales,nfems)) {
173 set(nrecs,add(1,nrecs))
174 if(lt(count,10)) { "0" }
175 d(count) col(sub(13,strlen(d(nmales)))) d(nmales)
176 col(sub(20,strlen(d(nfems)))) d(nfems) "\n"
177 set(nsample,add(nsample,1))
178 set(pboys,div(count,10))
179 set(pgirls,mod(count,10))
180 set(weight,add(nmales,nfems))
181 set(p,add(pboys,pgirls))
182
183 /* scales: pf-100, sqpf-10000, nf-100, sqnf-10000, prod-10000
184 i.e., express fractions as percent
185 This makes integer arithmetic acceptable.
186 Note that pfract is too small, on average, by 0.5, etc. */
187
188 set(pfract,div(mul(100,sub(pboys,pgirls)),p))
189 set(wtpfr,mul(weight,pfract))
190 set(sumpfract,add(sumpfract,wtpfr))
191 set(sumsqpfract,add(sumsqpfract,mul(pfract,wtpfr)))
192 set(wtnfr,mul(100,sub(nmales,nfems)))
193 set(nfract,div(wtnfr,weight))
194 /* set(sumnfract,add(sumnfract,wtnfr)) -- use grand difference */
195 set(sumsqnfract,add(sumsqnfract,mul(nfract,wtnfr)))
196 set(prodfract,add(prodfract,mul(wtnfr,pfract)))
197 }
198 set(count, add(count,1))
199 }
200
201 "Total:" col(sub(13,strlen(d(totmales)))) d(totmales)
202 col(sub(20,strlen(d(totfems)))) d(totfems) "\n"
203 d(nrecs) " birth combinations found\n"
204 d(tot) " 'next' individuals (excluding firstborns)\n\n"
205
206 /* Make approximate corrections for roundoff errors */
207 set(sqcorr,mul(50,sub(totmales,totfems)))
208 set(sumnfract,mul(100,sub(totmales,totfems)))
209 set(sumsqnfract,add(sumsqnfract,sqcorr))
210 set(procfract,add(prodfract,sqcorr))
211 set(sumpfract,add(sumpfract,div(tot,2)))
212 set(sumsqpfract,sub(add(sumsqpfract,sumpfract),div(tot,3)))
213
214 set(sumsqpfract,sub(sumsqpfract,div(mul(sumpfract,sumpfract),tot)))
215 set(sumsqnfract,sub(sumsqnfract,div(mul(sumnfract,sumnfract),tot)))
216 set(prodfract,sub(prodfract,div(mul(sumpfract,sumnfract),tot)))
217 set(rssp,sqrt(sumsqpfract))
218 set(rssn,sqrt(sumsqnfract))
219 set(correl,div(mul(div(prodfract,rssp),100),rssn))
220 set(rmsp,sqrt(div(sumsqpfract,tot)))
221 set(rmsn,sqrt(div(sumsqnfract,tot)))
222
223 "Male excess of previous births= " d(div(sumpfract,tot)) "% +/- " d(rmsp) "%\n"
224 "Male excess of next births = " d(div(sumnfract,tot)) "% +/- " d(rmsn) "%\n"
225 "Correlation between previous and next = " d(correl) "%\n"
226
227 set(count,1)
228 "\nFraction of births that match (in sex) a run of previous births in the"
229 "\nsame family. Children of unknown sex ignored in this tabulation.\n"
230 "\nRun" col(sub(13,5)) "Total" col(sub(25,9)) "Matching"
231 "\nLength" col(sub(13,5)) "Cases" col(sub(23,5)) "Cases" col(sub(29,1)) "%\n"
232
233 while(le(count,maxcount)) {
234 set(samesex,getel(samsex,count))
235 set(diffsex,getel(difsex,count))
236 set(allsex,add(diffsex,samesex))
237 if(gt(allsex,0)) {
238 d(count) col(sub(13,strlen(d(allsex)))) d(allsex)
239 col(sub(23,strlen(d(samesex)))) d(samesex)
240 set(percent,d(div(mul(100,samesex),allsex)))
241 col(sub(29,strlen(percent))) percent "\n"
242 }
243 set(count,add(1,count))
244 set(birth,"births")
245 }
246
247 }