# Simulation of the data: logitinv <- function(x) {1/(1+exp(-x))}; n <- 1000; myData <- data.frame(x1 = 2*runif(n)-1, x2 = 2*runif(n)^(1/2)-1); #param <- 3*c(-1,5); myData$y <- sign(-runif(n)+logitinv(param[1]+param[2]*(-myData$x1^2 + myData$x2))); param <- 6*c(-1,3); myData$y <- sign(-runif(n)+logitinv(param[1]+param[2]*(myData$x1^2 + myData$x2^2))); plot(myData$x1, myData$x2, col= c("blue", "red")[(myData$y+3)/2], pch=19); print(paste("proportion of label y=+1:", mean(myData$y==1))) # helper functions: entropyPurity <- function(w, s){ #in: w = total weight # s = weighted sum of labels p <- max(1e-10, min(1-1e-10, (s+w)/(2*w))); return(1 + p*log2(p) + (1-p)*log2(1-p)); } errorPurity <- function(w,s){ p <- max(1e-10, min(1-1e-10, (s+w)/(2*w))); return(1-2*min(p, 1-p)); } findBestCut <- function(y, w, purity=entropyPurity){ #in: y = list of labels, # w = weigths of the points, # purity = purity function to be optimized for the cut, #out: res$cut = index of the best cut in terms of the 0-1 valued purity function specified # res$margins = weighted sums of the labels before, and after the cut n <- length(y); wL <- w[1]; wR <- sum(w[-1]); sL <- w[1]*y[1]; sR <- sum(w[-1]*y[-1]); bestPurity <- wL + wR*purity(wR, sR); res <- list(cut = 1, margins <- c(sL, sR), visu <- array(bestPurity, n-1)); for (t in 2:(n-1)){ wL <- wL + w[t]; wR <- wR - w[t]; sL <- sL + w[t]*y[t]; sR <- sR - w[t]*y[t]; curPurity <- wL * purity(wL, sL) + wR * purity(wR, sR); res$visu[t] <- curPurity; if (curPurity > bestPurity){ bestPurity <- curPurity; res$margins <- c(sL, sR); res$cut <- t; } } return(res); } ## test: sigma1 <- order(myData$x1); x <- myData$x1[sigma1]; y <- myData$y[sigma1]; res <- findBestCut(y, array(1/n, n), entropyPurity) plot(res$visu) sigma2 <- order(myData$x2); x <- myData$x2[sigma2]; y <- myData$y[sigma2]; res <- findBestCut(y, array(1/n, n), entropyPurity) plot(res$visu) # boosting with best stump in random direction w <- array(1/n,n) T <- 1000; stumps <- array(0, c(T, 4)) # stumps[t, ] = (direction, value of cut, label of <=cut, label for >cut) epsilon <- array(0, T) alpha <- array(0, T) yh <- array(0, c(T, n)) testError <- array(0, T) sigma <- array(0, c(n, 2)); #sigma[,j] = permutation sorting x_j sx <- array(0, c(n, 2)); sy <- array(0, c(n, 2)); for(j in 1:2){ x <- if (j==1) {myData$x1} else {myData$x2;}; sigma[,j] <- order(x); sx[,j] <- x[sigma[,j]]; sy[,j] <- myData$y[sigma[,j]]; } stump.predict <- function(stump, x){ if (x[stump[1]] <= stump[2]){ pred <- stump[3]; } else{ pred <- stump[4]; } return(pred); } # main loop for(t in 1:T){ #readline("Press failure! # w <- array(1,n)/n # # T <- 10 # # coeffs <- array(0, c(T, 3)) # epsilon <- array(0, T) # alpha <- array(0, T) # yh <- array(0, c(T, n)) # testError <- array(0, T) # # for(t in 1:T){ # # #readline("Press