


##########################
###### EXERCISE 7.1
##########################

# Implementation of the Nadaraya-Watson estimator
nw <- function(x, X, Y, h, K) {
  
  # Arguments
  # x: evaluation points
  # X: vector (size n) with the predictors
  # Y: vector (size n) with the response variable
  # h: bandwidth
  # K: kernel
  
  # INSERT CODE HERE
}


make_S <- function(x, X, Y, h, K = dnorm){
  
  # INSERT CODE HERE
  
  # Weights

  return(W)
  
}

# Generate some data to test the implementation
set.seed(1)
#n <- 200
#eps <- ?
#m <- ?
#X <- ?
#Y <- m(X) + eps

x_grid <- seq(-10, 10, l = 500)

# Bandwidth
#h <- ?

# Plot data
plot(X, Y)
rug(X, side = 1); rug(Y, side = 2)
lines(x_grid, m(x_grid), col = 1)
lines(x_grid, nw(x = x_grid, X = X, Y = Y, h = h), col = 2)
legend("top", legend = c("True regression", "Nadaraya-Watson"),lwd = 2, col = 1:2)



# What is f^{-i}?
# S_ii <- ?

LOO_y_Sii <- (Y-nw(x = X, X = X, Y = Y, h = h) )/ (1-S_ii)

#pred_LOO_y <- ?

plot((Y-pred_LOO_y))
points(LOO_y_Sii,pch=19)

# they correspond ... 

# now to choose h.. LOO with squared loss... 
# INSERT CODE HERE




# choose the h 
# h = ?

# PLOT IT.... 

plot(X, Y)
rug(X, side = 1); rug(Y, side = 2)
lines(x_grid, m(x_grid), col = 1)
lines(x_grid, nw(x = x_grid, X = X, Y = Y, h = chosen_h), col = 2)
legend("top", legend = c("True regression", "Nadaraya-Watson"),lwd = 2, col = 1:2)



##########################
###### EXERCISE 7.2
##########################



# Plot data

# set Y = f
Y=f
Y_bin_obs <-  (1 / ( 1 + exp(-f) ) >0.5 )*1  



# INSERT CODE HERE ...

#####



calculate_roc <- function(df, cost_of_fp, cost_of_fn, n=100) {
  tpr <- function(df, threshold) {
    # INSERT CODE HERE ...
  }
  fpr <- function(df, threshold) {
    # INSERT CODE HERE ...
  }
  cost <- function(df, threshold, cost_of_fp, cost_of_fn) {
    # INSERT CODE HERE ...
  }
  roc <- data.frame(threshold = seq(0,1,length.out=n), tpr=NA, fpr=NA)
  roc$tpr <- sapply(roc$threshold, function(th) tpr(df, th))
  roc$fpr <- sapply(roc$threshold, function(th) fpr(df, th))
  roc$cost <- sapply(roc$threshold, function(th) cost(df, th, cost_of_fp, cost_of_fn))
  return(roc)
}




plot_roc <- function(roc, threshold, cost_of_fp, cost_of_fn) {
  library(gridExtra)
  library(ggplot2)
  library(grid)
  
  norm_vec <- function(v) (v - min(v))/diff(range(v))
  
  idx_threshold = which.min(abs(roc$threshold-threshold))
  
  col_ramp <- colorRampPalette(c("green","orange","red","black"))(100)
  col_by_cost <- col_ramp[ceiling(norm_vec(roc$cost)*99)+1]
  p_roc <- ggplot(roc, aes(fpr,tpr)) + 
    geom_line(color=rgb(0,0,1,alpha=0.3)) +
    geom_point(color=col_by_cost, size=4, alpha=0.5) +
    coord_fixed() +
    geom_line(aes(threshold,threshold), color=rgb(0,0,1,alpha=0.5)) +
    labs(title = sprintf("ROC")) + xlab("FPR") + ylab("TPR") +
    geom_hline(yintercept=roc[idx_threshold,"tpr"], alpha=0.5, linetype="dashed") +
    geom_vline(xintercept=roc[idx_threshold,"fpr"], alpha=0.5, linetype="dashed")
  
  p_cost <- ggplot(roc, aes(threshold, cost)) +
    geom_line(color=rgb(0,0,1,alpha=0.3)) +
    geom_point(color=col_by_cost, size=4, alpha=0.5) +
    labs(title = sprintf("cost function")) +
    geom_vline(xintercept=threshold, alpha=0.5, linetype="dashed")
  
  sub_title <- sprintf("threshold at %.2f - cost of FP = %d, cost of FN = %d", threshold, cost_of_fp, cost_of_fn)
  
  grid.arrange(p_roc, p_cost, ncol=2, sub=textGrob(sub_title, gp=gpar(cex=1), just="bottom"))
}




# equal fp and fn cost
cost_of_fp <- 1
cost_of_fn <- 1
# INSERT CODE HERE ...

# unequal fp and fn cost

cost_of_fp <- 4
cost_of_fn <- 1
# INSERT CODE HERE ...

# Plot just the ROC curve
# INSERT CODE HERE ...


library(pROC)
# INSERT CODE HERE ...




######
### BONUS 
######



# now also vary bandwidth.... ?


bands <- seq(0.002,0.2, length.out=20)

# INSERT CODE HERE ...





       