samplePoints <- function(n, beta, beta0){ s <- list() s$x <- matrix(runif(2*n), nrow = n); s$y <- 2*(s$x %*% beta + beta0 > 0)-1 s } n <- 50 theta <- c(0.5, -1); theta0 = 0.5; s <- samplePoints(n, theta, theta0) beta <- runif(2)-1/2; beta0 <- -sum(beta)/2; rho = 0.002; K <- which((s$x %*% beta + beta0 ) * s$y < 0); it <- 0; while (length(K)>0 & it<1000){ it <- it+1; plot(s$x[,1], s$x[,2], col=c("blue", "red")[(s$y+3)/2], pch = 19); lines(c(-1, 1), (-theta0 - theta[1]*c(-1,1))/theta[2], col = "black"); lines(c(-1, 1), (-beta0 - beta[1]*c(-1, 1))/beta[2], col="green"); points(s$x[K, 1], s$x[K, 2], pch=21, cex=2.5, lwd = 5, col="green"); if (length(K)>1) {i <- sample(K, 1)} else {i<-K;} points(s$x[i,1], s$x[i, 2], col = "magenta", pch = 20, cex=3); beta <- beta + rho * s$y[i]*s$x[i,]; beta0 <- beta0 + rho * s$y[i]; Sys.sleep(0.001); K <- which((s$x %*% beta + beta0 ) * s$y < 0); } plot(s$x[,1], s$x[,2], col=c("blue", "red")[(s$y+3)/2], pch = 19); lines(c(-1, 1), (-theta0 - theta[1]*c(-1,1))/theta[2], col = "black"); lines(c(-1, 1), (-beta0 - beta[1]*c(-1, 1))/beta[2], col="green"); points(s$x[K, 1], s$x[K, 2], pch=21, cex=2.5, lwd = 5, col="green"); cat("number of iterations :", it, "\n");