# QUESTION 1: DEVELOPMENT ACCOUNTING
dev_accounting <- function(yr) {
  pwt %>%
    filter(year == yr) %>%
    select(countrycode, country, cgdpe, cn, pop) %>%
    filter(!is.na(cgdpe), !is.na(cn), !is.na(pop),
           pop > 0, cgdpe > 0, cn > 0) %>%
    mutate(
      y    = cgdpe / pop,
      k    = cn    / pop,
      A    = y / k^alpha,
      ln_y = log(y),
      ln_A = log(A),
      alnk = alpha * log(k)
    )
}
# Q1(a)
d90 <- dev_accounting(1990)
cat("=== Q1(a): 1990 Summary Statistics ===\n")
## === Q1(a): 1990 Summary Statistics ===
cat("Number of countries:", nrow(d90), "\n\n")
## Number of countries: 180
# Income relative to the US
us_y90 <- d90$y[d90$countrycode == "USA"]
d90     <- d90 %>% mutate(y_ratio = y / us_y90)
# Percentile ranks (sort by y)
d90_sorted <- d90 %>% arrange(y)
n90        <- nrow(d90_sorted)
med90 <- d90_sorted[round(n90 * 0.50), ]
p10_90 <- d90_sorted[round(n90 * 0.10), ]
p90_90 <- d90_sorted[round(n90 * 0.90), ]
cat("Median country (50th pct):\n")
## Median country (50th pct):
cat("  Country:", med90$country, "(", med90$countrycode, ")\n")
##   Country: Panama ( PAN )
cat("  y / y_US:", round(med90$y_ratio, 4), "\n\n")
##   y / y_US: 0.168
cat("10th percentile country:\n")
## 10th percentile country:
cat("  Country:", p10_90$country, "(", p10_90$countrycode, ")\n")
##   Country: Niger ( NER )
cat("  y / y_US:", round(p10_90$y_ratio, 4), "\n\n")
##   y / y_US: 0.0295
cat("90th percentile country:\n")
## 90th percentile country:
cat("  Country:", p90_90$country, "(", p90_90$countrycode, ")\n")
##   Country: Italy ( ITA )
cat("  y / y_US:", round(p90_90$y_ratio, 4), "\n\n")
##   y / y_US: 0.6923
# Variance decomposition — Equation (1)
var_lny90  <- var(d90$ln_y)
var_lnA90  <- var(d90$ln_A)
var_alnk90 <- var(d90$alnk)
cov_Ak90   <- cov(d90$ln_A, d90$alnk)

prod_contrib90   <- var_lnA90  / var_lny90
factor_contrib90 <- var_alnk90 / var_lny90
inter_contrib90  <- 2 * cov_Ak90 / var_lny90
cat("--- Variance Decomposition, 1990 ---\n")
## --- Variance Decomposition, 1990 ---
cat("Var(ln y)               :", round(var_lny90, 4), "\n")
## Var(ln y)               : 1.3727
cat("Productivity contribution:", round(prod_contrib90, 4), "\n")
## Productivity contribution: 0.3917
cat("Factor contribution      :", round(factor_contrib90, 4), "\n")
## Factor contribution      : 0.18
cat("Interaction contribution :", round(inter_contrib90, 4), "\n")
## Interaction contribution : 0.4283
cat("Sum (should = 1)         :", round(prod_contrib90 + factor_contrib90 + inter_contrib90, 4), "\n\n")
## Sum (should = 1)         : 1
# Individual country breakdown for the 3 percentile countries
countries_of_interest <- c("NER", "PAN", "ITA")

d90 %>%
  filter(countrycode %in% countries_of_interest) %>%
  mutate(
    productivity_share = ln_A / ln_y,
    factor_share       = alnk / ln_y
  ) %>%
  select(country, countrycode, productivity_share, factor_share)
