################################################# ### Comments to solution for OASI Problem 1.3 ### ### Frank Miller 2020-10-13 ### ################################################# # The function g to be maximised and gradient # The data can be options of the function like here or just defined as global variables # To have it as options allows to call the function also for a part of the data (one observation or mini-batch) g <- function(b,x,y) { # to ensure that h1<1, we add a little 1e-9; otherwise log(1-h1) might crash h1 <- 1/(1+exp(-b[1]-b[2]*x)+1e-9) sum(log(h1)*y + log(1-h1)*(1-y)) } gradient <- function(b,x,y) { h1 <- 1/(1+exp(-b[1]-b[2]*x)) dg1 <- sum((y - h1)) dg2 <- sum((y - h1)*x) c(dg1,dg2) } # Data xobs <- c(0,0,0, 0.1,0.1, 0.3,0.3, 0.9,0.9,0.9) yobs <- c(0,0,1, 0, 1, 1, 1, 0, 1, 1) # call functions e.g. with g(c(-0.2,1),xobs,yobs) or gradient(c(-0.2,1),xobs,yobs) where c(-0.2,1) is the point of evaluation