@actuarial-ts/core 0.3.0 → 0.4.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.
Files changed (47) hide show
  1. package/README.md +104 -0
  2. package/dist/berquist.d.ts.map +1 -1
  3. package/dist/berquist.js +4 -12
  4. package/dist/berquist.js.map +1 -1
  5. package/dist/caseOutstanding.d.ts.map +1 -1
  6. package/dist/caseOutstanding.js +2 -9
  7. package/dist/caseOutstanding.js.map +1 -1
  8. package/dist/casualtyDiagnostics.d.ts +53 -0
  9. package/dist/casualtyDiagnostics.d.ts.map +1 -0
  10. package/dist/casualtyDiagnostics.js +122 -0
  11. package/dist/casualtyDiagnostics.js.map +1 -0
  12. package/dist/fisherLange.d.ts.map +1 -1
  13. package/dist/fisherLange.js +2 -9
  14. package/dist/fisherLange.js.map +1 -1
  15. package/dist/freqSev.d.ts.map +1 -1
  16. package/dist/freqSev.js +3 -10
  17. package/dist/freqSev.js.map +1 -1
  18. package/dist/index.d.ts +3 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +3 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/metricDiagnostics.d.ts +212 -0
  23. package/dist/metricDiagnostics.d.ts.map +1 -0
  24. package/dist/metricDiagnostics.js +627 -0
  25. package/dist/metricDiagnostics.js.map +1 -0
  26. package/dist/munichChainLadder.d.ts.map +1 -1
  27. package/dist/munichChainLadder.js +2 -7
  28. package/dist/munichChainLadder.js.map +1 -1
  29. package/dist/periods.d.ts +46 -0
  30. package/dist/periods.d.ts.map +1 -0
  31. package/dist/periods.js +121 -0
  32. package/dist/periods.js.map +1 -0
  33. package/dist/util.d.ts +8 -0
  34. package/dist/util.d.ts.map +1 -1
  35. package/dist/util.js +15 -0
  36. package/dist/util.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/berquist.ts +4 -15
  39. package/src/caseOutstanding.ts +12 -14
  40. package/src/casualtyDiagnostics.ts +202 -0
  41. package/src/fisherLange.ts +12 -14
  42. package/src/freqSev.ts +11 -15
  43. package/src/index.ts +3 -0
  44. package/src/metricDiagnostics.ts +876 -0
  45. package/src/munichChainLadder.ts +6 -12
  46. package/src/periods.ts +177 -0
  47. package/src/util.ts +20 -0