## # A tibble: 3 × 4
##   country countrycode productivity_share factor_share
##   <chr>   <chr>                    <dbl>        <dbl>
## 1 Italy   ITA                      0.622        0.378
## 2 Niger   NER                      0.594        0.406
## 3 Panama  PAN                      0.643        0.357
d19 <- dev_accounting(2019)
cat("=== Q1(b): 2019 Summary Statistics ===\n")
## === Q1(b): 2019 Summary Statistics ===
cat("Number of countries:", nrow(d19), "\n\n")
## Number of countries: 180
us_y19 <- d19$y[d19$countrycode == "USA"]
d19    <- d19 %>% mutate(y_ratio = y / us_y19)
d19_sorted <- d19 %>% arrange(y)
n19        <- nrow(d19_sorted)
med19  <- d19_sorted[round(n19 * 0.50), ]
p10_19 <- d19_sorted[round(n19 * 0.10), ]
p90_19 <- d19_sorted[round(n19 * 0.90), ]
cat("Median country (50th pct):\n")
## Median country (50th pct):
cat("  Country:", med19$country, "(", med19$countrycode, ")\n")
##   Country: Lebanon ( LBN )
cat("  y / y_US:", round(med19$y_ratio, 4), "\n\n")
##   y / y_US: 0.2309
cat("10th percentile country:\n")
## 10th percentile country:
cat("  Country:", p10_19$country, "(", p10_19$countrycode, ")\n")
##   Country: Gambia ( GMB )
cat("  y / y_US:", round(p10_19$y_ratio, 4), "\n\n")
##   y / y_US: 0.0344
cat("90th percentile country:\n")
## 90th percentile country:
cat("  Country:", p90_19$country, "(", p90_19$countrycode, ")\n")
##   Country: Austria ( AUT )
cat("  y / y_US:", round(p90_19$y_ratio, 4), "\n\n")
##   y / y_US: 0.8766
var_lny19  <- var(d19$ln_y)
var_lnA19  <- var(d19$ln_A)
var_alnk19 <- var(d19$alnk)
cov_Ak19   <- cov(d19$ln_A, d19$alnk)
prod_contrib19   <- var_lnA19  / var_lny19
factor_contrib19 <- var_alnk19 / var_lny19
inter_contrib19  <- 2 * cov_Ak19 / var_lny19
cat("--- Variance Decomposition, 2019 ---\n")
## --- Variance Decomposition, 2019 ---
cat("Var(ln y) 2019          :", round(var_lny19, 4), "\n")
## Var(ln y) 2019          : 1.4665
cat("Var(ln y) 1990          :", round(var_lny90, 4), "\n")
## Var(ln y) 1990          : 1.3727
cat("Change in Var(ln y)     :", round(var_lny19 - var_lny90, 4), "\n\n")
## Change in Var(ln y)     : 0.0938
cat("Productivity contribution:", round(prod_contrib19, 4), "\n")
## Productivity contribution: 0.4243
cat("Factor contribution      :", round(factor_contrib19, 4), "\n")
## Factor contribution      : 0.1447
cat("Interaction contribution :", round(inter_contrib19, 4), "\n")
## Interaction contribution : 0.4311
cat("Sum (should = 1)         :", round(prod_contrib19 + factor_contrib19 + inter_contrib19, 4), "\n\n")
## Sum (should = 1)         : 1
# Individual country breakdown for 2019 percentile countries
countries_of_interest_19 <- c("GMB", "LBN", "AUT")

d19 %>%
  filter(countrycode %in% countries_of_interest_19) %>%
  mutate(
    productivity_share = ln_A / ln_y,
    factor_share       = alnk / ln_y
  ) %>%
  select(country, countrycode, productivity_share, factor_share)
## # A tibble: 3 × 4
##   country countrycode productivity_share factor_share
##   <chr>   <chr>                    <dbl>        <dbl>
## 1 Austria AUT                      0.617        0.383
## 2 Gambia  GMB                      0.633        0.367
## 3 Lebanon LBN                      0.609        0.391
# QUESTION 2: GROWTH ACCOUNTING
growth_accounting <- function(ccode) {
  pwt %>%
    filter(countrycode == ccode, year %in% c(1990, 2019)) %>%
    select(countrycode, country, year, rgdpna, rnna, pop) %>%
    filter(!is.na(rgdpna), !is.na(rnna), !is.na(pop), pop > 0) %>%
    mutate(
      y = rgdpna / pop,
      k = rnna   / pop,
      A = y / k^alpha
    ) %>%
    arrange(year)
}

