********************************************************************************
*  Empirical Exercise 1 - Descriptive Statistics and Confidence Intervals
*  Stock & Watson - Introduction to Econometrics (4th Edition)
*
*  Code: Fatih Kansoy - www.fatih.ai
*  Course: Financial Econometrics, SJTU Summer School 2025
********************************************************************************

clear all
set more off

* Set the working directory to the folder that holds the data before running,
* for example:  cd "path/to/data"

* Load data
import delimited "CPS96_15.csv", clear

* Quick data inspection
describe
summarize
tab year

********************************************************************************
* PARAMETERS: CPI Values for Inflation Adjustment
********************************************************************************
* Source: U.S. Bureau of Labor Statistics (BLS)
* CPI-U (Consumer Price Index for All Urban Consumers)
* Base period: 1982-84 = 100
*
* These values are given in the problem:
*   - CPI in 1996: 156.9
*   - CPI in 2015: 237.0
*
* To convert 1996 dollars to 2015 dollars:
*   Real_1996 = Nominal_1996 × (CPI_2015 / CPI_1996)
*            = Nominal_1996 × (237.0 / 156.9)
*            = Nominal_1996 × 1.511

local cpi_1996 = 156.9
local cpi_2015 = 237.0
local cpi_base = `cpi_2015'    // We express everything in 2015 dollars

* Display the adjustment factor for reference
display "Inflation adjustment factor (1996 → 2015): " `cpi_2015'/`cpi_1996'

********************************************************************************
* DATA PREPARATION: Create Real Wages
********************************************************************************

* Step 1: Assign CPI value to each observation based on year
gen cpi = `cpi_1996'
replace cpi = `cpi_2015' if year == 2015

