Skip to content

Instantly share code, notes, and snippets.

@MichaelChirico
Last active April 13, 2024 19:28
Show Gist options
  • Save MichaelChirico/04f7397629bb4f34f96bcac6ac917b33 to your computer and use it in GitHub Desktop.
Save MichaelChirico/04f7397629bb4f34f96bcac6ac917b33 to your computer and use it in GitHub Desktop.
Tax calculation functions for 2023
medicare_tax = function(medi_income, filing_status="joint") {
addl_floor = switch(filing_status, joint=250000, mfs=125000, single=200000)
.0145*medi_income + pmax(0, .009*(medi_income-addl_floor))
}
social_security_tax = function(income) .062 * pmin(income, 160200)
tax_with_deductible_brackets = function(income, deductible, bracket_rates, bracket_mins) {
sum(pmax(0, diff(bracket_rates) * (income - deductible - bracket_mins)))
}
# (assuming standard deduction)
# income: AGI
federal_income_tax = function(income, filing_status="joint") {
standard_deduction = 27700
bracket_rates = c(0, 10, 12, 22, 24, 32, 35, 37) / 100
bracket_mins = c(0, 22000, 89450, 190750, 364200, 462500, 693750)
if (filing_status != "joint") {
standard_deduction = standard_deduction/2
bracket_mins = bracket_mins/2
if (filing_status == "single") bracket_mins[which(bracket_rates == .35)] = 578125
}
tax_with_deductible_brackets(income, standard_deduction, bracket_rates, bracket_mins)
}
massachusetts_income_tax = function(income, filing_status="joint") {
standard_deduction = if (filing_status == "joint") 8800 else 4400
.05 * pmax(0, income - standard_deduction)
}
california_income_tax = function(income, filing_status="joint") {
standard_deduction = 10726
bracket_rates = c(0, 1, 2, 4, 6, 8, 9.3, 10.3, 11.3, 12.3) / 100
bracket_mins = c(0, 20824, 49368, 77918, 108162, 136700, 698274, 837922, 1396542)
if (filing_status != "joint") {
standard_deduction = standard_deduction/2
bracket_mins = bracket_mins/2
}
tax_with_deductible_brackets(income, standard_deduction, bracket_rates, bracket_mins)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment