@idlekit/core 0.5.0 → 0.7.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 +77 -15
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ltv/cohort.d.ts +59 -0
- package/dist/ltv/cohort.js +170 -0
- package/dist/ltv/cohort.js.map +1 -0
- package/dist/ltv/projection.d.ts +7 -6
- package/dist/ltv/projection.js +7 -2
- package/dist/ltv/projection.js.map +1 -1
- package/dist/ltv/public.d.ts +24 -16
- package/dist/ltv/public.js +60 -20
- package/dist/ltv/public.js.map +1 -1
- package/dist/ltv/types.d.ts +17 -0
- package/dist/ltv/uncertainty.d.ts +9 -10
- package/dist/ltv/uncertainty.js +35 -5
- package/dist/ltv/uncertainty.js.map +1 -1
- package/dist/report/compare.js +51 -20
- package/dist/report/compare.js.map +1 -1
- package/dist/scenario/scenarioContract.d.ts +2 -2
- package/dist/scenario/scenarioContract.js +2 -2
- package/dist/sim/analysis/experiment.d.ts +11 -1
- package/dist/sim/analysis/experiment.js +325 -236
- package/dist/sim/analysis/experiment.js.map +1 -1
- package/dist/sim/analysis/flow.d.ts +7 -7
- package/dist/sim/analysis/flow.js +51 -11
- package/dist/sim/analysis/flow.js.map +1 -1
- package/dist/sim/analysis/judge.d.ts +12 -1
- package/dist/sim/analysis/judge.js +64 -19
- package/dist/sim/analysis/judge.js.map +1 -1
- package/dist/sim/executionBackend.d.ts +2 -2
- package/dist/sim/executionBackend.js +8 -2
- package/dist/sim/executionBackend.js.map +1 -1
- package/dist/sim/integrationMatrix.d.ts +17 -6
- package/dist/sim/integrationMatrix.js +30 -10
- package/dist/sim/integrationMatrix.js.map +1 -1
- package/dist/sim/milestoneKeys.d.ts +11 -0
- package/dist/sim/milestoneKeys.js +16 -0
- package/dist/sim/milestoneKeys.js.map +1 -0
- package/dist/sim/observation.d.ts +3 -1
- package/dist/sim/observation.js +9 -4
- package/dist/sim/observation.js.map +1 -1
- package/dist/sim/offline.d.ts +1 -1
- package/dist/sim/offline.js +1 -1
- package/dist/sim/session.d.ts +6 -2
- package/dist/sim/session.js +18 -6
- package/dist/sim/session.js.map +1 -1
- package/dist/sim/sessionOffline.d.ts +40 -0
- package/dist/sim/sessionOffline.js +53 -0
- package/dist/sim/sessionOffline.js.map +1 -0
- package/dist/sim/strategy/opt/finiteAmountLog10.d.ts +10 -0
- package/dist/sim/strategy/opt/finiteAmountLog10.js +19 -0
- package/dist/sim/strategy/opt/finiteAmountLog10.js.map +1 -0
- package/dist/sim/strategy/opt/finiteMean.d.ts +2 -0
- package/dist/sim/strategy/opt/finiteMean.js +37 -0
- package/dist/sim/strategy/opt/finiteMean.js.map +1 -0
- package/dist/sim/strategy/opt/objectives/builtins.js +4 -4
- package/dist/sim/strategy/opt/objectives/builtins.js.map +1 -1
- package/dist/sim/strategy/opt/runner.js +12 -4
- package/dist/sim/strategy/opt/runner.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,32 +20,94 @@ Use this package when you need:
|
|
|
20
20
|
|
|
21
21
|
## LTV estimates
|
|
22
22
|
|
|
23
|
-
Import the three LTV functions
|
|
24
|
-
|
|
25
|
-
`
|
|
26
|
-
|
|
23
|
+
Import the three LTV functions, the `retentionAtDay` / `progressionFactor` helpers and the
|
|
24
|
+
`MonetizationConfig`, `LtvEstimateArgs`, `LtvDistributionArgs`, `LtvPointEstimate` and
|
|
25
|
+
`LtvDistribution` types from `@idlekit/core`. Pass a `ScenarioV1` to `deriveMonetizationConfig`;
|
|
26
|
+
pass the returned config, `horizonDays` and `progression` to `estimateLtvPerUser`.
|
|
27
|
+
`estimateLtvDistribution` also reads `draws`, `quantiles` and `seed`; an omitted one falls back
|
|
28
|
+
to `config.uncertainty`, and an explicit one wins. It always samples: `config.uncertainty.enabled`
|
|
29
|
+
is the caller's switch for whether to call it, as in the CLI. The CLI uses the same implementation.
|
|
30
|
+
|
|
31
|
+
LTV is undiscounted cumulative value. A horizon of `H` days covers install days `[0, H)`: day 0
|
|
32
|
+
is the install day, active by definition, as in telemetry and `idk calibrate`. A fractional last
|
|
33
|
+
day is prorated at that day's retention, so 0.5 days is half of the install day. Earlier releases
|
|
34
|
+
covered days `[1, H]`, dropped install-day revenue and charged a 0.5-day horizon at D1
|
|
35
|
+
retention. The same horizon now adds the install day and drops day `H`, so LTV values change
|
|
36
|
+
and usually rise.
|
|
27
37
|
|
|
28
38
|
Inputs are hypothetical assumptions, in one caller-chosen currency per installed user.
|
|
29
39
|
Defaults are not observed retention or measured revenue. Net revenue applies platform fees
|
|
30
|
-
and gross margin; LTV then subtracts CPI, including at a zero-day horizon.
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
and gross margin; LTV then subtracts CPI, including at a zero-day horizon. Finite progression
|
|
41
|
+
is clamped to 0..1. Quantile bands describe sensitivity to assumptions, not fitted confidence
|
|
42
|
+
intervals or evidence of enjoyment/retention.
|
|
33
43
|
|
|
34
44
|
Synchronous limits: 0..36500 days, 1..100000 draws and at most 10000000 draw-days (at least
|
|
35
|
-
one work unit per draw). Quantiles must be
|
|
36
|
-
percent
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
one work unit per draw). Quantiles must be 1..101 distinct values in 0..1. Each output key is
|
|
46
|
+
the exact percent (`q50`, `q90`, `q2.5` for 0.025, `q12.4` for 0.124), so keys never collide or
|
|
47
|
+
round. The seed must be an integer in 0..4294967295; other seeds throw instead of being folded.
|
|
48
|
+
`retentionAtDay` requires a finite day >= 0 and a valid curve (day 0 is 1, any day in (0, 1] reads
|
|
49
|
+
D1, and anchors from D1 on are linear); `progressionFactor` rejects NaN
|
|
50
|
+
logs and a span that is not finite and positive, and treats an infinite worth log (zero worth)
|
|
51
|
+
as no progression. Invalid inputs and numeric overflow throw `RangeError`. Sampling shrinks
|
|
52
|
+
inconsistent correlation matrices toward independence.
|
|
53
|
+
Complete-cohort retention observation and safe patch application are available in core;
|
|
54
|
+
the public `idk calibrate` CLI shares the observation and planning implementation.
|
|
55
|
+
Public estimate configs accept quantile endpoints too. The point estimate validates the whole
|
|
56
|
+
config, including the uncertainty defaults the distribution reads; key distinctness and the
|
|
57
|
+
seed range are checked when the distribution uses them. `ScenarioV1` authored quantiles retain
|
|
41
58
|
their existing `(0, 1)` schema when passed to `deriveMonetizationConfig`.
|
|
42
59
|
|
|
43
60
|
Partial scenario retention overrides keep the CLI's independent defaults. For example,
|
|
44
61
|
`d1: 0.1` alone retains the default `d7: 0.2`; estimates preserve that existing interpolation.
|
|
45
62
|
Explicit complete scenario curves still follow scenario ordering validation. Direct estimate
|
|
46
63
|
configs validate each rate's range without adding an ordering constraint to completed defaults.
|
|
47
|
-
|
|
48
|
-
|
|
64
|
+
The point projection and the distribution interpolate such a rising curve the same way: one
|
|
65
|
+
shared retention shock scales every anchor, and sampling no longer clips a later anchor to the
|
|
66
|
+
preceding one, so a zero-uncertainty distribution equals the point projection.
|
|
67
|
+
|
|
68
|
+
## Observed cohort retention
|
|
69
|
+
|
|
70
|
+
Import `observeCohort`, `compareRetention`, `planRetentionPatch` and `applyRetentionPatch`
|
|
71
|
+
from `@idlekit/core`. `CohortActivityRow` requires `userId` and integer `day`; `active`
|
|
72
|
+
defaults to true. Money fields are optional and are never fitted by these retention APIs.
|
|
73
|
+
Every installed user needs a day-zero row, including nonreturners, and the caller must
|
|
74
|
+
provide a complete activity export with a common, fully mature `observedThroughDay`.
|
|
75
|
+
The API checks consistency; it cannot prove export completeness.
|
|
76
|
+
|
|
77
|
+
`observeCohort(rows, cutoff)` preserves future rates/intervals as null. `planRetentionPatch`
|
|
78
|
+
returns `ready`, `unavailable` or `requires-review`; every unobserved target day requires
|
|
79
|
+
an explicit retained assumption. Observations that rise or conflict with retained values
|
|
80
|
+
remain unchanged and cannot become an automatic patch. `applyRetentionPatch({ scenario,
|
|
81
|
+
observation })` recomputes the plan, validates and returns an independent scenario copy.
|
|
82
|
+
It throws if the patch is not ready and never replaces revenue, fees, CPI, uncertainty,
|
|
83
|
+
cohort size or unobserved retention. `assertCohortObservation` validates serialized input.
|
|
84
|
+
|
|
85
|
+
See [the complete SDK example](https://github.com/imjlk/idlekit/blob/main/examples/telemetry/cohort-sdk.ts)
|
|
86
|
+
and [the cohort export guide](https://github.com/imjlk/idlekit/blob/main/docs/player-telemetry.md).
|
|
87
|
+
|
|
88
|
+
## Game-specific offline settlement
|
|
89
|
+
|
|
90
|
+
`simulateSessionPattern({ scenario, pattern, prepareOffline })` optionally calls a synchronous
|
|
91
|
+
`SessionOfflinePreparer` once per nonempty absence. It receives `ctx`, `model`, a state snapshot,
|
|
92
|
+
`seconds`, `wallStartT`, `wallEndT` and the current policy snapshot. Numeric prototypes are
|
|
93
|
+
preserved in the snapshots; treat the shared engine, context and model as immutable.
|
|
94
|
+
Use the explicit wall timestamps for expiry checks after earlier capped gaps.
|
|
95
|
+
|
|
96
|
+
Return any of `model`, `stepSec` and `policy`. They apply only to this gap; omission inherits
|
|
97
|
+
the scenario setting. `policy` replaces cap/decay, so `{}` removes both. The session's action
|
|
98
|
+
policy stays in `scenario.run.offline.actions`; a callback cannot override it. A replacement
|
|
99
|
+
model declares its own `clocks.respondsTo` needs. The SDK still performs reward ticks, goals,
|
|
100
|
+
until checks, action filtering, observations and wall/reward accounting.
|
|
101
|
+
|
|
102
|
+
A game with one atomic settlement can compute it on the snapshot and return a model that
|
|
103
|
+
imports its result in one reward tick, with `stepSec` equal to its effective credited seconds
|
|
104
|
+
(use a positive step when credit is zero). Stops are checked at tick boundaries; a one-tick
|
|
105
|
+
settlement does not expose intermediate game states. Preparation itself must not grant an
|
|
106
|
+
external reward: zero credit or an already-reached goal may execute no transition at all.
|
|
107
|
+
For partial settlement or intra-gap goals, supply a model that evolves per smaller tick.
|
|
108
|
+
|
|
109
|
+
This is an SDK callback, not a JSON scenario field. The default session path is unchanged.
|
|
110
|
+
See [the big-number offline example](https://github.com/imjlk/idlekit/blob/main/examples/session/offline-preparation.ts).
|
|
49
111
|
|
|
50
112
|
## Quick example
|
|
51
113
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AAEvC,cAAc,eAAe,CAAC;AAE9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AAEnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8BAA8B,CAAC;AAE7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AAEjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AAEnC,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAElC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAu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AAc,oBAAoB,CAAC;AACnC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,4BAA4B,CAAC;AAE3C,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AAEjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ScenarioV1 } from "../scenario/types";
|
|
2
|
+
/** Aggregated activity for a complete install cohort. Missing revenue is not measured zero. */
|
|
3
|
+
export type CohortActivityRow = Readonly<{
|
|
4
|
+
userId: string;
|
|
5
|
+
day: number;
|
|
6
|
+
active?: boolean;
|
|
7
|
+
iapRevenue?: number;
|
|
8
|
+
adRevenue?: number;
|
|
9
|
+
acquisitionCost?: number;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const RETENTION_DAYS: readonly [1, 7, 30, 90];
|
|
12
|
+
export type RetentionKey = `d${typeof RETENTION_DAYS[number]}`;
|
|
13
|
+
export type RetentionAssumptions = Partial<Record<RetentionKey, number>>;
|
|
14
|
+
export type CohortObservation = Readonly<{
|
|
15
|
+
observedThroughDay: number;
|
|
16
|
+
users: number;
|
|
17
|
+
retention: readonly Readonly<{
|
|
18
|
+
day: number;
|
|
19
|
+
status: "observed" | "not-observed";
|
|
20
|
+
users: number;
|
|
21
|
+
activeUsers: number | null;
|
|
22
|
+
rate: number | null;
|
|
23
|
+
interval95: Readonly<{
|
|
24
|
+
min: number;
|
|
25
|
+
max: number;
|
|
26
|
+
}> | null;
|
|
27
|
+
}>[];
|
|
28
|
+
}>;
|
|
29
|
+
/** Exact-day retention for one complete install cohort, with an explicit export cutoff. */
|
|
30
|
+
export declare function observeCohort(rows: readonly CohortActivityRow[], observedThroughDay: number): CohortObservation;
|
|
31
|
+
/** Validate serialized observations, including invariants portable JSON Schema cannot express. */
|
|
32
|
+
export declare function assertCohortObservation(observation: unknown): asserts observation is CohortObservation;
|
|
33
|
+
/** Return only the fully observed anchors; future days remain absent. */
|
|
34
|
+
export declare function observedRetentionRates(observation: CohortObservation): RetentionAssumptions;
|
|
35
|
+
export type RetentionPatchPlan = Readonly<{
|
|
36
|
+
status: "ready" | "unavailable" | "requires-review";
|
|
37
|
+
retention: RetentionAssumptions;
|
|
38
|
+
warnings: readonly string[];
|
|
39
|
+
}>;
|
|
40
|
+
/** Keep exact observations separate from the non-increasing curve scenario validation requires. */
|
|
41
|
+
export declare function planRetentionPatch(observation: CohortObservation, assumptions?: RetentionAssumptions): RetentionPatchPlan;
|
|
42
|
+
export type RetentionComparison = Readonly<{
|
|
43
|
+
day: number;
|
|
44
|
+
assumption: number | null;
|
|
45
|
+
observed: number | null;
|
|
46
|
+
differencePercentagePoints: number | null;
|
|
47
|
+
status: "not-observed" | "unconfigured" | "compared";
|
|
48
|
+
}>;
|
|
49
|
+
/** Compare observed anchors with explicit assumptions, preserving unobserved states. */
|
|
50
|
+
export declare function compareRetention(observation: CohortObservation, assumptions: RetentionAssumptions): RetentionComparison[];
|
|
51
|
+
/**
|
|
52
|
+
* Recompute a ready patch against this scenario, apply observed anchors to a copy,
|
|
53
|
+
* and validate the result. All unobserved and non-retention assumptions stay intact.
|
|
54
|
+
* A valid observation does not prove that the caller exported every install/activity.
|
|
55
|
+
*/
|
|
56
|
+
export declare function applyRetentionPatch(args: {
|
|
57
|
+
scenario: ScenarioV1;
|
|
58
|
+
observation: CohortObservation;
|
|
59
|
+
}): ScenarioV1;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { validateScenarioV1 } from "../scenario/validate";
|
|
2
|
+
import { deepClonePreservingPrototype } from "../utils/deepClone";
|
|
3
|
+
export const RETENTION_DAYS = Object.freeze([1, 7, 30, 90]);
|
|
4
|
+
function wilsonInterval(activeUsers, users) {
|
|
5
|
+
// Wilson score interval: finite bounds even for zero or complete retention.
|
|
6
|
+
const rate = activeUsers / users;
|
|
7
|
+
const z2 = 1.959963984540054 ** 2;
|
|
8
|
+
const denominator = 1 + z2 / users;
|
|
9
|
+
const center = (rate + z2 / (2 * users)) / denominator;
|
|
10
|
+
const radius = Math.sqrt(z2 * (rate * (1 - rate) / users + z2 / (4 * users * users))) / denominator;
|
|
11
|
+
return { min: activeUsers === 0 ? 0 : Math.max(0, center - radius), max: activeUsers === users ? 1 : Math.min(1, center + radius) };
|
|
12
|
+
}
|
|
13
|
+
/** Exact-day retention for one complete install cohort, with an explicit export cutoff. */
|
|
14
|
+
export function observeCohort(rows, observedThroughDay) {
|
|
15
|
+
if (!Number.isSafeInteger(observedThroughDay) || observedThroughDay < 0)
|
|
16
|
+
throw new Error("observed-through-day must be a nonnegative safe integer");
|
|
17
|
+
if (!Array.isArray(rows) || rows.length === 0)
|
|
18
|
+
throw new Error("Observed cohort requires at least one install record");
|
|
19
|
+
const users = new Map();
|
|
20
|
+
for (const row of rows) {
|
|
21
|
+
if (!row || typeof row.userId !== "string" || !row.userId.trim() || !Number.isSafeInteger(row.day) || row.day < 0 || row.day > observedThroughDay) {
|
|
22
|
+
throw new Error("Observed cohort requires a user ID and integer days within observed-through-day");
|
|
23
|
+
}
|
|
24
|
+
if (![row.iapRevenue, row.adRevenue, row.acquisitionCost].every(v => v === undefined || (typeof v === "number" && Number.isFinite(v) && v >= 0))) {
|
|
25
|
+
throw new Error("Observed cohort revenue and acquisition costs must be finite and nonnegative");
|
|
26
|
+
}
|
|
27
|
+
if (row.active !== undefined && typeof row.active !== "boolean")
|
|
28
|
+
throw new Error("Observed cohort active must be boolean");
|
|
29
|
+
const user = users.get(row.userId) ?? { days: new Set(), active: new Set() };
|
|
30
|
+
if (user.days.has(row.day))
|
|
31
|
+
throw new Error("Observed cohort requires one aggregated row per user and day");
|
|
32
|
+
user.days.add(row.day);
|
|
33
|
+
if (row.active ?? true)
|
|
34
|
+
user.active.add(row.day);
|
|
35
|
+
users.set(row.userId, user);
|
|
36
|
+
}
|
|
37
|
+
for (const user of users.values()) {
|
|
38
|
+
if (!user.days.has(0))
|
|
39
|
+
throw new Error("Observed cohort requires a day 0 install record for every user, including inactive users");
|
|
40
|
+
}
|
|
41
|
+
const n = users.size;
|
|
42
|
+
const observation = {
|
|
43
|
+
observedThroughDay,
|
|
44
|
+
users: n,
|
|
45
|
+
retention: RETENTION_DAYS.map(day => {
|
|
46
|
+
if (day > observedThroughDay)
|
|
47
|
+
return { day, status: "not-observed", users: n, activeUsers: null, rate: null, interval95: null };
|
|
48
|
+
const activeUsers = [...users.values()].filter(user => user.active.has(day)).length;
|
|
49
|
+
const rate = activeUsers / n;
|
|
50
|
+
return { day, status: "observed", users: n, activeUsers, rate, interval95: wilsonInterval(activeUsers, n) };
|
|
51
|
+
}),
|
|
52
|
+
};
|
|
53
|
+
assertCohortObservation(observation);
|
|
54
|
+
return observation;
|
|
55
|
+
}
|
|
56
|
+
function object(value) {
|
|
57
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
58
|
+
}
|
|
59
|
+
/** Validate serialized observations, including invariants portable JSON Schema cannot express. */
|
|
60
|
+
export function assertCohortObservation(observation) {
|
|
61
|
+
if (!object(observation) || typeof observation.users !== "number" || !Number.isSafeInteger(observation.users) || observation.users < 1 || typeof observation.observedThroughDay !== "number" || !Number.isSafeInteger(observation.observedThroughDay) || observation.observedThroughDay < 0) {
|
|
62
|
+
throw new Error("Invalid cohort population or observation cutoff");
|
|
63
|
+
}
|
|
64
|
+
if (!Array.isArray(observation.retention) || observation.retention.length !== RETENTION_DAYS.length)
|
|
65
|
+
throw new Error("Cohort retention must contain D1, D7, D30 and D90");
|
|
66
|
+
for (const [index, value] of observation.retention.entries()) {
|
|
67
|
+
const point = value;
|
|
68
|
+
if (!object(point) || typeof point.day !== "number" || point.day !== RETENTION_DAYS[index] || point.users !== observation.users)
|
|
69
|
+
throw new Error("Cohort retention days and population must match the observation");
|
|
70
|
+
const observed = point.day <= observation.observedThroughDay;
|
|
71
|
+
if (point.status !== (observed ? "observed" : "not-observed"))
|
|
72
|
+
throw new Error("Cohort retention status must match the observation cutoff");
|
|
73
|
+
if (!observed) {
|
|
74
|
+
if (point.activeUsers !== null || point.rate !== null || point.interval95 !== null)
|
|
75
|
+
throw new Error("Future retention must remain unobserved");
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (typeof point.activeUsers !== "number" || !Number.isSafeInteger(point.activeUsers) || point.activeUsers < 0 || point.activeUsers > observation.users || point.rate !== point.activeUsers / observation.users) {
|
|
79
|
+
throw new Error("Retained users and retention rate must match the installed population");
|
|
80
|
+
}
|
|
81
|
+
const interval = point.interval95;
|
|
82
|
+
if (typeof point.rate !== "number" || !object(interval) || typeof interval.min !== "number" || typeof interval.max !== "number" || !Number.isFinite(interval.min) || !Number.isFinite(interval.max) || interval.min < 0 || interval.max > 1 || interval.min > point.rate || interval.max < point.rate) {
|
|
83
|
+
throw new Error("Retention confidence interval must be finite and contain the observed rate");
|
|
84
|
+
}
|
|
85
|
+
const expected = wilsonInterval(point.activeUsers, observation.users);
|
|
86
|
+
if (interval.min !== expected.min || interval.max !== expected.max) {
|
|
87
|
+
throw new Error("Retention confidence interval must match the Wilson 95% bounds for the observed population");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** Return only the fully observed anchors; future days remain absent. */
|
|
92
|
+
export function observedRetentionRates(observation) {
|
|
93
|
+
assertCohortObservation(observation);
|
|
94
|
+
const rates = {};
|
|
95
|
+
for (const row of observation.retention) {
|
|
96
|
+
if (row.rate !== null)
|
|
97
|
+
rates[`d${row.day}`] = row.rate;
|
|
98
|
+
}
|
|
99
|
+
return rates;
|
|
100
|
+
}
|
|
101
|
+
function assertRetentionAssumptions(assumptions) {
|
|
102
|
+
if (!object(assumptions))
|
|
103
|
+
throw new RangeError("Retention assumptions must be an object");
|
|
104
|
+
for (const day of RETENTION_DAYS) {
|
|
105
|
+
const rate = assumptions[`d${day}`];
|
|
106
|
+
if (rate !== undefined && (typeof rate !== "number" || !Number.isFinite(rate) || rate < 0 || rate > 1)) {
|
|
107
|
+
throw new RangeError(`Retention assumption d${day} must be finite and in 0..1`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Keep exact observations separate from the non-increasing curve scenario validation requires. */
|
|
112
|
+
export function planRetentionPatch(observation, assumptions = {}) {
|
|
113
|
+
assertRetentionAssumptions(assumptions);
|
|
114
|
+
const rates = observedRetentionRates(observation);
|
|
115
|
+
if (Object.keys(rates).length === 0)
|
|
116
|
+
return { status: "unavailable", retention: {}, warnings: ["No retention day has been fully observed."] };
|
|
117
|
+
const merged = { ...assumptions, ...rates };
|
|
118
|
+
const warnings = [];
|
|
119
|
+
for (const day of RETENTION_DAYS) {
|
|
120
|
+
if (merged[`d${day}`] === undefined)
|
|
121
|
+
warnings.push(`Unobserved D${day} has no explicit retained assumption. An LTV default could conflict with the observations; supply monetization.retention.d${day} in the scenario or observe this day before applying a patch.`);
|
|
122
|
+
}
|
|
123
|
+
let previous;
|
|
124
|
+
for (const day of RETENTION_DAYS) {
|
|
125
|
+
const rate = merged[`d${day}`];
|
|
126
|
+
if (rate === undefined)
|
|
127
|
+
continue;
|
|
128
|
+
if (previous && previous.rate < rate)
|
|
129
|
+
warnings.push(`D${previous.day}=${previous.rate} is below D${day}=${rate}. A complete scenario retention curve must satisfy d1 >= d7 >= d30 >= d90, and the LTV model would interpolate a rise as given; review observations and retained assumptions before applying a patch.`);
|
|
130
|
+
previous = { day, rate };
|
|
131
|
+
}
|
|
132
|
+
return warnings.length ? { status: "requires-review", retention: {}, warnings } : { status: "ready", retention: rates, warnings };
|
|
133
|
+
}
|
|
134
|
+
/** Compare observed anchors with explicit assumptions, preserving unobserved states. */
|
|
135
|
+
export function compareRetention(observation, assumptions) {
|
|
136
|
+
assertCohortObservation(observation);
|
|
137
|
+
assertRetentionAssumptions(assumptions);
|
|
138
|
+
return observation.retention.map(row => {
|
|
139
|
+
const assumption = assumptions[`d${row.day}`] ?? null;
|
|
140
|
+
return {
|
|
141
|
+
day: row.day,
|
|
142
|
+
assumption,
|
|
143
|
+
observed: row.rate,
|
|
144
|
+
differencePercentagePoints: row.rate === null || assumption === null ? null : (row.rate - assumption) * 100,
|
|
145
|
+
status: row.rate === null ? "not-observed" : assumption === null ? "unconfigured" : "compared",
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Recompute a ready patch against this scenario, apply observed anchors to a copy,
|
|
151
|
+
* and validate the result. All unobserved and non-retention assumptions stay intact.
|
|
152
|
+
* A valid observation does not prove that the caller exported every install/activity.
|
|
153
|
+
*/
|
|
154
|
+
export function applyRetentionPatch(args) {
|
|
155
|
+
const valid = validateScenarioV1(args.scenario);
|
|
156
|
+
if (!valid.ok)
|
|
157
|
+
throw new RangeError(`Invalid scenario: ${valid.issues.map(issue => issue.message).join("; ")}`);
|
|
158
|
+
const plan = planRetentionPatch(args.observation, args.scenario.monetization?.retention);
|
|
159
|
+
if (plan.status !== "ready")
|
|
160
|
+
throw new RangeError(`Retention patch ${plan.status}: ${plan.warnings.join(" ")}`);
|
|
161
|
+
const copy = deepClonePreservingPrototype(args.scenario);
|
|
162
|
+
const result = { ...copy, monetization: {
|
|
163
|
+
...copy.monetization, retention: { ...copy.monetization?.retention, ...plan.retention },
|
|
164
|
+
} };
|
|
165
|
+
const applied = validateScenarioV1(result);
|
|
166
|
+
if (!applied.ok)
|
|
167
|
+
throw new RangeError(`Invalid calibrated scenario: ${applied.issues.map(issue => issue.message).join("; ")}`);
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=cohort.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cohort.js","sourceRoot":"","sources":["../../src/ltv/cohort.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,4BAA4B,EAAE,MAAM,oBAAoB,CAAC;AAYlE,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAU,CAAC,CAAC;AAgBrE,SAAS,cAAc,CAAC,WAAmB,EAAE,KAAa;IACxD,4EAA4E;IAC5E,MAAM,IAAI,GAAG,WAAW,GAAG,KAAK,CAAC;IACjC,MAAM,EAAE,GAAG,iBAAiB,IAAI,CAAC,CAAC;IAClC,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;IACnC,MAAM,MAAM,GAAG,CAAC,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,WAAW,CAAC;IACvD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,KAAK,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC;IACpG,OAAO,EAAE,GAAG,EAAE,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,EAAE,GA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M,IAAI,UAAU,CAAC,gCAAgC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC/H,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/ltv/projection.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { ScenarioV1 } from "../scenario/types";
|
|
2
|
-
import type { LtvPointEstimate, MonetizationConfig } from "./types";
|
|
2
|
+
import type { LtvEstimateArgs, LtvPointEstimate, MonetizationConfig } from "./types";
|
|
3
3
|
export declare function deriveMonetizationConfig(scenario: ScenarioV1): MonetizationConfig;
|
|
4
4
|
export declare function retentionAtDay(ret: MonetizationConfig["retention"], day: number): number;
|
|
5
5
|
export declare function progressionFactor(startWorthLog10: number, endWorthLog10: number, span: number): number;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Undiscounted revenue over install days [0, horizonDays). Day 0 is the install day,
|
|
8
|
+
* active by definition, as telemetry and calibration index it. A fractional last day
|
|
9
|
+
* is prorated at that day's retention.
|
|
10
|
+
*/
|
|
11
|
+
export declare function estimateLtvPerUser(args: LtvEstimateArgs): LtvPointEstimate;
|
package/dist/ltv/projection.js
CHANGED
|
@@ -74,6 +74,11 @@ export function progressionFactor(startWorthLog10, endWorthLog10, span) {
|
|
|
74
74
|
}
|
|
75
75
|
return clamp01((endWorthLog10 - startWorthLog10) / span);
|
|
76
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Undiscounted revenue over install days [0, horizonDays). Day 0 is the install day,
|
|
79
|
+
* active by definition, as telemetry and calibration index it. A fractional last day
|
|
80
|
+
* is prorated at that day's retention.
|
|
81
|
+
*/
|
|
77
82
|
export function estimateLtvPerUser(args) {
|
|
78
83
|
const { config, horizonDays } = args;
|
|
79
84
|
const progression = clamp01(args.progression);
|
|
@@ -83,12 +88,12 @@ export function estimateLtvPerUser(args) {
|
|
|
83
88
|
const fullDays = Math.floor(Math.max(0, horizonDays));
|
|
84
89
|
const remainder = Math.max(0, horizonDays - fullDays);
|
|
85
90
|
let gross = 0;
|
|
86
|
-
for (let day =
|
|
91
|
+
for (let day = 0; day < fullDays; day++) {
|
|
87
92
|
const active = retentionAtDay(config.retention, day);
|
|
88
93
|
gross += active * (payerConversion * arppuDaily + adArpDau);
|
|
89
94
|
}
|
|
90
95
|
if (remainder > 0) {
|
|
91
|
-
const active = retentionAtDay(config.retention, fullDays
|
|
96
|
+
const active = retentionAtDay(config.retention, fullDays);
|
|
92
97
|
gross += remainder * active * (payerConversion * arppuDaily + adArpDau);
|
|
93
98
|
}
|
|
94
99
|
const net = gross * (1 - config.revenue.platformFeeRate) * config.revenue.grossMarginRate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection.js","sourceRoot":"","sources":["../../src/ltv/projection.ts"],"names":[],"mappings":"AAGA,SAAS,OAAO,CAAC,CAAS;IACxB,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IACpB,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAC;IACpB,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,UAAU,wBAAwB,CAAC,QAAoB;IAC3D,MAAM,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC;IAChC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,EAAE,SAAS,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC;IACtG,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAA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AC;QAC1D,KAAK,IAAI,SAAS,GAAG,MAAM,GAAG,CAAC,eAAe,GAAG,UAAU,GAAG,QAAQ,CAAC,CAAC;IAC1E,CAAC;IAED,MAAM,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;IAC1F,MAAM,GAAG,GAAG,GAAG,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC;IAEzC,OAAO;QACL,6BAA6B,EAAE,KAAK;QACpC,2BAA2B,EAAE,GAAG;QAChC,oBAAoB,EAAE,GAAG;KAC1B,CAAC;AACJ,CAAC"}
|
package/dist/ltv/public.d.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import type { ScenarioV1 } from "../scenario/types";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import type { MonetizationConfig } from "./types";
|
|
5
|
-
export type { MonetizationConfig, LtvPointEstimate } from "./types";
|
|
6
|
-
export { progressionFactor, retentionAtDay } from "./projection";
|
|
2
|
+
import type { LtvDistribution, LtvDistributionArgs, LtvEstimateArgs, LtvPointEstimate, MonetizationConfig } from "./types";
|
|
3
|
+
export type { MonetizationConfig, LtvPointEstimate, LtvEstimateArgs, LtvDistributionArgs, LtvDistribution, } from "./types";
|
|
7
4
|
/** Complete a scenario's hypothetical assumptions; this does not fit observed player data. */
|
|
8
5
|
export declare function deriveMonetizationConfig(scenario: ScenarioV1): MonetizationConfig;
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Retention on install-day index `day`. Day 0 is the install day (1) and any day in (0, 1]
|
|
8
|
+
* reads D1; there is no ramp from day 0. From D1 to D90 it is linear between the anchors,
|
|
9
|
+
* and after D90 it decays. The LTV estimators only read whole days.
|
|
10
|
+
*/
|
|
11
|
+
export declare function retentionAtDay(retention: MonetizationConfig["retention"], day: number): number;
|
|
12
|
+
/**
|
|
13
|
+
* Share of `span` log10 steps between two worth logs, clamped to 0..1. An infinite
|
|
14
|
+
* worth log (zero or unbounded worth) contributes no progression.
|
|
15
|
+
*/
|
|
16
|
+
export declare function progressionFactor(startWorthLog10: number, endWorthLog10: number, span: number): number;
|
|
17
|
+
/**
|
|
18
|
+
* Undiscounted per-install value over install days [0, horizonDays), in one
|
|
19
|
+
* caller-chosen currency, including acquisition cost.
|
|
20
|
+
*/
|
|
21
|
+
export declare function estimateLtvPerUser(args: LtvEstimateArgs): LtvPointEstimate;
|
|
22
|
+
/**
|
|
23
|
+
* Seeded assumption sensitivity, not a confidence interval fitted to a player population.
|
|
24
|
+
* Omitted `draws`, `quantiles` and `seed` use `config.uncertainty`. It always samples;
|
|
25
|
+
* `config.uncertainty.enabled` is the caller's switch for whether to call it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function estimateLtvDistribution(args: LtvDistributionArgs): LtvDistribution;
|
package/dist/ltv/public.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { validateScenarioV1 } from "../scenario/validate";
|
|
2
|
-
import { deriveMonetizationConfig as derive, estimateLtvPerUser as point } from "./projection";
|
|
3
|
-
import { estimateLtvDistribution as distribution } from "./uncertainty";
|
|
4
|
-
export { progressionFactor, retentionAtDay } from "./projection";
|
|
2
|
+
import { deriveMonetizationConfig as derive, estimateLtvPerUser as point, progressionFactor as progressionFactorUnchecked, retentionAtDay as retentionAtDayUnchecked, } from "./projection";
|
|
3
|
+
import { estimateLtvDistribution as distribution, quantileKey } from "./uncertainty";
|
|
5
4
|
function assertMonetization(monetization) {
|
|
6
5
|
const valid = validateScenarioV1({
|
|
7
6
|
schemaVersion: 1, unit: { code: "LTV" }, policy: { mode: "drop" },
|
|
@@ -21,10 +20,10 @@ function assertQuantileValues(quantiles) {
|
|
|
21
20
|
throw new RangeError("quantiles must be nonempty and in 0..1");
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
|
-
function
|
|
25
|
-
const { retention, ...other } = config;
|
|
23
|
+
function assertRetention(retention) {
|
|
26
24
|
// Partial scenario retention has historically used independent defaults. Do not
|
|
27
|
-
// impose a new ordering constraint after filling those defaults in the CLI
|
|
25
|
+
// impose a new ordering constraint after filling those defaults in the CLI; the
|
|
26
|
+
// point estimate and the distribution both interpolate the anchors as given.
|
|
28
27
|
for (const day of ["d1", "d7", "d30", "d90"]) {
|
|
29
28
|
if (!Number.isFinite(retention[day]) || retention[day] < 0 || retention[day] > 1) {
|
|
30
29
|
throw new RangeError(`retention.${day} must be finite and in 0..1`);
|
|
@@ -33,10 +32,15 @@ function assertConfig(config) {
|
|
|
33
32
|
if (!Number.isFinite(retention.longTailDailyDecay) || retention.longTailDailyDecay < 0) {
|
|
34
33
|
throw new RangeError("retention.longTailDailyDecay must be finite and >= 0");
|
|
35
34
|
}
|
|
35
|
+
}
|
|
36
|
+
function assertConfig(config) {
|
|
37
|
+
const { retention, ...other } = config;
|
|
38
|
+
assertRetention(retention);
|
|
36
39
|
const { quantiles, ...uncertainty } = other.uncertainty;
|
|
37
40
|
assertQuantileValues(quantiles);
|
|
38
|
-
// The public estimator permits endpoints; ScenarioV1's authored quantile
|
|
39
|
-
//
|
|
41
|
+
// The public estimator permits endpoints; ScenarioV1's authored quantile schema
|
|
42
|
+
// is narrower. The whole config is checked once because draws, quantiles and seed
|
|
43
|
+
// are the distribution's defaults.
|
|
40
44
|
assertMonetization({ ...other, uncertainty });
|
|
41
45
|
}
|
|
42
46
|
/** Complete a scenario's hypothetical assumptions; this does not fit observed player data. */
|
|
@@ -46,6 +50,29 @@ export function deriveMonetizationConfig(scenario) {
|
|
|
46
50
|
assertConfig(config);
|
|
47
51
|
return config;
|
|
48
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Retention on install-day index `day`. Day 0 is the install day (1) and any day in (0, 1]
|
|
55
|
+
* reads D1; there is no ramp from day 0. From D1 to D90 it is linear between the anchors,
|
|
56
|
+
* and after D90 it decays. The LTV estimators only read whole days.
|
|
57
|
+
*/
|
|
58
|
+
export function retentionAtDay(retention, day) {
|
|
59
|
+
assertRetention(retention);
|
|
60
|
+
if (!Number.isFinite(day) || day < 0)
|
|
61
|
+
throw new RangeError("day must be finite and >= 0");
|
|
62
|
+
return retentionAtDayUnchecked(retention, day);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Share of `span` log10 steps between two worth logs, clamped to 0..1. An infinite
|
|
66
|
+
* worth log (zero or unbounded worth) contributes no progression.
|
|
67
|
+
*/
|
|
68
|
+
export function progressionFactor(startWorthLog10, endWorthLog10, span) {
|
|
69
|
+
if (Number.isNaN(startWorthLog10) || Number.isNaN(endWorthLog10)) {
|
|
70
|
+
throw new RangeError("worth logs must not be NaN");
|
|
71
|
+
}
|
|
72
|
+
if (!Number.isFinite(span) || span <= 0)
|
|
73
|
+
throw new RangeError("span must be finite and > 0");
|
|
74
|
+
return progressionFactorUnchecked(startWorthLog10, endWorthLog10, span);
|
|
75
|
+
}
|
|
49
76
|
function assertPoint(args) {
|
|
50
77
|
assertConfig(args.config);
|
|
51
78
|
if (!Number.isFinite(args.horizonDays) || args.horizonDays < 0 || args.horizonDays > 36500) {
|
|
@@ -60,27 +87,40 @@ function finiteResult(result) {
|
|
|
60
87
|
}
|
|
61
88
|
return result;
|
|
62
89
|
}
|
|
63
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* Undiscounted per-install value over install days [0, horizonDays), in one
|
|
92
|
+
* caller-chosen currency, including acquisition cost.
|
|
93
|
+
*/
|
|
64
94
|
export function estimateLtvPerUser(args) {
|
|
65
95
|
assertPoint(args);
|
|
66
96
|
return finiteResult(point(args));
|
|
67
97
|
}
|
|
68
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* Seeded assumption sensitivity, not a confidence interval fitted to a player population.
|
|
100
|
+
* Omitted `draws`, `quantiles` and `seed` use `config.uncertainty`. It always samples;
|
|
101
|
+
* `config.uncertainty.enabled` is the caller's switch for whether to call it.
|
|
102
|
+
*/
|
|
69
103
|
export function estimateLtvDistribution(args) {
|
|
70
104
|
assertPoint(args);
|
|
71
|
-
|
|
72
|
-
|
|
105
|
+
const draws = args.draws ?? args.config.uncertainty.draws;
|
|
106
|
+
const quantiles = args.quantiles ?? args.config.uncertainty.quantiles;
|
|
107
|
+
const seed = args.seed ?? args.config.uncertainty.seed;
|
|
108
|
+
if (!Number.isSafeInteger(draws) || draws < 1 || draws > 100000
|
|
109
|
+
|| draws * Math.max(1, Math.ceil(args.horizonDays)) > 10000000) {
|
|
73
110
|
throw new RangeError("draws must be 1..100000 and draws × ceil(horizonDays) at most 10000000");
|
|
74
111
|
}
|
|
75
|
-
if (
|
|
76
|
-
throw new RangeError("seed
|
|
77
|
-
if (
|
|
78
|
-
throw new RangeError("
|
|
79
|
-
|
|
80
|
-
if (
|
|
81
|
-
throw new RangeError("quantiles
|
|
112
|
+
if (seed === undefined)
|
|
113
|
+
throw new RangeError("seed is required: pass seed or set config.uncertainty.seed");
|
|
114
|
+
if (!Number.isInteger(seed) || seed < 0 || seed > 0xffffffff) {
|
|
115
|
+
throw new RangeError("seed must be an integer in 0..4294967295");
|
|
116
|
+
}
|
|
117
|
+
if (quantiles.length > 101)
|
|
118
|
+
throw new RangeError("quantiles exceed 101 entries");
|
|
119
|
+
assertQuantileValues(quantiles);
|
|
120
|
+
if (new Set(quantiles.map(quantileKey)).size !== quantiles.length) {
|
|
121
|
+
throw new RangeError("quantiles must be distinct values in 0..1");
|
|
82
122
|
}
|
|
83
|
-
const result = distribution(args);
|
|
123
|
+
const result = distribution({ ...args, draws, quantiles, seed });
|
|
84
124
|
finiteResult(result.mean);
|
|
85
125
|
finiteResult(result.quantiles);
|
|
86
126
|
return result;
|