KKT and feasibility analysis
Download the notebook (executed for this page) · source
Projected-gradient infinity norm is a first-order stationarity diagnostic on the actual bounded affine groups. It is not a proof of global optimality or a second-order KKT certificate. Compare it with independent bound and group-sum violations.
from pathlib import Path
import os, sys
ROOT = next(p for p in [Path.cwd(), *Path.cwd().parents] if (p / 'pixi.toml').exists())
os.environ['TFOPT_CONFIG'] = str(ROOT / 'notebooks/data/toy.toml')
os.environ.setdefault('MPLBACKEND', 'Agg')
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
import tfopt
import numpy as np
import pandas as pd
from notebooks.tutorial import build_problem, optimize, OUTPUT
p = build_problem()
from tfopt.optimality.KKT import diagnostics
from tfopt.local.objfn.minfn import objective_
result=optimize(p)
report=diagnostics(result.x,lambda x:objective_(x,*p['args']),p['geometry'])
display(report)
assert report['feasible'] and report['finite_gradient']
for idx,target in p['geometry'].groups:
print('group',idx,'sum',result.x[idx].sum(),'target',target)
{'constraint_violation': 0.0,
'projected_gradient_inf': 2.3883981348227223e-08,
'feasible': True,
'stationary': True,
'finite_gradient': True}
group [0] sum 1.0 target 1.0
group [3] sum 1.0 target 1.0
group [6] sum 1.0 target 1.0
group [ 9 10] sum 1.0 target 1.0
group [12 13 14] sum 1.0 target 1.0
group [15] sum 1.0 target 1.0
group [16 17] sum 1.0 target 1.0
group [18 19 20] sum 1.0 target 1.0
KKT conditions
tfopt.optimality.kkt_diagnostics estimates the Lagrange multipliers of the sum-to-one constraints and the active
bounds and reports the four KKT residuals (stationarity, primal and dual feasibility, complementary slackness), each
with a tolerance and a pass/fail verdict. A run writes the same table to the workbook sheet "KKT Diagnostics".
from tfopt.optimality.kkt_diagnostics import kkt_report
kkt=kkt_report(result.x,lambda x:objective_(x,*p['args']),p['geometry'])
print('multipliers:',kkt.source,'| all conditions pass:',kkt.passed)
display(kkt.conditions)
assert kkt.conditions.set_index('condition').loc['primal feasibility','passed']
multipliers: estimated | all conditions pass: True
| condition | residual | tolerance | passed | |
|---|---|---|---|---|
| 0 | stationarity | 2.388398e-08 | 1.000000e-05 | True |
| 1 | primal feasibility | 0.000000e+00 | 1.000000e-10 | True |
| 2 | dual feasibility | -0.000000e+00 | 1.000000e-10 | True |
| 3 | complementary slackness | 0.000000e+00 | 1.000000e-08 | True |
Interpretation
Check both fit and feasibility. For biological data, identifiability, scaling and regulatory coverage need domain review. Results and figures belong only in notebooks/outputs/.