********************************************************************************
*  Empirical Exercise 5.2 - Growth and Trade: Hypothesis Testing
*  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 "Growth.csv", clear

* Quick data inspection
describe
summarize

********************************************************************************
* DATA OVERVIEW (Excluding Malta)
********************************************************************************

* Count observations
count
count if country_name != "Malta"

di ""
di "Sample: 65 countries, excluding Malta → 64 observations"

********************************************************************************
* KEY FORMULAS FOR HYPOTHESIS TESTING
********************************************************************************
* t-statistic: t = β̂₁ / SE(β̂₁)  under H₀: β₁ = 0
*
* Critical values (two-sided, df ≈ ∞):
*   - 10% level: |t| > 1.645
*   - 5% level:  |t| > 1.96
*   - 1% level:  |t| > 2.576
*
* 95% CI: β̂₁ ± 1.96 × SE(β̂₁)
* 90% CI: β̂₁ ± 1.645 × SE(β̂₁)

********************************************************************************
* REGRESSION: GROWTH ON TRADESHARE (Excluding Malta)
********************************************************************************

di "=============================================="
di "REGRESSION: Growth on TradeShare (Excluding Malta)"
di "=============================================="

regress growth tradeshare if country_name != "Malta"

* Store results
local b0 = _b[_cons]
local b1 = _b[tradeshare]
local se0 = _se[_cons]
local se1 = _se[tradeshare]
local t1 = _b[tradeshare] / _se[tradeshare]
local df = e(df_r)
local n = e(N)
local r2 = e(r2)

