A Newton / log-barrier interior-point solver written from first principles in NumPy, then used to train hard- and soft-margin support vector machines with no off-the-shelf optimiser.
Interior-point methods sit underneath most production convex solvers, but they are usually used as black boxes. This project builds the full stack by hand: Newton's method with backtracking line search, an equality-constrained Newton solver based on the KKT system, a logarithmic barrier method, and a Phase I procedure to find a strictly feasible start. Only basic linear algebra (np.linalg.solve) is used. The same solver is then pointed at a machine-learning problem, training maximum-margin linear classifiers (SVMs) as convex quadratic programmes, which shows how the optimisation theory and the ML model fit together.
-
Hard-margin SVM trained by the in-house barrier method: converged in 9 outer iterations to
$w^\star = (0.466, 0.529)$ ,$b^\star = -0.176$ , giving a geometric margin of$r = 1/\lVert w^\star\rVert_2 = 1.42$ . The support vectors lie exactly on the margin:$\min_i y_i(w^{\star\top}x_i + b^\star) = 1$ . -
Constrained problem with Phase I: Phase I returned a strictly feasible point with
$s^\star = -0.348 < 0$ . The barrier method then converged in 8 outer iterations to$x^\star \approx (0, 1)$ with duality-gap estimate$m/t = 3.0\times10^{-7}$ and the active constraint at$f_3(x^\star) = -6.7\times10^{-8}$ . -
Newton's method: reached the unique minimiser
$x^\star = (-0.5, -0.5)$ in 3 iterations from a distant start. Equality-constrained Newton converged in 1 iteration from the minimum-norm feasible point. -
Soft-margin SVM (116 points, 119 decision variables, 232 inequality constraints): sweeping the penalty
$c$ over four orders of magnitude traces the margin/violation trade-off:
| Margin |
Misclassified | Margin violations | Total slack |
|
|---|---|---|---|---|
| 2.36 | 16 | 44 | 25.4 | |
| 1.93 | 16 | 26 | 23.8 | |
| 1.82 | 16 | 24 | 23.7 | |
| 1.71 | 15 | 24 | 23.7 | |
| 1.68 | 15 | 24 | 23.7 |
The margin shrinks monotonically as
Test objective. src/functions.py).
Newton with backtracking (src/optimizer.py). Step
Equality-constrained Newton (newton_eq). Each step solves the KKT system
checks feasibility of the initial point, and projects back onto
Barrier method with Phase I (src/barrier.py). The inner solves minimise
SVMs (examples/svm_hard_margin.py, examples/svm_soft_margin.py). The hard-margin primal
Analysis. The study also derives the SVM dual and argues that strong duality holds via Slater's condition. It gives a rank condition,
Verification. The example scripts cross-check against SciPy reference solutions (scipy.optimize.minimize). They also verify KKT stationarity
interior-point-solver/
├── src/
│ ├── functions.py # f0, gradient and Hessian (stable log-sum-exp / softmax)
│ ├── optimizer.py # backtracking line search, Newton, equality-constrained Newton (KKT)
│ ├── barrier.py # log-barrier method + Phase I feasibility solver
│ └── plotting.py # contour and iterate-path plots
└── examples/
├── newton.py # unconstrained Newton
├── newton_equality.py # equality-constrained Newton + KKT check
├── barrier_phase1.py # barrier method + Phase I
├── svm_hard_margin.py # hard-margin SVM via barrier method
└── svm_soft_margin.py # soft-margin SVM, sweep over c
Run from the repository root. Figures are written to plots/.
uv run --with numpy --with scipy --with matplotlib python examples/newton.py
uv run --with numpy --with scipy --with matplotlib python examples/newton_equality.py
uv run --with numpy --with scipy --with matplotlib python examples/barrier_phase1.py
uv run --with numpy --with scipy --with pandas --with matplotlib python examples/svm_hard_margin.py # needs data/svm_training_data.csv (not included)
uv run --with numpy --with scikit-learn --with matplotlib python examples/svm_soft_margin.pysvm_hard_margin.py expects the original training set (not redistributed) at data/svm_training_data.csv (columns x_1, x_2, y). Any 2-D labelled CSV in that format will run. svm_soft_margin.py generates its dataset deterministically (make_blobs, seed 42, plus 16 boundary points).
Python · NumPy (all optimisation: linear solves only) · SciPy (reference cross-checks only) · pandas · scikit-learn (synthetic data) · Matplotlib
Originally developed for 4M17 Practical Optimisation, Department of Engineering, University of Cambridge, under a no-off-the-shelf-solvers constraint. The unconstrained Newton routine builds on a provided baseline; the equality-constrained Newton solver, barrier method, Phase I and SVM formulations are my own.



