@higher.archi/boe 1.0.22 → 1.0.24
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/core/types/rule.d.ts +1 -1
- package/dist/core/types/rule.d.ts.map +1 -1
- package/dist/engines/prediction/compiler.d.ts +11 -0
- package/dist/engines/prediction/compiler.d.ts.map +1 -0
- package/dist/engines/prediction/compiler.js +153 -0
- package/dist/engines/prediction/compiler.js.map +1 -0
- package/dist/engines/prediction/engine.d.ts +49 -0
- package/dist/engines/prediction/engine.d.ts.map +1 -0
- package/dist/engines/prediction/engine.js +91 -0
- package/dist/engines/prediction/engine.js.map +1 -0
- package/dist/engines/prediction/index.d.ts +9 -0
- package/dist/engines/prediction/index.d.ts.map +1 -0
- package/dist/engines/prediction/index.js +25 -0
- package/dist/engines/prediction/index.js.map +1 -0
- package/dist/engines/prediction/strategy.d.ts +20 -0
- package/dist/engines/prediction/strategy.d.ts.map +1 -0
- package/dist/engines/prediction/strategy.js +441 -0
- package/dist/engines/prediction/strategy.js.map +1 -0
- package/dist/engines/prediction/types.d.ts +150 -0
- package/dist/engines/prediction/types.d.ts.map +1 -0
- package/dist/engines/prediction/types.js +59 -0
- package/dist/engines/prediction/types.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/types/rule.ts +1 -1
- package/src/engines/prediction/compiler.ts +186 -0
- package/src/engines/prediction/engine.ts +120 -0
- package/src/engines/prediction/index.ts +49 -0
- package/src/engines/prediction/strategy.ts +573 -0
- package/src/engines/prediction/types.ts +236 -0
- package/src/index.ts +39 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prediction Engine Types
|
|
3
|
+
*
|
|
4
|
+
* Trajectory forecasting engine that takes historical score snapshots,
|
|
5
|
+
* fits a model, and projects forward. Supports linear regression,
|
|
6
|
+
* exponential smoothing, and weighted moving average strategies.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ========================================
|
|
10
|
+
// Semantic Types
|
|
11
|
+
// ========================================
|
|
12
|
+
|
|
13
|
+
/** Prediction algorithm to use */
|
|
14
|
+
export type PredictionStrategy = 'linear-regression' | 'exponential-smoothing' | 'weighted-moving-average';
|
|
15
|
+
|
|
16
|
+
/** Trend direction derived from slope */
|
|
17
|
+
export type TrendDirection = 'rising' | 'stable' | 'declining' | 'insufficient-data';
|
|
18
|
+
|
|
19
|
+
/** Exponential smoothing sensitivity presets */
|
|
20
|
+
export type SmoothingPreset = 'responsive' | 'balanced' | 'smooth';
|
|
21
|
+
|
|
22
|
+
/** Forecast horizon presets */
|
|
23
|
+
export type HorizonPreset = '1-day' | '1-week' | '1-month' | '3-months' | '6-months' | '1-year';
|
|
24
|
+
|
|
25
|
+
/** Human-readable confidence bracket */
|
|
26
|
+
export type ConfidenceBracket = 'high' | 'moderate' | 'low' | 'insufficient-data';
|
|
27
|
+
|
|
28
|
+
// ========================================
|
|
29
|
+
// Preset Value Maps
|
|
30
|
+
// ========================================
|
|
31
|
+
|
|
32
|
+
export const SMOOTHING_PRESET_VALUES: Record<SmoothingPreset, { alpha: number; beta: number }> = {
|
|
33
|
+
'responsive': { alpha: 0.8, beta: 0.3 },
|
|
34
|
+
'balanced': { alpha: 0.5, beta: 0.2 },
|
|
35
|
+
'smooth': { alpha: 0.2, beta: 0.1 }
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const HORIZON_PRESET_VALUES: Record<HorizonPreset, number> = {
|
|
39
|
+
'1-day': 86_400_000,
|
|
40
|
+
'1-week': 604_800_000,
|
|
41
|
+
'1-month': 2_592_000_000,
|
|
42
|
+
'3-months': 7_776_000_000,
|
|
43
|
+
'6-months': 15_552_000_000,
|
|
44
|
+
'1-year': 31_104_000_000
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// ========================================
|
|
48
|
+
// Type Guards
|
|
49
|
+
// ========================================
|
|
50
|
+
|
|
51
|
+
export function isSmoothingPreset(value: unknown): value is SmoothingPreset {
|
|
52
|
+
return typeof value === 'string' && value in SMOOTHING_PRESET_VALUES;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isHorizonPreset(value: unknown): value is HorizonPreset {
|
|
56
|
+
return typeof value === 'string' && value in HORIZON_PRESET_VALUES;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ========================================
|
|
60
|
+
// Resolver Functions
|
|
61
|
+
// ========================================
|
|
62
|
+
|
|
63
|
+
/** Map a confidence value (0-1) to a semantic bracket */
|
|
64
|
+
export function resolveConfidenceBracket(confidence: number): ConfidenceBracket {
|
|
65
|
+
if (confidence >= 0.8) return 'high';
|
|
66
|
+
if (confidence >= 0.5) return 'moderate';
|
|
67
|
+
if (confidence > 0) return 'low';
|
|
68
|
+
return 'insufficient-data';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Map a slope (per ms) to a semantic trend direction */
|
|
72
|
+
export function resolveTrendDirection(slopePerMs: number, stableThreshold: number): TrendDirection {
|
|
73
|
+
if (Math.abs(slopePerMs) <= stableThreshold) return 'stable';
|
|
74
|
+
return slopePerMs > 0 ? 'rising' : 'declining';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ========================================
|
|
78
|
+
// Field Mapping
|
|
79
|
+
// ========================================
|
|
80
|
+
|
|
81
|
+
/** Configurable dot-path field mapping into fact.data */
|
|
82
|
+
export type FieldMapping = {
|
|
83
|
+
entityId: string;
|
|
84
|
+
score: string;
|
|
85
|
+
timestamp: string;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// ========================================
|
|
89
|
+
// Threshold Definition
|
|
90
|
+
// ========================================
|
|
91
|
+
|
|
92
|
+
export type ThresholdDefinition = {
|
|
93
|
+
id: string;
|
|
94
|
+
name?: string;
|
|
95
|
+
value: number;
|
|
96
|
+
direction: 'above' | 'below';
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// ========================================
|
|
100
|
+
// Strategy-Specific Config Types
|
|
101
|
+
// ========================================
|
|
102
|
+
|
|
103
|
+
export type LinearRegressionConfig = {
|
|
104
|
+
stableThreshold?: number; // default: 0.001
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type ExponentialSmoothingConfig = {
|
|
108
|
+
smoothing?: SmoothingPreset | { alpha: number; beta: number }; // default: 'balanced'
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export type WeightedMovingAverageConfig = {
|
|
112
|
+
window?: number; // default: 5
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// ========================================
|
|
116
|
+
// Source RuleSet Types (Discriminated Union)
|
|
117
|
+
// ========================================
|
|
118
|
+
|
|
119
|
+
type PredictionRuleSetBase = {
|
|
120
|
+
id: string;
|
|
121
|
+
name?: string;
|
|
122
|
+
mode: 'prediction';
|
|
123
|
+
snapshotType: string;
|
|
124
|
+
fields: FieldMapping;
|
|
125
|
+
horizon: HorizonPreset | number;
|
|
126
|
+
thresholds?: ThresholdDefinition[];
|
|
127
|
+
defaultTrend?: number; // score units per month
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export type LinearRegressionRuleSet = PredictionRuleSetBase & {
|
|
131
|
+
strategy: 'linear-regression';
|
|
132
|
+
config?: LinearRegressionConfig;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export type ExponentialSmoothingRuleSet = PredictionRuleSetBase & {
|
|
136
|
+
strategy: 'exponential-smoothing';
|
|
137
|
+
config?: ExponentialSmoothingConfig;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export type WeightedMovingAverageRuleSet = PredictionRuleSetBase & {
|
|
141
|
+
strategy: 'weighted-moving-average';
|
|
142
|
+
config?: WeightedMovingAverageConfig;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export type PredictionRuleSet =
|
|
146
|
+
| LinearRegressionRuleSet
|
|
147
|
+
| ExponentialSmoothingRuleSet
|
|
148
|
+
| WeightedMovingAverageRuleSet;
|
|
149
|
+
|
|
150
|
+
// ========================================
|
|
151
|
+
// Compiled RuleSet Types
|
|
152
|
+
// ========================================
|
|
153
|
+
|
|
154
|
+
type CompiledPredictionRuleSetBase = {
|
|
155
|
+
id: string;
|
|
156
|
+
name?: string;
|
|
157
|
+
mode: 'prediction';
|
|
158
|
+
snapshotType: string;
|
|
159
|
+
fields: FieldMapping;
|
|
160
|
+
horizonMs: number;
|
|
161
|
+
thresholds: ThresholdDefinition[];
|
|
162
|
+
defaultTrend?: number; // score units per month
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export type CompiledLinearRegressionRuleSet = CompiledPredictionRuleSetBase & {
|
|
166
|
+
strategy: 'linear-regression';
|
|
167
|
+
config: {
|
|
168
|
+
stableThreshold: number;
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export type CompiledExponentialSmoothingRuleSet = CompiledPredictionRuleSetBase & {
|
|
173
|
+
strategy: 'exponential-smoothing';
|
|
174
|
+
config: {
|
|
175
|
+
alpha: number;
|
|
176
|
+
beta: number;
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export type CompiledWeightedMovingAverageRuleSet = CompiledPredictionRuleSetBase & {
|
|
181
|
+
strategy: 'weighted-moving-average';
|
|
182
|
+
config: {
|
|
183
|
+
window: number;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export type CompiledPredictionRuleSet =
|
|
188
|
+
| CompiledLinearRegressionRuleSet
|
|
189
|
+
| CompiledExponentialSmoothingRuleSet
|
|
190
|
+
| CompiledWeightedMovingAverageRuleSet;
|
|
191
|
+
|
|
192
|
+
// ========================================
|
|
193
|
+
// Result Types
|
|
194
|
+
// ========================================
|
|
195
|
+
|
|
196
|
+
export type ThresholdCrossing = {
|
|
197
|
+
thresholdId: string;
|
|
198
|
+
thresholdName?: string;
|
|
199
|
+
value: number;
|
|
200
|
+
direction: 'above' | 'below';
|
|
201
|
+
estimatedAt: Date;
|
|
202
|
+
estimatedAtIso: string;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export type Forecast = {
|
|
206
|
+
date: Date;
|
|
207
|
+
dateIso: string;
|
|
208
|
+
predictedScore: number;
|
|
209
|
+
confidenceInterval?: { lower: number; upper: number };
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export type EntityPrediction = {
|
|
213
|
+
entityId: string;
|
|
214
|
+
dataPoints: number;
|
|
215
|
+
trend: TrendDirection;
|
|
216
|
+
trendSlope: number; // score units per day
|
|
217
|
+
confidence: number; // 0-1
|
|
218
|
+
confidenceBracket: ConfidenceBracket;
|
|
219
|
+
currentScore: number; // most recent observed score
|
|
220
|
+
forecast: Forecast; // projected score at horizon
|
|
221
|
+
thresholdCrossings: ThresholdCrossing[];
|
|
222
|
+
model: { strategy: PredictionStrategy; params: Record<string, number> };
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export type PredictionResult = {
|
|
226
|
+
predictions: EntityPrediction[];
|
|
227
|
+
totalEntities: number;
|
|
228
|
+
strategy: PredictionStrategy;
|
|
229
|
+
horizonMs: number;
|
|
230
|
+
executionTimeMs: number;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export type PredictionOptions = {
|
|
234
|
+
onPredict?: (prediction: EntityPrediction) => void;
|
|
235
|
+
asOf?: Date;
|
|
236
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -445,6 +445,45 @@ export type {
|
|
|
445
445
|
RankingResult
|
|
446
446
|
} from './engines/ranking';
|
|
447
447
|
|
|
448
|
+
// Prediction
|
|
449
|
+
export {
|
|
450
|
+
PredictionEngine,
|
|
451
|
+
PredictionExecutor,
|
|
452
|
+
compilePredictionRuleSet,
|
|
453
|
+
predictionStrategy,
|
|
454
|
+
SMOOTHING_PRESET_VALUES,
|
|
455
|
+
HORIZON_PRESET_VALUES,
|
|
456
|
+
isSmoothingPreset,
|
|
457
|
+
isHorizonPreset,
|
|
458
|
+
resolveConfidenceBracket,
|
|
459
|
+
resolveTrendDirection
|
|
460
|
+
} from './engines/prediction';
|
|
461
|
+
export type {
|
|
462
|
+
PredictionStrategy,
|
|
463
|
+
TrendDirection,
|
|
464
|
+
SmoothingPreset,
|
|
465
|
+
HorizonPreset,
|
|
466
|
+
ConfidenceBracket,
|
|
467
|
+
FieldMapping,
|
|
468
|
+
ThresholdDefinition,
|
|
469
|
+
LinearRegressionConfig,
|
|
470
|
+
ExponentialSmoothingConfig,
|
|
471
|
+
WeightedMovingAverageConfig,
|
|
472
|
+
LinearRegressionRuleSet,
|
|
473
|
+
ExponentialSmoothingRuleSet,
|
|
474
|
+
WeightedMovingAverageRuleSet,
|
|
475
|
+
PredictionRuleSet,
|
|
476
|
+
CompiledLinearRegressionRuleSet,
|
|
477
|
+
CompiledExponentialSmoothingRuleSet,
|
|
478
|
+
CompiledWeightedMovingAverageRuleSet,
|
|
479
|
+
CompiledPredictionRuleSet,
|
|
480
|
+
ThresholdCrossing,
|
|
481
|
+
Forecast,
|
|
482
|
+
EntityPrediction,
|
|
483
|
+
PredictionResult,
|
|
484
|
+
PredictionOptions
|
|
485
|
+
} from './engines/prediction';
|
|
486
|
+
|
|
448
487
|
// ========================================
|
|
449
488
|
// Utility Functions (from old modules, re-exported for convenience)
|
|
450
489
|
// ========================================
|