if(.Platform$OS.type=="windows") invisible(Sys.setlocale("LC_CTYPE","English_United States.utf8"))
C<-read.csv("math15-sources/korea-2014-2024-gdp-linear-projection.csv",check.names=FALSE);u<-C$centered_year_u;y<-C$reported_GDPpc_constant2015USD_per_person;a<-mean(y);b<-sum(u*y)/sum(u^2);anchor<-y[6]
P<-read.csv("math10-sources/power-2006-12-17.csv",check.names=FALSE);m1<-mean(P$Sub_metering_1);m2<-mean(P$Sub_metering_2);B<-3;p<-(B+m1-m2)/2;q<-B-p;lp<-(m1+m2-B)/2;floor<-mean((P$Sub_metering_1-m1)^2+(P$Sub_metering_2-m2)^2)/2
A<-read.csv("math16-sources/apple-sales-two-year-gram-input.csv",check.names=FALSE);x<-as.numeric(A$reported_FY2023_net_sales_million_USD);z<-as.numeric(A$reported_FY2024_net_sales_million_USD);xx<-sum(x*x);xz<-sum(x*z);alpha<-xz/xx;cap<-1.01
results<-c(country_n=length(y),country_a0=a,country_b=b,country_anchor=anchor,country_lambda=a-anchor,country_loss=mean((anchor+b*u-y)^2)/2,household_n=nrow(P),household_mu1=m1,household_mu2=m2,household_threshold=m2-m1,household_B3_a=p,household_B3_b=q,household_B3_lambda=lp,household_B3_loss=floor+((p-m1)^2+(q-m2)^2)/2,company_alpha0=alpha,company_cap=cap,company_nu_upper=(xz-xx*cap)/length(x),company_loss=mean((cap*x-z)^2)/2)
stopifnot(abs(p-m1+lp)<1e-12,abs(q-m2+lp)<1e-12)
write.csv(data.frame(metric=names(results),value=unname(results)),"math20-r-results.csv",row.names=FALSE)
cat("MATH-20: R constrained descriptive fits and multipliers reproduced.\n")
