/* DB Benefit calculator based on approximations
	computed by Shawn
---------------------------------------------------- */

program define DbBenefit,
	version 9, missing
	syntax varlist [if], gen(namelist)
	marksample touse
	tokenize `varlist'
	#delimit;
	tempvar ry_earn tenure edu age era nra yr sex;

	///1. Assign Data to Variables -------------------

	qui gen `ry_earn'  = `1'  if `touse';
	qui gen `tenure'	 = `2'  if `touse';
	qui gen `edu'	 = `3'  if `touse';
	qui gen `age'	 = `4'  if `touse';
	qui gen `era'      = `5'  if `touse';
	qui gen `nra'      = `6'  if `touse';
	qui gen `yr'       = `7'  if `touse';
	qui gen `sex'      = `8'  if `touse';


	///2. Calculating variables of interest
	tempvar age_s age_s2 logearn ten_bnra ten_anra edu1 edu3 era_c nra_c type;
	tempname mean_m mean_f;
	matrix input `mean_f' = (
	10.104,
	19.468,
	2.142,
	0.052,
	0.336,
	37.357,
	1487.887);
	matrix input `mean_m' = (
	10.561,
	23.138,
	1.956,
	0.115,
	0.303,
	34.012,
	1277.413);
	matrix colnames `mean_f' = `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2';
	matrix colnames `mean_m' = `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2';
	qui gen `age_s' = `age'-`tenure' if `touse';
	qui gen `age_s2' = `age_s'^2 if `touse';
	qui gen `logearn' = log(`ry_earn') if `touse';
      qui gen `ten_bnra' = `tenure' if `age'<`nra'&`touse';
      qui replace `ten_bnra' = `nra'-`age_s' if `age'>=`nra'&`touse';
	qui gen `ten_anra' = `age'-`nra' if `age'>`nra'&`touse';
	qui replace `ten_anra' = 0 if `age'<=`nra'&`touse';
	qui gen `edu1' = `edu'==1 if `touse';
	qui gen `edu3' = `edu'==3 if `touse';
	local pos = 0;
	foreach var of varlist `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2' {;
		local pos = `pos'+1;
		qui replace `var' = `var' - `mean_f'[1,`pos'] if `touse'&`sex'==0;
		qui replace `var' = `var' - `mean_m'[1,`pos'] if `touse'&`sex'==1;
	};

	qui recode `era' (min/47=45) (48/52=50) (53/57=55) (58/max=60) if `touse', gen(`era_c');
	qui recode `nra' (min/57=55) (58/62=60) (63/max=65) if `touse', gen(`nra_c');
	sum `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2';

	egen `type' = dbtype(`sex' `era_c' `nra_c') if `touse';

	sum `type';
	tempname pars_m pars_f;
	matrix input `pars_f' = (
		0.986445,
		0.084910,
		0.038673,
		-0.204026,
		0.111589,
		0.117000,
		-0.000943,
		1.000);
	matrix colnames `pars_f' = `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2' `type';
	matrix input `pars_m' = (
		1.08835,
		0.07272,
		0.03849,
		-0.10443,
		0.01962,
		0.11168,
		-0.00093,
		1.000);
	matrix colnames `pars_m' = `logearn' `ten_bnra' `ten_anra' `edu1' `edu3' `age_s' `age_s2' `type';


	/// Generating Prediction
	tempvar dbben;
	qui gen `dbben' = 0 if `touse';
	qui matrix score `dbben' = `pars_f' if `sex'==0&`touse', replace;
	qui matrix score `dbben' = `pars_m' if `sex'==1&`touse', replace;
	sum `dbben';
	qui replace `dbben' = exp(`dbben') if `touse';

	tokenize `gen';
	qui gen `1' = `dbben'*(`age'>=`era') if `touse';
end;