size <- 100;
grid <- mat.or.vec((size+1)*(size+1),2);
for (i in 0:size) {
for (j in 0:size) {
  grid[(size+1)*j+i+1,1] <- i + 0.001;
  grid[(size+1)*j+i+1,2] <- j + 0.001;
}
}
grid <- grid/(size+1.001);

mx1 <- qlogis(grid[,1]);
mx2 <- qlogis(grid[,2]);

dflt <- 1.2*(1.0-mx1) + 0.2*(mx2-0.5)^2;
edge <- (1.99<dflt)&(dflt<2.01);
edge[198] <- TRUE;
dflt <- (dflt>2.0);

train <- read.table("loans.dat",header=F,col.names=rbind("x1","x2","y"));

x1 <- train$x1;
x2 <- train$x2;
y  <- (train$y==1);

source("psopts.r");
postscript(file="true.eps");

plot(x=x1,y=x2,type='n',xlab="FICO Score",ylab="P-Index");
points(x=x1[y],y=x2[y],pch='o',col="red");
points(x=x1[!y],y=x2[!y],pch='o',col="green");
points(grid[dflt,],pch='.',col="red");
points(grid[!dflt,],pch='.',col="green");
lines(grid[edge,]);

dev.off();

test <- read.table("eval.dat",header=F,col.names=rbind("x1","x2","y"));

tx1 <- test$x1;
tx2 <- test$x2;
ty  <- (test$y==1);

mx1 <- qlogis(tx1);
mx2 <- qlogis(tx2);

dflt <- 1.2*(1.0-mx1) + 0.2*(mx2-0.5)^2;
dflt <- (dflt>2.0);

dflt <- as.numeric(dflt);

err <- abs(ty-dflt);
print(mean(err));

