-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2b.pl
745 lines (498 loc) · 17.2 KB
/
m2b.pl
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
########################
# m2b.pl #
# Perl Program #
# Aaron Gullickson #
# 9/14/00 #
########################
######################################################################
# SUMMARY
#
# This program will link births to the marriages they must have come
# from. It should work pretty well, because the combination of
# husband's and wive's names are almost always unique. We will also be
# able to easily compare it to Marcia's work in combodat.
#
# For each birth, the script will look at the reported names of the parents.
# If it can find an exact match in the marriage records, it accepts it because
# this is almost certainly the right one (no duplicates). If not, then it
# collects all marriages in which three of the four names matched (both first
# names must always match but it allows one of the last names of either spouse to be missing). It then scores all of these possibilities based upon various
# factors including birth spacing and the age of the mother at birth (where age
# of wife is reported) and picks the top score.
#
# This will now be the first script run as it is the most reliable - so
# we will use it to make decisions about b2d and b2m rather than the other
# way around.
#
# In addition to finding children of parent, this script will also build
# "kinsets" - groups of children who appear to be related by parents names
# but whose parents cannot be found in the marriage records. This will allow
# us to look at lateral kin relations later on.
#
######################################################################
require generalsubs;
$na="NA";
$none="";
$mindur=-4;
$maxdur=40;
$maxint=10;
$minscore=40;
%weights = (
"parish" => 5,
"pof" => 10,
"pom" => 5,
"int" => 20,
"name" => 10,
"witness" => 5,
);
#these are the minimum intervals accepted between births - most are
#based on biological necessity (more or less). The first one is based on the
#earliest pre-marital birth in Marcia's dataset.
%minint = (
"0" => -3.578,
"1" => .75,
"2" => .75,
"3" => .75,
"4" => .75,
"5" => .75,
"6" => .75,
"7" => .75,
"8" => .75,
"9" => .75,
"10" => .75,
"11" => .75,
"12" => .75,
"13" => .75,
"14" => .75,
);
#intervals between births for scoring - based on combodat
%maxint = (
"0" => 24.7,
"1" => 19.77,
"2" => 24.22,
"3" => 19.38,
"4" => 16.6,
"5" => 19.32,
"6" => 14.26,
"7" => 10.99,
"8" => 9.024,
"9" => 9.605,
"10" => 7.77,
"11" => 9.532,
"12" => 6.709,
"13" => 3.148,
"14" => 3,
);
%lowerqint = (
"0" => 0.887,
"1" => 1.579,
"2" => 1.697,
"3" => 1.781,
"4" => 1.788,
"5" => 1.78,
"6" => 1.776,
"7" => 1.641,
"8" => 1.665,
"9" => 1.575,
"10" => 1.529,
"11" => 1.663,
"12" => 1.483,
"13" => 1.274,
"14" => 1.25,
);
%upperqint = (
"0" => 2.209,
"1" => 3.086,
"2" => 3.13,
"3" => 3.164,
"4" => 3.244,
"5" => 3.167,
"6" => 3.153,
"7" => 3.114,
"8" => 3.122,
"9" => 3.115,
"10" => 2.856,
"11" => 2.924,
"12" => 2.493,
"13" => 2.256,
"14" => 2.25,
);
%meanint = (
"0" => 1.97,
"1" => 2.611,
"2" => 2.657,
"3" => 2.648,
"4" => 2.696,
"5" => 2.606,
"6" => 2.584,
"7" => 2.539,
"8" => 2.488,
"9" => 2.456,
"10" => 2.24,
"11" => 2.382,
"12" => 2.061,
"13" => 1.627,
"14" => 1.5,
);
&get_date_time;
#input files
$binfile="input/sortedbirths.tsv";
$minfile="input/sortedmars.tsv";
open (BIRIN,"<$binfile") || die ("cant open"." $binfile");
open (MARIN,"<$minfile") || die ("cant open"." $minfile");
#output files
$matchfile="output/m2b.matches.tsv";
#$prelimfile="output/m2b.match.prelim.txt";
$detailfile="output/diagnostics/m2b.diag.txt";
#open (PRELIM, ">$prelimfile") || die ("cant open"." $prelimfile");
open (FINAL,">$matchfile") || die ("cant open "."$matchfile");
open (DETAIL, ">$detailfile") || die ("cant open "."$detailfile");
print ("Program name is m2b.pl\n");
print(DETAIL "Detail file for m2b.pl\n");
print(DETAIL "Program started on "."$daterun "."$yrrun"." at "."$timerun\n\n");
@MAR = <MARIN>;
$mar_length = scalar @MAR;
@BIRTH = <BIRIN>;
$bir_length = scalar @BIRTH;
#################
# Section 1: set up hashes
#Set up a hash with the marriage name combo as the key
print("Beginning Section 1: Building hashes\n");
%mar_hash=();
$missing_hfn=0;
$missing_wfn=0;
$missing_hln=0;
$missing_wln=0;
%m2b_hash=();
%mdate_hash=();
%bdate_hash=();
%temp1_hash=();
%temp2_hash=();
foreach $mar(@MAR) {
($mpar,$mi,$mid,$muni,$myrdate,$myyy,$mmm,$mdd,$mfnh,$mlnh,$mmstath,
$mageh,$mpoh,$mfnhfa,$mfnw,$mlnw,$mmstatw,$magew,$mpow,$mfnwrel,
$mtypwrel,$mfnwit1,$mlnwit1,$mpowit1,$mfnwit2,$mlnwit2,$mpowit2,
$mlnhfull,$mlnwfull,$mlnwit1full,$mlnwit2full,$mnamef,$mnames,
$mmatchflag)=split("\t", $mar);
$line=join("\t", $mid, $na, 0);
$m2b_hash{$mid}=$line;
$mdate_hash{$mid}=$myrdate;
$mfnh = &stripwhite($mfnh);
$mlnh = &stripwhite($mlnh);
$mfnw = &stripwhite($mfnw);
$mlnw = &stripwhite($mlnw);
$missing_hfn=$missing_hfn + &isnull($mfnh);
$missing_wfn=$missing_wfn + &isnull($mfnw);
$missing_hln=$missing_hln + &isnull($mlnh);
$missing_wln=$missing_wln + &isnull($mlnw);
#I will match on three possibilitiess:
# - father first and last and mother first
# - father first and mother first
# - all four names.
#match on three
$names="$mfnh"."$mlnh"."$mfnw";
push(@{$mar_hash{$names}}, $mar);
$names="$mfnh"."$mfnw"."$mlnw";
push(@{$mar_hash{$names}}, $mar);
#match on all four
$names="$mfnh"."$mlnh"."$mfnw"."$mlnw";
push(@{$mar_hash{$names}}, $mar);
$temp1_hash{$names}=1;
}
#I can see how many unique name combos there were for all four
$unique1=scalar keys %temp1_hash;
print("Section 1 Done\n");
################
# Now loop through births and find corresponding potential marriages
print("Beginning Section 2: linking births to parent's marriage\n");
$match=0;
$ties=0;
@links=();
@unlinked=();
$missing_ffn=0;
$missing_mfn=0;
$missing_fln=0;
$missing_mln=0;
foreach $birth(@BIRTH) {
($bpar,$bid1,$bid2,$uni,$bdate,$byyy,$bmm,$bdd,$bfn,$bsx,$bfnf,$blnf,
$bfnm,$blnm,$bpob,
$bfng,$blng,$bpog) = split("\t",$birth);
$bid2=~s/\s//g;
$bdate_hash{$bid2}=$bdate;
$bfnf = &stripwhite($bfnf);
$blnf = &stripwhite($blnf);
$bfnm = &stripwhite($bfnm);
$blnm = &stripwhite($blnm);
$missing_ffn = $missing_ffn + &isnull($bfnf);
$missing_mfn = $missing_mfn + &isnull($bfnm);
$missing_fln = $missing_fln + &isnull($blnf);
$missing_mln = $missing_mln + &isnull($blnm);
$key="$bfnf"."$blnf"."$bfnm"."$blnm";
@marriages = @{$mar_hash {$key}};
#if this is empty then look for other marriages. If it is
#not empty then it is certainly the right one - so go no further
if(&isnull($marriages[0])) {
$key="$bfnf"."$blnf"."$bfnm";
@marriages2 = @{$mar_hash {$key}};
foreach $line(@marriages2) {
push(@marriages, $line);
}
$key="$bfnf"."$bfnm"."$blnm";
@marriages2 = @{$mar_hash {$key}};
foreach $line(@marriages2) {
push(@marriages, $line);
}
}
$empty=1;
@temp=();
#now score each of these potential marriages. See matchrecs below
#for details
foreach $mar(@marriages) {
$score=0;
($mpar,$mi,$mid,$muni,$myrdate,$myyy,$mmm,$mdd,$mfnh,$mlnh,$mmstath,
$mageh,$mpoh,$mfnhfa,$mfnw,$mlnw,$mmstatw,$magew,$mpow,$mfnwrel,
$mtypwrel,$mfnwit1,$mlnwit1,$mpowit1,$mfnwit2,$mlnwit2,$mpowit2,
$mlnhfull,$mlnwfull,$mlnwit1full,$mlnwit2full,$mnamef,$mnames,
$mmatchflag)=split("\t", $mar);
$mar_history=$m2b_hash{$mid};
($mid, $lastdate, $numkids, $kids)=split("\t", $mar_history);
#for now only one limit, birth must occur within certain time period from
#marriage
$duration = $bdate - $myrdate;
$birth_interval=$bdate - $lastdate;
#put in another requirement - if magew is given, birth must occur before she hits age 55
if(&isnot_na($magew)) {
$motherage=$duration+$magew;
} else {
$motherage=25;
}
if($duration>$mindur && $duration<$maxdur && $birth_interval>=0 && $numkids<14 && $motherage<55) {
$score=&matchrecs;
if($score>$minscore) {
$empty=0;
push(@temp, join("\t", $score, $bdate, $bid2, $mid));
}
}
}
if($empty) {
#printf(PRELIM "%6d\t %s\t %s\t %s\n", $bid2, $na, $na, $na);
#assign this birth to the unlinked birth array to later
#build sibsets with.
push(@unlinked, $birth);
} else {
$match = $match + 1;
@temp = sort {$b<=>$a} @temp;
($score1, $bdate1, $bid1, $mid1)=split("\t", $temp[0]);
($score2, $bdate2, $bid2, $mid2)=split("\t", $temp[1]);
$mar_history=$m2b_hash{$mid1};
($mid, $lastdate, $numkids, $kids)=split("\t", $mar_history);
$lastdate>$bdate1 && die("birthdates out of order");
$lastdate=$bdate1;
if(&isnull($kids)) {
$kids="$bid1";
$numkids=1;
} else {
$kids="$kids $bid1";
$numkids=$numkids+1;
}
$m2b_hash{$mid}=join("\t", $mid, $lastdate, $numkids, $kids);
if($score1==$score2 && $mid1!=$mid2) {
$ties=$ties+1;
}
foreach $ref(@temp) {
($score, $bdate, $bid, $mid)=split("\t", $ref);
#printf(PRELIM "%6d\t %6d\t %6d\t %6d\n", $bid, $mid, $bdate, $score);
}
}
}
print("Section 2 Done\n");
print("Beginning Section 3: linking kin-sets\n");
%kin_hash=();
$unmatched=scalar @unlinked;
$accepted1=0;
$accepted2=0;
$keychanger=1;
foreach $birth(@unlinked) {
($bpar,$bid1,$bid2,$uni,$bdate,$byyy,$bmm,$bdd,$bfn,$bsx,$bfnf,$blnf,$bfnm,
$blnm,$bpob,$bfng,$blng,$bpog) = split("\t",$birth);
$bfnf = &stripwhite($bfnf);
$blnf = &stripwhite($blnf);
$bfnm = &stripwhite($bfnm);
$blnm = &stripwhite($blnm);
if(&isnotnull($bfnf) && &isnotnull($blnf) && &isnotnull($bfnm)) {
$accepted1++;
$key="$bfnf"."$blnf"."$bfnm";
$line=$kin_hash{"$key"};
($firstdate, $lastdate, $numkids, $kids)=split("\t", $line);
#make some simple rules governing acceptance into kin set.
#no more than $maxint years between births, no more than max time between
#first and final birth - if they dont, make it here then start a new kinset
if(&isnull($lastdate)) {
$firstdate=$bdate;
$lastdate=$bdate-.75;
}
$duration=$bdate-$firstdate;
$birth_interval=$bdate-$lastdate;
$lastdate=$bdate;
$numkids=$numkids+1;
$kids=join(" ", $kids, $bid2);
if(($birth_interval>=.75 || $birth_interval==0) && $birth_interval<=$maxint && $duration<$maxdur) {
$accepted2++;
$kin_hash{"$key"} = join("\t", $firstdate, $lastdate, $numkids, $kids);
} else {
#start new kinset
#all of these should be in temporal order, so all subsequent kids will also
#fall into this new kinset
#first change the key on the old one so that we can use this one
$kin_hash{"$key".$keychanger}=$line;
$accepted2++;
$keychanger++;
$firstdate=$bdate;
$lastdate=$bdate;
$numkids=1;
$kids=$bid2;
$kin_hash{"$key"} = join("\t", $firstdate, $lastdate, $numkids, $kids);
}
}
}
print("Section 3 Done\n");
print("Beginning Section 4: Printing results\n");
@keys=keys(%m2b_hash);
@kids_linked=();
foreach $key(@keys) {
$marriage=$m2b_hash{$key};
($mid,$lastdate,$numkids,$kids)=split("\t", $marriage);
push(@kids_linked, $numkids);
($idk1,$idk2,$idk3,$idk4,$idk5,$idk6,$idk7,$idk8,$idk9,
$idk10,$idk11,$idk12,$idk13,$idk14)=split(" ", $kids);
$idk1=~s/\s//g;
$firstdur=$bdate_hash{$idk1}-$mdate_hash{$mid};
$lastdur=$lastdate-$mdate_hash{$mid};
if(&isnull($idk1)) {$idk1=$na};
if(&isnull($idk2)) {$idk2=$na};
if(&isnull($idk3)) {$idk3=$na};
if(&isnull($idk4)) {$idk4=$na};
if(&isnull($idk5)) {$idk5=$na};
if(&isnull($idk6)) {$idk6=$na};
if(&isnull($idk7)) {$idk7=$na};
if(&isnull($idk8)) {$idk8=$na};
if(&isnull($idk9)) {$idk9=$na};
if(&isnull($idk10)) {$idk10=$na};
if(&isnull($idk11)) {$idk11=$na};
if(&isnull($idk12)) {$idk12=$na};
if(&isnull($idk13)) {$idk13=$na};
if(&isnull($idk14)) {$idk14=$na};
$line=join("\t", $mid,$lastdate,$numkids,$idk1,$idk2,$idk3,$idk4,$idk5,$idk6,$idk7,$idk8,$idk9,
$idk10,$idk11,$idk12,$idk13,$idk14);
print(FINAL "$line\n");
}
#Now print out kinsets
@keys=keys(%kin_hash);
@kids_unlinked=();
$id=30001;
foreach $key(@keys) {
$marriage=$kin_hash{$key};
($firstdate, $lastdate,$numkids,$kids)=split("\t", $marriage);
push(@kids_unlinked, $numkids);
($idk1,$idk2,$idk3,$idk4,$idk5,$idk6,$idk7,$idk8,$idk9,
$idk10,$idk11,$idk12,$idk13,$idk14)=split(" ", $kids);
if(&isnull($idk1)) {$idk1=$na};
if(&isnull($idk2)) {$idk2=$na};
if(&isnull($idk3)) {$idk3=$na};
if(&isnull($idk4)) {$idk4=$na};
if(&isnull($idk5)) {$idk5=$na};
if(&isnull($idk6)) {$idk6=$na};
if(&isnull($idk7)) {$idk7=$na};
if(&isnull($idk8)) {$idk8=$na};
if(&isnull($idk9)) {$idk9=$na};
if(&isnull($idk10)) {$idk10=$na};
if(&isnull($idk11)) {$idk11=$na};
if(&isnull($idk12)) {$idk12=$na};
if(&isnull($idk13)) {$idk13=$na};
if(&isnull($idk14)) {$idk14=$na};
$line=join("\t", $id,$lastdate,$numkids,$idk1,$idk2,$idk3,$idk4,$idk5,$idk6,$idk7,$idk8,$idk9,
$idk10,$idk11,$idk12,$idk13,$idk14);
print(FINAL "$line\n");
$id=$id+1;
}
$family_size_linked = &sum (@kids_linked) / scalar @kids_linked;
$family_size_unlinked = &sum (@kids_unlinked) / scalar @kids_unlinked;
print("Section 4 Done\n");
####
print(DETAIL "There were $mar_length marriages\n");
print(DETAIL "There were $bir_length births\n\n");
print(DETAIL "There were $unique1 unique name combinations when all four were used\n\n");
print(DETAIL "$missing_hfn marriages were missing husband's first name\n");
print(DETAIL "$missing_hfn marriages were missing wife's first name\n");
print(DETAIL "$missing_hln marriages were missing husband's last name\n");
print(DETAIL "$missing_wln marriages were missing wife's last name\n\n");
print(DETAIL "$missing_ffn births were missing father's first name\n");
print(DETAIL "$missing_mfn births were missing mother's first name\n");
print(DETAIL "$missing_fln births were missing father's last name\n");
print(DETAIL "$missing_mln births were missing mother's last name\n\n");
print(DETAIL "There were $match matches\n");
$percent = $match/$bir_length * 100;
print(DETAIL "$percent percent of births were linked to a marriage\n\n");
print(DETAIL "There were $unmatched unmatched births\n");
print(DETAIL "Of these, $accepted1 had sufficient parental names\n");
print(DETAIL "Of these, $accepted2 were accepted into kinsets\n\n");
print(DETAIL "The average family size in linked families was $family_size_linked\n");
print(DETAIL "The average family size in unlinked families was $family_size_unlinked\n");
print(DETAIL "There were $ties births that had tied marriage links\n\n");
&get_date_time;
print(DETAIL "Program ended on "."$daterun "."$yrrun"." at "."$timerun\n\n");
print("Program complete.\n");
###############################################################
# Matching routine
sub matchrecs {
#first add to score if mother's last name matches as well
#for first children, the birth interval will
#be the duration
if($numkids==0) {
$birth_interval=$duration;
}
#first make sure that birth interval falls within min and max
if($birth_interval<$minint{$numkids} || $birth_interval>$maxint{$numkids}) {
if($birth_interval==0) {
$intmatch=$weights{"int"};
} else {
$score=0;
return($score);
}
}
#if it falls in range, then
if($birth_interval>$lowerqint{$numkids} && $birth_interval<$upperqint{$numkids}) {
#give the maximum score
$intmatch = $weights{"int"};
} else {
#give linearly declining score
if($birth_interval<=$lowerqint{$numkids}) {
$diff = $lowerqint{$numkids} - $birth_interval;
$scale = 1 - $diff/(abs($lowerqint{$numkids}-abs($minint{$numkids})));
$intmatch = $scale * $weights{"int"};
}
if($birth_interval>=$upperqint{$numkids}) {
$diff = $birth_interval - $upperqint{$numkids};
$scale = 1 - $diff/($maxint{$numkids}-$upperqint{$numkids});
$intmatch = $scale * $weights{"int"};
}
}
#if birth interval is zero handle in a special way
if($birth_interval==0) {
$intmatch=$weights{"int"};
}
$mfnh = &stripwhite($mfnh);
$mlnh = &stripwhite($mlnh);
$mfnw = &stripwhite($mfnw);
$mlnw = &stripwhite($mlnw);
$score = $score + $intmatch;
if("$mfnw" eq "$bfnm" && &isnotnull($bfnm)) {$score=$score+$weights{"name"};}
if("$mlnw" eq "$blnm" && &isnotnull($bfnm)) {$score=$score+$weights{"name"};}
if("$mfnh" eq "$bfnf" && &isnotnull($bfnm)) {$score=$score+$weights{"name"};}
if("$mlnh" eq "$blnf" && &isnotnull($bfnm)) {$score=$score+$weights{"name"};}
if("$bpar" eq "$mpar" && &isnotnull($bpar)) {$score=$score+$weights{"parish"};}
if("$mlnwit1" eq "$blng" && &isnotnull($blng)) {$score=$score+$weights{"witness"};}
if("$bpob" eq "$mpoh" && &isnotnull($bpob)) {$score=$score+$weights{"pof"};}
if("$bpob" eq "$mpow" && &isnotnull($bpob)) {$score=$score+$weights{"pom"};}
return($score);
}