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)

plot(s$x[s$y==1, 1], s$x[s$y==1, 2], xlim=c(0,1), ylim = c(0,1), col="blue")
points(s$x[s$y==-1, 1], s$x[s$y==-1, 2], col="red")
t = seq(0,1,0.01)
lines(t, -(theta[1]*t+theta0)/theta[2], col="green")

beta <- runif(2)-1/2;
beta0 <- runif(1);
lines(t, -(beta[1]*t+beta0)/beta[2], col="black")

rho = 0.1;

K <- which((s$x %*% beta + beta0 ) * s$y < 0);
it <- 1;
while (length(K)>=1 & it<1000){
  it <- it+1;
  i <- K[1+floor(length(K)*runif(1))];
  beta <- beta + rho * s$y[i]*s$x[i,];
  beta0 <- beta0 + rho * s$y[i];
  K <- which((s$x %*% beta + beta0 ) * s$y < 0);
}
print(it);
lines(t, -(beta[1]*t+beta0)/beta[2], col="magenta")