For a better understanding of inner workings and a simple road to further experimentation, I'm trying to implement a (regularized) logistic regression on R.
It is known that one way of implementing it is by solving the following problem:
$$ \arg \min_{\beta} \sum(-y_i ( x_i' \beta) + log(1 + e^{x_i' \beta})) + \lambda \|\beta\| $$
However, for any larger value of $x_i' \beta$ the exp explodes, returning an Inf value. One solution that I have found is by using Rmpfr::mpfr and adding some precision, i.e.,
x <- mpfr(x, precBits = 106)
But this adds quite an overhead performance wise. On the other hand, base implementation of glm or glmnet manages to get the solution rather quickly. How is it so? What are the possible ways of avoiding the calculation of exp(x %*% beta)? Or is this achieved only by reducing to some other implementation, i.e., in C or Fortran?
Note:
I'm trying to do this using Rsolnp::solnp Augmented Lagrangian optimizer with certain constraints on the parameters. For simple regression problems this seems OK performance wise due to the ability to add a gradient, however for logistic regression this might not be too great?
Rsolnp:::solnp(pars = beta0, fun = logistic_optimizer,
eqfun = constraint,
ineqfun = positive_constraint,
ineqLB = rep(0, length(beta0)),
ineqUB =rep(Inf, length(beta0)),
LB = rep(0, length(beta0)),
UB = rep(Inf, length(beta0))
)
It would be nice to know if there are more efficient ways to manually solve this, without reducing to known libraries such as glm or glmnet, since I want the control over logistic_optimizer expression.