@@ -0,0 +1,202 @@
1
+ import {
2
+ measureExpressionComponents,
3
+ type AmountLayerDefinition,
4
+ type MeasureExpression,
5
+ type MetricDefinition,
6
+ type MetricWarningRule,
7
+ } from "./metricDiagnostics.js";
8
+ import { ReservingError } from "./types.js";
9
+
10
+ const m = (measure: string): MeasureExpression => ({ op: "measure", measure });
11
+ const sub = (left: MeasureExpression, right: MeasureExpression): MeasureExpression => ({ op: "subtract", left, right });
12
+
13
+ function definedProperties<T extends object>(value: T | undefined): Partial<T> {
14
+ if (value === undefined) return {};
15
+ return Object.fromEntries(
16
+ Object.entries(value).filter(([, item]) => item !== undefined),
17
+ ) as Partial<T>;
18
+ }
19
+
20
+ export interface CasualtyDiagnosticComponentKeys {
21
+ reported: string;
22
+ open: string;
23
+ closedNoPay: string;
24
+ closedWithPay: string;
25
+ exposure: string;
26
+ paid250: string;
27
+ incurred250: string;
28
+ paidPrimary: string;
29
+ incurredPrimary: string;
30
+ }
31
+
32
+ export const CASUALTY_DIAGNOSTIC_COMPONENTS: Readonly<CasualtyDiagnosticComponentKeys> = {
33
+ reported: "reportedCount",
34
+ open: "openCount",
35
+ closedNoPay: "closedNoPayCount",
36
+ closedWithPay: "closedWithPayCount",
37
+ exposure: "exposure",
38
+ paid250: "paid250",
39
+ incurred250: "incurred250",
40
+ paidPrimary: "paidPrimary",
41
+ incurredPrimary: "incurredPrimary",
42
+ } as const;
43
+
44
+ type MetricDisplayOverride = Partial<Pick<
45
+ MetricDefinition,
46
+ "displayName" | "description" | "unit" | "numeratorLabel" | "denominatorLabel" | "basis"
47
+ >>;
48
+
49
+ export interface CasualtyMetricPresetOptions {
50
+ /** Caller source/output measure keys, including the exposure key. */
51
+ components?: Partial<CasualtyDiagnosticComponentKeys>;
52
+ frequencyScale?: number;
53
+ frequencyUnit?: MetricDefinition["unit"];
54
+ definitionVersion?: string;
55
+ basisLabels?: { limited250?: string; primary?: string; counts?: string };
56
+ /** Per-metric display/basis overrides; formulas remain the documented preset formulas. */
57
+ displayOverrides?: Readonly<Record<string, MetricDisplayOverride>>;
58
+ }
59
+
60
+ function metric(
61
+ id: string,
62
+ displayName: string,
63
+ description: string,
64
+ unit: MetricDefinition["unit"],
65
+ scale: number,
66
+ numerator: MeasureExpression,
67
+ denominator: MeasureExpression,
68
+ numeratorLabel: string,
69
+ denominatorLabel: string,
70
+ basis: string,
71
+ version: string,
72
+ override: MetricDisplayOverride | undefined,
73
+ warningRules?: readonly MetricWarningRule[],
74
+ ): MetricDefinition {
75
+ const definition: MetricDefinition = {
76
+ id,
77
+ version,
78
+ displayName,
79
+ description,
80
+ unit,
81
+ scale,
82
+ numerator,
83
+ denominator,
84
+ numeratorLabel,
85
+ denominatorLabel,
86
+ basis,
87
+ requiredComponents: [...new Set([
88
+ ...measureExpressionComponents(numerator),
89
+ ...measureExpressionComponents(denominator),
90
+ ])],
91
+ warningRules,
92
+ };
93
+ return { ...definition, ...definedProperties(override) };
94
+ }
95
+
96
+ const paidWarning: readonly MetricWarningRule[] = [{
97
+ code: "PAID_EXCEEDS_INCURRED",
98
+ when: "numerator-greater-than-denominator",
99
+ message: "Paid exceeds incurred on the selected amount basis",
100
+ tolerance: 1e-9,
101
+ }];
102
+
103
+ /** Builds the optional 20-metric reference preset from caller-selected keys and display metadata. */
104
+ export function createCasualtyQuarterlyMetrics(
105
+ options: CasualtyMetricPresetOptions = {},
106
+ ): readonly MetricDefinition[] {
107
+ const C = {
108
+ ...CASUALTY_DIAGNOSTIC_COMPONENTS,
109
+ ...definedProperties(options.components),
110
+ };
111
+ const frequencyScale = options.frequencyScale ?? 1_000_000;
112
+ if (!Number.isFinite(frequencyScale) || frequencyScale <= 0) {
113
+ throw new ReservingError("BAD_RATIO", `Casualty frequency scale must be positive; got ${frequencyScale}`);
114
+ }
115
+ const version = options.definitionVersion ?? "casualty-quarterly-v1";
116
+ const frequencyUnit = options.frequencyUnit ?? (frequencyScale === 1_000_000
117
+ ? "count-per-million"
118
+ : "count-per-exposure-scale");
119
+ const limitedBasis = options.basisLabels?.limited250 ?? "$250K pre-capped total";
120
+ const primaryBasis = options.basisLabels?.primary ?? "$1M capped indemnity plus unlimited expense";
121
+ const countBasis = options.basisLabels?.counts ?? "count";
122
+ const override = (id: string) => options.displayOverrides?.[id];
123
+ const nonClosedNoPay = sub(m(C.reported), m(C.closedNoPay));
124
+ return [
125
+ metric("reported-frequency", "Reported claim frequency", "Reported claims per scaled exposure units", frequencyUnit, frequencyScale, m(C.reported), m(C.exposure), "reported claims", "exposure", countBasis, version, override("reported-frequency")),
126
+ metric("open-frequency", "Open claim frequency", "Open claims per scaled exposure units", frequencyUnit, frequencyScale, m(C.open), m(C.exposure), "open claims", "exposure", countBasis, version, override("open-frequency")),
127
+ metric("closed-no-pay-frequency", "Closed-no-pay frequency", "Closed-no-pay claims per scaled exposure units", frequencyUnit, frequencyScale, m(C.closedNoPay), m(C.exposure), "closed-no-pay claims", "exposure", countBasis, version, override("closed-no-pay-frequency")),
128
+ metric("closed-with-pay-frequency", "Closed-with-pay frequency", "Closed-with-pay claims per scaled exposure units", frequencyUnit, frequencyScale, m(C.closedWithPay), m(C.exposure), "closed-with-pay claims", "exposure", countBasis, version, override("closed-with-pay-frequency")),
129
+ metric("non-closed-no-pay-frequency", "Non-closed-no-pay frequency", "Reported less closed-no-pay claims per scaled exposure units", frequencyUnit, frequencyScale, nonClosedNoPay, m(C.exposure), "reported less closed-no-pay claims", "exposure", countBasis, version, override("non-closed-no-pay-frequency")),
130
+ metric("closed-no-pay-share", "Closed-no-pay share", "Closed-no-pay claims divided by reported claims", "ratio", 1, m(C.closedNoPay), m(C.reported), "closed-no-pay claims", "reported claims", countBasis, version, override("closed-no-pay-share")),
131
+ metric("closed-with-pay-share", "Closed-with-pay share", "Closed-with-pay claims divided by reported claims", "ratio", 1, m(C.closedWithPay), m(C.reported), "closed-with-pay claims", "reported claims", countBasis, version, override("closed-with-pay-share")),
132
+ metric("open-share", "Open share", "Open claims divided by reported claims", "ratio", 1, m(C.open), m(C.reported), "open claims", "reported claims", countBasis, version, override("open-share")),
133
+ metric("paid-to-incurred-250", "Paid-to-incurred ($250K)", "Paid divided by incurred on the pre-capped $250K basis", "ratio", 1, m(C.paid250), m(C.incurred250), "$250K paid", "$250K incurred", limitedBasis, version, override("paid-to-incurred-250"), paidWarning),
134
+ metric("paid-to-incurred-primary", "Paid-to-incurred (primary)", "Paid divided by incurred on the primary basis", "ratio", 1, m(C.paidPrimary), m(C.incurredPrimary), "primary paid", "primary incurred", primaryBasis, version, override("paid-to-incurred-primary"), paidWarning),
135
+ metric("incurred-250-per-exposure", "Incurred per exposure ($250K)", "$250K incurred divided by exposure", "currency-per-exposure", 1, m(C.incurred250), m(C.exposure), "$250K incurred", "exposure", limitedBasis, version, override("incurred-250-per-exposure")),
136
+ metric("incurred-primary-per-exposure", "Incurred per exposure (primary)", "Primary incurred divided by exposure", "currency-per-exposure", 1, m(C.incurredPrimary), m(C.exposure), "primary incurred", "exposure", primaryBasis, version, override("incurred-primary-per-exposure")),
137
+ metric("incurred-250-per-non-cnp", "Incurred severity ($250K)", "$250K incurred divided by reported less closed-no-pay claims", "currency-per-claim", 1, m(C.incurred250), nonClosedNoPay, "$250K incurred", "reported less closed-no-pay claims", limitedBasis, version, override("incurred-250-per-non-cnp")),
138
+ metric("incurred-primary-per-non-cnp", "Incurred severity (primary)", "Primary incurred divided by reported less closed-no-pay claims", "currency-per-claim", 1, m(C.incurredPrimary), nonClosedNoPay, "primary incurred", "reported less closed-no-pay claims", primaryBasis, version, override("incurred-primary-per-non-cnp")),
139
+ metric("paid-250-per-exposure", "Paid per exposure ($250K)", "$250K paid divided by exposure", "currency-per-exposure", 1, m(C.paid250), m(C.exposure), "$250K paid", "exposure", limitedBasis, version, override("paid-250-per-exposure")),
140
+ metric("paid-primary-per-exposure", "Paid per exposure (primary)", "Primary paid divided by exposure", "currency-per-exposure", 1, m(C.paidPrimary), m(C.exposure), "primary paid", "exposure", primaryBasis, version, override("paid-primary-per-exposure")),
141
+ metric("paid-250-per-closed-with-pay", "Paid severity ($250K)", "$250K paid divided by closed-with-pay claims", "currency-per-claim", 1, m(C.paid250), m(C.closedWithPay), "$250K paid", "closed-with-pay claims", limitedBasis, version, override("paid-250-per-closed-with-pay")),
142
+ metric("paid-primary-per-closed-with-pay", "Paid severity (primary)", "Primary paid divided by closed-with-pay claims", "currency-per-claim", 1, m(C.paidPrimary), m(C.closedWithPay), "primary paid", "closed-with-pay claims", primaryBasis, version, override("paid-primary-per-closed-with-pay")),
143
+ metric("case-250-per-open", "Case reserve per open claim ($250K)", "$250K incurred less paid divided by open claims", "currency-per-claim", 1, sub(m(C.incurred250), m(C.paid250)), m(C.open), "$250K incurred less paid", "open claims", limitedBasis, version, override("case-250-per-open")),
144
+ metric("case-primary-per-open", "Case reserve per open claim (primary)", "Primary incurred less paid divided by open claims", "currency-per-claim", 1, sub(m(C.incurredPrimary), m(C.paidPrimary)), m(C.open), "primary incurred less paid", "open claims", primaryBasis, version, override("case-primary-per-open")),
145
+ ];
146
+ }
147
+
148
+ export const CASUALTY_QUARTERLY_METRICS = createCasualtyQuarterlyMetrics();
149
+
150
+ export interface CasualtyAmountLayerOptions {
151
+ components?: Partial<Pick<CasualtyDiagnosticComponentKeys, "paid250" | "incurred250" | "paidPrimary" | "incurredPrimary">>;
152
+ limited250?: {
153
+ id?: string;
154
+ displayName?: string;
155
+ paidSourceMeasure?: string;
156
+ incurredSourceMeasure?: string;
157
+ };
158
+ primary?: {
159
+ id?: string;
160
+ displayName?: string;
161
+ indemnityPaidMeasure?: string;
162
+ indemnityIncurredMeasure?: string;
163
+ expensePaidMeasure?: string;
164
+ expenseIncurredMeasure?: string;
165
+ indemnityLimit?: number;
166
+ };
167
+ }
168
+
169
+ export function createCasualtyAmountLayers(
170
+ options: CasualtyAmountLayerOptions = {},
171
+ ): readonly AmountLayerDefinition[] {
172
+ const C = {
173
+ ...CASUALTY_DIAGNOSTIC_COMPONENTS,
174
+ ...definedProperties(options.components),
175
+ };
176
+ const limit = options.primary?.indemnityLimit ?? 1_000_000;
177
+ if (!Number.isFinite(limit) || limit <= 0) {
178
+ throw new ReservingError("BAD_CAP", `Primary indemnity limit must be positive; got ${limit}`);
179
+ }
180
+ return [
181
+ {
182
+ id: options.limited250?.id ?? "250k-pre-capped-total",
183
+ displayName: options.limited250?.displayName ?? "$250K pre-capped total",
184
+ paidMeasure: C.paid250,
185
+ incurredMeasure: C.incurred250,
186
+ paid: { op: "measure", measure: options.limited250?.paidSourceMeasure ?? "preCapped250Paid" },
187
+ incurred: { op: "measure", measure: options.limited250?.incurredSourceMeasure ?? "preCapped250Incurred" },
188
+ basis: "pre-capped-additive",
189
+ },
190
+ {
191
+ id: options.primary?.id ?? "primary-1m-indemnity-plus-expense",
192
+ displayName: options.primary?.displayName ?? "Primary: $1M capped indemnity plus unlimited expense",
193
+ paidMeasure: C.paidPrimary,
194
+ incurredMeasure: C.incurredPrimary,
195
+ paid: { op: "add", terms: [{ op: "claim-cap", measure: options.primary?.indemnityPaidMeasure ?? "indemnityPaid", limit }, { op: "measure", measure: options.primary?.expensePaidMeasure ?? "expensePaid" }] },
196
+ incurred: { op: "add", terms: [{ op: "claim-cap", measure: options.primary?.indemnityIncurredMeasure ?? "indemnityIncurred", limit }, { op: "measure", measure: options.primary?.expenseIncurredMeasure ?? "expenseIncurred" }] },
197
+ basis: "claim-level-cap",
198
+ },
199
+ ];
200
+ }
201
+
202
+ export const CASUALTY_AMOUNT_LAYERS = createCasualtyAmountLayers();
@@ -1,7 +1,13 @@
1
1
  import type { Triangle } from "./types.js";
