forked from Revenue-Academy/pitaxdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corp_cross_sample_prep.py
356 lines (319 loc) · 15.5 KB
/
corp_cross_sample_prep.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
"""
This script prepares the cross-section sample for 2017. For now, we treat this
sample as being representative (even though it may not be).
We assume that aggregate totals have already been calcuated for the full data.
They must be saved in some form, and we will store them in agg_results.
For now, we produce the sample weight for the entire sample. A subsequent
improvement should produce aggregate results by industry/sector, and produce
weights by industry/sector.
We may also want to consider weight adjustments to target other results, such
as totals for other measures and the distribution of firm sizes.
"""
import numpy as np
import pandas as pd
# Get data for 2017
data_full = pd.read_excel('ITR6_2017_2013_BAL_PANEL_FINAL.xlsx',
sheet_name='Sheet2')
# Rename some variables
renames = {'SHORT_TERM_15PER': 'ST_CG_AMT_1', 'SHORT_TERM_30PER': 'ST_CG_AMT_2',
'LONG_TERM_10PER': 'LT_CG_AMT_1', 'LONG_TERM_20PER': 'LT_CG_AMT_2',
'SHORT_TERM_APPRATE': 'ST_CG_AMT_APPRATE',
'TOTAL_INCOME_ALL':'GTI_BEFORE_LOSSES', 'PAN_NO_HASH': 'ID_NO',
'AY_0910_AMT_AMT_LOSS_BUSOTHSPL': 'AY_0910_AMT_LOSS_BUSOTHSPL'}
data_full = data_full.rename(renames, axis=1)
data_full = data_full.fillna(0)
"""
The following code handles the losses.
"""
"""
# Create empty loss variables
loss_lag8 = np.zeros(len(data_full))
loss_lag7 = np.zeros(len(data_full))
loss_lag6 = np.zeros(len(data_full))
loss_lag5 = np.zeros(len(data_full))
loss_lag4 = np.zeros(len(data_full))
loss_lag3 = np.zeros(len(data_full))
loss_lag2 = np.zeros(len(data_full))
loss_lag1 = np.zeros(len(data_full))
def get_loss_type(year, lagnum, losstype):
Returns an array of the given loss type with the appropriate lag from the
given year.
loss = np.zeros(len(data_full))
lagyear = year - lagnum
if lagyear < 2007:
loss = np.zeros(len(data_full))
elif lagyear == 2007:
loss = np.array(data_full['AY_0708_AMT_LOSS_' + losstype])
elif lagyear == 2008:
loss = np.array(data_full['AY_0809_AMT_LOSS_' + losstype])
elif lagyear == 2009:
loss = np.array(data_full['AY_0910_AMT_LOSS_' + losstype])
elif lagyear == 2010:
loss = np.array(data_full['AY_1011_AMT_LOSS_' + losstype])
elif lagyear == 2011:
loss = np.array(data_full['AY_1112_AMT_LOSS_' + losstype])
elif lagyear == 2012:
loss = np.array(data_full['AY_1213_AMT_LOSS_' + losstype])
elif lagyear == 2013:
loss = np.array(data_full['AY_1314_AMT_LOSS_' + losstype])
elif lagyear == 2014:
loss = np.array(data_full['AY_1415_AMT_LOSS_' + losstype])
else:
loss = np.zeros(len(data_full))
loss2 = np.where(data_full.ASSESSMENT_YEAR == year, loss, 0)
return loss2
losstypelist = ['HPL', 'BUSOTHSPL', 'LSPCLTVBUS', 'LSPCFDBUS', 'STCL', 'LTCL',
'OSLHR']
for losstype in losstypelist:
loss_lag1 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 1, losstype), 0.)
loss_lag2 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 2, losstype), 0.)
loss_lag3 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 3, losstype), 0.)
loss_lag4 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 4, losstype), 0.)
loss_lag5 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 5, losstype), 0.)
loss_lag6 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 6, losstype), 0.)
loss_lag7 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 7, losstype), 0.)
loss_lag8 += np.where(data_full.ASSESSMENT_YEAR == 2017,
get_loss_type(2017, 8, losstype), 0.)
data_full['LOSS_LAG1'] = loss_lag1
data_full['LOSS_LAG2'] = loss_lag2
data_full['LOSS_LAG3'] = loss_lag3
data_full['LOSS_LAG4'] = loss_lag4
data_full['LOSS_LAG5'] = loss_lag5
data_full['LOSS_LAG6'] = loss_lag6
data_full['LOSS_LAG7'] = loss_lag7
data_full['LOSS_LAG8'] = loss_lag8
"""
data13 = data_full[data_full.ASSESSMENT_YEAR == 2013].reset_index()
data14 = data_full[data_full.ASSESSMENT_YEAR == 2014].reset_index()
data15 = data_full[data_full.ASSESSMENT_YEAR == 2015].reset_index()
data16 = data_full[data_full.ASSESSMENT_YEAR == 2016].reset_index()
data17 = data_full[data_full.ASSESSMENT_YEAR == 2017].reset_index()
"""
The following code handles the losses.
"""
# Create empty loss variables
loss_lag8 = np.zeros(len(data13))
loss_lag7 = np.zeros(len(data13))
loss_lag6 = np.zeros(len(data13))
loss_lag5 = np.zeros(len(data13))
loss_lag4 = np.zeros(len(data13))
loss_lag3 = np.zeros(len(data13))
loss_lag2 = np.zeros(len(data13))
loss_lag1 = np.zeros(len(data13))
def get_loss_type(year, lagnum, losstype):
"""
Returns an array of the given loss type with the appropriate lag from the
given year.
"""
loss = np.zeros(len(data_full))
lagyear = year - lagnum
if lagyear < 2007:
loss = np.zeros(len(data_full))
elif lagyear == 2007:
loss = np.array(data_full['AY_0708_AMT_LOSS_' + losstype])
elif lagyear == 2008:
loss = np.array(data_full['AY_0809_AMT_LOSS_' + losstype])
elif lagyear == 2009:
loss = np.array(data_full['AY_0910_AMT_LOSS_' + losstype])
elif lagyear == 2010:
loss = np.array(data_full['AY_1011_AMT_LOSS_' + losstype])
elif lagyear == 2011:
loss = np.array(data_full['AY_1112_AMT_LOSS_' + losstype])
elif lagyear == 2012:
loss = np.array(data_full['AY_1213_AMT_LOSS_' + losstype])
elif lagyear == 2013:
loss = np.array(data_full['AY_1314_AMT_LOSS_' + losstype])
elif lagyear == 2014:
loss = np.array(data_full['AY_1415_AMT_LOSS_' + losstype])
else:
loss = np.zeros(len(data_full))
loss2 = loss[data_full.ASSESSMENT_YEAR == year]
return loss2
losstypelist = ['HPL', 'BUSOTHSPL', 'LSPCLTVBUS', 'LSPCFDBUS', 'STCL', 'LTCL',
'OSLHR']
# Produce the loss lags for 2013
for losstype in losstypelist:
loss_lag1 += get_loss_type(2013, 1, losstype)
loss_lag2 += get_loss_type(2013, 2, losstype)
loss_lag3 += get_loss_type(2013, 3, losstype)
loss_lag4 += get_loss_type(2013, 4, losstype)
loss_lag5 += get_loss_type(2013, 5, losstype)
loss_lag6 += get_loss_type(2013, 6, losstype)
loss_lag7 += get_loss_type(2013, 7, losstype)
loss_lag8 += get_loss_type(2013, 8, losstype)
def calc_new_lags(dat):
"""
Calculates the new lags
"""
LOSS_LAGS = [dat.LOSS_LAG1, dat.LOSS_LAG2, dat.LOSS_LAG3, dat.LOSS_LAG4,
dat.LOSS_LAG5, dat.LOSS_LAG6, dat.LOSS_LAG7, dat.LOSS_LAG8]
PRFT_GAIN_BP_INC_115BBF = np.zeros(len(dat))
Income_BP = (dat.PRFT_GAIN_BP_OTHR_SPECLTV_BUS + dat.PRFT_GAIN_BP_SPECLTV_BUS +
dat.PRFT_GAIN_BP_SPCFD_BUS + PRFT_GAIN_BP_INC_115BBF)
GTI_Before_Loss = (dat.INCOME_HP + Income_BP + dat.ST_CG_AMT_1 + dat.ST_CG_AMT_2 +
dat.ST_CG_AMT_APPRATE + dat.LT_CG_AMT_1 + dat.LT_CG_AMT_2 +
dat.TOTAL_INCOME_OS)
CY_Losses = np.array(dat['CYL_SET_OFF'])
GTI1 = np.maximum(GTI_Before_Loss - CY_Losses, 0.)
newloss1 = GTI1 - GTI_Before_Loss + CY_Losses
USELOSS = [np.zeros(len(dat))] * 8
for i in range(8, 0, -1):
USELOSS[i-1] = np.minimum(GTI1, LOSS_LAGS[i-1])
GTI1 = GTI1 - USELOSS[i-1]
dat['newloss1'] = newloss1
dat['newloss2'] = LOSS_LAGS[0] - USELOSS[0]
dat['newloss3'] = LOSS_LAGS[1] - USELOSS[1]
dat['newloss4'] = LOSS_LAGS[2] - USELOSS[2]
dat['newloss5'] = LOSS_LAGS[3] - USELOSS[3]
dat['newloss6'] = LOSS_LAGS[4] - USELOSS[4]
dat['newloss7'] = LOSS_LAGS[5] - USELOSS[5]
dat['newloss8'] = LOSS_LAGS[6] - USELOSS[6]
return(dat)
data13['LOSS_LAG1'] = loss_lag1
data13['LOSS_LAG2'] = loss_lag2
data13['LOSS_LAG3'] = loss_lag3
data13['LOSS_LAG4'] = loss_lag4
data13['LOSS_LAG5'] = loss_lag5
data13['LOSS_LAG6'] = loss_lag6
data13['LOSS_LAG7'] = loss_lag7
data13['LOSS_LAG8'] = loss_lag8
data13c = calc_new_lags(data13)
carryforward_df = pd.DataFrame({'ID_NO': data13c.ID_NO,
'newloss1': data13c.newloss1,
'newloss2': data13c.newloss2,
'newloss3': data13c.newloss3,
'newloss4': data13c.newloss4,
'newloss5': data13c.newloss5,
'newloss6': data13c.newloss6,
'newloss7': data13c.newloss7,
'newloss8': data13c.newloss8})
# Update loss lags for 2014 data
data14a = data14.merge(right=carryforward_df, how='outer', on='ID_NO', indicator=True)
merge_info = np.array(data14a['_merge'])
to_update = np.where(merge_info == 'both', True, False)
to_keep = np.where(merge_info != 'right_only', True, False)
data14a['LOSS_LAG1'] = np.where(to_update, data14a['newloss1'], 0)
data14a['LOSS_LAG2'] = np.where(to_update, data14a['newloss2'], 0)
data14a['LOSS_LAG3'] = np.where(to_update, data14a['newloss3'], 0)
data14a['LOSS_LAG4'] = np.where(to_update, data14a['newloss4'], 0)
data14a['LOSS_LAG5'] = np.where(to_update, data14a['newloss5'], 0)
data14a['LOSS_LAG6'] = np.where(to_update, data14a['newloss6'], 0)
data14a['LOSS_LAG7'] = np.where(to_update, data14a['newloss7'], 0)
data14a['LOSS_LAG8'] = np.where(to_update, data14a['newloss8'], 0)
data14b = data14a[to_keep].reset_index()
data14c = calc_new_lags(data14b)
# Repeat for 2015
carryforward_df = pd.DataFrame({'ID_NO': data14c.ID_NO,
'newloss1': data14c.newloss1,
'newloss2': data14c.newloss2,
'newloss3': data14c.newloss3,
'newloss4': data14c.newloss4,
'newloss5': data14c.newloss5,
'newloss6': data14c.newloss6,
'newloss7': data14c.newloss7,
'newloss8': data14c.newloss8})
# Update loss lags for 2015 data
data15a = data15.merge(right=carryforward_df, how='outer', on='ID_NO', indicator=True)
merge_info = np.array(data15a['_merge'])
to_update = np.where(merge_info == 'both', True, False)
to_keep = np.where(merge_info != 'right_only', True, False)
data15a['LOSS_LAG1'] = np.where(to_update, data15a['newloss1'], 0)
data15a['LOSS_LAG2'] = np.where(to_update, data15a['newloss2'], 0)
data15a['LOSS_LAG3'] = np.where(to_update, data15a['newloss3'], 0)
data15a['LOSS_LAG4'] = np.where(to_update, data15a['newloss4'], 0)
data15a['LOSS_LAG5'] = np.where(to_update, data15a['newloss5'], 0)
data15a['LOSS_LAG6'] = np.where(to_update, data15a['newloss6'], 0)
data15a['LOSS_LAG7'] = np.where(to_update, data15a['newloss7'], 0)
data15a['LOSS_LAG8'] = np.where(to_update, data15a['newloss8'], 0)
data15b = data15a[to_keep].reset_index()
data15c = calc_new_lags(data15b)
# Repeat for 2016
carryforward_df = pd.DataFrame({'ID_NO': data15c.ID_NO,
'newloss1': data15c.newloss1,
'newloss2': data15c.newloss2,
'newloss3': data15c.newloss3,
'newloss4': data15c.newloss4,
'newloss5': data15c.newloss5,
'newloss6': data15c.newloss6,
'newloss7': data15c.newloss7,
'newloss8': data15c.newloss8})
# Update loss lags for 2016 data
data16a = data16.merge(right=carryforward_df, how='outer', on='ID_NO', indicator=True)
merge_info = np.array(data16a['_merge'])
to_update = np.where(merge_info == 'both', True, False)
to_keep = np.where(merge_info != 'right_only', True, False)
data16a['LOSS_LAG1'] = np.where(to_update, data16a['newloss1'], 0)
data16a['LOSS_LAG2'] = np.where(to_update, data16a['newloss2'], 0)
data16a['LOSS_LAG3'] = np.where(to_update, data16a['newloss3'], 0)
data16a['LOSS_LAG4'] = np.where(to_update, data16a['newloss4'], 0)
data16a['LOSS_LAG5'] = np.where(to_update, data16a['newloss5'], 0)
data16a['LOSS_LAG6'] = np.where(to_update, data16a['newloss6'], 0)
data16a['LOSS_LAG7'] = np.where(to_update, data16a['newloss7'], 0)
data16a['LOSS_LAG8'] = np.where(to_update, data16a['newloss8'], 0)
data16b = data16a[to_keep].reset_index()
data16c = calc_new_lags(data16b)
# Repeat for 2017
carryforward_df = pd.DataFrame({'ID_NO': data16c.ID_NO,
'newloss1': data16c.newloss1,
'newloss2': data16c.newloss2,
'newloss3': data16c.newloss3,
'newloss4': data16c.newloss4,
'newloss5': data16c.newloss5,
'newloss6': data16c.newloss6,
'newloss7': data16c.newloss7,
'newloss8': data16c.newloss8})
data17a = data17.merge(right=carryforward_df, how='outer', on='ID_NO', indicator=True)
merge_info = np.array(data17a['_merge'])
to_update = np.where(merge_info == 'both', True, False)
to_keep = np.where(merge_info != 'right_only', True, False)
data17a['LOSS_LAG1'] = np.where(to_update, data17a['newloss1'], 0)
data17a['LOSS_LAG2'] = np.where(to_update, data17a['newloss2'], 0)
data17a['LOSS_LAG3'] = np.where(to_update, data17a['newloss3'], 0)
data17a['LOSS_LAG4'] = np.where(to_update, data17a['newloss4'], 0)
data17a['LOSS_LAG5'] = np.where(to_update, data17a['newloss5'], 0)
data17a['LOSS_LAG6'] = np.where(to_update, data17a['newloss6'], 0)
data17a['LOSS_LAG7'] = np.where(to_update, data17a['newloss7'], 0)
data17a['LOSS_LAG8'] = np.where(to_update, data17a['newloss8'], 0)
data17 = data17a[to_keep].reset_index()
#data17 = data_full[data_full['ASSESSMENT_YEAR'] == 2017].reset_index()
count = len(data17)
# Average amounts per company from the 2017 full sample
total_returns = 790443.0
# Calculate weights
#WGT2017 = total_returns / count
WGT2017 = 3954771854602 / sum(data_full['AGGREGATE_LIABILTY'])
# Assume 10% growth rate in number of firms filing
firms_filing_growth_rate = 1.1
"""
weights_df = pd.DataFrame({'WT2017': [WGT2017] * count,
'WT2018': [WGT2017 * firms_filing_growth_rate] * count,
'WT2019': [WGT2017 * firms_filing_growth_rate**2] * count,
'WT2020': [WGT2017 * firms_filing_growth_rate**3] * count,
'WT2021': [WGT2017 * firms_filing_growth_rate**4] * count})
"""
weights_df = pd.DataFrame({'WT2017': [WGT2017] * count})
weights_df['WT2018'] = weights_df['WT2017'] * firms_filing_growth_rate
weights_df['WT2019'] = weights_df['WT2017'] * firms_filing_growth_rate**2
weights_df['WT2020'] = weights_df['WT2017'] * firms_filing_growth_rate**3
weights_df['WT2021'] = weights_df['WT2017'] * firms_filing_growth_rate**4
weights_df['WT2022'] = weights_df['WT2017'] * firms_filing_growth_rate**5
weights_df['WT2023'] = weights_df['WT2017'] * firms_filing_growth_rate**6
"""
'WT2018': [WT2017 * firms_filing_growth_rate] * count,
'WT2019': [WT2018 * firms_filing_growth_rate] * count,
'WT2020': [WT2019 * firms_filing_growth_rate] * count,
'WT2021': [WT2020 * firms_filing_growth_rate] * count,
'WT2022': [WT2021 * firms_filing_growth_rate] * count,
'WT2023': [WT2022 * firms_filing_growth_rate] * count})
"""
# Export results
data17.round(6)
data17.to_csv('cit_cross.csv', index=False)
weights_df.to_csv('cit_cross_wgts.csv', index=False)