Created
January 24, 2017 22:51
-
-
Save mrecos/251bbc07e3dfef24ef4a4fd41721674c to your computer and use it in GitHub Desktop.
Analytical solution to Logistic Kernel Ridge Regression problem, optimized to reduce alpha to within argument 'tol'. Arguments: K = Gram or kernel matrix, y = response, lambda = regularizing coefficient, maxiter = maximum number of iterations, tol = tolerance for reducing alpha parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KRR_logit_optim <- function(K, y, lambda, maxiter = 100, tol = 0.01){ | |
# LOGISTIC - optimize alpha | |
# example: KRR_logit_optim(K, y, 0.1, 100, 0.01) | |
N = nrow(K) | |
alpha = rep(1/N, N) # initialize alpha value | |
iter = 1 | |
while (TRUE) { | |
Kalpha = as.vector(K %*% alpha) | |
spec = 1 + exp(-Kalpha) | |
pi = 1 / spec | |
diagW = pi * (1 - pi) | |
e = (presence - pi) / diagW | |
q = Kalpha + e | |
theSol = try(solve(K + lambda * Diagonal(x=1/diagW), q)) | |
if (class(theSol) == "try-error") { | |
break | |
} | |
alphan = as.vector(theSol) | |
if (any(is.nan(alphan)) || all(abs(alphan - alpha) <= tol)) { | |
break | |
} | |
else if (iter > maxiter) { | |
cat("klogreg:maxiter!") | |
break | |
} | |
else { | |
alpha = alphan | |
iter = iter + 1 | |
} | |
} | |
log_pred <- 1 / (1 + exp(-as.vector(K %*% theSol))) | |
return(list(pred = log_pred, alphas = theSol)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment