Skip to content

Commit

Permalink
update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdebacker committed Oct 25, 2023
1 parent 1ae2e44 commit da726a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
9 changes: 6 additions & 3 deletions iot/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# %%

from iot.iot_user import iot_comparison

# %%
Expand All @@ -10,7 +11,6 @@
baseline_policies=[None, None],
labels=["2017 Law", "Biden 2020"],
years=[2017, 2020],
mtr_smoother="spline",
)

iot2 = iot_comparison(
Expand All @@ -21,8 +21,7 @@
baseline_policies=[None, None],
labels=["2017 Law", "Biden 2020"],
years=[2017, 2020],
inc_elast=2,
mtr_smoother="poly",
inc_elast=0.2,
)

# %%
Expand All @@ -33,6 +32,7 @@
gzplot2 = iot2.plot()
fplot = iot1.plot(var="f")
mtrplot1 = iot1.plot(var="mtr")
mtrplot2 = iot2.plot(var="mtr")
thetaplot1 = iot1.plot(var="theta_z")

# %%
Expand All @@ -43,3 +43,6 @@
gzplot2.show()

# %%
mtrplot1.show()
mtrplot2.show()
# %%
11 changes: 3 additions & 8 deletions iot/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,28 @@ def gen_microdata(
recs = tc.Records(df)
if baseline_policy:
pol1 = tc.Policy()
print("Using baseline passed in")
# implement reforms in loop to layer on changes to move to whatever
# baseline is desired
for i, s in enumerate(baseline_policy):
if isinstance(s, str):
print("baseline policy is a string and its being read")
baseline = tc.Policy.read_json_reform(s)
else:
baseline = s
pol1.implement_reform(baseline, print_warnings=False, raise_errors=False)
pol1.implement_reform(
baseline, print_warnings=False, raise_errors=False
)
else:
pol1 = tc.Policy()

# reform should be a string (with path to json file) or a dictionary
# if a string, the next lines read it in to a dict
if isinstance(reform, str):
print("reform is a string and its being read")
reform = tc.Policy.read_json_reform(reform)

# Now layer on the reform policy on top of the baseline
pol1.implement_reform(reform, print_warnings=False, raise_errors=False)

print("RRC phaseout = ", dict(pol1.items())["RRC_ps"])

calc1 = tc.Calculator(policy=pol1, records=recs)
print("Year want to be at is: ", year)
print("Current taxcalc year is ", calc1.current_year)
calc1.advance_to_year(year)
calc1.calc_all()

Expand Down
58 changes: 35 additions & 23 deletions iot/inverse_optimal_tax.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,17 @@ def compute_mtr_dist(
"""
bins = 1000 # number of equal-width bins
data.loc[:, ["z_bin"]] = pd.cut(
data[income_measure], bins, include_lowest=True
)
data[income_measure], bins, include_lowest=True
)
binned_data = pd.DataFrame(
data[["mtr", income_measure, "z_bin", weight_var]]
.groupby(["z_bin"])
.apply(lambda x: wm(x[["mtr", income_measure]], x[weight_var]))
)
data[["mtr", income_measure, "z_bin", weight_var]]
.groupby(["z_bin"])
.apply(lambda x: wm(x[["mtr", income_measure]], x[weight_var]))
)
# make column 0 into two columns
binned_data[['mtr', income_measure]] = pd.DataFrame(binned_data[0].tolist(), index= binned_data.index)
binned_data[["mtr", income_measure]] = pd.DataFrame(
binned_data[0].tolist(), index=binned_data.index
)
binned_data.drop(columns=0, inplace=True)
binned_data.reset_index(inplace=True)
if mtr_smoother == "kreg":
Expand All @@ -146,7 +148,7 @@ def compute_mtr_dist(
idx = np.where(self.z > 900000)[0][0]
mtr[idx:] = mtr[idx]
else:
print('Please enter a value mtr_smoother method')
print("Please enter a value mtr_smoother method")
assert False
mtr_prime = np.gradient(mtr, edge_order=2)

Expand Down Expand Up @@ -203,19 +205,21 @@ def compute_income_dist(

# analytical derivative of lognormal
sigma = np.sqrt(sigmasq)
F = (1 / 2) * (1 + scipy.special.erf((np.log(z_line) - mu) / (np.sqrt(2) * sigma)))
F = (1 / 2) * (
1
+ scipy.special.erf(
(np.log(z_line) - mu) / (np.sqrt(2) * sigma)
)
)
f = (
(1 / np.sqrt(2 * np.pi * sigma))
* np.exp(-((np.log(z_line) - mu) ** 2) / (2 * sigma**2))
* (1 / z_line)
)
f_prime = (
-(
np.exp(-((np.log(z_line) - mu) ** 2) / (2 * sigma**2))
* (np.log(z_line) + sigma**2 - mu)
)
/ (np.sqrt(2) * np.sqrt(np.pi) * sigma ** (5 / 2) * z_line ** 2)
)
f_prime = -(
np.exp(-((np.log(z_line) - mu) ** 2) / (2 * sigma**2))
* (np.log(z_line) + sigma**2 - mu)
) / (np.sqrt(2) * np.sqrt(np.pi) * sigma ** (5 / 2) * z_line**2)
elif dist_type == "kde":
# uses the original full data for kde estimation
f_function = st.gaussian_kde(
Expand All @@ -228,7 +232,7 @@ def compute_income_dist(
f = f / np.sum(f)
f_prime = np.gradient(f, edge_order=2)
else:
print('Please enter a valid value for dist_type')
print("Please enter a valid value for dist_type")
assert False

return z_line, F, f, f_prime
Expand Down Expand Up @@ -261,16 +265,24 @@ def sw_weights(self):
)
)
elif method == "LW": # use Lockwood and Weinzierl formula
print('F max and sum is: ", ', self.F.max(), self.F.sum(), self.F[-1])
bracket_term = (1 - self.F - (self.mtr / (1-self.mtr)) * self.inc_elast * self.z * self.f)
print(
'F max and sum is: ", ', self.F.max(), self.F.sum(), self.F[-1]
)
bracket_term = (
1
- self.F
- (self.mtr / (1 - self.mtr))
* self.inc_elast
* self.z
* self.f
)
d_dz_bracket = np.gradient(bracket_term, edge_order=2)
g_z = (
- (1 / self.f) * d_dz_bracket)
g_z[:10] = g_z[10] # some issue at the bottom of the distribution
g_z = -(1 / self.f) * d_dz_bracket
g_z[:10] = g_z[10] # some issue at the bottom of the distribution
# normalize so sum to one
g_z = g_z / (g_z * self.f).sum()
else:
print('Please enter a valid value for method')
print("Please enter a valid value for method")
assert False
return g_z

Expand Down

0 comments on commit da726a0

Please sign in to comment.