report_growth <- function(ccode) {
  g <- growth_accounting(ccode)
  cname <- g$country[1]
  
  y90 <- g$y[g$year == 1990]; y19 <- g$y[g$year == 2019]
  k90 <- g$k[g$year == 1990]; k19 <- g$k[g$year == 2019]
  A90 <- g$A[g$year == 1990]; A19 <- g$A[g$year == 2019]
  
  ln_y <- log(y19 / y90)                  # total log-growth in income
  ln_A <- log(A19 / A90)                  # TFP log-growth
  ln_k <- alpha * log(k19 / k90)          # capital log-growth (alpha-weighted)
  ann  <- ln_y / 29                       # annualised (1990-2019 = 29 years)
  
  tfp_contrib    <- ln_A / ln_y
  factor_contrib <- ln_k / ln_y
  
  cat("=== Growth Accounting:", cname, "(", ccode, ") ===\n")
  cat("y per capita 1990         :", round(y90, 2), "\n")
  cat("y per capita 2019         :", round(y19, 2), "\n")
  cat("Total log growth ln(y19/y90):", round(ln_y, 4),
      " (", scales::percent(exp(ln_y) - 1, accuracy = 0.01), ")\n")
  cat("Avg annualised growth     :", round(ann, 4),
      " (", scales::percent(exp(ann) - 1, accuracy = 0.01), "/yr)\n")
  cat("TFP contribution          :", round(tfp_contrib, 4), "\n")
  cat("Factor contribution       :", round(factor_contrib, 4), "\n")
  cat("Sum (should = 1)          :", round(tfp_contrib + factor_contrib, 4), "\n\n")
  
  invisible(list(y90 = y90, y19 = y19, A90 = A90, A19 = A19,
                 k90 = k90, k19 = k19, ln_y = ln_y, ln_A = ln_A, ln_k = ln_k))
}
#Q2(a)
usa <- report_growth("USA")
## === Growth Accounting: United States ( USA ) ===
## y per capita 1990         : 40010.88 
## y per capita 2019         : 62490.99 
## Total log growth ln(y19/y90): 0.4459  ( 56.18% )
## Avg annualised growth     : 0.0154  ( 1.55% /yr)
## TFP contribution          : 0.7593 
## Factor contribution       : 0.2407 
## Sum (should = 1)          : 1
#Q2(b)
chn <- report_growth("CHN")
## === Growth Accounting: China ( CHN ) ===
## y per capita 1990         : 2689.36 
## y per capita 2019         : 14347.52 
## Total log growth ln(y19/y90): 1.6743  ( 433.49% )
## Avg annualised growth     : 0.0577  ( 5.94% /yr)
## TFP contribution          : 0.5907 
## Factor contribution       : 0.4093 
## Sum (should = 1)          : 1
#Q2(c)
cat("=== Q2(c): Convergence — China vs. USA ===\n")
## === Q2(c): Convergence — China vs. USA ===
ratio90 <- chn$y90 / usa$y90
ratio19 <- chn$y19 / usa$y19

log_gap90 <- log(usa$y90 / chn$y90)
log_gap19 <- log(usa$y19 / chn$y19)

tfp_gap90  <- log(usa$A90 / chn$A90)
tfp_gap19  <- log(usa$A19 / chn$A19)

cap_gap90  <- alpha * log(usa$k90 / chn$k90)
cap_gap19  <- alpha * log(usa$k19 / chn$k19)

cat("China income / US income  1990:", round(ratio90, 4), "\n")
## China income / US income  1990: 0.0672
cat("China income / US income  2019:", round(ratio19, 4), "\n\n")
## China income / US income  2019: 0.2296
cat("Log income gap (US - CHN) 1990:", round(log_gap90, 4), "\n")
## Log income gap (US - CHN) 1990: 2.6998
cat("Log income gap (US - CHN) 2019:", round(log_gap19, 4), "\n")
## Log income gap (US - CHN) 2019: 1.4714
cat("Gap narrowed by               :", round(log_gap90 - log_gap19, 4), "\n\n")
## Gap narrowed by               : 1.2284
cat("TFP gap 1990 :", round(tfp_gap90, 4), "  |  2019:", round(tfp_gap19, 4),
    " | narrowed:", round(tfp_gap90 - tfp_gap19, 4), "\n")
## TFP gap 1990 : 1.6917   |  2019: 1.0413  | narrowed: 0.6504
cat("Cap gap 1990 :", round(cap_gap90, 4), "  |  2019:", round(cap_gap19, 4),
    " | narrowed:", round(cap_gap90 - cap_gap19, 4), "\n")
## Cap gap 1990 : 1.0082   |  2019: 0.4301  | narrowed: 0.5781
total_narrowing <- log_gap90 - log_gap19
cat("\nTFP share of convergence :",
    round((tfp_gap90 - tfp_gap19) / total_narrowing, 4), "\n")
## 
## TFP share of convergence : 0.5294
cat("Capital share of convergence:",
    round((cap_gap90 - cap_gap19) / total_narrowing, 4), "\n")
## Capital share of convergence: 0.4706

R Markdown