* Step 2: Convert all wages to 2015 dollars
* Formula: Real_Wage = Nominal_Wage × (CPI_base / CPI_current)
gen ahe_real = ahe * (`cpi_base' / cpi)

* Verify: 2015 wages should be unchanged, 1996 wages should be ~1.51× higher
tabstat ahe ahe_real, by(year) stat(mean) format(%9.2f)

label variable ahe_real "Average Hourly Earnings (Real $2015)"
label variable ahe "Average Hourly Earnings (Nominal)"

********************************************************************************
* (a) NOMINAL AHE STATISTICS
********************************************************************************

* Summary statistics by year
tabstat ahe, by(year) stat(mean sd n) format(%9.2f)

* 95% CI for population mean in each year
* ci means: computes confidence interval assuming normal distribution
* Uses formula: mean ± t_(α/2) × SE, where SE = sd/√n
ci means ahe if year == 1996
ci means ahe if year == 2015

* Two-sample t-test for difference in means
* unequal: uses Welch's t-test (does not assume equal variances)
* This is more robust when sample variances differ
ttest ahe, by(year) unequal

********************************************************************************
* (b) REAL AHE IN 2015 DOLLARS
********************************************************************************

* Summary statistics - real wages
tabstat ahe_real, by(year) stat(mean sd n) format(%9.2f)

* 95% CI for real wages
ci means ahe_real if year == 1996
ci means ahe_real if year == 2015

* Test for difference in real means
ttest ahe_real, by(year) unequal

*-------------------------------------------------------------------------------
* FIGURE 1: Nominal vs Real Wage Growth
* Purpose: Visualize how inflation adjustment changes our interpretation
*-------------------------------------------------------------------------------

preserve
	collapse (mean) ahe ahe_real, by(year)

	* Reshape for grouped bar chart
	rename ahe wage1
	rename ahe_real wage2
	reshape long wage, i(year) j(type)
	label define typelbl 1 "Nominal" 2 "Real ($2015)"
	label values type typelbl

	* Create grouped bar chart
	graph bar wage, over(type) over(year) ///
		title("Average Hourly Earnings: Nominal vs Real") ///
		subtitle("Inflation masks true wage growth") ///
		ytitle("Dollars per Hour") ///
		blabel(bar, format(%4.2f)) ///
		bar(1, color(navy)) bar(2, color(maroon)) ///
		legend(off) ///
		note("Source: CPS 1996 & 2015. Real wages in 2015 dollars (CPI-adjusted).")
	graph export "fig1_nominal_vs_real.png", replace width(1200)
restore

*-------------------------------------------------------------------------------
* FIGURE 2: Wage Distribution by Year
* Purpose: Show not just means but entire distributions changed
*-------------------------------------------------------------------------------

twoway (kdensity ahe_real if year == 1996, lcolor(navy) lwidth(medium)) ///
       (kdensity ahe_real if year == 2015, lcolor(maroon) lwidth(medium)), ///
	   title("Distribution of Real Wages: 1996 vs 2015") ///
	   subtitle("Rightward shift + increased dispersion") ///
	   xtitle("Real Hourly Earnings ($2015)") ///
	   ytitle("Density") ///
	   legend(order(1 "1996" 2 "2015") position(2) ring(0)) ///
	   note("Source: CPS. Kernel density estimates.")
graph export "fig2_wage_distribution.png", replace width(1200)

********************************************************************************
* (c) INTERPRETATION
* Answer: Use (b) for purchasing power. Nominal wages include inflation effects.
* Real increase ($2.06) is only 24% of nominal increase ($8.54).
********************************************************************************

********************************************************************************
* (d) 2015 DATA: HIGH SCHOOL VS COLLEGE
********************************************************************************

* tabstat by education for 2015
* bachelor = 0: high school diploma only
* bachelor = 1: college degree (B.A./B.S.)
tabstat ahe if year == 2015, by(bachelor) stat(mean sd n) format(%9.2f)

* 95% CI for each education group
ci means ahe if year == 2015 & bachelor == 0
ci means ahe if year == 2015 & bachelor == 1

* Test for college premium (difference in means)
* Positive difference = college graduates earn more
ttest ahe if year == 2015, by(bachelor) unequal

********************************************************************************
* (e) 1996 DATA IN $2015: HIGH SCHOOL VS COLLEGE
********************************************************************************

tabstat ahe_real if year == 1996, by(bachelor) stat(mean sd n) format(%9.2f)

ci means ahe_real if year == 1996 & bachelor == 0
ci means ahe_real if year == 1996 & bachelor == 1

ttest ahe_real if year == 1996, by(bachelor) unequal

*-------------------------------------------------------------------------------
* FIGURE 3: Education Premium Over Time
* Purpose: Show college wage premium increased from 1996 to 2015
*-------------------------------------------------------------------------------

preserve
	collapse (mean) ahe_real, by(year bachelor)

	* Calculate gaps for annotation
	sort year bachelor
	list

	graph bar ahe_real, over(bachelor, relabel(1 "High School" 2 "College")) ///
		over(year) ///
		title("Education Wage Premium: 1996 vs 2015") ///
		subtitle("College premium increased from $6.77 to $9.23") ///
		ytitle("Real Hourly Earnings ($2015)") ///
		blabel(bar, format(%4.2f)) ///
		bar(1, color(navy)) bar(2, color(maroon)) ///
		legend(off) ///
		note("Source: CPS. Gap = College mean − High School mean.")
	graph export "fig3_education_premium.png", replace width(1200)
restore

********************************************************************************
* (f) DID REAL WAGES INCREASE? DID THE GAP INCREASE?
********************************************************************************

* (i) High school graduates: test if real wages changed
* If CI includes zero → not statistically significant
ttest ahe_real if bachelor == 0, by(year) unequal

* (ii) College graduates: test if real wages changed
ttest ahe_real if bachelor == 1, by(year) unequal

* (iii) Compare education gaps across years
* Gap 1996:
ttest ahe_real if year == 1996, by(bachelor) unequal

* Gap 2015:
ttest ahe_real if year == 2015, by(bachelor) unequal

*-------------------------------------------------------------------------------
* FIGURE 4: Real Wage Changes by Education
* Purpose: College wages grew; HS wages stagnated
*-------------------------------------------------------------------------------

preserve
	collapse (mean) mean_wage=ahe_real (sd) sd_wage=ahe_real (count) n=ahe_real, ///
		by(year bachelor)
	gen se_wage = sd_wage / sqrt(n)
	gen ci_lo = mean_wage - 1.96*se_wage
	gen ci_hi = mean_wage + 1.96*se_wage

	* Reshape for line graph
	sort bachelor year

	twoway (connected mean_wage year if bachelor == 0, ///
				lcolor(navy) mcolor(navy) msymbol(O)) ///
		   (rcap ci_lo ci_hi year if bachelor == 0, lcolor(navy)) ///
		   (connected mean_wage year if bachelor == 1, ///
				lcolor(maroon) mcolor(maroon) msymbol(S)) ///
		   (rcap ci_lo ci_hi year if bachelor == 1, lcolor(maroon)), ///
		   title("Real Wage Trends by Education") ///
		   subtitle("College wages grew; High school wages flat") ///
		   xtitle("Year") ytitle("Real Hourly Earnings ($2015)") ///
		   xlabel(1996 2015) ///
		   legend(order(1 "High School" 3 "College") position(5) ring(0)) ///
		   note("Error bars show 95% confidence intervals.")
	graph export "fig4_wage_trends.png", replace width(1200)
restore

********************************************************************************
* (g) GENDER GAP FOR HIGH SCHOOL GRADUATES
********************************************************************************

* 1996 - High school only (in real 2015 dollars)
* female = 0: male, female = 1: female
tabstat ahe_real if year == 1996 & bachelor == 0, by(female) stat(mean sd n) format(%9.2f)
ttest ahe_real if year == 1996 & bachelor == 0, by(female) unequal

* 2015 - High school only
tabstat ahe_real if year == 2015 & bachelor == 0, by(female) stat(mean sd n) format(%9.2f)
ttest ahe_real if year == 2015 & bachelor == 0, by(female) unequal

*-------------------------------------------------------------------------------
* FIGURE 5: Gender Gap Comparison (High School vs College)
* Purpose: Compare gender gaps across education levels and time
*-------------------------------------------------------------------------------

preserve
	* Calculate means by year, education, gender
	collapse (mean) ahe_real, by(year bachelor female)

	* Calculate gender gaps (Male - Female)
	reshape wide ahe_real, i(year bachelor) j(female)
	gen gender_gap = ahe_real0 - ahe_real1

	* Labels for graph
	gen group = ""
	replace group = "HS 1996" if year == 1996 & bachelor == 0
	replace group = "HS 2015" if year == 2015 & bachelor == 0
	replace group = "College 1996" if year == 1996 & bachelor == 1
	replace group = "College 2015" if year == 2015 & bachelor == 1

	encode group, gen(group_n)

	graph bar gender_gap, over(group_n, relabel(1 "College 1996" 2 "College 2015" ///
		3 "HS 1996" 4 "HS 2015") label(angle(45))) ///
		title("Gender Wage Gap: Male − Female") ///
		subtitle("HS gap narrowed; College gap widened") ///
		ytitle("Gap in Real Hourly Earnings ($2015)") ///
		blabel(bar, format(%4.2f)) ///
		bar(1, color(navy)) ///
		note("Source: CPS. Positive values indicate men earn more than women.")
	graph export "fig5_gender_gap.png", replace width(1200)
restore

*-------------------------------------------------------------------------------
* FIGURE 6: Gender Gap for High School (Detailed)
* Purpose: Replicate Table 3.1 structure visually
*-------------------------------------------------------------------------------

preserve
	keep if bachelor == 0
	collapse (mean) ahe_real, by(year female)

	graph bar ahe_real, over(female, relabel(1 "Male" 2 "Female")) ///
		over(year) ///
		title("Gender Wage Gap: High School Graduates") ///
		subtitle("Gap narrowed from $4.02 (1996) to $3.29 (2015)") ///
		ytitle("Real Hourly Earnings ($2015)") ///
		blabel(bar, format(%4.2f)) ///
		bar(1, color(navy)) bar(2, color(maroon)) ///
		legend(off) ///
		note("Source: CPS. All values in 2015 dollars.")
	graph export "fig6_gender_gap_hs.png", replace width(1200)
restore

********************************************************************************
* END OF DO FILE
********************************************************************************