logistic {CVXR}R Documentation

Logistic Function

Description

The elementwise logistic function, \log(1 + e^x). This is a special case of log(sum(exp)) that evaluates to a vector rather than to a scalar, which is useful for logistic regression.

Usage

logistic(x)

Arguments

x

An Expression, vector, or matrix.

Value

An Expression representing the logistic function evaluated at the input.

Examples

set.seed(92)
n <- 20
m <- 1000
sigma <- 45

beta_true <- stats::rnorm(n)
idxs <- sample(n, size = 0.8*n, replace = FALSE)
beta_true[idxs] <- 0
X <- matrix(stats::rnorm(m*n, 0, 5), nrow = m, ncol = n)
y <- sign(X %*% beta_true + stats::rnorm(m, 0, sigma))

beta <- Variable(n)
X_sign <- apply(X, 2, function(x) { ifelse(y <= 0, -1, 1) * x })
obj <- -sum(logistic(-X[y <= 0,] %*% beta)) - sum(logistic(X[y == 1,] %*% beta))
prob <- Problem(Maximize(obj))
result <- solve(prob)

log_odds <- result$getValue(X %*% beta)
beta_res <- result$getValue(beta)
y_probs <- 1/(1 + exp(-X %*% beta_res))
log(y_probs/(1 - y_probs))

[Package CVXR version 1.0-12 Index]