∇ProvenDecisions
Ctrl K

Logistic regression: a convex optimization example, solved and checked

The logistic regression is a classic convex optimization problem. Here it is solved by a real solver (CLARABEL) and checked: every constraint, and the optimality certificate. Change the numbers below and solve your own.

Answer. Assumed: the negative log-likelihood is the sum over points of log(1+exp(-z)) when y=1 and log(1+exp(z)) when y=0, with z = w1*x1 + w2*x2; points 1 and 4 share the same term log(1+exp(-w1)), and points 2 and 5 share log(1+exp(w2)), so each appears twice. The smallest objective value is 2.66615, at w1 = 0.936689, w2 = -0.419661. Optimal, numerically checked.

Solve your own version free, no account needed

The problem

Fit P(y = 1) = 1/(1 + exp(-(w1 x1 + w2 x2))) (no intercept) to data (x1, x2, y): (1, 0, 1), (0, 1, 0), (1, 1, 1), (-1, 0, 0), (0, -1, 1), minimizing the negative log-likelihood plus 0.5 (w1^2 + w2^2).

How the model was built

The objective as written from the problem, before the numbers are multiplied out:

Assumptions:

The model

Each line has a plain-language note on the right.

make as small as possible: regularized negative log-likelihood (nats) of the 5 data points, plus the ridge penalty 0.5*(w1^2 + w2^2)

What is being decided

There are 2 decision variables.

VariableTypeMeaning
w1real numberweight on feature x1 (log-odds per unit x1)
w2real numberweight on feature x2 (log-odds per unit x2)

Goal and limits

Minimize regularized negative log-likelihood (nats) of the 5 data points, plus the ridge penalty 0.5*(w1^2 + w2^2) subject to 0 constraints.

Problem type

Conic (convex), complexity level 4: convex nonlinear functions such as log, exp, square roots or powers. Solved with CLARABEL at the Medium solver tier.

Picture

Shaded: every point that satisfies all the constraints. Dot: the optimum. Dashed: the objective's level line through it.

(0.936689, -0.419661)-0.4-0.820.034-0.5150.468-0.210.9030.0951.3370.4w1w2

The answer at a glance

The optimal value of each decision, as the solver returned it.

w10.936689w2-0.419661

Algebraic model (low level)

Decision variables, objective and every constraint written out; this is exactly what the solver receives.

Model code (cvxpy)

The same model in cvxpy, ready to run.

import cvxpy as cp

w1 = cp.Variable(name='w1')  # weight on feature x1 (log-odds per unit x1)
w2 = cp.Variable(name='w2')  # weight on feature x2 (log-odds per unit x2)

objective = cp.Minimize(0.5*w1**2 + 0.5*w2**2 + 2*cp.log(1 + cp.exp(-w1)) + 2*cp.log(cp.exp(w2) + 1) + cp.log(cp.exp(-w1 - w2) + 1))
constraints = [
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='CLARABEL')
print(problem.status, problem.value)
print('w1 =', w1.value)
print('w2 =', w2.value)

Solve your own

This is the model as the app reads it. Change any number and solve it in the free box on the front page (no account needed).

minimize 2*log(1 + exp(-w1)) + 2*log(1 + exp(w2)) + log(1 + exp(-(w1 + w2))) + 0.5*(w1^2 + w2^2)
subject to
w1 free
w2 free
# w1 = weight on feature x1 (log-odds per unit x1)
# w2 = weight on feature x2 (log-odds per unit x2)
# objective = regularized negative log-likelihood (nats) of the 5 data points, plus the ridge penalty 0.5*(w1^2 + w2^2)
# assume: the negative log-likelihood is the sum over points of log(1+exp(-z)) when y=1 and log(1+exp(z)) when y=0, with z = w1*x1 + w2*x2
# assume: points 1 and 4 share the same term log(1+exp(-w1)), and points 2 and 5 share log(1+exp(w2)), so each appears twice

Change the numbers and solve it

Checked: the app's answer matches the reference answer in our test set, which two solvers (CLARABEL, SCS) agree on. Built 2026-11-10.

Related examples

All 50 solved examples

Try ProvenDecisions free: type, photograph or say an optimization problem; every answer is checked by a solver.

What do you want to optimize?

Type the math, describe it in words, snap a photo or say it. ProvenDecisions builds the model, solves it, checks it and walks you through it.

One constraint per line, or separated by commas. Add integer: x, y or binary: z for whole-number decisions. LaTeX works too.

Try: