∇ProvenDecisions
Ctrl K

Principal component as an optimization problem: an eigenvalue problem example, solved and checked

The principal component as an optimization problem is a classic eigenvalue problem problem. Here it is solved by a real solver (eigenvalue decomposition (NumPy)) and checked: every constraint, and the optimality certificate. Change the numbers below and solve your own.

Answer. You asked for: the unit direction (w1, w2) of greatest variance and the variance along it (the objective value). The largest objective value is 5.56155, at w1 = 0.788205, w2 = 0.615412. The constraint is satisfied (largest violation 1e-16, within the solver's tolerance). Optimal, numerically checked.

Solve your own version free, no account needed

The problem

Two features have covariance matrix [[4, 2], [2, 3]]. Find the direction (unit vector) of greatest variance and the variance along it.

How the model was built

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

The model

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

make this as large as possible
limit 1: left side must equal 1

What is being decided

There are 2 decision variables.

VariableTypeMeaning
w1real numbercomponent of the direction vector on feature 1 (unitless)
w2real numbercomponent of the direction vector on feature 2 (unitless)

Goal and limits

Maximize the objective subject to 1 constraint.

Problem type

Eigenvalue problem, complexity level 4: a quadratic form on a sphere: by the spectral theorem the optimum is an extreme eigenvalue. Solved with eigenvalue decomposition (NumPy) 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.788205, 0.615412)-0.4-0.4-0.003-0.0460.3940.3080.7910.6621.1881.015w1w2

The answer at a glance

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

w10.788205w20.615412

Which constraints hold the answer in place

Binding (no slack left): (1) w1^2 + w2^2 = 1. Changing a binding constraint changes the answer; a slack one can move a little without effect.

#ConstraintValueLimitSlackBinding
1w1^2 + w2^2 = 1110yes

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')  # component of the direction vector on feature 1 (unitless)
w2 = cp.Variable(name='w2')  # component of the direction vector on feature 2 (unitless)

objective = cp.Maximize(4*w1**2 + 4*w1*w2 + 3*w2**2)
constraints = [
    w1**2 + w2**2 == 1,
]
problem = cp.Problem(objective, constraints)
problem.solve(solver='eigenvalue')
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).

maximize 4*w1^2 + 4*w1*w2 + 3*w2^2
subject to
w1^2 + w2^2 = 1
# w1 = component of the direction vector on feature 1 (unitless)
# w2 = component of the direction vector on feature 2 (unitless)
# asked: the unit direction (w1, w2) of greatest variance and the variance along it (the objective value)

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: