-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_subsidy_increase.py
116 lines (93 loc) · 3.91 KB
/
get_subsidy_increase.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
import csv
hholds = {}
expenditures_lookup = {}
increased_expenditures = []
# a matrix to find households with identical characteristics
# hh_matrix = [[{} for _ in range(20)] for _ in range(20)]
def get_kids(hh: dict):
"""Get the number of infants (girls + boys) and young_kids.
Parameters:
hh (dict): a dictionary of household characteristics
Returns:
infants (int) number of girls + boys 0-3
young_kids (int) number of girls + boys 4-8
"""
infants = float(hh["F 00-03"]) + float(hh["M 00-03"])
young_kids = float(hh["F 04-08"]) + float(hh["M 04-08"])
# the 'check string' allows us to quickly find another household with the
# same characteristics for non-children. The intent here is to
# to match up households and compare expenses, and households
# with + or minus an infant or young child
check_string = "-".join(
[
hh["F 09-13"],
hh["F 14-18"],
hh["F 19-30"],
hh["F 31-50"],
hh["F 51+"],
hh["M 00-03"],
hh["M 04-08"],
hh["M 09-13"],
hh["M 14-18"],
hh["M 19-30"],
hh["M 31-50"],
hh["M 51+"],
]
)
return round(infants), round(young_kids), check_string
with open("expenditures.csv") as xcsv:
xreader = csv.DictReader(xcsv)
for row in xreader:
these_expenditures = (
expenditures_lookup[row["i"]] if row["i"] in expenditures_lookup else []
)
these_expenditures.append(row)
expenditures_lookup[row["i"]] = these_expenditures
def build_expenditures_with_subsidy(subsidy: float):
"""Build up a matrix of households by number of infants, young_kids,
and expenditures.
Parameters:
subsidy: int
Returns:
percentage: float
"""
with open("hh.csv") as csvfile:
reader = csv.DictReader(csvfile)
increased_expenditures.clear()
for row in reader:
infants, young_kids, check_string = get_kids(row)
matrix_entry = (row["i"], row["t"], check_string)
if infants > 0 or young_kids > 0:
# here we create a matrix of similar households
# tbd - actually use this matrix to compare similar households
# hh_matrix[infants][young_kids][check_string] = matrix_entry
# now look up expenditures
if row["i"] in expenditures_lookup:
this_household_expenditures = expenditures_lookup[row["i"]]
if this_household_expenditures is not None:
# this is a week's expenditures; add $1/kid = 3807 Ugandan shillings
for expense in this_household_expenditures:
index_cols = ["i", "t", "m"]
total_exp = 0.0
for col in expense.keys():
if col not in index_cols:
item_cost = expense[col]
if len(item_cost) > 0:
total_exp += float(expense[col])
new_expense = total_exp + ((3807 * subsidy) * (infants + young_kids))
percent_increase = new_expense / total_exp
increased_expenditures.append(percent_increase)
# now get the average increase
def get_mean_increase(subsidy: float):
"""Get the mean increase in food budgets for households"""
build_expenditures_with_subsidy(subsidy)
total_all_increases = 0.0
for increase in increased_expenditures:
total_all_increases += increase
mean_increase = ((total_all_increases / len(increased_expenditures)) - 1) * 100
return mean_increase
if __name__ == '__main__':
foo = get_mean_increase(1.0)
print (foo)
foo = get_mean_increase(0.0)
print (foo)