# Set seed
set.seed(57365)
# Parameters
N=100000;
k1=0.3
A=8000; B=1.5; C=0.15; theta=c(A,B,C)
# True mean function
k2_true=function(n) powerlaw(n,theta)
# True OHS
nx=1:N
ohs_true=nx[which.min(k1*nx + k2_true(nx)*(N-nx))]
# Values of n for which cost has been estimated
np=50 # this many points
nset=round(runif(np,1,N))
var_k2=runif(np,0.001,0.0015)
k2=rnorm(np,mean=k2_true(nset),sd=sqrt(var_k2))
# Compute OHS
res1=optimal_holdout_size_emulation(nset,k2,var_k2,N,k1)
# Error estimates
ex=error_ohs_emulation(nset,k2,var_k2,N,k1)
# Plot
plot(res1)
# Add error
abline(v=ohs_true)
abline(v=ex,col=rgb(1,0,0,alpha=0.2))
# Show justification for error
n=seq(1,N,length=1000)
mu=mu_fn(n,nset,k2,var_k2,N,k1); psi=pmax(0,psi_fn(n, nset, var_k2, N)); Z=-qnorm(0.1/2)
lines(n,mu - Z*sqrt(psi),lty=2,lwd=2)
legend("topright",
c("Err. region",expression(paste(mu(n)- "z"[alpha/2]*sqrt(psi(n))))),
pch=c(16,NA),lty=c(NA,2),lwd=c(NA,2),col=c("pink","black"),bty="n")
Run the code above in your browser using DataLab