/* Federal Income Tax: ------------------------------
Based on 2004 Tax Function
Pierre-Carl Michaud, March 2008
---------------------------------------------------- */

program define FedTax, rclass
	version 9
	syntax varlist [if] , gen(namelist)
	marksample touse
	tokenize `varlist'
	#delimit ;

	tempvar ry_earn sy_earn ry_pub sy_pub
			y_ben y_pen y_ot y_as mar rage sage yr;
	///1. Assign Data to Variables -------------------

	qui gen `ry_earn' = `1' if `touse';
	qui gen `sy_earn' = `2' if `touse';
	qui gen `ry_pub'  = `3' if `touse';
	qui gen `sy_pub'  = `4' if `touse';
	qui gen `y_ben'   = `5' if `touse';
	qui gen `y_pen'   = `6' if `touse';
	qui gen `y_ot'    = `7'	if `touse';
	qui gen `y_as'    = `8' if `touse';
	qui gen `mar'     = `9'	if `touse';
	qui gen `rage'    = `10' if `touse';
	qui gen `sage' 	= `11' if `touse';

	///2. Tax Parameters used -------------------------

	/// Federal tax schedule
	/// brackets single (s) and couples (c)

	local sbra1 = 7150;
	local sbra2 = 29050;
	local sbra3 = 70350;
	local sbra4 = 146750;
	local sbra5 = 319100;
	local cbra1 = 14300;
	local cbra2 = 58100;
	local cbra3 = 117250;
	local cbra4 = 178650;
	local cbra5 = 319100;

	tempname mtr1 mtr2 mtr3 mtr3 mtr4 mtr5 mtr6;

	scalar `mtr1' = 0.10;
	scalar `mtr2' = 0.15;
	scalar `mtr3' = 0.25;
	scalar `mtr4' = 0.28;
	scalar `mtr5' = 0.33;
	scalar `mtr6' = 0.35;

	/// Basic deduction and old age deduction
	tempname bded_sing bded_coup oded_sing oded_coup
				pded pded_r pded_s pded_tcoup pded_tsing;
	scalar `bded_sing' = 4850;
	scalar `bded_coup' = 7850;
	scalar `oded_sing' = 5850;
	scalar `oded_coup' = 9650;
	scalar `pded' = 3100;
	scalar `pded_r' = 0.02;
	scalar `pded_s' = 2500;
	scalar `pded_tcoup' = 214500;
	scalar `pded_tsing' = 142700;


	/// taxation of social security benefits
	tempname base_ssa_sing base_ssa_coup base2_ssa_sing base2_ssa_coup;
	scalar `base_ssa_sing' = 25000;
	scalar `base_ssa_coup' = 32000;
	scalar `base2_ssa_sing' = 9000;
      scalar `base2_ssa_coup' = 12000;

	/// tax credit for low-income elderly
	tempname tc_max1_sing tc_max1_one65 tc_max1_both65
					tc_max2_sing tc_max2_one65;
	scalar `tc_max1_sing' = 17500;
	scalar `tc_max1_one65' = 20000;
	scalar `tc_max1_both65' = 25000;
	scalar `tc_max2_sing' = 5000;
	scalar `tc_max2_one65' = 7500;

	/// Earned Income Tax Credit
	tempname eic_rate eic_lim eic_tre_mar eic_tre_sig eic_phase;
	scalar `eic_rate' = 0.0765;
	scalar `eic_lim' = 5100;
	scalar `eic_tre_mar' = 12490;
	scalar `eic_tre_sig' = 11490;
	scalar `eic_phase' = 0.765;

	/// 3. Calculations -------------------------------
	/// indicators
	/// Total Income
	tempvar y_total;
		qui gen `y_total' = `ry_earn' + `sy_earn' + `ry_pub' + `sy_pub'
				+ `y_pen' + `y_ben' + `y_ot' + `y_as' if `touse';
	/// Age Status
	 tempvar agestat sstax ;
	 qui gen `agestat' = 1 if `rage'<65&`sage'<65&`touse';
	 qui replace `agestat' = 2 if ((`rage'>=65&`sage'<65)|
   				     (`rage'<65&`sage'>=65))&`touse';
       qui replace `agestat' = 3 if `rage'>=65&`sage'>=65&`touse';

	/// Taxable SS benefit
	tempvar rtax_ben rothinc t1 t2;
	qui gen `rtax_ben' = 0.5*`ry_pub' if `touse';

	qui gen `rothinc' = `sy_pub' + `y_pen'
		+ `ry_earn' + `sy_earn' + `y_ben' + `y_ot' + `y_as' if `touse';

	qui gen     `t1' = 0.85*max(`rtax_ben' + `rothinc'
						- `base_ssa_sing',0) if `mar'==0&`touse';
	qui replace `t1' = 0.85*max(`rtax_ben' + `rothinc'
						- `base_ssa_coup',0) if `mar'==1&`touse';

	qui gen 	  `t2' = min(`rtax_ben' + `rothinc',
						    `base_ssa_sing') if `mar'==0&`touse';
	qui replace `t2' = min(`rtax_ben' + `rothinc',
						    `base_ssa_coup') if `mar'==1&`touse';
	qui replace `t2' = min(`rtax_ben',0.5*`t2') if `touse';
	qui replace `t1' = min(`t1' + `t2',0.85*`ry_pub') if `touse';
	qui replace `rtax_ben' = `t1' if `touse';
	drop `t1' `t2' `rothinc';

	tempvar stax_ben sothinc t1 t2;
	qui gen `stax_ben' = 0.5*`sy_pub' if `touse';
	qui gen `sothinc' = `ry_pub' + `y_pen'
		+ `ry_earn' + `sy_earn' + `y_ben' + `y_ot' + `y_as' if `touse';
	qui gen     `t1' = 0 if `mar'==0&`touse';
	qui replace `t1' = 0.85*max(`stax_ben' + `sothinc'
						- `base_ssa_coup',0) if `mar'==1&`touse';
	qui gen 	  `t2' = 0 if `mar'==0&`touse';
	qui replace `t2' = min(`stax_ben' + `sothinc',
						    `base_ssa_coup') if `mar'==1&`touse';
	qui replace `t2' = min(`stax_ben',0.5*`t2') if `touse';
	qui replace `t1' = min(`t1' + `t2',0.85*`sy_pub') if `touse';
	qui replace `stax_ben' = `t1' if `touse';
	drop `t1' `t2' `sothinc';

	/// Taxable income for federal tax
      tempvar y_gross a65 ded exempt cut y_taxble;
	qui gen `y_gross' = `ry_earn' + `sy_earn'
							+ `rtax_ben' + `stax_ben'
							+ `y_pen' + `y_ben'
							+ `y_ot' + `y_as'  if `touse';

	qui gen `a65' = `agestat'>1 if `touse';
	qui gen     `ded' = `bded_sing' if `a65'==0&`mar'==0&`touse';
	qui replace `ded' = `bded_coup' if `a65'==0&`mar'==1&`touse';
	qui replace `ded' = `oded_sing' if `a65'==1&`mar'==0&`touse';
	qui replace `ded' = `oded_coup' if `a65'==1&`mar'==1&`touse';

	qui gen `exempt' = `pded'*(1+`mar') if `touse';
	qui gen `cut' = min(`pded_r'*floor((max(`y_gross'-`pded_tcoup',0))/`pded_s'),1) if `mar'==1&`touse';
	qui replace `cut' = min(`pded_r'*floor((max(`y_gross'-`pded_tsing',0))/`pded_s'),1) if `mar'==0&`touse';
	qui replace `exempt' = (1-`cut')*`exempt' if `touse';

	qui gen `y_taxble' = max(`y_gross'-`ded' - `exempt',0) if `touse';
	drop `ded' `a65' `cut' `exempt';


	/// Federal taxes paid
	tempvar ftax_gross ts1 ts2 ts3 ts4 ts5 ts6 tc1 tc2 tc3 tc4 tc5 tc6;
	qui mkspline `ts1' `sbra1' `ts2' `sbra2' `ts3' `sbra3' `ts4' `sbra4' `ts5' `sbra5' `ts6' = `y_taxble' if `touse';
	qui mkspline `tc1' `cbra1' `tc2' `cbra2' `tc3' `cbra3' `tc4' `cbra4' `tc5' `cbra5' `tc6' = `y_taxble' if `touse';
	qui gen `ftax_gross' = `mtr1'*`ts1' + `mtr2'*`ts2' + `mtr3'*`ts3' + `mtr4'*`ts4' + `mtr5'*`ts5' + `mtr6'*`ts6' if `mar'==0&`touse';
	qui replace `ftax_gross' = `mtr1'*`tc1' + `mtr2'*`tc2' + `mtr3'*`tc3'
							+ `mtr4'*`tc4' + `mtr5'*`tc5' + `mtr6'*`tc6' if `mar'==1&`touse';

	/// Tax credit on federal tax for low income elderly
	tempvar test1 nontaxable test2 tcredit ftax;
												/// determine eligibility
	qui gen `test1' = `y_gross'<`tc_max1_sing' if `mar'==0&`rage'>=65&`touse';
	qui replace `test1' = `y_gross'<`tc_max1_one65' if `mar'==1&`agestat'==2&`touse';
	qui replace `test1' = `y_gross'<`tc_max1_both65' if `mar'==1&`agestat'==3&`touse';
	qui replace `test1' = 0 if missing(`test1')==1&`touse';
	qui gen `nontaxable' = `ry_pub' + `sy_pub' - `rtax_ben' - `stax_ben' if `touse';
	qui gen `test2' = `nontaxable' < `tc_max2_sing' if `mar'==0&`rage'>=65&`touse';
	qui replace `test2' = `nontaxable' < `tc_max2_one65' if `mar'==1&`agestat'>1&`touse';
	qui replace `test2' = 0 if missing(`test2')==1&`touse';
													/// determine credit
	qui gen `tcredit' = `tc_max2_sing' if (`test1'==1&`test2'==1)&`touse';
	qui replace `tcredit' = `tc_max2_one65' if `mar'==1&(`test1'==1&`test2'==1)&`agestat'==3&`touse';
	qui replace `tcredit' = 0 if missing(`tcredit')==1&`touse';
	drop `test1' `test2' `nontaxable';
	qui gen `ftax' = max(`ftax_gross' - `tcredit',0) if `touse';

	/// Earned Income Tax Credit
	tempvar hy_earn eic pen;
	qui gen `hy_earn' = `ry_earn' + `sy_earn' if `touse';

	qui gen `eic' = min(`eic_rate'*`hy_earn',`eic_lim') if `touse';
	qui gen `pen' = `eic_phase'*max(`hy_earn'-`eic_tre_mar',0) if `touse'&`mar'==1;
	qui replace `pen' = `eic_phase'*max(`hy_earn'-`eic_tre_sig',0) if `touse'&`mar'==0;

	qui replace `eic' = max(`eic' - `pen',0) if `touse';
	qui replace `eic' = 0 if `mar'==1&min(`rage',`sage')>=65&`touse';
	qui replace `eic' = 0 if `mar'==0&`rage'>=65&`touse';

	/// Net income
	tokenize `gen';
	qui gen `1' = `ftax' - `eic' if `touse';
end;