Skip to content

Instantly share code, notes, and snippets.

@neubig
Created November 18, 2010 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neubig/704943 to your computer and use it in GitHub Desktop.
Save neubig/704943 to your computer and use it in GitHub Desktop.
A file to compute and plot LR and SVM margins
set.seed(123141)
fcount <- 20
tcount <- 1
alpha <- 3
xf <- rnorm(fcount,mean=-1, sd=0.7)
xt <- rnorm(tcount,mean=1, sd=0.7)
yf <- mat.or.vec(fcount,1)
yt <- mat.or.vec(tcount,1)
png(filename=paste("graph-",fcount,"-",tcount,".png",sep=""),width=400,height=200)
plot(xf, yf, col="red", main=paste(fcount,"False,",tcount,"True Samples"), xlab="x value", xlim=c(-2,2), pch=19, yaxt='n', asp=0.1)
points(xt, yt, col="green", pch=19)
# draw the SVM margin
svmmarg <- (min(xt)+max(xf))/2
abline(v=svmmarg,col="blue",lty=1,lwd=2)
# solve the LR problem using the newton method
yt <- yt-1
yf <- yf+1
xs <- c(xt, xf)
ys <- c(yt, yf)
count <- tcount+fcount
lrmarg <- svmmarg
lastlik <- 0;
lastfirst <- 0;
for(iter in 0:10) {
lik <- 0; first <- 0; second <- 0
for(i in 1:count) {
val <- ys[i]*(xs[i]-lrmarg)*alpha
lik <- lik - log(1+exp(val))
first <- first+ys[i]*exp(val)/(1+exp(val))
second <- second - exp(val)/((1+exp(val))^2)*alpha
}
lrmarg <- lrmarg - first/second
print(paste("Likelihood",lik,"lrmarg",lrmarg,"old",lrmarg+first/second,"first",first,"second",second,"lastliketc",(lastlik-lik)*100,"lastfirst",(lastfirst-first)*100))
lastlik <- lik;
lastfirst <- first;
}
abline(v=lrmarg,col="black",lty=2,lwd=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment