assignment-user-matcher 1.16.0 → 1.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -20
- package/dist/escalation/policy.d.ts +11 -0
- package/dist/escalation/policy.d.ts.map +1 -1
- package/dist/escalation/policy.js +22 -1
- package/dist/forecasting/backtest.d.ts +52 -0
- package/dist/forecasting/backtest.d.ts.map +1 -0
- package/dist/forecasting/backtest.js +195 -0
- package/dist/forecasting/calendar.d.ts +44 -0
- package/dist/forecasting/calendar.d.ts.map +1 -0
- package/dist/forecasting/calendar.js +85 -0
- package/dist/forecasting/cover.d.ts +69 -0
- package/dist/forecasting/cover.d.ts.map +1 -0
- package/dist/forecasting/cover.js +103 -0
- package/dist/forecasting/days.d.ts +17 -0
- package/dist/forecasting/days.d.ts.map +1 -0
- package/dist/forecasting/days.js +35 -0
- package/dist/forecasting/distributions.d.ts +31 -0
- package/dist/forecasting/distributions.d.ts.map +1 -0
- package/dist/forecasting/distributions.js +154 -0
- package/dist/forecasting/effects.d.ts +88 -0
- package/dist/forecasting/effects.d.ts.map +1 -0
- package/dist/forecasting/effects.js +303 -0
- package/dist/forecasting/erlang.d.ts +31 -0
- package/dist/forecasting/erlang.d.ts.map +1 -0
- package/dist/forecasting/erlang.js +86 -0
- package/dist/forecasting/forecast.d.ts +7 -0
- package/dist/forecasting/forecast.d.ts.map +1 -0
- package/dist/forecasting/forecast.js +221 -0
- package/dist/forecasting/index.d.ts +18 -0
- package/dist/forecasting/index.d.ts.map +1 -0
- package/dist/forecasting/index.js +54 -0
- package/dist/forecasting/model.d.ts +145 -0
- package/dist/forecasting/model.d.ts.map +1 -0
- package/dist/forecasting/model.js +583 -0
- package/dist/forecasting/nowcast.d.ts +26 -0
- package/dist/forecasting/nowcast.d.ts.map +1 -0
- package/dist/forecasting/nowcast.js +92 -0
- package/dist/forecasting/simulation.d.ts +91 -0
- package/dist/forecasting/simulation.d.ts.map +1 -0
- package/dist/forecasting/simulation.js +478 -0
- package/dist/forecasting/spikes.d.ts +6 -0
- package/dist/forecasting/spikes.d.ts.map +1 -0
- package/dist/forecasting/spikes.js +73 -0
- package/dist/forecasting/staffing.d.ts +64 -0
- package/dist/forecasting/staffing.d.ts.map +1 -0
- package/dist/forecasting/staffing.js +765 -0
- package/dist/forecasting/stats.d.ts +11 -0
- package/dist/forecasting/stats.d.ts.map +1 -0
- package/dist/forecasting/stats.js +45 -0
- package/dist/forecasting/types.d.ts +299 -0
- package/dist/forecasting/types.d.ts.map +1 -0
- package/dist/forecasting/types.js +8 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/learning/auto-weights.d.ts +1 -5
- package/dist/learning/auto-weights.d.ts.map +1 -1
- package/dist/learning/auto-weights.js +0 -3
- package/dist/lua/book-slot.lua +84 -0
- package/dist/lua/claim-queued.lua +47 -0
- package/dist/managers/DecisionTraceManager.d.ts +15 -0
- package/dist/managers/DecisionTraceManager.d.ts.map +1 -1
- package/dist/managers/DecisionTraceManager.js +117 -11
- package/dist/managers/LearningManager.d.ts +12 -0
- package/dist/managers/LearningManager.d.ts.map +1 -1
- package/dist/managers/LearningManager.js +21 -7
- package/dist/managers/ReliabilityManager.d.ts +1 -2
- package/dist/managers/ReliabilityManager.d.ts.map +1 -1
- package/dist/managers/ReliabilityManager.js +1 -1
- package/dist/managers/TelemetryManager.d.ts.map +1 -1
- package/dist/managers/TelemetryManager.js +9 -3
- package/dist/managers/WorkflowManager.d.ts.map +1 -1
- package/dist/managers/WorkflowManager.js +3 -4
- package/dist/matcher.class.d.ts +319 -4
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +1428 -102
- package/dist/queries/lane-stats.d.ts +51 -0
- package/dist/queries/lane-stats.d.ts.map +1 -0
- package/dist/queries/lane-stats.js +78 -0
- package/dist/schedule/recurrence.d.ts +11 -0
- package/dist/schedule/recurrence.d.ts.map +1 -1
- package/dist/schedule/recurrence.js +19 -2
- package/dist/schedule/slot.d.ts +60 -0
- package/dist/schedule/slot.d.ts.map +1 -0
- package/dist/schedule/slot.js +86 -0
- package/dist/scheduling/constraints/availability.d.ts +2 -14
- package/dist/scheduling/constraints/availability.d.ts.map +1 -1
- package/dist/scheduling/constraints/availability.js +14 -15
- package/dist/scheduling/constraints/consecutive.d.ts +10 -0
- package/dist/scheduling/constraints/consecutive.d.ts.map +1 -1
- package/dist/scheduling/constraints/consecutive.js +64 -1
- package/dist/scheduling/constraints/contract.d.ts.map +1 -1
- package/dist/scheduling/constraints/contract.js +12 -3
- package/dist/scheduling/constraints/employee-scope.d.ts.map +1 -1
- package/dist/scheduling/constraints/employee-scope.js +10 -2
- package/dist/scheduling/constraints/group-composition.js +2 -2
- package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -1
- package/dist/scheduling/constraints/min-staffing.js +51 -8
- package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -1
- package/dist/scheduling/constraints/rolling-hours.js +29 -4
- package/dist/scheduling/constraints/support.d.ts +4 -8
- package/dist/scheduling/constraints/support.d.ts.map +1 -1
- package/dist/scheduling/constraints/support.js +23 -2
- package/dist/scheduling/constraints/weekly-rest.d.ts +28 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -1
- package/dist/scheduling/constraints/weekly-rest.js +108 -21
- package/dist/scheduling/engine/construction.d.ts.map +1 -1
- package/dist/scheduling/engine/construction.js +56 -7
- package/dist/scheduling/engine/objective.d.ts +0 -1
- package/dist/scheduling/engine/objective.d.ts.map +1 -1
- package/dist/scheduling/engine/objective.js +65 -6
- package/dist/scheduling/engine/search.d.ts +4 -0
- package/dist/scheduling/engine/search.d.ts.map +1 -1
- package/dist/scheduling/engine/search.js +2 -1
- package/dist/scheduling/engine/state.d.ts +14 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -1
- package/dist/scheduling/engine/state.js +7 -0
- package/dist/scheduling/engine/timeline.d.ts +23 -1
- package/dist/scheduling/engine/timeline.d.ts.map +1 -1
- package/dist/scheduling/engine/timeline.js +85 -27
- package/dist/scheduling/engine/verdicts.d.ts +40 -1
- package/dist/scheduling/engine/verdicts.d.ts.map +1 -1
- package/dist/scheduling/engine/verdicts.js +177 -17
- package/dist/scheduling/index.d.ts +3 -2
- package/dist/scheduling/index.d.ts.map +1 -1
- package/dist/scheduling/index.js +7 -1
- package/dist/scheduling/model.d.ts +8 -4
- package/dist/scheduling/model.d.ts.map +1 -1
- package/dist/scheduling/model.js +239 -23
- package/dist/scheduling/operations.d.ts +174 -2
- package/dist/scheduling/operations.d.ts.map +1 -1
- package/dist/scheduling/operations.js +558 -21
- package/dist/scheduling/preference-report.d.ts +15 -0
- package/dist/scheduling/preference-report.d.ts.map +1 -0
- package/dist/scheduling/preference-report.js +111 -0
- package/dist/scheduling/preferences.d.ts +30 -0
- package/dist/scheduling/preferences.d.ts.map +1 -0
- package/dist/scheduling/preferences.js +85 -0
- package/dist/scheduling/resilience.d.ts +46 -0
- package/dist/scheduling/resilience.d.ts.map +1 -0
- package/dist/scheduling/resilience.js +45 -0
- package/dist/scheduling/scheduler.class.d.ts +12 -1
- package/dist/scheduling/scheduler.class.d.ts.map +1 -1
- package/dist/scheduling/scheduler.class.js +28 -4
- package/dist/scheduling/sites.d.ts +1 -1
- package/dist/scheduling/sites.d.ts.map +1 -1
- package/dist/scheduling/sites.js +34 -1
- package/dist/scheduling/time.d.ts +28 -6
- package/dist/scheduling/time.d.ts.map +1 -1
- package/dist/scheduling/time.js +59 -17
- package/dist/scheduling/types.d.ts +247 -7
- package/dist/scheduling/types.d.ts.map +1 -1
- package/dist/scoring/fair-arbitration.d.ts.map +1 -1
- package/dist/scoring/fair-arbitration.js +2 -1
- package/dist/tracing/decision-trace.d.ts +5 -0
- package/dist/tracing/decision-trace.d.ts.map +1 -1
- package/dist/tracing/decision-trace.js +3 -0
- package/dist/types/matcher.d.ts +369 -2
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/updates/assignment-update.d.ts +93 -0
- package/dist/updates/assignment-update.d.ts.map +1 -0
- package/dist/updates/assignment-update.js +91 -0
- package/dist/utils/keys.d.ts +10 -0
- package/dist/utils/keys.d.ts.map +1 -1
- package/dist/utils/keys.js +43 -0
- package/package.json +25 -3
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spreadStrata = exports.gammaMeanOneQuantile = exports.normalQuantile = exports.sampleGammaMeanOne = exports.samplePoisson = exports.exponentialQuantiles = exports.meanOf = exports.sampleQuantiles = exports.assertDistribution = exports.createPrng = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Seeded sampling for the staffing simulation: measured distributions by
|
|
6
|
+
* inverse CDF, Poisson counts, and the Gamma draw that overdisperses arrivals
|
|
7
|
+
* around the forecast. Every draw takes a `rand` from `createPrng`, so a run
|
|
8
|
+
* is reproducible from its seed.
|
|
9
|
+
*/
|
|
10
|
+
const prng_1 = require("../scheduling/engine/prng");
|
|
11
|
+
Object.defineProperty(exports, "createPrng", { enumerable: true, get: function () { return prng_1.createPrng; } });
|
|
12
|
+
/** Validates a quantile distribution: ≥ 2 finite, non-negative, non-decreasing points. */
|
|
13
|
+
function assertDistribution(d, what) {
|
|
14
|
+
if (!d || !Array.isArray(d.q) || d.q.length < 2)
|
|
15
|
+
throw new RangeError(`${what} needs at least two quantiles`);
|
|
16
|
+
for (let i = 0; i < d.q.length; i++) {
|
|
17
|
+
const v = d.q[i];
|
|
18
|
+
if (!Number.isFinite(v) || v < 0)
|
|
19
|
+
throw new RangeError(`${what} quantiles must be finite and non-negative`);
|
|
20
|
+
if (i > 0 && v < d.q[i - 1])
|
|
21
|
+
throw new RangeError(`${what} quantiles must not decrease`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.assertDistribution = assertDistribution;
|
|
25
|
+
/** Inverse CDF with linear interpolation between the evenly spaced quantiles. */
|
|
26
|
+
function sampleQuantiles(d, rand) {
|
|
27
|
+
const at = rand() * (d.q.length - 1);
|
|
28
|
+
const lo = Math.floor(at);
|
|
29
|
+
const hi = Math.min(d.q.length - 1, lo + 1);
|
|
30
|
+
return d.q[lo] + (d.q[hi] - d.q[lo]) * (at - lo);
|
|
31
|
+
}
|
|
32
|
+
exports.sampleQuantiles = sampleQuantiles;
|
|
33
|
+
/** Mean of a quantile distribution (trapezoid over the evenly spaced points). */
|
|
34
|
+
function meanOf(d) {
|
|
35
|
+
let sum = 0;
|
|
36
|
+
for (let i = 1; i < d.q.length; i++)
|
|
37
|
+
sum += (d.q[i] + d.q[i - 1]) / 2;
|
|
38
|
+
return sum / (d.q.length - 1);
|
|
39
|
+
}
|
|
40
|
+
exports.meanOf = meanOf;
|
|
41
|
+
/** An exponential distribution as 101 quantiles (p100 capped at p99.9), for tests and defaults. */
|
|
42
|
+
function exponentialQuantiles(meanValue, points = 101) {
|
|
43
|
+
const q = [];
|
|
44
|
+
for (let i = 0; i < points; i++) {
|
|
45
|
+
const p = Math.min(i / (points - 1), 0.999);
|
|
46
|
+
q.push(-meanValue * Math.log(1 - p));
|
|
47
|
+
}
|
|
48
|
+
return { q };
|
|
49
|
+
}
|
|
50
|
+
exports.exponentialQuantiles = exponentialQuantiles;
|
|
51
|
+
/** Poisson count: Knuth for small means, a rounded normal approximation above 60. */
|
|
52
|
+
function samplePoisson(lambda, rand) {
|
|
53
|
+
if (lambda <= 0)
|
|
54
|
+
return 0;
|
|
55
|
+
if (lambda > 60) {
|
|
56
|
+
const u = Math.max(rand(), 1e-12);
|
|
57
|
+
const v = rand();
|
|
58
|
+
const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
59
|
+
return Math.max(0, Math.round(lambda + Math.sqrt(lambda) * z));
|
|
60
|
+
}
|
|
61
|
+
const limit = Math.exp(-lambda);
|
|
62
|
+
let k = 0;
|
|
63
|
+
let p = rand();
|
|
64
|
+
while (p > limit) {
|
|
65
|
+
k += 1;
|
|
66
|
+
p *= rand();
|
|
67
|
+
}
|
|
68
|
+
return k;
|
|
69
|
+
}
|
|
70
|
+
exports.samplePoisson = samplePoisson;
|
|
71
|
+
/** Gamma(shape k, scale 1/k) — mean 1 — by Marsaglia–Tsang. */
|
|
72
|
+
function sampleGammaMeanOne(k, rand) {
|
|
73
|
+
if (!Number.isFinite(k) || k <= 0)
|
|
74
|
+
return 1;
|
|
75
|
+
const shape = k < 1 ? k + 1 : k;
|
|
76
|
+
const d = shape - 1 / 3;
|
|
77
|
+
const c = 1 / Math.sqrt(9 * d);
|
|
78
|
+
let value;
|
|
79
|
+
for (;;) {
|
|
80
|
+
let x;
|
|
81
|
+
let v;
|
|
82
|
+
do {
|
|
83
|
+
const u1 = Math.max(rand(), 1e-12);
|
|
84
|
+
const u2 = rand();
|
|
85
|
+
x = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
|
86
|
+
v = 1 + c * x;
|
|
87
|
+
} while (v <= 0);
|
|
88
|
+
v = v * v * v;
|
|
89
|
+
const u = rand();
|
|
90
|
+
if (u < 1 - 0.0331 * x * x * x * x || Math.log(Math.max(u, 1e-12)) < 0.5 * x * x + d * (1 - v + Math.log(v))) {
|
|
91
|
+
value = d * v;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (k < 1)
|
|
96
|
+
value *= Math.pow(Math.max(rand(), 1e-12), 1 / k);
|
|
97
|
+
return value / k;
|
|
98
|
+
}
|
|
99
|
+
exports.sampleGammaMeanOne = sampleGammaMeanOne;
|
|
100
|
+
/** Inverse standard normal CDF (Acklam's rational approximation, relative error < 1.2e-9). */
|
|
101
|
+
function normalQuantile(p) {
|
|
102
|
+
const a = [
|
|
103
|
+
-39.69683028665376, 220.9460984245205, -275.9285104469687, 138.357751867269, -30.66479806614716, 2.506628277459239,
|
|
104
|
+
];
|
|
105
|
+
const b = [-54.47609879822406, 161.5858368580409, -155.6989798598866, 66.80131188771972, -13.28068155288572];
|
|
106
|
+
const c = [
|
|
107
|
+
-0.007784894002430293, -0.3223964580411365, -2.400758277161838, -2.549732539343734, 4.374664141464968,
|
|
108
|
+
2.938163982698783,
|
|
109
|
+
];
|
|
110
|
+
const d = [0.007784695709041462, 0.3224671290700398, 2.445134137142996, 3.754408661907416];
|
|
111
|
+
const low = 0.02425;
|
|
112
|
+
if (p < low) {
|
|
113
|
+
const q = Math.sqrt(-2 * Math.log(p));
|
|
114
|
+
return ((((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5]) /
|
|
115
|
+
((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1));
|
|
116
|
+
}
|
|
117
|
+
if (p > 1 - low)
|
|
118
|
+
return -normalQuantile(1 - p);
|
|
119
|
+
const q = p - 0.5;
|
|
120
|
+
const r = q * q;
|
|
121
|
+
return (((((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5]) * q) /
|
|
122
|
+
(((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1));
|
|
123
|
+
}
|
|
124
|
+
exports.normalQuantile = normalQuantile;
|
|
125
|
+
/** Quantile `p` of Gamma(shape k, scale 1/k) — mean 1 — by Wilson–Hilferty; 1 for k = Infinity. */
|
|
126
|
+
function gammaMeanOneQuantile(k, p) {
|
|
127
|
+
if (!Number.isFinite(k) || k <= 0)
|
|
128
|
+
return 1;
|
|
129
|
+
const v = 1 / (9 * k);
|
|
130
|
+
return Math.max(0, (1 - v + normalQuantile(p) * Math.sqrt(v)) ** 3);
|
|
131
|
+
}
|
|
132
|
+
exports.gammaMeanOneQuantile = gammaMeanOneQuantile;
|
|
133
|
+
/**
|
|
134
|
+
* `count` strata of (0, 1), in an order whose every prefix is spread out (van der Corput, base 2): returns
|
|
135
|
+
* the stratum midpoints, so the first eight already sample the whole range and all `count` sample it evenly.
|
|
136
|
+
*/
|
|
137
|
+
function spreadStrata(count) {
|
|
138
|
+
const used = new Set();
|
|
139
|
+
const out = [];
|
|
140
|
+
for (let i = 1; out.length < count; i++) {
|
|
141
|
+
let x = 0;
|
|
142
|
+
let f = 0.5;
|
|
143
|
+
for (let n = i; n > 0; n >>= 1, f /= 2)
|
|
144
|
+
if (n & 1)
|
|
145
|
+
x += f;
|
|
146
|
+
const stratum = Math.floor(x * count);
|
|
147
|
+
if (used.has(stratum))
|
|
148
|
+
continue;
|
|
149
|
+
used.add(stratum);
|
|
150
|
+
out.push((stratum + 0.5) / count);
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
exports.spreadStrata = spreadStrata;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { KnownOccurrence, LearnedEffect } from './types';
|
|
2
|
+
/** How many days after an event ends are read for a knock-on wave. */
|
|
3
|
+
export declare const KNOCK_ON_DAYS = 14;
|
|
4
|
+
/** First-time defaults by kind: a wide guess, always reported as low confidence. */
|
|
5
|
+
export declare const DEFAULT_EFFECTS: Record<string, {
|
|
6
|
+
size: number;
|
|
7
|
+
low: number;
|
|
8
|
+
high: number;
|
|
9
|
+
}>;
|
|
10
|
+
/** An occurrence with its dates as day numbers, converted once per run. */
|
|
11
|
+
export interface Occ {
|
|
12
|
+
o: KnownOccurrence;
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
holiday: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function prepareOccurrences(occurrences: readonly KnownOccurrence[]): Occ[];
|
|
18
|
+
/**
|
|
19
|
+
* Days whose volume is not ordinary: inside an occurrence, or inside the
|
|
20
|
+
* knock-on window after an event. They are left out of every baseline estimate.
|
|
21
|
+
*/
|
|
22
|
+
export declare function specialDays(occurrences: readonly Occ[]): Set<number>;
|
|
23
|
+
/** Days the baseline expects fewer tasks than this are too noisy to read a ratio from. */
|
|
24
|
+
export declare const MIN_EXPECTED = 5;
|
|
25
|
+
/**
|
|
26
|
+
* Evidence, in expected tasks, at which a learned size is trusted halfway: the
|
|
27
|
+
* size is shrunk towards 1 as `1 + (k̂ − 1) · n / (n + EFFECT_SHRINK)`, n the
|
|
28
|
+
* expected volume over the occurrences' days. One quiet occurrence cannot
|
|
29
|
+
* teach a large effect from noise.
|
|
30
|
+
*/
|
|
31
|
+
export declare const EFFECT_SHRINK = 50;
|
|
32
|
+
/**
|
|
33
|
+
* Day-level dispersion of a series: the share of a day's expected volume squared that real days scatter
|
|
34
|
+
* by beyond Poisson, `c = max(0, Σ((a − e)² − e) ÷ Σe²)` over ordinary days. A day expected at `e` then
|
|
35
|
+
* has variance `e + c·e²`. Real arrivals are rarely Poisson at the day grain (weather, a mailing, one
|
|
36
|
+
* client's batch), and at 7,000 a week even 8 % day noise is several Poisson sigmas: a gate on Poisson
|
|
37
|
+
* alone reads that noise as knock-on waves.
|
|
38
|
+
*/
|
|
39
|
+
export declare function dayDispersion(days: ReadonlyArray<{
|
|
40
|
+
actual: number;
|
|
41
|
+
expected: number;
|
|
42
|
+
}>): number;
|
|
43
|
+
/**
|
|
44
|
+
* Learns one effect per label from past occurrences, judged day by day
|
|
45
|
+
* against `expected` (the baseline with no effects) and `actual`. Days the
|
|
46
|
+
* baseline expects under `MIN_EXPECTED` tasks are skipped: a ratio of almost
|
|
47
|
+
* nothing is noise. `dispersion` is the series' day-level dispersion
|
|
48
|
+
* (`dayDispersion`); 0 is pure Poisson.
|
|
49
|
+
*
|
|
50
|
+
* Size is Σ actual ÷ Σ expected over the label's days, shrunk towards 1 by the
|
|
51
|
+
* evidence (`EFFECT_SHRINK`). A single occurrence's range is its uncertainty
|
|
52
|
+
* (± 2 · √(actual + c·Σe²) ÷ expected, widened to cover the unshrunk
|
|
53
|
+
* estimate); several occurrences range over their own sizes. The shape is
|
|
54
|
+
* indexed by day of the event (`day − from`), so a skipped day stays 1 and
|
|
55
|
+
* does not shift the days after it. A knock-on day is kept only when the
|
|
56
|
+
* pooled actual is `2.5 · √(expected + c·Σe²)` off the baseline and it sits in
|
|
57
|
+
* a run of at least two such days off the same way; its factor is shrunk the
|
|
58
|
+
* same way as the size.
|
|
59
|
+
*/
|
|
60
|
+
export declare function learnEffects(past: readonly Occ[], actual: (day: number) => number | null, expected: (day: number) => number, dispersion?: number): Map<string, LearnedEffect>;
|
|
61
|
+
/**
|
|
62
|
+
* The effect a (usually future) occurrence gets: its own label's history,
|
|
63
|
+
* else the planner's estimate, else what its kind did, else the default.
|
|
64
|
+
*/
|
|
65
|
+
export declare function effectFor(o: KnownOccurrence, learned: ReadonlyMap<string, LearnedEffect>): LearnedEffect;
|
|
66
|
+
/**
|
|
67
|
+
* What the forecast would assume for an occurrence, given the effects a
|
|
68
|
+
* forecast learned (`SeriesForecast.effects`): the platform's "size, shape,
|
|
69
|
+
* knock-on" suggestion for an event a planner is typing in (spec §7.2).
|
|
70
|
+
*/
|
|
71
|
+
export declare function suggestEffect(o: KnownOccurrence, learned: readonly LearnedEffect[]): LearnedEffect;
|
|
72
|
+
export interface DayEffect {
|
|
73
|
+
/** Product of every holiday factor touching the day. */
|
|
74
|
+
holiday: number;
|
|
75
|
+
/** Every non-holiday effect touching the day (inside the event, or its knock-on). */
|
|
76
|
+
events: Array<{
|
|
77
|
+
label: string;
|
|
78
|
+
factor: number;
|
|
79
|
+
low: number;
|
|
80
|
+
high: number;
|
|
81
|
+
}>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* What every occurrence does to one day. Inside an occurrence the factor is
|
|
85
|
+
* `size × shape[day]`; after it, the knock-on day's factor.
|
|
86
|
+
*/
|
|
87
|
+
export declare function dayEffect(day: number, occurrences: readonly Occ[], effects: ReadonlyMap<Occ, LearnedEffect>): DayEffect;
|
|
88
|
+
//# sourceMappingURL=effects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effects.d.ts","sourceRoot":"","sources":["../../src/forecasting/effects.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG9D,sEAAsE;AACtE,eAAO,MAAM,aAAa,KAAK,CAAC;AAMhC,oFAAoF;AACpF,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAMvF,CAAC;AAGF,2EAA2E;AAC3E,MAAM,WAAW,GAAG;IAChB,CAAC,EAAE,eAAe,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,SAAS,eAAe,EAAE,GAAG,GAAG,EAAE,CAEjF;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,SAAS,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,CAOpE;AAED,0FAA0F;AAC1F,eAAO,MAAM,YAAY,IAAI,CAAC;AAC9B;;;;;GAKG;AACH,eAAO,MAAM,aAAa,KAAK,CAAC;AAahC;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,aAAa,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,CAS/F;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CACxB,IAAI,EAAE,SAAS,GAAG,EAAE,EACpB,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,EACtC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,EACjC,UAAU,SAAI,GACf,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CA0F5B;AAuCD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,eAAe,EAAE,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,aAAa,CAyBxG;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,eAAe,EAAE,OAAO,EAAE,SAAS,aAAa,EAAE,GAAG,aAAa,CAElG;AAED,MAAM,WAAW,SAAS;IACtB,wDAAwD;IACxD,OAAO,EAAE,MAAM,CAAC;IAChB,qFAAqF;IACrF,MAAM,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC/E;AAID;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,GAAG,EAAE,EAAE,OAAO,EAAE,WAAW,CAAC,GAAG,EAAE,aAAa,CAAC,GAAG,SAAS,CAsBvH"}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dayEffect = exports.suggestEffect = exports.effectFor = exports.learnEffects = exports.dayDispersion = exports.EFFECT_SHRINK = exports.MIN_EXPECTED = exports.specialDays = exports.prepareOccurrences = exports.DEFAULT_EFFECTS = exports.KNOCK_ON_DAYS = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Events and holidays: which days they touch, and what they do to volume.
|
|
6
|
+
*
|
|
7
|
+
* An effect is learned per `label` from past occurrences — its size (mean
|
|
8
|
+
* factor), its shape (day 2 is the peak of a three-day sale) and its knock-on
|
|
9
|
+
* (returns a week after the sale). Without history for the label the planner's
|
|
10
|
+
* own estimate is used, then what other events of the same kind did, then a
|
|
11
|
+
* wide first-time default. Pure: the baseline it is judged against is passed in.
|
|
12
|
+
*/
|
|
13
|
+
const days_1 = require("./days");
|
|
14
|
+
const stats_1 = require("./stats");
|
|
15
|
+
/** How many days after an event ends are read for a knock-on wave. */
|
|
16
|
+
exports.KNOCK_ON_DAYS = 14;
|
|
17
|
+
/** A knock-on day closer to 1 than this is noise, not a wave. */
|
|
18
|
+
const KNOCK_ON_MIN = 0.1;
|
|
19
|
+
/** The longest shape remembered, in days. */
|
|
20
|
+
const MAX_SHAPE_DAYS = 60;
|
|
21
|
+
/** First-time defaults by kind: a wide guess, always reported as low confidence. */
|
|
22
|
+
exports.DEFAULT_EFFECTS = {
|
|
23
|
+
sale: { size: 1.3, low: 1.0, high: 1.8 },
|
|
24
|
+
campaign: { size: 1.25, low: 1.0, high: 1.7 },
|
|
25
|
+
launch: { size: 1.3, low: 1.0, high: 2.0 },
|
|
26
|
+
season: { size: 1.2, low: 0.9, high: 1.5 },
|
|
27
|
+
holiday: { size: 0.7, low: 0.3, high: 1.2 },
|
|
28
|
+
};
|
|
29
|
+
const FALLBACK = { size: 1.2, low: 0.9, high: 1.6 };
|
|
30
|
+
function prepareOccurrences(occurrences) {
|
|
31
|
+
return occurrences.map((o) => ({ o, from: (0, days_1.dayOf)(o.from), to: (0, days_1.dayOf)(o.to), holiday: o.kind === 'holiday' }));
|
|
32
|
+
}
|
|
33
|
+
exports.prepareOccurrences = prepareOccurrences;
|
|
34
|
+
/**
|
|
35
|
+
* Days whose volume is not ordinary: inside an occurrence, or inside the
|
|
36
|
+
* knock-on window after an event. They are left out of every baseline estimate.
|
|
37
|
+
*/
|
|
38
|
+
function specialDays(occurrences) {
|
|
39
|
+
const out = new Set();
|
|
40
|
+
for (const occ of occurrences) {
|
|
41
|
+
for (let d = occ.from; d <= occ.to; d++)
|
|
42
|
+
out.add(d);
|
|
43
|
+
if (!occ.holiday)
|
|
44
|
+
for (let j = 1; j <= exports.KNOCK_ON_DAYS; j++)
|
|
45
|
+
out.add(occ.to + j);
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
exports.specialDays = specialDays;
|
|
50
|
+
/** Days the baseline expects fewer tasks than this are too noisy to read a ratio from. */
|
|
51
|
+
exports.MIN_EXPECTED = 5;
|
|
52
|
+
/**
|
|
53
|
+
* Evidence, in expected tasks, at which a learned size is trusted halfway: the
|
|
54
|
+
* size is shrunk towards 1 as `1 + (k̂ − 1) · n / (n + EFFECT_SHRINK)`, n the
|
|
55
|
+
* expected volume over the occurrences' days. One quiet occurrence cannot
|
|
56
|
+
* teach a large effect from noise.
|
|
57
|
+
*/
|
|
58
|
+
exports.EFFECT_SHRINK = 50;
|
|
59
|
+
/**
|
|
60
|
+
* A knock-on day counts only when it is this many sigmas off the baseline, the variance being Poisson
|
|
61
|
+
* plus the series' own day-level dispersion (`dayDispersion`)…
|
|
62
|
+
*/
|
|
63
|
+
const KNOCK_ON_SIGMAS = 2.5;
|
|
64
|
+
/** …and only inside a run of at least this many such days, all off the same way: one odd day is not a wave. */
|
|
65
|
+
const KNOCK_ON_RUN = 2;
|
|
66
|
+
/** Sigmas either side of a single occurrence's size for its range. */
|
|
67
|
+
const RANGE_SIGMAS = 2;
|
|
68
|
+
const shrink = (ratio, evidence) => 1 + ((ratio - 1) * evidence) / (evidence + exports.EFFECT_SHRINK);
|
|
69
|
+
/**
|
|
70
|
+
* Day-level dispersion of a series: the share of a day's expected volume squared that real days scatter
|
|
71
|
+
* by beyond Poisson, `c = max(0, Σ((a − e)² − e) ÷ Σe²)` over ordinary days. A day expected at `e` then
|
|
72
|
+
* has variance `e + c·e²`. Real arrivals are rarely Poisson at the day grain (weather, a mailing, one
|
|
73
|
+
* client's batch), and at 7,000 a week even 8 % day noise is several Poisson sigmas: a gate on Poisson
|
|
74
|
+
* alone reads that noise as knock-on waves.
|
|
75
|
+
*/
|
|
76
|
+
function dayDispersion(days) {
|
|
77
|
+
let excess = 0;
|
|
78
|
+
let squares = 0;
|
|
79
|
+
for (const { actual, expected } of days) {
|
|
80
|
+
if (expected < exports.MIN_EXPECTED)
|
|
81
|
+
continue;
|
|
82
|
+
excess += (actual - expected) ** 2 - expected;
|
|
83
|
+
squares += expected * expected;
|
|
84
|
+
}
|
|
85
|
+
return squares > 0 ? Math.max(0, excess / squares) : 0;
|
|
86
|
+
}
|
|
87
|
+
exports.dayDispersion = dayDispersion;
|
|
88
|
+
/**
|
|
89
|
+
* Learns one effect per label from past occurrences, judged day by day
|
|
90
|
+
* against `expected` (the baseline with no effects) and `actual`. Days the
|
|
91
|
+
* baseline expects under `MIN_EXPECTED` tasks are skipped: a ratio of almost
|
|
92
|
+
* nothing is noise. `dispersion` is the series' day-level dispersion
|
|
93
|
+
* (`dayDispersion`); 0 is pure Poisson.
|
|
94
|
+
*
|
|
95
|
+
* Size is Σ actual ÷ Σ expected over the label's days, shrunk towards 1 by the
|
|
96
|
+
* evidence (`EFFECT_SHRINK`). A single occurrence's range is its uncertainty
|
|
97
|
+
* (± 2 · √(actual + c·Σe²) ÷ expected, widened to cover the unshrunk
|
|
98
|
+
* estimate); several occurrences range over their own sizes. The shape is
|
|
99
|
+
* indexed by day of the event (`day − from`), so a skipped day stays 1 and
|
|
100
|
+
* does not shift the days after it. A knock-on day is kept only when the
|
|
101
|
+
* pooled actual is `2.5 · √(expected + c·Σe²)` off the baseline and it sits in
|
|
102
|
+
* a run of at least two such days off the same way; its factor is shrunk the
|
|
103
|
+
* same way as the size.
|
|
104
|
+
*/
|
|
105
|
+
function learnEffects(past, actual, expected, dispersion = 0) {
|
|
106
|
+
var _a;
|
|
107
|
+
const byLabel = new Map();
|
|
108
|
+
for (const occ of past) {
|
|
109
|
+
if (occ.o.closed)
|
|
110
|
+
continue;
|
|
111
|
+
const ratios = [];
|
|
112
|
+
let sa = 0;
|
|
113
|
+
let se = 0;
|
|
114
|
+
let se2 = 0;
|
|
115
|
+
for (let d = occ.from; d <= occ.to; d++) {
|
|
116
|
+
const a = actual(d);
|
|
117
|
+
const e = expected(d);
|
|
118
|
+
if (a === null || e < exports.MIN_EXPECTED)
|
|
119
|
+
continue;
|
|
120
|
+
ratios.push({ at: d - occ.from, ratio: a / e });
|
|
121
|
+
sa += a;
|
|
122
|
+
se += e;
|
|
123
|
+
se2 += e * e;
|
|
124
|
+
}
|
|
125
|
+
if (ratios.length === 0)
|
|
126
|
+
continue;
|
|
127
|
+
const size = sa / se;
|
|
128
|
+
const entry = byLabel.get(occ.o.label) ?? {
|
|
129
|
+
kind: occ.o.kind,
|
|
130
|
+
sizes: [],
|
|
131
|
+
a: 0,
|
|
132
|
+
e: 0,
|
|
133
|
+
e2: 0,
|
|
134
|
+
shape: [],
|
|
135
|
+
knockA: [],
|
|
136
|
+
knockE: [],
|
|
137
|
+
knockE2: [],
|
|
138
|
+
};
|
|
139
|
+
entry.sizes.push(size);
|
|
140
|
+
entry.a += sa;
|
|
141
|
+
entry.e += se;
|
|
142
|
+
entry.e2 += se2;
|
|
143
|
+
for (const { at, ratio } of ratios) {
|
|
144
|
+
if (at >= MAX_SHAPE_DAYS)
|
|
145
|
+
break;
|
|
146
|
+
((_a = entry.shape)[at] ?? (_a[at] = [])).push(size > 0 ? ratio / size : 1);
|
|
147
|
+
}
|
|
148
|
+
if (!occ.holiday) {
|
|
149
|
+
for (let j = 0; j < exports.KNOCK_ON_DAYS; j++) {
|
|
150
|
+
const a = actual(occ.to + j + 1);
|
|
151
|
+
const e = expected(occ.to + j + 1);
|
|
152
|
+
if (a === null || e < exports.MIN_EXPECTED)
|
|
153
|
+
continue;
|
|
154
|
+
entry.knockA[j] = (entry.knockA[j] ?? 0) + a;
|
|
155
|
+
entry.knockE[j] = (entry.knockE[j] ?? 0) + e;
|
|
156
|
+
entry.knockE2[j] = (entry.knockE2[j] ?? 0) + e * e;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
byLabel.set(occ.o.label, entry);
|
|
160
|
+
}
|
|
161
|
+
const out = new Map();
|
|
162
|
+
for (const [label, entry] of byLabel) {
|
|
163
|
+
const raw = entry.a / entry.e;
|
|
164
|
+
const size = (0, stats_1.clamp)(shrink(raw, entry.e), 0.05, 20);
|
|
165
|
+
let low;
|
|
166
|
+
let high;
|
|
167
|
+
if (entry.sizes.length > 1) {
|
|
168
|
+
low = Math.min(...entry.sizes.map((k) => shrink(k, entry.e)));
|
|
169
|
+
high = Math.max(...entry.sizes.map((k) => shrink(k, entry.e)));
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const sigma = (RANGE_SIGMAS * Math.sqrt(Math.max(entry.a, 1) + dispersion * entry.e2)) / entry.e;
|
|
173
|
+
low = Math.max(0, Math.min(size, raw) - sigma);
|
|
174
|
+
high = Math.max(size, raw) + sigma;
|
|
175
|
+
}
|
|
176
|
+
out.set(label, {
|
|
177
|
+
label,
|
|
178
|
+
kind: entry.kind,
|
|
179
|
+
source: 'label',
|
|
180
|
+
occurrences: entry.sizes.length,
|
|
181
|
+
size,
|
|
182
|
+
low: Math.min(low, size),
|
|
183
|
+
high: Math.max(high, size),
|
|
184
|
+
// A day of the event never read (it expected too little) is an ordinary day of it: 1.
|
|
185
|
+
shape: Array.from(entry.shape, (values) => (values ? (0, stats_1.clamp)((0, stats_1.mean)(values), 0.05, 20) : 1)),
|
|
186
|
+
knockOn: knockOnOf(entry.knockA, entry.knockE, entry.knockE2, dispersion),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
exports.learnEffects = learnEffects;
|
|
192
|
+
/**
|
|
193
|
+
* Knock-on factors per day after an event: significant days in same-direction runs of `KNOCK_ON_RUN`,
|
|
194
|
+
* trailing 1s trimmed. A day is significant when the pooled actual is off the pooled expectation by more
|
|
195
|
+
* than `KNOCK_ON_SIGMAS · √(E + c·Σe²)`.
|
|
196
|
+
*/
|
|
197
|
+
function knockOnOf(actuals, expecteds, squares, dispersion) {
|
|
198
|
+
const factors = [];
|
|
199
|
+
// +1 above the baseline, −1 below, 0 not significant.
|
|
200
|
+
const direction = [];
|
|
201
|
+
for (let j = 0; j < exports.KNOCK_ON_DAYS; j++) {
|
|
202
|
+
const a = actuals[j];
|
|
203
|
+
const e = expecteds[j];
|
|
204
|
+
const known = a !== undefined && e !== undefined;
|
|
205
|
+
const k = known ? (0, stats_1.clamp)(shrink(a / e, e), 0.2, 5) : 1;
|
|
206
|
+
factors.push(k);
|
|
207
|
+
const sd = known ? Math.sqrt(e + dispersion * (squares[j] ?? 0)) : 0;
|
|
208
|
+
const significant = known && Math.abs(a - e) > KNOCK_ON_SIGMAS * sd && Math.abs(k - 1) >= KNOCK_ON_MIN;
|
|
209
|
+
direction.push(significant ? Math.sign(a - e) : 0);
|
|
210
|
+
}
|
|
211
|
+
const knockOn = factors.map((k, j) => {
|
|
212
|
+
const sign = direction[j];
|
|
213
|
+
if (sign === 0)
|
|
214
|
+
return 1;
|
|
215
|
+
let from = j;
|
|
216
|
+
while (from > 0 && direction[from - 1] === sign)
|
|
217
|
+
from--;
|
|
218
|
+
let to = j;
|
|
219
|
+
while (to < exports.KNOCK_ON_DAYS - 1 && direction[to + 1] === sign)
|
|
220
|
+
to++;
|
|
221
|
+
return to - from + 1 >= KNOCK_ON_RUN ? k : 1;
|
|
222
|
+
});
|
|
223
|
+
while (knockOn.length > 0 && knockOn[knockOn.length - 1] === 1)
|
|
224
|
+
knockOn.pop();
|
|
225
|
+
return knockOn;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The effect a (usually future) occurrence gets: its own label's history,
|
|
229
|
+
* else the planner's estimate, else what its kind did, else the default.
|
|
230
|
+
*/
|
|
231
|
+
function effectFor(o, learned) {
|
|
232
|
+
const base = { label: o.label, kind: o.kind, shape: [1], knockOn: [] };
|
|
233
|
+
if (o.closed)
|
|
234
|
+
return { ...base, source: 'planned', occurrences: 0, size: 0, low: 0, high: 0 };
|
|
235
|
+
const own = learned.get(o.label);
|
|
236
|
+
if (own)
|
|
237
|
+
return own;
|
|
238
|
+
if (o.planned) {
|
|
239
|
+
const size = Math.max(0, 1 + o.planned.likely / 100);
|
|
240
|
+
const a = Math.max(0, 1 + (o.planned.low ?? o.planned.likely * 0.5) / 100);
|
|
241
|
+
const b = Math.max(0, 1 + (o.planned.high ?? o.planned.likely * 1.5) / 100);
|
|
242
|
+
return { ...base, source: 'planned', occurrences: 0, size, low: Math.min(a, b, size), high: Math.max(a, b, size) };
|
|
243
|
+
}
|
|
244
|
+
const sameKind = [...learned.values()].filter((e) => e.kind === o.kind && e.source === 'label');
|
|
245
|
+
if (sameKind.length > 0) {
|
|
246
|
+
const size = (0, stats_1.mean)(sameKind.map((e) => e.size));
|
|
247
|
+
return {
|
|
248
|
+
...base,
|
|
249
|
+
source: 'kind',
|
|
250
|
+
occurrences: sameKind.reduce((n, e) => n + e.occurrences, 0),
|
|
251
|
+
size,
|
|
252
|
+
low: Math.min(size, ...sameKind.map((e) => e.low)),
|
|
253
|
+
high: Math.max(size, ...sameKind.map((e) => e.high)),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
const d = exports.DEFAULT_EFFECTS[o.kind] ?? FALLBACK;
|
|
257
|
+
return { ...base, source: 'default', occurrences: 0, ...d };
|
|
258
|
+
}
|
|
259
|
+
exports.effectFor = effectFor;
|
|
260
|
+
/**
|
|
261
|
+
* What the forecast would assume for an occurrence, given the effects a
|
|
262
|
+
* forecast learned (`SeriesForecast.effects`): the platform's "size, shape,
|
|
263
|
+
* knock-on" suggestion for an event a planner is typing in (spec §7.2).
|
|
264
|
+
*/
|
|
265
|
+
function suggestEffect(o, learned) {
|
|
266
|
+
return effectFor(o, new Map(learned.filter((e) => e.source === 'label').map((e) => [e.label, e])));
|
|
267
|
+
}
|
|
268
|
+
exports.suggestEffect = suggestEffect;
|
|
269
|
+
const NONE = { holiday: 1, events: [] };
|
|
270
|
+
/**
|
|
271
|
+
* What every occurrence does to one day. Inside an occurrence the factor is
|
|
272
|
+
* `size × shape[day]`; after it, the knock-on day's factor.
|
|
273
|
+
*/
|
|
274
|
+
function dayEffect(day, occurrences, effects) {
|
|
275
|
+
let holiday = 1;
|
|
276
|
+
let events = null;
|
|
277
|
+
for (const occ of occurrences) {
|
|
278
|
+
if (day < occ.from || day > occ.to + exports.KNOCK_ON_DAYS)
|
|
279
|
+
continue;
|
|
280
|
+
const effect = effects.get(occ);
|
|
281
|
+
if (!effect)
|
|
282
|
+
continue;
|
|
283
|
+
if (day <= occ.to) {
|
|
284
|
+
const shape = effect.shape[Math.min(day - occ.from, effect.shape.length - 1)] ?? 1;
|
|
285
|
+
const factor = effect.size * shape;
|
|
286
|
+
if (occ.holiday)
|
|
287
|
+
holiday *= factor;
|
|
288
|
+
else
|
|
289
|
+
(events ?? (events = [])).push({ label: occ.o.label, factor, low: effect.low * shape, high: effect.high * shape });
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (occ.holiday)
|
|
293
|
+
continue;
|
|
294
|
+
const j = day - occ.to;
|
|
295
|
+
if (j <= effect.knockOn.length) {
|
|
296
|
+
const factor = effect.knockOn[j - 1];
|
|
297
|
+
if (factor !== 1)
|
|
298
|
+
(events ?? (events = [])).push({ label: occ.o.label, factor, low: factor, high: factor });
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return holiday === 1 && events === null ? NONE : { holiday, events: events ?? [] };
|
|
302
|
+
}
|
|
303
|
+
exports.dayEffect = dayEffect;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Erlang A (M/M/n+M): a queue with exponential patience. Used **only** to
|
|
3
|
+
* bracket the staffing simulation's search — never as the answer. It assumes
|
|
4
|
+
* one skill, exponential handle times and no concurrency, which is exactly
|
|
5
|
+
* what the simulation exists to get past (spec §7.3 as amended: no naive
|
|
6
|
+
* Erlang C).
|
|
7
|
+
*/
|
|
8
|
+
export interface ErlangAInput {
|
|
9
|
+
/** Arrivals per ms. */
|
|
10
|
+
lambda: number;
|
|
11
|
+
/** Mean handle time, ms. */
|
|
12
|
+
meanHandleMs: number;
|
|
13
|
+
/** Mean patience, ms; Infinity or 0 = nobody leaves (Erlang C). */
|
|
14
|
+
meanPatienceMs: number;
|
|
15
|
+
/** Target wait, ms. */
|
|
16
|
+
targetMs: number;
|
|
17
|
+
}
|
|
18
|
+
/** Probability of waiting and the approximate share served within the target with `n` servers. */
|
|
19
|
+
export declare function erlangA(n: number, input: ErlangAInput): {
|
|
20
|
+
pWait: number;
|
|
21
|
+
serviceLevel: number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Share of arrivals that give up before anybody picks them up: θ·E[queue] ÷ λ (exact for M/M/n+M).
|
|
25
|
+
* 0 when nobody leaves; 1 with nobody to serve. Validation only — the tests hold the simulation to it;
|
|
26
|
+
* not exported from the module.
|
|
27
|
+
*/
|
|
28
|
+
export declare function erlangAAbandonment(n: number, input: ErlangAInput): number;
|
|
29
|
+
/** Smallest n meeting `share` within the target, searched up to `max`. */
|
|
30
|
+
export declare function erlangASeed(input: ErlangAInput, share: number, max: number): number;
|
|
31
|
+
//# sourceMappingURL=erlang.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"erlang.d.ts","sourceRoot":"","sources":["../../src/forecasting/erlang.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,YAAY;IACzB,uBAAuB;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,4BAA4B;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,mEAAmE;IACnE,cAAc,EAAE,MAAM,CAAC;IACvB,uBAAuB;IACvB,QAAQ,EAAE,MAAM,CAAC;CACpB;AA+BD,kGAAkG;AAClG,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAU/F;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG,MAAM,CAQzE;AAED,0EAA0E;AAC1E,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAOnF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Erlang A (M/M/n+M): a queue with exponential patience. Used **only** to
|
|
4
|
+
* bracket the staffing simulation's search — never as the answer. It assumes
|
|
5
|
+
* one skill, exponential handle times and no concurrency, which is exactly
|
|
6
|
+
* what the simulation exists to get past (spec §7.3 as amended: no naive
|
|
7
|
+
* Erlang C).
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.erlangASeed = exports.erlangAAbandonment = exports.erlangA = void 0;
|
|
11
|
+
const abandonRate = (input) => Number.isFinite(input.meanPatienceMs) && input.meanPatienceMs > 0 ? 1 / input.meanPatienceMs : 0;
|
|
12
|
+
/**
|
|
13
|
+
* Stationary distribution of the birth–death chain (number in system), computed in log space: states up
|
|
14
|
+
* to `n` are Erlang's, the tail above `n` loses `(k − n)·θ` a ms to abandonment. Null when it has none.
|
|
15
|
+
*/
|
|
16
|
+
function stationary(n, input) {
|
|
17
|
+
const mu = 1 / input.meanHandleMs;
|
|
18
|
+
const theta = abandonRate(input);
|
|
19
|
+
if (theta === 0 && input.lambda >= n * mu)
|
|
20
|
+
return null;
|
|
21
|
+
const logs = [0];
|
|
22
|
+
const a = input.lambda / mu;
|
|
23
|
+
for (let k = 1; k <= n; k++)
|
|
24
|
+
logs.push(logs[k - 1] + Math.log(a) - Math.log(k));
|
|
25
|
+
let k = n;
|
|
26
|
+
let last = logs[n];
|
|
27
|
+
const floor = last - 30; // e^-30 relative: negligible
|
|
28
|
+
while (k < n + 100000) {
|
|
29
|
+
k += 1;
|
|
30
|
+
last += Math.log(input.lambda) - Math.log(n * mu + (k - n) * theta);
|
|
31
|
+
logs.push(last);
|
|
32
|
+
if (last < floor && k > n + 10)
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
const max = logs.reduce((m, l) => Math.max(m, l), -Infinity);
|
|
36
|
+
const p = logs.map((l) => Math.exp(l - max));
|
|
37
|
+
const total = p.reduce((s, v) => s + v, 0);
|
|
38
|
+
return p.map((v) => v / total);
|
|
39
|
+
}
|
|
40
|
+
/** Probability of waiting and the approximate share served within the target with `n` servers. */
|
|
41
|
+
function erlangA(n, input) {
|
|
42
|
+
if (input.lambda <= 0)
|
|
43
|
+
return { pWait: 0, serviceLevel: 1 };
|
|
44
|
+
if (n <= 0)
|
|
45
|
+
return { pWait: 1, serviceLevel: 0 };
|
|
46
|
+
const pi = stationary(n, input);
|
|
47
|
+
if (!pi)
|
|
48
|
+
return { pWait: 1, serviceLevel: 0 };
|
|
49
|
+
let pWait = 0;
|
|
50
|
+
for (let k = n; k < pi.length; k++)
|
|
51
|
+
pWait += pi[k];
|
|
52
|
+
const theta = abandonRate(input);
|
|
53
|
+
const decay = Math.max(n / input.meanHandleMs - input.lambda + theta, theta, 1e-15);
|
|
54
|
+
return { pWait, serviceLevel: Math.max(0, 1 - pWait * Math.exp(-decay * input.targetMs)) };
|
|
55
|
+
}
|
|
56
|
+
exports.erlangA = erlangA;
|
|
57
|
+
/**
|
|
58
|
+
* Share of arrivals that give up before anybody picks them up: θ·E[queue] ÷ λ (exact for M/M/n+M).
|
|
59
|
+
* 0 when nobody leaves; 1 with nobody to serve. Validation only — the tests hold the simulation to it;
|
|
60
|
+
* not exported from the module.
|
|
61
|
+
*/
|
|
62
|
+
function erlangAAbandonment(n, input) {
|
|
63
|
+
const theta = abandonRate(input);
|
|
64
|
+
if (input.lambda <= 0 || theta === 0)
|
|
65
|
+
return 0;
|
|
66
|
+
if (n <= 0)
|
|
67
|
+
return 1;
|
|
68
|
+
const pi = stationary(n, input);
|
|
69
|
+
let queue = 0;
|
|
70
|
+
for (let k = n + 1; k < pi.length; k++)
|
|
71
|
+
queue += (k - n) * pi[k];
|
|
72
|
+
return (theta * queue) / input.lambda;
|
|
73
|
+
}
|
|
74
|
+
exports.erlangAAbandonment = erlangAAbandonment;
|
|
75
|
+
/** Smallest n meeting `share` within the target, searched up to `max`. */
|
|
76
|
+
function erlangASeed(input, share, max) {
|
|
77
|
+
if (input.lambda <= 0)
|
|
78
|
+
return 0;
|
|
79
|
+
const offered = Math.ceil(input.lambda * input.meanHandleMs);
|
|
80
|
+
for (let n = Math.max(1, Math.floor(offered * 0.5)); n <= max; n++) {
|
|
81
|
+
if (erlangA(n, input).serviceLevel >= share)
|
|
82
|
+
return n;
|
|
83
|
+
}
|
|
84
|
+
return max;
|
|
85
|
+
}
|
|
86
|
+
exports.erlangASeed = erlangASeed;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ForecastWorkspaceInput, ForecastWorkspaceResult } from './types';
|
|
2
|
+
export declare const DEFAULT_HORIZON_HOURS = 672;
|
|
3
|
+
export declare const DEFAULT_HORIZON_WEEKS = 26;
|
|
4
|
+
export declare const DEFAULT_BACKTEST_ORIGINS = 6;
|
|
5
|
+
export declare const DEFAULT_BACKTEST_SERIES = 10;
|
|
6
|
+
export declare function forecastWorkspace(input: ForecastWorkspaceInput): ForecastWorkspaceResult;
|
|
7
|
+
//# sourceMappingURL=forecast.d.ts.map
|