Skip to main content
Open In Colab This example fits a degree-9 polynomial to a small sample of real MISO grid load by stochastic gradient descent (SGD) on the regularized empirical risk, the same ridge objective solved in closed form on the linear regression page. You reach the solution iteratively, and you determine the regularization strength λ\lambda properly: an outer Optuna hyperparameter search wrapped around the inner SGD fit selects the λ\lambda^\ast that minimizes held-out error, tuned on this data rather than borrowed.

The dataset

Read the daily grid-load cycle as your regression target: the input xx is normalized time across one day and the output yy is the system-wide load. You fetch one real day of MISO load from the gridstatus.io API, standardize it to zero mean and unit variance, and then sample just ten readings as the training set, deliberately small so a degree-9 polynomial can overfit it, the same regime the closed-form page studied. The remaining readings of the day are held out to score the regularization strength λ\lambda.
MISO is the Midcontinent Independent System Operator, the regional grid operator that runs the wholesale electricity market and balances generation against demand across 15 US states and the Canadian province of Manitoba. The daily rise-and-fall you are modeling is the real load pattern published on the gridstatus.io MISO load dataset; the figure below is one real day of it, pulled from the miso_load dataset via the gridstatus API.MISO system load versus time for 2026-06-30 from gridstatus.io, rising from an overnight low near 84 GW to an afternoon peak near 124 GWReal MISO system load on 2026-06-30, five-minute data from the gridstatus.io miso_load dataset.Getting this prediction wrong is not academic. Operators schedule generation ahead of time against a load forecast, and the grid must match supply to demand second by second. Underpredict and too little generation is online to meet demand, forcing emergency purchases, frequency drops, and in the worst case load shedding (rolling blackouts); overpredict and expensive units are committed and paid for nothing. A model that generalizes poorly, one that chases the noise instead of the true demand curve, feeds a bad forecast straight into grid stability.
Output from cell 3

Standardized polynomial features

The model is a degree-9 polynomial, g(x;θ)=yˉ+k=19θkϕk(x)g(x;\boldsymbol{\theta}) = \bar{y} + \sum_{k=1}^{9}\theta_k\,\phi_k(x). Raw monomials x,x2,,x9x, x^2, \dots, x^9 on [0,1][0,1] span many orders of magnitude, which makes the gradient steps lopsided and the penalty λθ2\lambda\lVert\boldsymbol{\theta}\rVert^2 act unevenly across coordinates. Standardizing each feature to zero mean and unit variance puts every coordinate on the same footing, so a single learning rate and a single λ\lambda are meaningful and the value of λ\lambda^\ast transfers directly from the closed-form page, which uses the same standardized features. The intercept is absorbed by centering the targets at yˉ\bar{y}.

The regularized objective and the SGD update

SGD minimizes the same ridge objective the closed-form page solves exactly, the sum of squared residuals plus an 2\ell_2 penalty, J(θ)=i=1n(ϕiθy~i)2+λθ2,y~i=yiyˉ.J(\boldsymbol{\theta}) = \sum_{i=1}^{n}\big(\boldsymbol{\phi}_i^{\top}\boldsymbol{\theta} - \tilde{y}_i\big)^2 + \lambda\lVert\boldsymbol{\theta}\rVert^2, \qquad \tilde{y}_i = y_i - \bar{y}. Its minimizer satisfies the normal equations (ΦΦ+λI)θ=Φy~(\boldsymbol{\Phi}^{\top}\boldsymbol{\Phi} + \lambda\mathbf{I})\boldsymbol{\theta} = \boldsymbol{\Phi}^{\top}\tilde{\mathbf{y}}, so using this convention makes λ\lambda here identical to the closed-form λ\lambda^\ast. Each SGD step draws a mini-batch B\mathcal{B} of size BB and follows an unbiased estimate of the full gradient, scaling the batch sum by n/Bn/B: θθη(2nBΦB(ΦBθy~B)+2λθ).\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta\Big(\tfrac{2n}{B}\,\boldsymbol{\Phi}_{\mathcal{B}}^{\top}(\boldsymbol{\Phi}_{\mathcal{B}}\boldsymbol{\theta} - \tilde{\mathbf{y}}_{\mathcal{B}}) + 2\lambda\boldsymbol{\theta}\Big).
The regularization strength λ\lambda is a hyperparameter: it cannot be read off the training loss, which always prefers λ0\lambda \to 0 because less shrinkage fits the ten points more tightly. It has to be chosen by held-out performance, and that takes two nested loops.
  • The inner loop is a full SGD fit of the degree-9 model at a fixed λ\lambda; it returns the validation error of the trained weights.
  • The outer loop is the hyperparameter search. Optuna proposes a candidate λ\lambda over a wide logarithmic range, reads back the validation error from the inner fit, and uses it to propose the next candidate, converging on the λ\lambda^\ast that minimizes held-out error.
Nothing is borrowed here: λ\lambda^\ast is determined from this grid-load sample.
Output from cell 7

The selected fit

With λ\lambda^\ast chosen by the search, fit the degree-9 model at that value and compare the iterative SGD solution against the closed-form ridge solution at the same λ\lambda^\ast. They should coincide: SGD is just an iterative route to the same regularized optimum.
Output from cell 9

Takeaways

  • Choosing λ\lambda is model selection by nested search: an outer Optuna loop proposes λ\lambda over a wide logarithmic range, and an inner SGD loop trains the degree-9 model at each candidate and reports its held-out error. The training loss alone cannot choose λ\lambda, it always prefers less shrinkage.
  • SGD minimizes the regularized empirical risk: the penalty enters the gradient as 2λθ2\lambda\boldsymbol{\theta}, shrinking the weights every step and curbing the degree-9 overfitting. At the selected λ\lambda^\ast the iterative SGD fit and the direct closed-form solve reach the same regularized optimum.
  • Standardizing both the features and the load makes a single λ\lambda meaningful across every coordinate, so the outer search operates on a well-conditioned problem. Tuned from scratch on this real grid-load sample, the search settles near 4×1034\times10^{-3}, the same order as the 3.04×1033.04\times10^{-3} the closed-form page found on a unit-variance sinusoid, an independent confirmation rather than a borrowed constant.
Key references: (Keskar et al., 2016; Bottou et al., 2016; Andrychowicz et al., 2016)

References

  • Andrychowicz, M., Denil, M., Gomez, S., Hoffman, M., Pfau, D., et al. (2016). Learning to learn by gradient descent by gradient descent.
  • Bottou, L., Curtis, F., Nocedal, J. (2016). Optimization Methods for Large-Scale Machine Learning.
  • Keskar, N., Mudigere, D., Nocedal, J., Smelyanskiy, M., Tang, P. (2016). On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima.