@adaptic/utils 0.0.1030 → 0.0.1032
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/dist/index.cjs +74286 -68970
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +74265 -68973
- package/dist/index.mjs.map +1 -1
- package/dist/test.js +5087 -25
- package/dist/test.js.map +1 -1
- package/dist/types/__tests__/indicator-parity/generate.d.ts +64 -0
- package/dist/types/__tests__/indicator-parity/generate.d.ts.map +1 -0
- package/dist/types/__tests__/indicator-parity/record.d.ts +144 -0
- package/dist/types/__tests__/indicator-parity/record.d.ts.map +1 -0
- package/dist/types/__tests__/indicator-parity/reference.d.ts +92 -0
- package/dist/types/__tests__/indicator-parity/reference.d.ts.map +1 -0
- package/dist/types/__tests__/indicator-parity/series.d.ts +64 -0
- package/dist/types/__tests__/indicator-parity/series.d.ts.map +1 -0
- package/dist/types/__tests__/indicator-parity/subjects.d.ts +55 -0
- package/dist/types/__tests__/indicator-parity/subjects.d.ts.map +1 -0
- package/dist/types/__tests__/support/statistic.d.ts +18 -0
- package/dist/types/__tests__/support/statistic.d.ts.map +1 -0
- package/dist/types/alpaca/streams/trading-stream.d.ts +7 -3
- package/dist/types/alpaca/streams/trading-stream.d.ts.map +1 -1
- package/dist/types/display-manager.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/massive-indices.d.ts.map +1 -1
- package/dist/types/massive.d.ts.map +1 -1
- package/dist/types/metrics-calcs.d.ts +25 -0
- package/dist/types/metrics-calcs.d.ts.map +1 -1
- package/dist/types/performance-metrics.d.ts +16 -4
- package/dist/types/performance-metrics.d.ts.map +1 -1
- package/dist/types/sample-statistic.d.ts +123 -0
- package/dist/types/sample-statistic.d.ts.map +1 -0
- package/dist/types/schemas/alpaca-schemas.d.ts +4 -4
- package/dist/types/schemas/massive-schemas.d.ts +97 -79
- package/dist/types/schemas/massive-schemas.d.ts.map +1 -1
- package/dist/types/strategy-metrics.d.ts +38 -16
- package/dist/types/strategy-metrics.d.ts.map +1 -1
- package/dist/types/types/alpaca-types.d.ts +38 -3
- package/dist/types/types/alpaca-types.d.ts.map +1 -1
- package/dist/types/types/massive-types.d.ts +9 -2
- package/dist/types/types/massive-types.d.ts.map +1 -1
- package/package.json +4 -1
|
@@ -78,10 +78,22 @@ export declare function alignReturnsByDate(portfolioHistory: PortfolioHistory, b
|
|
|
78
78
|
alignedBenchmarkReturns: number[];
|
|
79
79
|
};
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
81
|
+
* Beta of a portfolio against a benchmark, from paired period returns.
|
|
82
|
+
*
|
|
83
|
+
* The two series are index-aligned pairs by contract: every mean, covariance
|
|
84
|
+
* and variance below is taken over the SAME row set. A length mismatch is
|
|
85
|
+
* therefore reported as invalid input rather than absorbed, because dividing
|
|
86
|
+
* one series' sum by the other series' length produces a mean of a population
|
|
87
|
+
* that does not exist — a number with no cohort, which is the failure this
|
|
88
|
+
* return type exists to make impossible.
|
|
89
|
+
*
|
|
90
|
+
* An uncomputable beta is returned as the unavailable branch, never as `0`.
|
|
91
|
+
* Zero beta is a claim of no market exposure, and downstream alpha attributes
|
|
92
|
+
* the entire benchmark move to the strategy when it believes that claim.
|
|
93
|
+
*
|
|
94
|
+
* @param portfolioReturns - Portfolio period returns.
|
|
95
|
+
* @param benchmarkReturns - Benchmark period returns, index-aligned to the portfolio.
|
|
96
|
+
* @returns The beta components with their cohort, or a typed unavailable result.
|
|
85
97
|
*/
|
|
86
98
|
export declare function calculateBetaFromReturns(portfolioReturns: number[], benchmarkReturns: number[]): CalculateBetaResult;
|
|
87
99
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"performance-metrics.d.ts","sourceRoot":"","sources":["../../src/performance-metrics.ts"],"names":[],"mappings":"AASA,OAAO,EACL,sBAAsB,EACtB,gBAAgB,EAChB,wBAAwB,EACxB,YAAY,EACZ,mBAAmB,EAEnB,uBAAuB,EACvB,UAAU,EAEX,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,kBAAkB,EAClB,4BAA4B,EAC7B,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"performance-metrics.d.ts","sourceRoot":"","sources":["../../src/performance-metrics.ts"],"names":[],"mappings":"AASA,OAAO,EACL,sBAAsB,EACtB,gBAAgB,EAChB,wBAAwB,EACxB,YAAY,EACZ,mBAAmB,EAEnB,uBAAuB,EACvB,UAAU,EAEX,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,kBAAkB,EAClB,4BAA4B,EAC7B,MAAM,uBAAuB,CAAC;AAqD/B;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CAAC,EAC1C,SAAS,EACT,MAAM,EACN,aAAa,GACd,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAiF3C;AAwBD;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,MAAM,CAAC,CA2CjB;AAqMD;;;;;GAKG;AACH,wBAAsB,qBAAqB,CACzC,gBAAgB,EAAE,wBAAwB,EAC1C,aAAa,EAAE,YAAY,EAAE,GAC5B,OAAO,CAAC;IACT,KAAK,EAAE,MAAM,CAAC;IACd,eAAe,EAAE,MAAM,CAAC;IACxB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC,CA+OD;AAkCD,UAAU,cAAc;IACtB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB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T,aAAa,GACd,EAAE,4BAA4B,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA0J5D"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A measured statistic and the cohort it was measured on, carried as one
|
|
3
|
+
* inseparable value.
|
|
4
|
+
*
|
|
5
|
+
* A ratio is meaningless without the population it was taken over: the same
|
|
6
|
+
* `0.42` is a strong result on 2,000 trades and noise on five, and a `0.0`
|
|
7
|
+
* returned because nothing could be computed is indistinguishable from a `0.0`
|
|
8
|
+
* that was genuinely measured. Both confusions are the same error — a number
|
|
9
|
+
* read apart from its unit and its cohort — and both have produced wrong
|
|
10
|
+
* conclusions from correct arithmetic.
|
|
11
|
+
*
|
|
12
|
+
* This type removes the option. Every statistic shaped by a population carries
|
|
13
|
+
* `sampleCount` (how many observations actually entered the computation) and
|
|
14
|
+
* `coverage` (what fraction of the observations the caller offered were usable),
|
|
15
|
+
* on BOTH branches: an unavailable statistic still reports how much data it
|
|
16
|
+
* saw, because "we had nothing" and "we had 900 rows and still could not
|
|
17
|
+
* compute it" are different facts with different responses.
|
|
18
|
+
*
|
|
19
|
+
* Absence is a branch of the union rather than a sentinel value. There is no
|
|
20
|
+
* number a caller can read without first proving the statistic exists, which is
|
|
21
|
+
* what keeps an unknown from silently becoming a zero on its way to a decision.
|
|
22
|
+
*
|
|
23
|
+
* @module sample-statistic
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* Why a statistic could not be computed from the observations offered.
|
|
27
|
+
*
|
|
28
|
+
* The distinctions are the point: a caller that cannot tell "too few rows" from
|
|
29
|
+
* "the rows were unusable" from "the population is degenerate" cannot tell a
|
|
30
|
+
* warm-up from a data outage from a genuinely constant series, and will treat
|
|
31
|
+
* all three the same way.
|
|
32
|
+
*/
|
|
33
|
+
export type StatisticUnavailableReason =
|
|
34
|
+
/** Fewer usable observations than the computation requires. */
|
|
35
|
+
"insufficient_samples"
|
|
36
|
+
/** Observations were offered but none survived validation (non-finite, unpaired). */
|
|
37
|
+
| "no_usable_samples"
|
|
38
|
+
/** Enough usable observations, but the population cannot support the statistic
|
|
39
|
+
* (a zero-variance denominator, an undefined ratio). */
|
|
40
|
+
| "degenerate_population"
|
|
41
|
+
/** The request itself is malformed — mismatched series lengths, a non-positive window. */
|
|
42
|
+
| "invalid_input";
|
|
43
|
+
/**
|
|
44
|
+
* How many observations a statistic was actually computed from, against how
|
|
45
|
+
* many the caller offered.
|
|
46
|
+
*
|
|
47
|
+
* `coverage` is the ratio that makes silent row-dropping visible: a function
|
|
48
|
+
* that filters non-finite rows and reports only the surviving statistic hides
|
|
49
|
+
* the size of what it discarded, and a statistic computed on 12% of the
|
|
50
|
+
* requested window is a different claim from the same number computed on all
|
|
51
|
+
* of it.
|
|
52
|
+
*/
|
|
53
|
+
export interface SampleCohort {
|
|
54
|
+
/** Observations that entered the computation. Never negative. */
|
|
55
|
+
readonly sampleCount: number;
|
|
56
|
+
/** Observations the caller offered, or the window width the caller asked for. */
|
|
57
|
+
readonly requestedCount: number;
|
|
58
|
+
/** `sampleCount / requestedCount`, clamped to [0, 1]; `0` when nothing was requested. */
|
|
59
|
+
readonly coverage: number;
|
|
60
|
+
}
|
|
61
|
+
/** A statistic that was computed, carrying the cohort it was computed on. */
|
|
62
|
+
export interface AvailableStatistic<T> extends SampleCohort {
|
|
63
|
+
readonly available: true;
|
|
64
|
+
/** The measured value. */
|
|
65
|
+
readonly value: T;
|
|
66
|
+
}
|
|
67
|
+
/** A statistic that could not be computed, still carrying what data was seen. */
|
|
68
|
+
export interface UnavailableStatistic extends SampleCohort {
|
|
69
|
+
readonly available: false;
|
|
70
|
+
/** Which class of failure prevented the computation. */
|
|
71
|
+
readonly reason: StatisticUnavailableReason;
|
|
72
|
+
/** Human-readable specifics, for logs and error messages. Never parsed. */
|
|
73
|
+
readonly detail: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A statistic that either exists with its cohort, or does not exist and says
|
|
77
|
+
* why — never a number standing in for an unknown.
|
|
78
|
+
*/
|
|
79
|
+
export type SampleStatistic<T> = AvailableStatistic<T> | UnavailableStatistic;
|
|
80
|
+
/**
|
|
81
|
+
* Build the cohort descriptor for a computation.
|
|
82
|
+
*
|
|
83
|
+
* `coverage` is derived here rather than supplied, so it cannot drift from the
|
|
84
|
+
* counts it claims to summarise. A zero request yields zero coverage: no
|
|
85
|
+
* observations were asked for, so none were covered, and the alternative (`1`)
|
|
86
|
+
* would report a vacuous computation as fully covered.
|
|
87
|
+
*
|
|
88
|
+
* @param requestedCount - Observations offered, or the window width requested.
|
|
89
|
+
* @param sampleCount - Observations that entered the computation.
|
|
90
|
+
* @returns The cohort descriptor with `coverage` derived from the two counts.
|
|
91
|
+
* @throws When either count is negative or non-finite, which is a programming
|
|
92
|
+
* error rather than a data condition.
|
|
93
|
+
*/
|
|
94
|
+
export declare function sampleCohort(requestedCount: number, sampleCount: number): SampleCohort;
|
|
95
|
+
/**
|
|
96
|
+
* Wrap a computed value with its cohort.
|
|
97
|
+
*
|
|
98
|
+
* @param value - The measured statistic.
|
|
99
|
+
* @param cohort - The cohort it was measured on.
|
|
100
|
+
* @returns The available branch of {@link SampleStatistic}.
|
|
101
|
+
*/
|
|
102
|
+
export declare function availableStatistic<T>(value: T, cohort: SampleCohort): AvailableStatistic<T>;
|
|
103
|
+
/**
|
|
104
|
+
* Record that a statistic could not be computed, and what was seen instead.
|
|
105
|
+
*
|
|
106
|
+
* @param reason - Which class of failure prevented the computation.
|
|
107
|
+
* @param detail - Specifics for logs; never machine-parsed.
|
|
108
|
+
* @param cohort - What data was available when the attempt was abandoned.
|
|
109
|
+
* @returns The unavailable branch of {@link SampleStatistic}.
|
|
110
|
+
*/
|
|
111
|
+
export declare function unavailableStatistic(reason: StatisticUnavailableReason, detail: string, cohort: SampleCohort): UnavailableStatistic;
|
|
112
|
+
/**
|
|
113
|
+
* Narrow a statistic to its available branch.
|
|
114
|
+
*
|
|
115
|
+
* Exists so consumers in other packages can discriminate without restating the
|
|
116
|
+
* predicate, and so the discriminant stays a single named concept if the shape
|
|
117
|
+
* ever grows a third branch.
|
|
118
|
+
*
|
|
119
|
+
* @param statistic - The statistic to test.
|
|
120
|
+
* @returns Whether the statistic carries a value.
|
|
121
|
+
*/
|
|
122
|
+
export declare function isAvailable<T>(statistic: SampleStatistic<T>): statistic is AvailableStatistic<T>;
|
|
123
|
+
//# sourceMappingURL=sample-statistic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-statistic.d.ts","sourceRoot":"","sources":["../../src/sample-statistic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH;;;;;;;GAOG;AACH,MAAM,MAAM,0BAA0B;AACpC,+DAA+D;AAC7D,sBAAsB;AACxB,qFAAqF;GACnF,mBAAmB;AACrB;yDACyD;GACvD,uBAAuB;AACzB,0FAA0F;GACxF,eAAe,CAAC;AAEpB;;;;;;;;;GASG;AACH,MAAM,WAAW,YAAY;IAC3B,iEAAiE;IACjE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,iFAAiF;IACjF,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,yFAAyF;IACzF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,6EAA6E;AAC7E,MAAM,WAAW,kBAAkB,CAAC,CAAC,CAAE,SAAQ,YAAY;IACzD,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,0BAA0B;IAC1B,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACnB;AAED,iFAAiF;AACjF,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC;IAC1B,wDAAwD;IACxD,QAAQ,CAAC,MAAM,EAAE,0BAA0B,CAAC;IAC5C,2EAA2E;IAC3E,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,kBAAkB,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC;AAE9E;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,MAAM,GAClB,YAAY,CAkBd;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,MAAM,EAAE,YAAY,GACnB,kBAAkB,CAAC,CAAC,CAAC,CAEvB;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,0BAA0B,EAClC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,YAAY,GACnB,oBAAoB,CAEtB;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC3B,SAAS,EAAE,eAAe,CAAC,CAAC,CAAC,GAC5B,SAAS,IAAI,kBAAkB,CAAC,CAAC,CAAC,CAEpC"}
|
|
@@ -141,8 +141,8 @@ export declare const AlpacaPositionSchema: z.ZodObject<{
|
|
|
141
141
|
asset_class: string;
|
|
142
142
|
side: "short" | "long";
|
|
143
143
|
qty: string;
|
|
144
|
-
asset_id: string;
|
|
145
144
|
exchange: string;
|
|
145
|
+
asset_id: string;
|
|
146
146
|
asset_marginable: boolean;
|
|
147
147
|
qty_available: string;
|
|
148
148
|
avg_entry_price: string;
|
|
@@ -160,8 +160,8 @@ export declare const AlpacaPositionSchema: z.ZodObject<{
|
|
|
160
160
|
asset_class: string;
|
|
161
161
|
side: "short" | "long";
|
|
162
162
|
qty: string;
|
|
163
|
-
asset_id: string;
|
|
164
163
|
exchange: string;
|
|
164
|
+
asset_id: string;
|
|
165
165
|
asset_marginable: boolean;
|
|
166
166
|
qty_available: string;
|
|
167
167
|
avg_entry_price: string;
|
|
@@ -200,8 +200,8 @@ export declare const AlpacaPositionsArraySchema: z.ZodArray<z.ZodObject<{
|
|
|
200
200
|
asset_class: string;
|
|
201
201
|
side: "short" | "long";
|
|
202
202
|
qty: string;
|
|
203
|
-
asset_id: string;
|
|
204
203
|
exchange: string;
|
|
204
|
+
asset_id: string;
|
|
205
205
|
asset_marginable: boolean;
|
|
206
206
|
qty_available: string;
|
|
207
207
|
avg_entry_price: string;
|
|
@@ -219,8 +219,8 @@ export declare const AlpacaPositionsArraySchema: z.ZodArray<z.ZodObject<{
|
|
|
219
219
|
asset_class: string;
|
|
220
220
|
side: "short" | "long";
|
|
221
221
|
qty: string;
|
|
222
|
-
asset_id: string;
|
|
223
222
|
exchange: string;
|
|
223
|
+
asset_id: string;
|
|
224
224
|
asset_marginable: boolean;
|
|
225
225
|
qty_available: string;
|
|
226
226
|
avg_entry_price: string;
|