Loading [MathJax]/extensions/tex2jax.js
MRFFL: MR Fortran Finance Library 2024-12-28
Computational Tools For Finance
All Namespaces Files Functions Variables
mrffl_us_taxes.f90
Go to the documentation of this file.
1! -*- Mode:F90; Coding:us-ascii-unix; fill-column:129 -*-
2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.H.S.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.H.E.!!
3!>
4!! @file mrffl_us_taxes.f90
5!! @author Mitch Richling http://www.mitchr.me/
6!! @date 2024-12-21
7!! @brief Compute Taxes for USA.@EOL
8!! @keywords finance fortran monte carlo inflation cashflow time value of money tvm percentages taxes stock market
9!! @std F2023
10!! @see https://github.com/richmit/FortranFinance
11!! @copyright
12!! @parblock
13!! Copyright (c) 2024, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
14!!
15!! Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
16!! conditions are met:
17!!
18!! 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following
19!! disclaimer.
20!!
21!! 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following
22!! disclaimer in the documentation and/or other materials provided with the distribution.
23!!
24!! 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
25!! derived from this software without specific prior written permission.
26!!
27!! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
28!! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29!! DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30!! EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31!! USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32!! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33!! OF THE POSSIBILITY OF SUCH DAMAGE.
34!! @endparblock
35!!
36
37!##################################################################################################################################
38!------------------------------------------------------------------------------------------------------------------------------
39!> Compute Taxes for USA.
40!!
42 use mrffl_config, only: rk=>mrfflrk, ik=>mrfflik, zero_epsilon
44 use mrffl_tvm, only: fv_from_pv_n_i
45 implicit none
46 private
47
48 integer(kind=ik), parameter, public :: seed_tax_year = 2024 ! The year the following constants hold
49 real(kind=rk), parameter, public :: std_tax_deduction_single = 14600
50 real(kind=rk), parameter, public :: std_tax_deduction_joint = 29200
51 real(kind=rk), parameter, public :: std_tax_deduction_separately = 14600
52 real(kind=rk), parameter, public :: std_tax_deduction_head = 21900
53 real(kind=rk), parameter, public :: tax_bracket_rates(7) = [ 10, 12, 22, 24, 32, 35, 37]
54 real(kind=rk), parameter, public :: tax_bracket_breaks_single(7) = [11600, 47150, 100525, 191950, 243725, 609350, 609351]
55 real(kind=rk), parameter, public :: tax_bracket_breaks_joint(7) = [23200, 94300, 201050, 383900, 487450, 731200, 731201]
56 real(kind=rk), parameter, public :: tax_bracket_breaks_head(7) = [16550, 63100, 100500, 191950, 243700, 609350, 609350]
57 real(kind=rk), parameter, public :: tax_bracket_breaks_separately(7) = [11600, 47150, 100525, 191950, 243725, 365600, 365601]
58
59 ! 2024 US Tax Brackets
60 !
61 ! | Tax rate | Single filer | Married filing jointly | Married filing separately | Head of household |
62 ! |----------+----------------------+------------------------+---------------------------+----------------------|
63 ! | 10% | $0 to $11,600 | $0 to $23,200 | $0 to $11,600 | $0 to $16,550 |
64 ! | 12% | $11,601 to $47,150 | $23,201 to $94,300 | $11,601 to $47,150 | $16,551 to $63,100 |
65 ! | 22% | $47,151 to $100,525 | $94,301 to $201,050 | $47,151 to $100,525 | $63,101 to $100,500 |
66 ! | 24% | $100,526 to $191,950 | $201,051 to $383,900 | $100,526 to $191,950 | $100,501 to $191,950 |
67 ! | 32% | $191,951 to $243,725 | $383,901 to $487,450 | $191,951 to $243,725 | $191,951 to $243,700 |
68 ! | 35% | $243,726 to $609,350 | $487,451 to $731,200 | $243,726 to $365,600 | $243,701 to $609,350 |
69 ! | 37% | $609,351 or more | $731,201 or more | $365,601 or more | $609,350 or more |
70 !
71
73
74contains
75
76 !------------------------------------------------------------------------------------------------------------------------------
77 !> Find the bracket val belongs in given an array of bracket end points.
78 !!
79 !! brackets is an array consisting of bracket endpoints @f$ [ e_1, e_2, ..., e_{n-1}, e_{n} ] @f$
80 !! such that @f$ e_1 < e_2 < ... < e_{n-1} < e_n @f$.
81 !!
82 !! The endpoints define a set of intervals:
83 !! @f$ (-\infty, e1], (e_1, e_2], ..., (e_{n-2}, e_{n-1}], (e_{n-1}, e_n), [e_n, \infity) @f$
84 !! Note the second to last interval is open -- the rest are half open.
85 !! We enumerate these intervals from 0 up to n.
86 !!
87 !! This function returns the index for the interval containing val.
88 !!
89 integer(kind=ik) function max_bracket(val, brackets)
90 real(kind=rk), intent(in) :: val
91 real(kind=rk), intent(in) :: brackets(:)
92 integer(kind=ik) :: i
93 if (val <= brackets(1)) then
94 max_bracket = 1_ik
95 else if (val >= brackets(size(brackets))) then
96 max_bracket = size(brackets, kind=ik)
97 else
98 max_bracket = 0_ik
99 do i=size(brackets, kind=ik)-1_ik,1_ik,-1_ik
100 if (val > brackets(i)) then
101 max_bracket = i+1_ik
102 exit
103 end if
104 end do
105 end if
106 end function max_bracket
107
108 !------------------------------------------------------------------------------------------------------------------------------
109 !> Compute tax given value, bracket breaks, and bracket rates.
110 !!
111 !! see max_bracket() for a description of breaks. The rates array are the tax rates for each bracket.
112 !!
113 real(kind=rk) function tax(val, breaks, rates)
114 real(kind=rk), intent(in) :: val
115 real(kind=rk), intent(in) :: breaks(:), rates(:)
116 real(kind=rk) :: val_in_bracket, last_break, cur_break
117 integer :: idx
118 if (val < zero_epsilon) then
119 tax = 0
120 else
121 last_break = 0
122 tax = 0
123 do idx=1,size(rates)-1
124 cur_break = breaks(idx)
125 val_in_bracket = min(val, cur_break) - last_break
126 last_break = cur_break
127 if (val_in_bracket > 0) then
128 tax = tax + percentage_of(val_in_bracket, rates(idx))
129 end if
130 end do
131 if (val >= breaks(size(breaks))) then
132 tax = tax + percentage_of(1 + val - breaks(size(breaks)), rates(idx))
133 end if
134 end if
135 end function tax
136
137 !------------------------------------------------------------------------------------------------------------------------------
138 !> Compute effective tax rate given value, bracket breaks, and bracket rates.
139 !!
140 !! see max_bracket() for a description of breaks. The rates array are the tax rates for each bracket.
141 !!
142 real(kind=rk) function effective_tax_rate(val, breaks, rates)
143 real(kind=rk), intent(in) :: val
144 real(kind=rk), intent(in) :: breaks(:), rates(:)
145 if (val < zero_epsilon) then
147 else
148 effective_tax_rate = percentage_of_total(val, tax(val, breaks, rates))
149 end if
150 end function effective_tax_rate
151
152 !------------------------------------------------------------------------------------------------------------------------------
153 !> Compute projected effective tax rate with tax() by adjusting the breaks into the future for inflation.
154 !!
155 !! If year is in the past with respect to our tax information, then -1 is returned.
156 !!
157 real(kind=rk) function projected_tax(val, breaks, rates, year, inflation)
158 real(kind=rk), intent(in) :: val, inflation
159 integer(kind=ik), intent(in) :: year
160 real(kind=rk), intent(in) :: breaks(:), rates(:)
161 if (year < seed_tax_year) then
162 projected_tax = -1
163 else if (year < seed_tax_year) then
164 projected_tax = tax(val, breaks, rates)
165 else
166 projected_tax = tax(val, fv_from_pv_n_i(breaks, year-seed_tax_year, inflation), rates)
167 end if
168 end function projected_tax
169
170end module mrffl_us_taxes
Configuration for MRFFL (MR Fortran Finance Library).
integer, parameter, public mrfflrk
Real kind used in interfaces.
real(kind=mrfflrk), parameter, public zero_epsilon
Used to test for zero.
integer, parameter, public mrfflik
Integer kinds used in interfaces.
Simple functions for working with percentages.
elemental real(kind=rk) function, public percentage_of_total(v_total, v_part)
Compute the percentage of a total.
elemental real(kind=rk) function, public percentage_of(v, p)
Compute the percentage of a value.
Solvers for TVM problems involving lump sums, level (fixed) annuities, and geometric (growing) annuit...
real(kind=rk) pure elemental function, public fv_from_pv_n_i(pv, n, i)
compute future value from present value (pv), number of periods (n), and an intrest rate (i).
Compute Taxes for USA.
real(kind=rk), parameter, public std_tax_deduction_single
real(kind=rk), parameter, public std_tax_deduction_head
integer(kind=ik) function, public max_bracket(val, brackets)
Find the bracket val belongs in given an array of bracket end points.
real(kind=rk), dimension(7), parameter, public tax_bracket_breaks_head
real(kind=rk) function, public tax(val, breaks, rates)
Compute tax given value, bracket breaks, and bracket rates.
real(kind=rk) function, public effective_tax_rate(val, breaks, rates)
Compute effective tax rate given value, bracket breaks, and bracket rates.
real(kind=rk) function, public projected_tax(val, breaks, rates, year, inflation)
Compute projected effective tax rate with tax() by adjusting the breaks into the future for inflation...
real(kind=rk), dimension(7), parameter, public tax_bracket_breaks_separately
real(kind=rk), parameter, public std_tax_deduction_joint
real(kind=rk), dimension(7), parameter, public tax_bracket_rates
integer(kind=ik), parameter, public seed_tax_year
real(kind=rk), parameter, public std_tax_deduction_separately
real(kind=rk), dimension(7), parameter, public tax_bracket_breaks_joint
real(kind=rk), dimension(7), parameter, public tax_bracket_breaks_single