-
Notifications
You must be signed in to change notification settings - Fork 30
/
cps_rets.py
1492 lines (1412 loc) · 47.4 KB
/
cps_rets.py
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Class for creating CPS tax units
"""
import pandas as pd
import numpy as np
from tqdm import tqdm
class Returns(object):
"""
Class used to create tax units from the CPS file
"""
def __init__(self, cps):
"""
Parameters
----------
cps: CPS file used
"""
# Set CPS and household numbers in the file
self.cps = cps
self.h_nums = np.unique(self.cps["h_seq"].values)
self.nunits = 0
# Set filing thresholds
self.single = 10150
self.single65 = 11700
self.hoh = 13050
self.hoh65 = 14600
self.joint = 20300
self.joint65one = 21500
self.joint65both = 22700
self.widow = 16350
self.widow65 = 17550
self.depwages = 0
self.depTotal = 1000
# Wage thresholds for non-dependent filers
self.wage1 = 1000
self.wage2 = 250
self.wage2nk = 10000
self.wage3 = 1
# Dependent exemption
self.depExempt = 3950
# Lists to hold tax units in each household
self.house_units = list()
self.tax_units = list()
# Set flags in CPS file
self.cps["h_flag"] = False # Tax unit head flag
self.cps["s_flag"] = False # Tax unit spouse flag
self.cps["d_flag"] = False # Tax Unit Dependent flag
self.cps["flag"] = False # General flag
def computation(self):
"""
Construct tax units based on type of household
1. Single person living alone
2. Persons living in group quarters
3. All other family structures
Returns
-------
CPS Tax Units file
"""
# Extract each household from full CPS file
# TODO: Check if this is actually needed
self.cps["alm_val"] = 0
for index, row in self.cps.iterrows():
if row["oi_off"] == 20:
row["alm_val"] = row["oi_val"]
for num in tqdm(self.h_nums):
self.nunits = 0
# Clear house_units list
del self.house_units[:]
# Pull households from CPS
household = self.cps[self.cps["h_seq"] == num]
household = household.sort_values("a_lineno", kind="mergesort")
house_dict = household.to_dict("records")
# Set flags for household type
single = house_dict[0]["h_type"] == 6 or house_dict[0]["h_type"] == 7
group = house_dict[0]["h_type"] == 9
# other = not single and not group
# Call create for each household
# Single persons living alone
if single:
self.house_units.append(self.create(house_dict[0], house_dict))
elif group:
for person in house_dict:
self.house_units.append(self.create(person, house_dict))
else:
for person in house_dict:
# Only call create method if not flagged
if (
not person["h_flag"]
and not person["s_flag"]
and not person["d_flag"]
):
self.house_units.append(self.create(person, house_dict))
# Check if dependent needs to file
if not person["s_flag"] and person["d_flag"]:
if self.must_file(person):
self.house_units.append(self.create(person, house_dict))
# Search for dependencies within the household
if self.nunits > 1:
self.tax_units_search()
# Check for head of household status
[self.hhstatus(unit) for unit in self.house_units]
# Add each unit to full tax unit list
for unit in self.house_units:
if not unit["t_flag"]:
continue
self.tax_units.append(self.output(unit, house_dict))
final_output = pd.DataFrame(self.tax_units)
# final_output.to_csv('CPSRETS2014.csv', index=False)
return final_output
def create(self, record, house):
"""
Create a CPS tax unit
Parameters
----------
record: dictionary record for the head of the unit
house: list of dictionaries, each containing a memeber of the hosuehold
Returns
-------
A tax unit
"""
# Set head of household as record
self.nunits += 1
# Flag head of household
record["flag"] = True
# Income items
was = record["wsal_val"]
wasp = was
intst = record["int_val"]
dbe = record["div_val"]
alimony = record["alm_val"]
bil = record["semp_val"]
pensions = record["rtm_val"]
rents = record["rnt_val"]
fil = record["frse_val"]
ucomp = record["uc_val"]
socsec = record["ss_val"]
# Weights and flags
wt = record["fsup_wgt"]
ifdept = record["d_flag"] # Tax unit dependent flag
record["h_flag"] = True # Tax unit head flag
# CPS identifiers
xhid = record["h_seq"]
xfid = record["ffpos"]
xpid = record["ph_seq"]
xstate = record["gestfips"]
xregion = record["gereg"]
# CPS evaluation criteria (head)
zifdep = record["d_flag"] # Tax unit dependent flag
zntdep = 0
zhhinc = record["hhinc"]
zagept = record["a_age"]
zagesp = 0
zoldes = 0
zyoung = 0
zworkc = record["wc_val"]
zsocse = record["ss_val"]
zssinc = record["ssi_val"]
zpubas = record["paw_val"]
zvetbe = record["vet_val"]
zchsup = 0
zfinas = 0
zdepin = 0
zowner = 0
zwaspt = record["wsal_val"]
zwassp = 0
# home Ownership Flag
if (self.nunits == 1) and (record["h_tenure"] == 1):
zowner = 1
# store dependents info
for i in range(1, 17):
record["dep" + str(i)] = np.nan
for i in range(1, 17):
record["depage" + str(i)] = np.nan
# marital status
ms = record["a_maritl"]
if ms == 1 or ms == 2 or ms == 3:
ms_type = 2
else:
ms_type = 1
sp_ptr = record["a_spouse"]
relcode = record["a_exprrp"]
# ftype = record['ftype']
ageh = record["a_age"]
if ageh >= 65:
agede = 1
else:
agede = 0
# Age related variables
record["nu06"] = 0 # Only checked for dependents
record["nu13"] = 0 # Only checked for dependents
record["nu18_dep"] = 0
record["nu18"] = 0
record["n1820"] = 0
record["n21"] = 0
record["elderly_dependents"] = 0
if 0 < record["a_age"] < 18:
record["nu18"] += 1
if 18 <= record["a_age"] < 21:
record["n1820"] += 1
if record["a_age"] >= 21:
record["n21"] += 1
depne = 0
ages = 0
wass = 0
# Single and separated individuals
if ms_type == 1:
js = 1
ages = np.nan
# Certain single individuals can file as head of household
if (record["h_type"] == 6 or record["h_type"] == 7) and record[
"h_numper"
] == 1:
if ms == 6:
js = 3
else:
js = 2
if sp_ptr != 0:
# Pull the spouse's record
try:
spouse = house[sp_ptr - 1]
# For households whose records are not in order, loop through
# the house to search for the spouse
except IndexError:
for person in house:
if (
person["a_lineno"] == record["a_spouse"]
and person["a_spouse"] == record["a_lineno"]
):
spouse = person
break
ages = spouse["a_age"]
# record['ages'] = ages
# assert not np.isnan(record['ages']), sp_ptr
if ages >= 65:
agede += 1
# Income items
# Determine spouse's age bracket
if 0 < spouse["a_age"] < 18:
record["nu18"] += 1
if 18 <= spouse["a_age"] < 21:
record["n1820"] += 1
if spouse["a_age"] >= 21:
record["n21"] += 1
wass = spouse["wsal_val"]
was += wass
intst += spouse["int_val"]
dbe += spouse["div_val"]
alimony += spouse["alm_val"]
bil += spouse["semp_val"]
pensions += spouse["rtm_val"]
rents += spouse["rnt_val"]
fil += spouse["frse_val"]
ucomp += spouse["uc_val"]
socsec += spouse["ss_val"]
# Tax unit spouse flag
spouse["s_flag"] = True
# CPS evaluation criteria
zagesp = spouse["a_age"]
zworkc += spouse["wc_val"]
zsocse += spouse["ss_val"]
zssinc += spouse["ssi_val"]
zpubas += spouse["paw_val"]
zvetbe += spouse["vet_val"]
zchsup += 0
zfinas += 0
zwassp = spouse["wsal_val"]
if intst > 400:
xschb = 1
else:
xschb = 0
if fil != 0:
xschf = 1
else:
xschf = 0
if rents != 0:
xsche = 1
else:
xsche = 0
if bil != 0:
xschc = 1
else:
xschc = 0
record["101"] = record["a_age"]
record["102"] = np.nan
if sp_ptr != 0:
record["102"] = spouse["a_age"]
# health insurance coverage
record["110"] = 0
record["111"] = 0
record["112"] = 0
record["113"] = np.nan
record["114"] = np.nan
record["115"] = np.nan
if sp_ptr != 0:
record["113"] = 0
record["114"] = 0
record["115"] = 0
# pension coverage
record["116"] = 0
record["117"] = 0
record["118"] = np.nan
record["119"] = np.nan
if sp_ptr != 0:
record["118"] = 0
record["119"] = 0
# health status
record["120"] = 0
record["121"] = np.nan
if sp_ptr != 0:
record["121"] = 0
# miscellaneous income amounts
record["122"] = record["ssi_val"] # SSI
record["123"] = record["paw_val"] # public assistance (TANF)
record["124"] = record["wc_val"] # workman's compensation
record["125"] = record["vet_val"] # veteran's benefits
record["126"] = 0 # child support
record["127"] = record["dsab_val"] # disablility income
record["128"] = record["ss_val"] # social security income
record["129"] = zowner # home ownership flag
record["130"] = 0 # wage share
if sp_ptr != 0:
record["122"] += spouse["ss_val"]
record["123"] += spouse["paw_val"]
record["124"] += spouse["wc_val"]
record["125"] += spouse["vet_val"]
record["126"] = 0
record["127"] += spouse["dsab_val"]
record["128"] += spouse["ss_val"]
totalwas = was
# Find total wage share
if totalwas > 0:
record["130"] = wasp / float(totalwas)
if self.nunits == 1:
record["131"] = 0
record["132"] = 0
record["133"] = 0
else:
record["131"] = np.nan
record["132"] = np.nan
record["133"] = np.nan
record["134"] = 0
record["135"] = record["ljcw"]
record["136"] = record["wemind"]
record["137"] = record["penatvty"]
record["138"] = np.nan
record["139"] = np.nan
record["140"] = np.nan
record["141"] = np.nan
if sp_ptr != 0:
record["138"] = 0
record["139"] = spouse["ljcw"]
record["140"] = spouse["wemind"]
record["141"] = spouse["penatvty"]
record["142"] = record["a_hga"] # Educational attainment
record["143"] = record["a_sex"] # Gender
record["144"] = np.nan
record["145"] = np.nan
if sp_ptr != 0:
record["144"] = spouse["a_hga"] # Spouse educational attainment
record["145"] = spouse["a_sex"] # Spouse gender
# self-employed industry - head and spouse
classofworker = record["ljcw"]
majorindustry = 0
senonfarm = 0
sefarm = 0
if classofworker == 6:
senonfarm = record["semp_val"]
sefarm = record["frse_val"]
majorindustry = record["wemind"]
if sp_ptr != 0:
classofworker = spouse["ljcw"]
if classofworker == 6:
senonfarm_sp = spouse["semp_val"]
sefarm_sp = spouse["frse_val"]
if abs(senonfarm_sp) > abs(senonfarm):
majorindustry = spouse["wemind"]
senonfarm += senonfarm_sp
sefarm += sefarm_sp
record["146"] = majorindustry
record["147"] = senonfarm
record["148"] = sefarm
record["151"] = record["a_age"]
record["152"] = record["care"]
record["153"] = record["caid"]
record["154"] = record["oth"]
record["155"] = record["hi"]
record["156"] = record["priv"]
record["157"] = record["paid"]
record["158"] = record["filestat"]
record["159"] = record["agi"]
record["160"] = 0 # capital gains no longer on file
record["161"] = np.nan
record["162"] = np.nan
record["163"] = np.nan
record["164"] = np.nan
record["165"] = np.nan
record["166"] = np.nan
record["167"] = np.nan
record["168"] = np.nan
record["169"] = np.nan
record["170"] = np.nan
if sp_ptr != 0:
record["161"] = spouse["a_age"]
record["162"] = spouse["care"]
record["163"] = spouse["caid"]
record["164"] = spouse["oth"]
record["165"] = spouse["hi"]
record["166"] = spouse["priv"]
record["167"] = spouse["paid"]
record["168"] = spouse["filestat"]
record["169"] = spouse["agi"]
record["170"] = 0
record["171"] = record["wsal_val"]
record["172"] = record["int_val"]
record["173"] = record["div_val"]
record["174"] = record["alm_val"]
record["175"] = record["semp_val"]
record["176"] = record["rtm_val"]
record["177"] = record["rnt_val"]
record["178"] = record["frse_val"]
record["179"] = record["uc_val"]
record["180"] = record["ss_val"] # capital gains no longer on file
record["181"] = np.nan
record["182"] = np.nan
record["183"] = np.nan
record["184"] = np.nan
record["185"] = np.nan
record["186"] = np.nan
record["187"] = np.nan
record["188"] = np.nan
record["189"] = np.nan
record["190"] = np.nan
if sp_ptr != 0:
record["181"] = spouse["wsal_val"]
record["182"] = spouse["int_val"]
record["183"] = spouse["div_val"]
record["184"] = spouse["alm_val"]
record["185"] = spouse["semp_val"]
record["186"] = spouse["rtm_val"]
record["187"] = spouse["rnt_val"]
record["188"] = spouse["frse_val"]
record["189"] = spouse["uc_val"]
record["190"] = spouse["ss_val"]
# retirement income
record["191"] = record["ret_val1"]
record["192"] = record["ret_sc1"]
record["193"] = record["ret_val2"]
record["194"] = record["ret_sc2"]
record["195"] = np.nan
record["196"] = np.nan
record["197"] = np.nan
record["198"] = np.nan
if sp_ptr != 0:
record["195"] = spouse["ret_val1"]
record["196"] = spouse["ret_sc1"]
record["197"] = spouse["ret_val2"]
record["198"] = spouse["ret_sc2"]
# disability income
record["199"] = record["dis_val1"]
record["200"] = record["dis_sc1"]
record["201"] = record["dis_val2"]
record["202"] = record["dis_sc2"]
record["203"] = np.nan
record["204"] = np.nan
record["205"] = np.nan
record["206"] = np.nan
if sp_ptr != 0:
record["203"] = spouse["dis_val1"]
record["204"] = spouse["dis_sc1"]
record["205"] = spouse["dis_val2"]
record["206"] = spouse["dis_sc2"]
# survivor income
record["207"] = record["sur_val1"]
record["208"] = record["sur_sc1"]
record["209"] = record["sur_val2"]
record["210"] = record["sur_sc2"]
record["211"] = np.nan
record["212"] = np.nan
record["213"] = np.nan
record["214"] = np.nan
if sp_ptr != 0:
record["211"] = spouse["sur_val1"]
record["212"] = spouse["sur_sc1"]
record["213"] = spouse["sur_val2"]
record["214"] = spouse["sur_sc2"]
# veterans income
record["215"] = record["vet_typ1"]
record["216"] = record["vet_typ2"]
record["217"] = record["vet_typ3"]
record["218"] = record["vet_typ4"]
record["219"] = record["vet_typ5"]
record["220"] = record["vet_val"]
record["221"] = np.nan
record["222"] = np.nan
record["223"] = np.nan
record["224"] = np.nan
record["225"] = np.nan
record["226"] = np.nan
if sp_ptr != 0:
record["221"] = spouse["vet_typ1"]
record["222"] = spouse["vet_typ2"]
record["223"] = spouse["vet_typ3"]
record["224"] = spouse["vet_typ4"]
record["225"] = spouse["vet_typ5"]
record["226"] = spouse["vet_val"]
record["227"] = sp_ptr
# household
record["228"] = record["fhip_val"]
record["229"] = record["fmoop"]
record["230"] = record["fotc_val"]
record["231"] = record["fmed_val"]
record["232"] = record["hmcaid"]
record["233"] = record["hrwicyn"]
record["234"] = record["hfdval"]
record["235"] = record["care_val"]
# taxpayer
record["236"] = record["paw_val"]
record["237"] = record["mcaid"]
record["238"] = record["pchip"]
record["239"] = record["wicyn"]
record["240"] = record["ssi_val"]
record["241"] = record["hi_yn"]
record["242"] = record["hiown"]
record["243"] = record["hiemp"]
record["244"] = record["hipaid"]
record["245"] = record["emcontrb"]
record["246"] = record["hi"]
record["247"] = record["hityp"]
record["248"] = record["paid"]
record["249"] = record["priv"]
record["250"] = record["prityp"]
record["251"] = record["ss_val"]
record["252"] = record["uc_val"]
record["253"] = record["mcare"]
record["254"] = record["wc_val"]
record["255"] = record["vet_val"]
record["256"] = np.nan
record["257"] = np.nan
record["258"] = np.nan
record["259"] = np.nan
record["260"] = np.nan
record["261"] = np.nan
record["262"] = np.nan
record["263"] = np.nan
record["264"] = np.nan
record["265"] = np.nan
record["266"] = np.nan
record["267"] = np.nan
record["268"] = np.nan
record["269"] = np.nan
record["270"] = np.nan
record["271"] = np.nan
record["272"] = np.nan
record["273"] = np.nan
record["274"] = np.nan
record["275"] = np.nan
if sp_ptr != 0:
record["256"] = spouse["paw_val"]
record["257"] = spouse["mcaid"]
record["258"] = spouse["pchip"]
record["259"] = spouse["wicyn"]
record["260"] = spouse["ssi_val"]
record["261"] = spouse["hi_yn"]
record["262"] = spouse["hiown"]
record["263"] = spouse["hiemp"]
record["264"] = spouse["hipaid"]
record["265"] = spouse["emcontrb"]
record["266"] = spouse["hi"]
record["267"] = spouse["hityp"]
record["268"] = spouse["paid"]
record["269"] = spouse["priv"]
record["270"] = spouse["prityp"]
record["271"] = spouse["ss_val"]
record["272"] = spouse["uc_val"]
record["273"] = spouse["mcare"]
record["274"] = spouse["wc_val"]
record["275"] = spouse["vet_val"]
totincx = (
was + intst + dbe + alimony + bil + pensions + rents + fil + ucomp + socsec
)
if not ifdept:
# Search for dependents among other members of the household who
# are not already claimed on another return.
for individual in house:
idxfid = individual["ffpos"]
idxhea = individual["h_flag"]
idxspo = individual["s_flag"]
idxdep = individual["d_flag"]
dflag = False
if (
(house.index(individual) != house.index(record))
and idxfid == xfid
and not idxdep
and not idxspo
and not idxhea
):
# Determine if Individual is a dependent of the reference
# person
test1 = 1
test2 = 1
test3 = 1
test4 = 0
test5 = 0
dflag = False
age = individual["a_age"]
income = (
individual["wsal_val"]
+ individual["semp_val"]
+ individual["frse_val"]
+ individual["uc_val"]
+ individual["ss_val"]
+ individual["rtm_val"]
+ individual["int_val"]
+ individual["div_val"]
+ individual["rnt_val"]
+ individual["alm_val"]
)
# set up child flag (related == -1)
reference_person = record["a_exprrp"]
index_person = individual["a_exprrp"]
if reference_person == 5:
genref = -1
elif reference_person == 7:
genref = -2
elif reference_person == 8:
genref = 1
elif reference_person == 9:
genref = 0
elif reference_person == 11:
genref = -1
else:
genref = 99
if index_person == 5:
genind = -1
elif index_person == 7:
genind = -2
elif index_person == 8:
genind = 1
elif index_person == 9:
genind = 0
elif index_person == 11:
genind = -1
else:
genind = 99
if genref != 99 and genind != 99:
related = genind - genref
else:
related = 99
# In general, a person's income must be less than $2,500 to
# be eligible to be a dependent.
# But there are exceptions for children.
if income <= 2500:
test4 = 1
if (relcode == 5) or (related == -1):
if age <= 18 or (age <= 23 and record["a_enrlw"] > 0):
test4 = 1
if totincx + income > 0:
if income / float(totincx + income) < 0.5:
test5 = 1
else:
test5 = 1
dtest = test1 + test2 + test3 + test4 + test5
if dtest == 5:
dflag = True
if dflag:
individual["d_flag"] = True
depne += 1
dage = individual["a_age"]
record[("dep" + str(depne))] = house.index(individual)
record["depage" + str(depne)] = dage
if individual["a_age"] < 6:
record["nu06"] += 1
if individual["a_age"] < 13:
record["nu13"] += 1
if 0 < individual["a_age"] < 18:
record["nu18"] += 1
record["nu18_dep"] += 1
if 18 <= individual["a_age"] < 21:
record["n1820"] += 1
if individual["a_age"] >= 21:
record["n21"] += 1
if individual["a_age"] >= 65:
record["elderly_dependents"] += 1
cahe = np.nan
record["t_flag"] = True # tax unit flag
returns = record["t_flag"]
namelist = [
"js",
"ifdept",
"agede",
"cahe",
"ageh",
"ages",
"was",
"intst",
"dbe",
"alimony",
"bil",
"pensions",
"rents",
"fil",
"ucomp",
"socsec",
"returns",
"wt",
"zifdep",
"zntdep",
"zhhinc",
"zagept",
"zagesp",
"zoldes",
"zyoung",
"zworkc",
"zsocse",
"zssinc",
"zpubas",
"zvetbe",
"zchsup",
"zfinas",
"zdepin",
"zowner",
"zwaspt",
"zwassp",
"wasp",
"wass",
"xregion",
"xschb",
"xschf",
"xsche",
"xschc",
"xhid",
"xfid",
"xpid",
"depne",
"totincx",
"xstate",
]
varlist = [
js,
ifdept,
agede,
cahe,
ageh,
ages,
was,
intst,
dbe,
alimony,
bil,
pensions,
rents,
fil,
ucomp,
socsec,
returns,
wt,
zifdep,
zntdep,
zhhinc,
zagept,
zagesp,
zoldes,
zyoung,
zworkc,
zsocse,
zssinc,
zpubas,
zvetbe,
zchsup,
zfinas,
zdepin,
zowner,
zwaspt,
zwassp,
wasp,
wass,
xregion,
xschb,
xschf,
xsche,
xschc,
xhid,
xfid,
xpid,
depne,
totincx,
xstate,
]
for name, var in zip(namelist, varlist):
record[name] = var
# record['ages'] = ages
# if np.isnan(ages) and record['js'] == 2:
# record['ages'] = spouse['a_age']
# assert not np.isnan(record['ages'])
return record
def hhstatus(self, unit):
"""
Determine head of household status
Parameters
----------
unit: a tax unit
"""
income = 0
# Find total income for the tax unit
for iunit in self.house_units:
totinc = (
iunit["was"]
+ iunit["intst"]
+ iunit["dbe"]
+ iunit["alimony"]
+ iunit["bil"]
+ iunit["pensions"]
+ iunit["rents"]
+ iunit["fil"]
+ iunit["ucomp"]
+ iunit["socsec"]
)
income += totinc
# Find income for the individual
if income > 0:
totincx = (
unit["was"]
+ unit["intst"]
+ unit["dbe"]
+ unit["alimony"]
+ unit["bil"]
+ unit["pensions"]
+ unit["rents"]
+ unit["fil"]
+ unit["ucomp"]
+ unit["socsec"]
)
indjs = unit["js"] # Filind status
indif = unit["ifdept"] # Dependency status
inddx = unit["depne"] # Number of dependent exemptions
if indjs == 1 and float(totincx) / income > 0.99:
if indif != 1 and inddx > 0:
unit["js"] = 3
def must_file(self, record):
"""
Determine if a dependent must file
Parameters
----------
record: record for the dependent
Returns
-------
True if person must file, False otherwise
"""
wages = record["wsal_val"]
income = (
wages
+ record["semp_val"]
+ record["frse_val"]
+ record["uc_val"]
+ record["ss_val"]
+ record["rtm_val"]
+ record["int_val"]
+ record["div_val"]
+ record["rnt_val"]
+ record["alm_val"]
)
# Determine if dependent exceeds filing thresholds
if wages > self.depwages or income > self.depTotal:
depfile = True
else:
depfile = False
return depfile
def convert(self, ix, iy):
"""
Convert existing tax unit (ix) to a dependent filer and add dependent
information to the target return (iy)
Parameters
----------
ix, iy: tax units
Returns
-------
None
"""
self.house_units[ix]["ifdept"] = True
ixdeps = self.house_units[ix]["depne"]
iydeps = self.house_units[iy]["depne"]
self.house_units[ix]["depne"] = 0
ixjs = self.house_units[ix]["js"]
if ixjs == 2:
self.house_units[iy]["depne"] += ixdeps + 2
self.house_units[iy]["dep" + str(iydeps + 1)] = ix
self.house_units[iy][("dep" + str(iydeps + 2))] = self.house_units[ix][
"sp_ptr"
]
self.house_units[iy][("depage" + str(iydeps + 1))] = self.house_units[ix][
"a_age"
]
self.house_units[iy][("depage" + str(iydeps + 2))] = self.house_units[ix][
"ages"
]
iybgin = iydeps + 2
else:
self.house_units[iy]["depne"] += ixdeps + 1
self.house_units[iy]["dep" + str(iydeps + 1)] = ix
self.house_units[iy][("depage" + str(iydeps + 1))] = self.house_units[ix][
"a_age"
]
iybgin = iydeps + 1
if ixdeps > 0:
# Assign any dependents to target record
for ndeps in range(1, ixdeps + 1):
dep = "dep" + str(iybgin + ndeps)
depx = "dep" + str(ndeps)
depage = "depage" + str(iybgin + ndeps)
depagex = "depage" + str(ndeps)
self.house_units[iy][dep] = self.house_units[ix][depx]
self.house_units[ix][dep] = 0
self.house_units[iy][depage] = self.house_units[ix][depagex]
# Add age variables together
self.house_units[iy]["nu06"] += self.house_units[ix]["nu06"]
self.house_units[iy]["nu13"] += self.house_units[ix]["nu13"]
self.house_units[iy]["nu18_dep"] += self.house_units[ix]["nu18_dep"]
self.house_units[iy]["nu18"] += self.house_units[ix]["nu18"]
self.house_units[iy]["n1820"] += self.house_units[ix]["n1820"]
self.house_units[iy]["n21"] += self.house_units[ix]["n21"]
elderly = self.house_units[ix]["elderly_dependents"]
self.house_units[iy]["elderly_dependents"] += elderly
def tax_units_search(self):
"""
Search for dependencies among tax units in a household
"""
highest = -9.9e32
idxhigh = 0
# Find tax unit with highest income
for ix in range(0, self.nunits):
totincx = self.house_units[ix]["totincx"]
if totincx > highest:
highest = totincx
idxhigh = ix
# If it is not already a dependent unit, search for dependents
if not self.house_units[idxhigh]["ifdept"]:
for ix in range(0, self.nunits):
idxjs = self.house_units[ix]["js"]
idxdepf = self.house_units[ix]["ifdept"]