127 lines
4.4 KiB
TypeScript
127 lines
4.4 KiB
TypeScript
import { Figure, PALETTE } from '../plot';
|
||
|
||
export interface CurvePrediction {
|
||
years: number;
|
||
p: number;
|
||
low?: number;
|
||
high?: number;
|
||
}
|
||
export interface CurveModel {
|
||
/** the model's own title, with its version */
|
||
text: string;
|
||
/** the model's index date */
|
||
index: string;
|
||
predictions: CurvePrediction[];
|
||
}
|
||
|
||
const percent = (n: number) =>
|
||
(Math.round(n * 1000) / 10).toLocaleString('es-MX', { minimumFractionDigits: 1 });
|
||
const title = (m: CurveModel) => m.text.replace(/ v\d+$/, '');
|
||
const interval = (p: CurvePrediction) => p.low !== undefined && p.high !== undefined;
|
||
|
||
/** The institutional models' risk by horizon: one series per model, drawn only at the horizons it
|
||
* publishes (a model with one horizon is a point), its 95 % interval as a band where it publishes
|
||
* one at two or more horizons and as an error bar at a single one. No horizon, interval or
|
||
* probability is made up: between published horizons the line joins them and nothing else. The
|
||
* NCI reference is not here. */
|
||
export function curveFigure(models: CurveModel[], top: number): Figure {
|
||
const shown = models.filter((m) => m.predictions.length);
|
||
const years = [...new Set(shown.flatMap((m) => m.predictions.map((p) => p.years)))].sort(
|
||
(a, b) => a - b,
|
||
);
|
||
const dates = [...new Set(shown.map((m) => m.index))];
|
||
return {
|
||
data: shown.flatMap((m, i) => {
|
||
const color = PALETTE[i % PALETTE.length];
|
||
const sorted = [...m.predictions].sort((a, b) => a.years - b.years);
|
||
const x = sorted.map((p) => p.years);
|
||
const name = title(m);
|
||
const bounded = sorted.filter(interval);
|
||
const traces: object[] = [];
|
||
if (bounded.length > 1) {
|
||
// a gap in the published intervals stays a gap: nothing is drawn between them
|
||
const at = (pick: (p: CurvePrediction) => number | undefined) =>
|
||
sorted.map((p) => (interval(p) ? (pick(p) as number) * 100 : null));
|
||
traces.push(
|
||
{
|
||
x,
|
||
y: at((p) => p.high),
|
||
mode: 'lines',
|
||
line: { width: 0 },
|
||
connectgaps: false,
|
||
showlegend: false,
|
||
hoverinfo: 'skip',
|
||
legendgroup: name,
|
||
},
|
||
{
|
||
x,
|
||
y: at((p) => p.low),
|
||
mode: 'lines',
|
||
line: { width: 0 },
|
||
fill: 'tonexty',
|
||
connectgaps: false,
|
||
fillcolor: color + '33',
|
||
name: `IC 95 % ${name}`,
|
||
legendgroup: name,
|
||
hoverinfo: 'skip',
|
||
},
|
||
);
|
||
}
|
||
traces.push({
|
||
x,
|
||
y: sorted.map((p) => p.p * 100),
|
||
mode: sorted.length > 1 ? 'lines+markers' : 'markers',
|
||
name,
|
||
legendgroup: name,
|
||
line: { color },
|
||
marker: { color, size: 8 },
|
||
...(bounded.length === 1
|
||
? {
|
||
error_y: {
|
||
type: 'data',
|
||
symmetric: false,
|
||
color,
|
||
array: sorted.map((p) => (interval(p) ? (p.high! - p.p) * 100 : 0)),
|
||
arrayminus: sorted.map((p) => (interval(p) ? (p.p - p.low!) * 100 : 0)),
|
||
},
|
||
}
|
||
: {}),
|
||
hovertemplate: `${name}<br>%{x} años: %{y:.2f} %<extra></extra>`,
|
||
});
|
||
return traces;
|
||
}) as Figure['data'],
|
||
layout: {
|
||
xaxis: {
|
||
title: {
|
||
text: `Horizonte (años desde la fecha índice${dates.length === 1 ? ` (${dates[0]})` : ''})`,
|
||
},
|
||
tickmode: 'array',
|
||
tickvals: years,
|
||
ticktext: years.map(String),
|
||
},
|
||
// At least 0–5 %: a near-zero risk reads as near zero, not as a full axis.
|
||
yaxis: {
|
||
title: { text: 'Probabilidad (%)' },
|
||
range: [0, Math.max(5, top * 110)],
|
||
tickformat: '.0f',
|
||
},
|
||
},
|
||
} as Figure;
|
||
}
|
||
|
||
/** The curves in words: each model, its index date and every horizon it publishes, with its
|
||
* interval where it has one. */
|
||
export function curveText(models: CurveModel[]): string[] {
|
||
return models
|
||
.filter((m) => m.predictions.length)
|
||
.map((m) => {
|
||
const sorted = [...m.predictions].sort((a, b) => a.years - b.years);
|
||
const said = sorted.map(
|
||
(p) =>
|
||
`${p.years === 0 ? 'al índice' : `${p.years} ${p.years === 1 ? 'año' : 'años'}`}: ${percent(p.p)} %` +
|
||
(interval(p) ? ` (IC 95 %: ${percent(p.low!)}–${percent(p.high!)} %)` : ''),
|
||
);
|
||
return `${title(m)} (fecha índice ${m.index}): ${said.join('; ')}.`;
|
||
});
|
||
}
|