Skip to content

Instantly share code, notes, and snippets.

@jasonlai
Created April 26, 2011 07:32
Show Gist options
  • Save jasonlai/941942 to your computer and use it in GitHub Desktop.
Save jasonlai/941942 to your computer and use it in GitHub Desktop.
Taxes Calculator
#!/usr/bin/env python
# This snippet generates a chart showing tax changes after China's 2011 Individual Income Tax Adjustment Draft.
# Both NumPy & PyLab are required here. Please make sure you have these libs installed.
import sys
from pylab import arange, array, frompyfunc, plot, show
from taxes import NEW_THRESHOLD, NEW_TAX_LEVELS, OLD_THRESHOLD, OLD_TAX_LEVELS, income_tax
def income_tax_ufunc(threshold, tax_levels):
def income_tax_func(income):
return income_tax(income, threshold=threshold, tax_levels=tax_levels)
return frompyfunc(income_tax_func, 1, 1)
new_income_tax_ufunc = income_tax_ufunc(threshold=NEW_THRESHOLD, tax_levels=NEW_TAX_LEVELS)
old_income_tax_ufunc = income_tax_ufunc(threshold=OLD_THRESHOLD, tax_levels=OLD_TAX_LEVELS)
if __name__ == '__main__':
try:
if len(sys.argv) == 1:
maximum = 50000.
else:
_, maximum = sys.argv
maximum = float(maximum)
except ValueError:
print >>sys.stderr, '%s: invalid maximum income value' % sys.argv[0]
sys.exit(1)
x = arange(0., maximum, 1000.)
y = new_income_tax_ufunc(x) - old_income_tax_ufunc(x)
plot([0, maximum], [0, 0])
plot([0, maximum], [min(y)] * 2)
plot(x, y, 'o-')
show()
#!/usr/bin/env python
NEW_HOUSING_FUND_CAP = 12603
OLD_HOUSING_FUND_CAP = 12111
NEW_INSURANCE_CAP = 12603
OLD_INSURANCE_CAP = 12111
NEW_TAX_LEVELS = ((0.05, 0), (0.1, 75), (0.2, 525), (0.25, 975), (0.3, 2725), (0.35, 5475), (0.45, 13475))
OLD_TAX_LEVELS = ((0.05, 0), (0.1, 25), (0.15, 125), (0.2, 375), (0.25, 1375), (0.3, 3375), (0.35, 6375), (0.4, 10375), (0.45, 15375))
NEW_THRESHOLD = 3000
OLD_THRESHOLD = 2000
HOUSING_FUND_RATE = 0.12
HOUSING_FUND_CAP = NEW_HOUSING_FUND_CAP
INSURANCE_RATES = (
(0.08, 0), # Pension
(0.02, 3), # Medical
(0.002, 0), # Unemployment
)
INSURANCE_CAP = NEW_INSURANCE_CAP
TAX_LEVELS = NEW_TAX_LEVELS
THRESHOLD = NEW_THRESHOLD
def income_tax(income, threshold, tax_levels):
deduced = income - threshold
return round(max(0, *(deduced * rate - qcd for (rate, qcd) in tax_levels)), 2)
def housing_fund(pretax_income, cap, rate):
base = min(pretax_income, cap)
return round(base * rate, 2)
def insurances(pretax_income, cap, rates):
base = min(pretax_income, cap)
return round(sum([base * rate + extra for (rate, extra) in rates]), 2)
def post_tax_income(pretax_income, threshold, tax_levels, housing_fund_cap, housing_fund_rate, insurance_cap, insurance_rates):
result = pretax_income
result -= housing_fund(pretax_income, housing_fund_cap, housing_fund_rate)
result -= insurances(pretax_income, insurance_cap, insurance_rates)
result -= income_tax(result, threshold, tax_levels)
return round(result, 2)
if __name__ == '__main__':
def post_tax_income_2011(pretax_income):
return post_tax_income(pretax_income, threshold=THRESHOLD, tax_levels=TAX_LEVELS, housing_fund_cap=HOUSING_FUND_CAP, housing_fund_rate=HOUSING_FUND_RATE, insurance_cap=INSURANCE_CAP, insurance_rates=INSURANCE_RATES)
import sys
try:
_, income = sys.argv
income = float(income)
except ValueError:
print >> sys.stderr, "%s: income value required" % sys.argv[0]
sys.exit(-1)
h = housing_fund(income, cap=HOUSING_FUND_CAP, rate=HOUSING_FUND_RATE)
i = insurances(income, cap=INSURANCE_CAP, rates=INSURANCE_RATES)
report = (
('Pre-tax Income', income),
('Deduced Housing Fund', h),
('Deduced Insurances', i),
('Pre-tax Income with Social Insurances off', income - h - i),
('Individual Income Tax', income_tax(income - h - i, THRESHOLD, TAX_LEVELS)),
('Post Tax Income', post_tax_income_2011(income)),
('Post Tax Income with Housing Funds', post_tax_income_2011(income) + 2 * h),
)
title_length = max([len(title) for title, _ in report])
title_length = (title_length + 5) / 4 * 4
format = '%%-%ds%%9.2f' % title_length
for title, value in report:
print format % (title + ':', value)
@bindu2k5
Copy link

bindu2k5 commented Jan 2, 2023

Output please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment