skills/timeseries/keras-multi-quantile-loss/SKILL.md
Single neural network outputting all quantiles simultaneously via pinball loss over a quantile vector for joint probabilistic forecasting
npx skillsauth add wenmin-wu/ds-skills timeseries-keras-multi-quantile-lossInstall 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.
Instead of training one model per quantile, a single neural network outputs all quantile predictions at once. The final Dense layer has num_quantiles outputs, and the loss function computes the pinball (quantile) loss across all quantiles jointly. This is 9x faster than training separate models and naturally produces coherent quantile orderings through shared representations.
import tensorflow as tf
from tensorflow.keras import backend as K
import numpy as np
QUANTILES = [0.005, 0.025, 0.165, 0.250, 0.500, 0.750, 0.835, 0.975, 0.995]
def multi_quantile_loss(y_true, y_pred):
q = tf.constant(np.array([QUANTILES]), dtype=tf.float32)
e = y_true - y_pred
v = tf.maximum(q * e, (q - 1) * e)
return K.mean(v)
def build_model(input_dim, num_quantiles=9):
inputs = tf.keras.Input(shape=(input_dim,))
x = tf.keras.layers.Dense(500, activation='relu')(inputs)
x = tf.keras.layers.Dropout(0.3)(x)
x = tf.keras.layers.Dense(500, activation='relu')(x)
x = tf.keras.layers.Dropout(0.3)(x)
outputs = tf.keras.layers.Dense(num_quantiles, activation='linear')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer='adam', loss=multi_quantile_loss)
return model
model = build_model(input_dim=50, num_quantiles=len(QUANTILES))
num_quantiles outputs in the final layermax(q * e, (q-1) * e) where e = y_true - y_predy_true is broadcast across the quantile dimensiondata-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