Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 2x 2x 2x 2x 8x 1x 1x 1x 5x 5x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x | /**
* Quantile helpers for working with TimesFM forecast output.
*
* Provides ergonomic access to per-series quantile forecasts
* without manual Float32Array[][] indexing.
*/
import { QUANTILE_INDICES, type ForecastOutput } from '../types';
/**
* Confidence level → quantile index mapping.
*
* TimesFM 2.5 provides 9 quantiles (q10 through q90). The widest
* prediction interval available is Q10–Q90, which provides **≥ 80%**
* coverage. Only 0.8 (80%) is supported because the model does not
* output finer-grained percentiles.
*/
const CI_MAP: Record<number, { lower: number; upper: number }> = {
0.8: { lower: QUANTILE_INDICES.Q10, upper: QUANTILE_INDICES.Q90 },
};
/**
* Extract a specific quantile forecast for a single series.
*
* Zero-copy — returns a direct reference to the underlying Float32Array.
*
* @param output Forecast output from `model.forecast()`.
* @param seriesIndex Index of the series (0-based).
* @param quantileIndex Quantile index (use QUANTILE_INDICES constants).
*
* @example
* ```typescript
* const q10 = getQuantile(output, 0, QUANTILE_INDICES.Q10);
* ```
*/
export function getQuantile(
output: ForecastOutput,
seriesIndex: number,
quantileIndex: number,
): Float32Array {
if (seriesIndex < 0 || seriesIndex >= output.pointForecast.length) {
throw new RangeError(
`seriesIndex ${seriesIndex} out of range [0, ${output.pointForecast.length})`,
);
}
if (quantileIndex < 0 || quantileIndex >= output.quantileForecast[seriesIndex]!.length) {
const max = output.quantileForecast[seriesIndex]!.length;
throw new RangeError(`quantileIndex ${quantileIndex} out of range [0, ${max})`);
}
return output.quantileForecast[seriesIndex]![quantileIndex]!;
}
/**
* Get a prediction interval for a single series.
*
* Returns the lower and upper bounds of the prediction interval
* at the given confidence level. Only 80% CI is available from
* TimesFM's 10-quantile output, which maps to Q10–Q90.
*
* @param output Forecast output from `model.forecast()`.
* @param seriesIndex Index of the series (0-based).
* @param confidence Confidence level — only 0.8 is supported.
*
* @example
* ```typescript
* const { lower, upper } = getPredictionInterval(output, 0, 0.8);
* ```
*/
export function getPredictionInterval(
output: ForecastOutput,
seriesIndex: number,
confidence: 0.8 = 0.8,
): { lower: Float32Array; upper: Float32Array } {
const mapping = CI_MAP[confidence];
if (!mapping) {
throw new RangeError(`Unsupported confidence: ${confidence}. Currently only 0.8 is supported.`);
}
return {
lower: getQuantile(output, seriesIndex, mapping.lower),
upper: getQuantile(output, seriesIndex, mapping.upper),
};
}
|