2
2
  import { ReservingError } from "./types.js";
3
3
  import { cumulativeToIncremental } from "./triangleAlgebra.js";
4
- import { isNum, lastJointObservedIndex, lastObservedIndex, safeRatio } from "./util.js";
4
+ import {
5
+ assertSameShape,
6
+ isNum,
7
+ lastJointObservedIndex,
8
+ lastObservedIndex,
9
+ safeRatio,
10
+ } from "./util.js";
5
11
 
6
12
  /**
7
13
  * Fisher-Lange disposal-rate (average-cost-per-claim-settled) method: the
@@ -92,25 +98,17 @@ export interface FisherLangeResult {
92
98
  warnings: string[];
93
99
  }
94
100
 
95
- function assertSameShape(a: Triangle, b: Triangle): void {
96
- const sameOrigins =
97
- a.origins.length === b.origins.length && a.origins.every((o, i) => o === b.origins[i]);
98
- const sameAges = a.ages.length === b.ages.length && a.ages.every((v, j) => v === b.ages[j]);
99
- if (!sameOrigins || !sameAges) {
100
- throw new ReservingError(
101
- "SHAPE",
102
- "The paid and closed-count triangles must share identical origins and development ages",
103
- );
104
- }
105
- }
106
-
107
101
  export function runFisherLange(
108
102
  paid: Triangle,
109
103
  closedCounts: Triangle,
110
104
  ultimateCounts: number[],
111
105
  options: FisherLangeOptions,
112
106
  ): FisherLangeResult {
113
- assertSameShape(paid, closedCounts);
107
+ assertSameShape(
108
+ paid,
109
+ closedCounts,
110
+ "The paid and closed-count triangles must share identical origins and development ages",
111
+ );
114
112
  const nOrigins = paid.origins.length;
115
113
  const nAges = paid.ages.length;
116
114
  if (nAges < 2) {
package/src/freqSev.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { TriangleKind, LdfSelections, Triangle } from "./types.js";
2
2
  import { ReservingError } from "./types.js";
3
- import { isNum, lastObservedIndex, safeRatio } from "./util.js";
3
+ import { assertSameShape, isNum, lastObservedIndex, safeRatio } from "./util.js";
4
4
  import { runChainLadder } from "./chainladder.js";
5
5
 
6
6
  /**
@@ -57,7 +57,11 @@ export interface FrequencySeverityResult {
57
57
  * missing, zero, or negative count yields a null severity cell.
58
58
  */
59
59
  export function severityTriangle(lossTri: Triangle, countTri: Triangle): Triangle {
60
- assertSameShape(lossTri, countTri);
60
+ assertSameShape(
61
+ lossTri,
62
+ countTri,
63
+ "The loss and count triangles must share identical origins and development ages",
64
+ );
61
65
  return {
62
66
  kind: lossTri.kind,
63
67
  origins: [...lossTri.origins],
@@ -68,24 +72,16 @@ export function severityTriangle(lossTri: Triangle, countTri: Triangle): Triangl
68
72
  };
69
73
  }
70
74
 
71
- function assertSameShape(a: Triangle, b: Triangle): void {
72
- const sameOrigins =
73
- a.origins.length === b.origins.length && a.origins.every((o, i) => o === b.origins[i]);
74
- const sameAges = a.ages.length === b.ages.length && a.ages.every((v, i) => v === b.ages[i]);
75
- if (!sameOrigins || !sameAges) {
76
- throw new ReservingError(
77
- "SHAPE",
78
- "The loss and count triangles must share identical origins and development ages",
79
- );
80
- }
81
- }
82
-
83
75
  export function runFrequencySeverity(
84
76
  lossTri: Triangle,
85
77
  countTri: Triangle,
86
78
  options: FrequencySeverityOptions,
87
79
  ): FrequencySeverityResult {
88
- assertSameShape(lossTri, countTri);
80
+ assertSameShape(
81
+ lossTri,
82
+ countTri,
83
+ "The loss and count triangles must share identical origins and development ages",
84
+ );
89
85
  const warnings: string[] = [];
90
86
 
91
87
  const countCl = runChainLadder(countTri, {
package/src/index.ts CHANGED
@@ -14,6 +14,9 @@ export * from "./tail.js";
14
14
  export * from "./berquist.js";
15
15
  export * from "./clark.js";
16
16
  export * from "./diagnostics.js";
17
+ export * from "./periods.js";
18
+ export * from "./metricDiagnostics.js";
19
+ export * from "./casualtyDiagnostics.js";
17
20
  export * from "./mack.js";
18
21
  export * from "./capping.js";
19
22
  export * from "./ilf.js";