Debian Codegraph @

t/lines/bias

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