Skip to content

Instantly share code, notes, and snippets.

@GabrielHoffman
Last active February 8, 2024 15:55
Show Gist options
  • Save GabrielHoffman/0ff1d65d673e76176e31c7f3242c181c to your computer and use it in GitHub Desktop.
Save GabrielHoffman/0ff1d65d673e76176e31c7f3242c181c to your computer and use it in GitHub Desktop.
Isotonic regression for calibrating predictions
# x is days, y is arbitrary scale
n = 100
x = 10 + 1:n
y = scale(x) + 10*scale(x^2) + rnorm(n)
# relationship is non-linear
# but we have stoing knowledge that it *must* be monotonic
plot(x, y)
# Goal: give a predicted score y, calibrate it so that
# it has a monotonic linear increasing map to x
# fit isotonic regression
# importantly, this does not assume a linear
# relationship between x and y
# Yet it creates a modified y values so that
# y_pred has a linear relationship with x
# and is on the same scale
# But *importantly* the mapping between y
# and calibrated y is non-linear. The only
# constraint is that it must be monotinic increasing
fit <- isoreg(y, x)
isofit <- as.stepfun(fit)
y_pred = isofit(y)
plot(x, y_pred, pch = 4)
abline(0, 1, col="red")
# See the mapping between original y
# and calibrated score
plot(y, y_pred, pch = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment