skills/tabular/nelder-mead-threshold-optimization/SKILL.md
Use scipy Nelder-Mead simplex to optimize regression-to-ordinal thresholds maximizing quadratic weighted kappa on OOF predictions
npx skillsauth add wenmin-wu/ds-skills tabular-nelder-mead-threshold-optimizationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
When a regression model predicts continuous scores that must be discretized into ordinal classes, the rounding boundaries directly impact QWK. Instead of grid search or manual tuning, use Nelder-Mead simplex optimization to find optimal thresholds on OOF predictions. Gradient-free, fast, and handles the non-differentiable QWK objective natively.
from scipy.optimize import minimize
from sklearn.metrics import cohen_kappa_score
import numpy as np
def threshold_rounder(preds, thresholds):
return np.where(preds < thresholds[0], 0,
np.where(preds < thresholds[1], 1,
np.where(preds < thresholds[2], 2, 3)))
def neg_qwk(thresholds, y_true, oof_preds):
rounded = threshold_rounder(oof_preds, thresholds)
return -cohen_kappa_score(y_true, rounded, weights='quadratic')
result = minimize(neg_qwk, x0=[0.5, 1.5, 2.5],
args=(y_true, oof_preds), method='Nelder-Mead')
optimal_thresholds = result.x
test_preds = threshold_rounder(test_raw, optimal_thresholds)
threshold_rounder for N-1 boundaries (N classes)scipy.optimize.minimize with method='Nelder-Mead' and initial guess at class midpointsdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF