Repository navigation
Expand file tree
/
Copy pathcrowd.go
More file actions
367 lines (335 loc) · 11.2 KB
/
Copy pathcrowd.go
File metadata and controls
367 lines (335 loc) · 11.2 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
// Counting people.
//
// A crowd is a number of people for every age from 0 to 100 and each sex.
// Keeping it split that way is what makes the count honest about the facts
// that depend on each other: nine in ten nurses are women, almost nobody is
// a grandparent at twenty five, and "I'm 34 and married" is a very
// different number from 34 times married. Every fact multiplies each cell
// by the share of people of that age and sex it is true of, and the lineup
// is what is left.
import (
"math"
"sync"
)
type Crowd struct {
Male, Female [101]float64
}
func (c Crowd) total() float64 {
t := 0.0
for a := range 101 {
t += c.Male[a] + c.Female[a]
}
return t
}
func (c *Crowd) times(share func(age int, male bool) float64) {
for a := range 101 {
c.Male[a] *= share(a, true)
c.Female[a] *= share(a, false)
}
}
func (c *Crowd) add(o Crowd, scale float64) {
for a := range 101 {
c.Male[a] += o.Male[a] * scale
c.Female[a] += o.Female[a] * scale
}
}
// shape is a country's people by age and sex as shares of one, so any
// population can be spread across ages the way that country's is.
func shape(k *Country) Crowd {
var c Crowd
total := 0.0
for a := range 101 {
total += k.Male[a] + k.Female[a]
}
if total == 0 {
return shape(census().World)
}
for a := range 101 {
c.Male[a] = k.Male[a] / total
c.Female[a] = k.Female[a] / total
}
return c
}
func countryOf(code string) *Country {
if k := census().Countries[code]; k != nil {
return k
}
return census().World
}
// crowdAt is everybody living in a set of places, each spread across ages
// the way its own country is.
func crowdAt(where []Place) Crowd {
var c Crowd
for _, p := range where {
c.add(shape(countryOf(p.Country)), p.Population)
}
return c
}
// everyone is the world.
func everyone() Crowd {
w := census().World
var c Crowd
c.Male, c.Female = w.Male, w.Female
return c
}
// speakersOf is everybody who speaks all of the given languages and, when
// the clock has placed the account, lives in that band of timezones.
func speakersOf(langs []string, b band) Crowd {
var c Crowd
for _, code := range census().codes() {
k := census().Countries[code]
share := 1.0
for _, l := range langs {
share *= k.Languages[l]
}
if b.known {
share *= b.share(k.Zones)
}
if share > 0 {
c.add(shape(k), k.Population*share)
}
}
return c
}
// inBand is everybody in a band of timezones, whatever they speak.
func inBand(b band) Crowd {
var c Crowd
for _, code := range census().codes() {
k := census().Countries[code]
if share := b.share(k.Zones); share > 0 {
c.add(shape(k), k.Population*share)
}
}
return c
}
// The shares below are rounded, and they are the kind of number that is
// right to within a factor of two in most of the places this will be run.
// They come from: the US Census Bureau's Current Population Survey
// (marriage, divorce, parenthood by age); Pew Research Center (family
// size, 2015); the CDC and the WHO (the conditions); Gallup and the
// Williams Institute (sexuality and gender identity, 2023); the American
// Pet Products Association and FEDIAF (pets); the OECD's Education at a
// Glance (degrees); and NCD-RisC (heights, 2020).
// married is the share of each age that is married now, rounded from the
// Census Bureau's figures for the whole country.
func married(age int, male bool) float64 {
return byAge(age, []float64{18, 0.05, 20, 0.1, 25, 0.27, 30, 0.45, 35, 0.53, 40, 0.57, 45, 0.59, 55, 0.6, 75, 0.45})
}
var partnerShare = map[string]func(age int, male bool) float64{
"married": married,
// "My partner" is said by the married as often as by anybody else, so
// it counts as being with somebody, married or not.
"partner": func(age int, male bool) float64 {
return math.Min(1, married(age, male)+byAge(age, []float64{16, 0.2, 25, 0.3, 35, 0.15, 50, 0.1}))
},
"divorced": func(age int, male bool) float64 {
return byAge(age, []float64{22, 0.01, 25, 0.02, 30, 0.06, 40, 0.11, 50, 0.14, 65, 0.12})
},
"widowed": func(age int, male bool) float64 {
f := byAge(age, []float64{25, 0.003, 45, 0.02, 55, 0.05, 65, 0.12, 75, 0.3})
if male {
// Women outlive their husbands; three in four widowed people
// are widows.
return f * 0.5
}
return f * 1.5
},
"single": func(age int, male bool) float64 {
return byAge(age, []float64{16, 0.7, 25, 0.45, 30, 0.3, 35, 0.2, 45, 0.15})
},
"engaged": func(age int, male bool) float64 {
return byAge(age, []float64{18, 0.02, 25, 0.04, 35, 0.02, 45, 0.005})
},
}
func parentShare(age int, male bool) float64 {
return byAge(age, []float64{16, 0.02, 20, 0.12, 25, 0.35, 30, 0.55, 35, 0.7, 40, 0.8, 45, 0.85})
}
func grandparentShare(age int, male bool) float64 {
return byAge(age, []float64{35, 0.02, 40, 0.08, 50, 0.35, 60, 0.65, 70, 0.8})
}
// familySize is how many children parents have, among parents. A statement
// of "at least" two, which is what "my kids" says, is the sum from two up.
var familySize = []float64{0, 0.22, 0.41, 0.24, 0.08, 0.03, 0.02}
func childrenShare(n float64, atLeast bool) float64 {
i := min(int(n), len(familySize)-1)
if i < 1 {
return 1
}
if !atLeast {
return familySize[i]
}
total := 0.0
for _, s := range familySize[i:] {
total += s
}
return total
}
var petShare = map[string]float64{
"a dog": 0.35, "a cat": 0.3, "a horse": 0.015, "a rabbit": 0.03,
"a bird": 0.04, "a reptile": 0.02, "a small mammal": 0.03,
}
var degreeShare = map[string]func(age int, male bool) float64{
"a PhD": func(age int, male bool) float64 {
return byAge(age, []float64{24, 0.005, 28, 0.012, 32, 0.018})
},
"a master's degree": func(age int, male bool) float64 {
return byAge(age, []float64{22, 0.03, 25, 0.1, 30, 0.14})
},
"no high school diploma": func(age int, male bool) float64 { return byAge(age, []float64{18, 0.1}) },
"no college degree": func(age int, male bool) float64 { return byAge(age, []float64{19, 0.12}) },
}
func flat(share float64) func(int, bool) float64 {
return func(age int, male bool) float64 { return share }
}
func women(share float64) func(int, bool) float64 {
return func(age int, male bool) float64 {
if male {
return 0
}
return share
}
}
func bySex(m, f float64) func(int, bool) float64 {
return func(age int, male bool) float64 {
if male {
return m
}
return f
}
}
var conditionShare = map[string]func(age int, male bool) float64{
"type 1 diabetes": flat(0.005),
"type 2 diabetes": func(age int, male bool) float64 { return byAge(age, []float64{20, 0.02, 40, 0.07, 60, 0.18}) },
"diabetes": func(age int, male bool) float64 {
return byAge(age, []float64{15, 0.01, 20, 0.025, 40, 0.08, 60, 0.19})
},
"ADHD": flat(0.05),
"autism": flat(0.02),
"epilepsy": flat(0.007),
"multiple sclerosis": bySex(0.0012, 0.003),
"Crohn's disease": flat(0.003),
"ulcerative colitis": flat(0.004),
"coeliac disease": flat(0.01),
"bipolar disorder": flat(0.03),
"schizophrenia": flat(0.005),
"cancer": func(age int, male bool) float64 { return byAge(age, []float64{0, 0.005, 20, 0.01, 40, 0.03, 60, 0.12}) },
"lupus": bySex(0.0002, 0.0018),
"fibromyalgia": bySex(0.005, 0.035),
"endometriosis": women(0.1),
"PCOS": women(0.1),
"cystic fibrosis": flat(0.0001),
"Parkinson's disease": func(age int, male bool) float64 {
return byAge(age, []float64{40, 0.0005, 50, 0.002, 65, 0.01, 80, 0.02})
},
"HIV": flat(0.004),
"cerebral palsy": flat(0.002),
"Down syndrome": flat(0.0008),
"deafness": flat(0.003),
"hearing loss": func(age int, male bool) float64 { return byAge(age, []float64{0, 0.02, 45, 0.08, 65, 0.3}) },
"blindness": flat(0.003),
"wheelchair user": flat(0.01),
"pregnant": func(age int, male bool) float64 {
if male {
return 0
}
return byAge(age, []float64{15, 0.02, 20, 0.07, 35, 0.04, 40, 0.01, 46, 0})
},
"amputee": flat(0.006),
"veteran": func(age int, male bool) float64 {
f := byAge(age, []float64{20, 0.02, 30, 0.04, 50, 0.08, 65, 0.15})
if male {
return f * 1.8
}
return f * 0.2
},
}
var sexualityShare = map[string]func(age int, male bool) float64{
"gay": bySex(0.022, 0.01),
"lesbian": women(0.016),
"bisexual": bySex(0.025, 0.06),
"pansexual": flat(0.01),
"asexual": flat(0.01),
"queer": flat(0.01),
}
var genderShare = map[string]func(age int, male bool) float64{
"trans": flat(0.006),
"nonbinary": func(age int, male bool) float64 { return byAge(age, []float64{14, 0.03, 30, 0.01, 50, 0.003}) },
}
// heights holds the average adult height in centimetres, men then women,
// for the countries this is most likely to be run in. Everywhere else gets
// the world's. The spread is 7.4 cm for men and 6.8 for women nearly
// everywhere it has been measured.
var heights = map[string][2]float64{
"US": {176, 162}, "GB": {177, 163}, "IE": {177, 164}, "CA": {177, 163},
"AU": {178, 164}, "NZ": {177, 164}, "ES": {176, 163}, "MX": {170, 157},
"AR": {174, 161}, "CO": {171, 158}, "CL": {172, 159}, "PE": {166, 154},
"VE": {171, 158}, "DE": {180, 166}, "FR": {178, 164}, "IT": {177, 163},
"NL": {183, 169}, "PT": {173, 162}, "BR": {173, 161}, "SE": {180, 166},
"NO": {180, 167}, "DK": {181, 168}, "PL": {178, 165}, "IN": {165, 153},
"CN": {172, 160}, "JP": {171, 158}, "KR": {175, 162},
}
func heightShare(country string, cm, window float64) func(int, bool) float64 {
means, ok := heights[country]
if !ok {
means = [2]float64{171, 159}
}
return func(age int, male bool) float64 {
if age < 18 {
return 0
}
mean, sd := means[1], 6.8
if male {
mean, sd = means[0], 7.4
}
lo := (cm - window/2 - mean) / sd
hi := (cm + window/2 - mean) / sd
return (math.Erf(hi/math.Sqrt2) - math.Erf(lo/math.Sqrt2)) / 2
}
}
// jobShare is the share of people of each age and sex who do a job. The
// BLS counts employed people sixteen and over in the United States, and the
// same share is used everywhere else, which is the weakest assumption in
// here and is said so in the README: a nurse is a much rarer thing in some
// countries than others.
func jobShare(title string) (func(int, bool) float64, bool) {
if s, ok := statuses[title]; ok {
return s.share, true
}
j, ok := census().Jobs[title]
if !ok {
return nil, false
}
men, wom := census().adults()
w := j.womenShare()
male := j.Employed * (1 - w) / men / workNorm()
female := j.Employed * w / wom / workNorm()
return func(age int, isMale bool) float64 {
if age < 16 {
return 0
}
if isMale {
return math.Min(1, male*working(age))
}
return math.Min(1, female*working(age))
}, true
}
// working is how much of the work at each age gets done, relative to the
// rest: most of any job is done between twenty and sixty five, and a share
// spread evenly over everybody past sixteen would hand as many nurses to
// the eighty year olds as to the thirty year olds. workNorm scales it so it
// averages one across the people the BLS counted, and the total number of
// nurses stays the number of nurses.
func working(age int) float64 {
return byAge(age, []float64{16, 0.35, 20, 1.3, 25, 1.45, 55, 1.2, 60, 0.9, 65, 0.3, 70, 0.1})
}
var workNorm = sync.OnceValue(func() float64 {
us := census().Countries["US"]
weighted, people := 0.0, 0.0
for a := 16; a <= 100; a++ {
n := us.Male[a] + us.Female[a]
weighted += n * working(a)
people += n
}
return weighted / people
})