```source("ols_lfn.R")
source("simulated_setup.R")

cat("Kick the tyres --\n")
atheta <- c(3,4,5)
res <- c(ols.lf1(atheta, y, X),
ols.lf1.inlogs(c(log(atheta[1]), 4, 5), y, X),
ols.lf2(atheta, y, X),
ols.lf3(atheta, y, X))

atheta <- theta.true
res <- rbind(res,
c(ols.lf1(atheta, y, X),
ols.lf1.inlogs(c(log(atheta[1]), atheta[-1]), y, X),
ols.lf2(atheta, y, X),
ols.lf3(atheta, y, X)))

atheta <- theta.ols
res <- rbind(res,
c(ols.lf1(atheta, y, X),
ols.lf1.inlogs(c(log(atheta[1]), atheta[-1]), y, X),
ols.lf2(atheta, y, X),
ols.lf3(atheta, y, X)))

measurement <- function(f) {
N <- 200
cost <- system.time(
for (i in 1:N) {
j <- f(atheta, y, X)
}
)
return(cost[1]*1000/N)                # cost per lf in milliseconds
}

# Make a row in the table for cost (in milliseconds)
cost <- numeric(4)
cost[1] <- measurement(ols.lf1)
cost[2] <- measurement(ols.lf1.inlogs)
cost[3] <- measurement(ols.lf2)
cost[4] <- measurement(ols.lf3)

res <- rbind(res, t(cost))
rownames(res) <- c("A weird theta", "True theta", "OLS theta", "Cost (ms)")
colnames(res) <- c("lf1()", "lf1() in logs", "lf2()", "lf3()")
print(res)

cat("\nDerivatives -- first let me do numerically --\n")
atheta <- c(2.1,4,6)                         # A point at which to play.
base.value <- ols.lf1(atheta, y, X)
h <- 1e-5
cat("  Derivative in sigma     -- ",
(ols.lf1(atheta+c(h,0,0), y, X)-base.value)/h, "\n")
cat("  Derivative in intercept --",
(ols.lf1(atheta+c(0,h,0), y, X)-base.value)/h, "\n")
cat("  Derivative in slope     --",
(ols.lf1(atheta+c(0,0,h), y, X)-base.value)/h, "\n")