* Calculate exact p-value (two-sided)
local p_value = 2 * ttail(`df', abs(`t1'))

di ""
di "=============================================="
di "SUMMARY OF RESULTS"
di "=============================================="
di ""
di "Estimated Equation: Growth = " %5.3f `b0' " + " %5.3f `b1' " × TradeShare"
di ""
di "  Slope (β̂₁):        " %10.4f `b1'
di "  Std. Error:        " %10.4f `se1'
di "  t-statistic:       " %10.4f `t1'
di "  p-value:           " %10.4f `p_value'
di "  Degrees of freedom:" %10.0f `df'
di "  R²:                " %10.4f `r2'
di "  Sample size:       " %10.0f `n'

********************************************************************************
* (a) IS THE SLOPE STATISTICALLY SIGNIFICANT?
********************************************************************************

di ""
di "=============================================="
di "(a) HYPOTHESIS TEST: Is the slope significant?"
di "=============================================="
di ""
di "H₀: β₁ = 0  (Trade share has no effect on growth)"
di "H₁: β₁ ≠ 0  (Trade share has an effect on growth)"
di ""
di "t-statistic = β̂₁ / SE(β̂₁) = " %5.3f `b1' " / " %5.3f `se1' " = " %5.2f `t1'
di ""
di "Decision Rules (two-sided test):"
di ""
di "  At 10% level: Critical value = 1.645"
di "    |t| = " %5.2f abs(`t1') " > 1.645?  " cond(abs(`t1') > 1.645, "YES → REJECT H₀", "NO → FAIL TO REJECT")
di ""
di "  At 5% level: Critical value = 1.96"
di "    |t| = " %5.2f abs(`t1') " > 1.96?   " cond(abs(`t1') > 1.96, "YES → REJECT H₀", "NO → FAIL TO REJECT")
di ""
di "  At 1% level: Critical value = 2.576"
di "    |t| = " %5.2f abs(`t1') " > 2.576?  " cond(abs(`t1') > 2.576, "YES → REJECT H₀", "NO → FAIL TO REJECT")

********************************************************************************
* (b) P-VALUE
********************************************************************************

di ""
di "=============================================="
di "(b) P-VALUE"
di "=============================================="
di ""
di "p-value = P(|t| > " %5.2f abs(`t1') ") = " %6.4f `p_value'
di ""
di "Interpretation:"
di "  The p-value is the probability of observing a t-statistic"
di "  as extreme as " %5.2f abs(`t1') " if H₀ were true."
di ""
if `p_value' < 0.01 {
    di "  Since p < 0.01, reject H₀ at 1% level"
}
else if `p_value' < 0.05 {
    di "  Since 0.01 < p < 0.05, reject H₀ at 5% but not 1% level"
}
else if `p_value' < 0.10 {
    di "  Since 0.05 < p < 0.10, reject H₀ at 10% but not 5% level"
}
else {
    di "  Since p > 0.10, fail to reject H₀ at 10% level"
}

********************************************************************************
* (c) 90% CONFIDENCE INTERVAL FOR β₁
********************************************************************************

di ""
di "=============================================="
di "(c) 90% CONFIDENCE INTERVAL FOR β₁"
di "=============================================="

* For 90% CI, use critical value 1.645 (for large samples)
* For exact calculation with df = 62, use t-distribution
local t_crit_90 = invttail(`df', 0.05)

* Calculate 90% CI
local ci90_lo = `b1' - `t_crit_90' * `se1'
local ci90_hi = `b1' + `t_crit_90' * `se1'

di ""
di "Formula: β̂₁ ± t_{α/2,df} × SE(β̂₁)"
di ""
di "  β̂₁ = " %6.4f `b1'
di "  SE(β̂₁) = " %6.4f `se1'
di "  t_{0.05, " %2.0f `df' "} = " %6.4f `t_crit_90'
di ""
di "90% CI: " %6.4f `b1' " ± " %6.4f `t_crit_90' " × " %6.4f `se1'
di "      = " %6.4f `b1' " ± " %6.4f `t_crit_90' * `se1'
di ""
di "90% Confidence Interval: (" %6.4f `ci90_lo' ", " %6.4f `ci90_hi' ")"

* Verify with Stata's built-in 90% CI
di ""
di "Verification using Stata's level(90) option:"
regress growth tradeshare if country_name != "Malta", level(90)

********************************************************************************
* ALSO SHOW 95% CI FOR COMPARISON
********************************************************************************

di ""
di "=============================================="
di "COMPARISON: 90% vs 95% Confidence Intervals"
di "=============================================="

local t_crit_95 = invttail(`df', 0.025)
local ci95_lo = `b1' - `t_crit_95' * `se1'
local ci95_hi = `b1' + `t_crit_95' * `se1'

di ""
di "  90% CI: (" %6.4f `ci90_lo' ", " %6.4f `ci90_hi' ") — Width: " %6.4f `ci90_hi' - `ci90_lo'
di "  95% CI: (" %6.4f `ci95_lo' ", " %6.4f `ci95_hi' ") — Width: " %6.4f `ci95_hi' - `ci95_lo'
di ""
di "Note: 90% CI is narrower but provides less confidence"

********************************************************************************
* SUMMARY
********************************************************************************

di ""
di "=============================================="
di "SUMMARY: E5.2 Results"
di "=============================================="
di ""
di "Regression: Growth = " %5.2f `b0' " + " %5.2f `b1' " × TradeShare"
di "            (excl. Malta, n = " `n' ")"
di ""
di "(a) Statistical Significance:"
di "    t = " %5.2f `t1' ", which is significant at 10% but NOT at 5% or 1%"
di ""
di "(b) p-value = " %6.4f `p_value'
di ""
di "(c) 90% CI for β₁: (" %5.3f `ci90_lo' ", " %5.3f `ci90_hi' ")"
di ""
di "Interpretation: There is WEAK evidence that trade share affects growth."
di "  - The effect is marginally significant (p ≈ 0.094)"
di "  - The 90% CI excludes zero, but the 95% CI includes zero"
di "  - Compare to regression WITH Malta: slope = 2.31, t = 2.98, p = 0.004"
di "  - Removing Malta substantially weakens the evidence for trade-growth link"

********************************************************************************
* END OF E5.2
********************************************************************************