Support sparse metric aggregation
This commit is contained in:
1 parent
d4e7cb6e8b
commit
77ba93b94a
4 files changed
+106
-21
No files matched your search
@@ -105,6 +105,52 @@ describe('FeatureExtractorService', () => {
|
|||||||
expect(feature.confidence).toBe(1);
|
expect(feature.confidence).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('leaves registry metrics without raw observations out of normalized values', () => {
|
||||||
|
const actualShared = jest.requireActual<
|
||||||
|
typeof import('@persona-lenz/shared')
|
||||||
|
>('@persona-lenz/shared');
|
||||||
|
const metrics = [
|
||||||
|
...actualShared.METRICS,
|
||||||
|
{
|
||||||
|
id: 'futureTimelineMetric',
|
||||||
|
label: 'Future timeline metric',
|
||||||
|
direction: 'higherIsBetter' as const,
|
||||||
|
kind: 'scalar' as const,
|
||||||
|
includeInVector: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
let feature:
|
||||||
|
| ReturnType<FeatureExtractorService['extractPlayerMatchFeature']>
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
jest.isolateModules(() => {
|
||||||
|
jest.doMock('@persona-lenz/shared', () => ({
|
||||||
|
...actualShared,
|
||||||
|
FEATURE_SCHEMA_VERSION: 99,
|
||||||
|
METRICS: metrics,
|
||||||
|
VECTOR_METRICS: metrics.filter((metric) => metric.includeInVector),
|
||||||
|
}));
|
||||||
|
const { FeatureExtractorService: MockedFeatureExtractorService } =
|
||||||
|
jest.requireActual<typeof import('./feature-extractor.service')>(
|
||||||
|
'./feature-extractor.service',
|
||||||
|
);
|
||||||
|
|
||||||
|
feature = new MockedFeatureExtractorService().extractPlayerMatchFeature(
|
||||||
|
createMatchPayload(),
|
||||||
|
'target-puuid',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
jest.dontMock('@persona-lenz/shared');
|
||||||
|
|
||||||
|
expect(feature?.features.futureTimelineMetric).toBeUndefined();
|
||||||
|
expect(feature?.normalized.futureTimelineMetric).toBeUndefined();
|
||||||
|
expect(feature?.vector).toHaveLength(metrics.length);
|
||||||
|
expect(feature?.vector.at(-1)).toBe(0);
|
||||||
|
expect(feature?.featureSchemaVersion).toBe(99);
|
||||||
|
});
|
||||||
|
|
||||||
it('falls back to manual calculations when challenges are missing', () => {
|
it('falls back to manual calculations when challenges are missing', () => {
|
||||||
const payload = createMatchPayload({
|
const payload = createMatchPayload({
|
||||||
participants: [
|
participants: [
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ function asNumber(value: unknown, fallback = 0): number {
|
|||||||
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function asFiniteMetricValue(value: number | undefined): number | undefined {
|
||||||
|
return typeof value === 'number' && Number.isFinite(value)
|
||||||
|
? value
|
||||||
|
: undefined;
|
||||||
|
}
|
||||||
|
|
||||||
function fromChallenge(
|
function fromChallenge(
|
||||||
challenges: Record<string, unknown>,
|
challenges: Record<string, unknown>,
|
||||||
key: string,
|
key: string,
|
||||||
@@ -210,10 +216,12 @@ export class FeatureExtractorService {
|
|||||||
const laneOpponent = resolveLaneOpponent(participant, participants, role);
|
const laneOpponent = resolveLaneOpponent(participant, participants, role);
|
||||||
const normalizationRole = resolveNormalizationRole(role);
|
const normalizationRole = resolveNormalizationRole(role);
|
||||||
const normalized = Object.fromEntries(
|
const normalized = Object.fromEntries(
|
||||||
METRICS.map((metric) => [
|
METRICS.flatMap((metric) => {
|
||||||
metric.id,
|
const rawValue = asFiniteMetricValue(features[metric.id]);
|
||||||
normalizeMetric(metric, normalizationRole, features[metric.id] ?? 0),
|
return rawValue === undefined
|
||||||
]),
|
? []
|
||||||
|
: [[metric.id, normalizeMetric(metric, normalizationRole, rawValue)]];
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -228,7 +236,7 @@ export class FeatureExtractorService {
|
|||||||
patch: resolvePatch(info.gameVersion),
|
patch: resolvePatch(info.gameVersion),
|
||||||
features,
|
features,
|
||||||
normalized,
|
normalized,
|
||||||
vector: VECTOR_METRICS.map((metric) => normalized[metric.id]),
|
vector: VECTOR_METRICS.map((metric) => normalized[metric.id] ?? 0),
|
||||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||||
normalizationMethod: 'registry-baseline',
|
normalizationMethod: 'registry-baseline',
|
||||||
confidence: this.resolveConfidence(role, gameDurationSec, laneOpponent),
|
confidence: this.resolveConfidence(role, gameDurationSec, laneOpponent),
|
||||||
|
|||||||
@@ -158,6 +158,33 @@ describe('ProfileBuilderService', () => {
|
|||||||
expect(profile.rareEventRate).toBeCloseTo(3 / 5);
|
expect(profile.rareEventRate).toBeCloseTo(3 / 5);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('aggregates scalar metrics only from features with observed normalized values', () => {
|
||||||
|
const metrics: MetricDefinition[] = [
|
||||||
|
{
|
||||||
|
id: 'observedScalar',
|
||||||
|
label: 'Observed scalar',
|
||||||
|
direction: 'higherIsBetter',
|
||||||
|
kind: 'scalar',
|
||||||
|
includeInVector: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const firstFeature = createFeature({
|
||||||
|
normalized: {},
|
||||||
|
confidence: 1,
|
||||||
|
});
|
||||||
|
const secondFeature = createFeature({
|
||||||
|
normalized: { observedScalar: 0.4 },
|
||||||
|
confidence: 0.5,
|
||||||
|
});
|
||||||
|
|
||||||
|
const profile = buildProfileMetricValues(
|
||||||
|
[firstFeature, secondFeature],
|
||||||
|
metrics,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(profile.observedScalar).toBe(0.4);
|
||||||
|
});
|
||||||
|
|
||||||
it('builds profile vectors from included metrics while retaining insight metrics', () => {
|
it('builds profile vectors from included metrics while retaining insight metrics', () => {
|
||||||
const actualShared = jest.requireActual<
|
const actualShared = jest.requireActual<
|
||||||
typeof import('@persona-lenz/shared')
|
typeof import('@persona-lenz/shared')
|
||||||
|
|||||||
@@ -37,16 +37,25 @@ function toFiniteMetricValue(value: number | undefined): number {
|
|||||||
function aggregateScalarMetric(
|
function aggregateScalarMetric(
|
||||||
features: readonly ProfileSourceFeature[],
|
features: readonly ProfileSourceFeature[],
|
||||||
metricId: string,
|
metricId: string,
|
||||||
safeTotalConfidence: number,
|
|
||||||
): number {
|
): number {
|
||||||
return (
|
let weightedSum = 0;
|
||||||
features.reduce(
|
let confidenceSum = 0;
|
||||||
(sum, feature) =>
|
|
||||||
sum +
|
for (const feature of features) {
|
||||||
toFiniteMetricValue(feature.normalized[metricId]) * feature.confidence,
|
const metricValue = feature.normalized[metricId];
|
||||||
0,
|
if (typeof metricValue !== 'number' || !Number.isFinite(metricValue)) {
|
||||||
) / safeTotalConfidence
|
continue;
|
||||||
);
|
}
|
||||||
|
|
||||||
|
weightedSum += metricValue * feature.confidence;
|
||||||
|
confidenceSum += feature.confidence;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (confidenceSum <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return weightedSum / confidenceSum;
|
||||||
}
|
}
|
||||||
|
|
||||||
function aggregateRatioMetric(
|
function aggregateRatioMetric(
|
||||||
@@ -76,18 +85,13 @@ export function buildProfileMetricValues(
|
|||||||
features: readonly ProfileSourceFeature[],
|
features: readonly ProfileSourceFeature[],
|
||||||
metrics: readonly MetricDefinition[],
|
metrics: readonly MetricDefinition[],
|
||||||
): Record<string, number> {
|
): Record<string, number> {
|
||||||
const totalConfidence = features.reduce(
|
|
||||||
(sum, feature) => sum + feature.confidence,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
const safeTotalConfidence = Math.max(totalConfidence, Number.EPSILON);
|
|
||||||
const profile: Record<string, number> = {};
|
const profile: Record<string, number> = {};
|
||||||
|
|
||||||
for (const metric of metrics) {
|
for (const metric of metrics) {
|
||||||
profile[metric.id] =
|
profile[metric.id] =
|
||||||
metric.kind === 'ratio'
|
metric.kind === 'ratio'
|
||||||
? aggregateRatioMetric(features, metric.id)
|
? aggregateRatioMetric(features, metric.id)
|
||||||
: aggregateScalarMetric(features, metric.id, safeTotalConfidence);
|
: aggregateScalarMetric(features, metric.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return profile;
|
return profile;
|
||||||
@@ -161,7 +165,7 @@ export class ProfileBuilderService {
|
|||||||
left.championId - right.championId,
|
left.championId - right.championId,
|
||||||
),
|
),
|
||||||
profile,
|
profile,
|
||||||
vector: VECTOR_METRICS.map((metric) => profile[metric.id]),
|
vector: VECTOR_METRICS.map((metric) => profile[metric.id] ?? 0),
|
||||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||||
normalizationMethod: 'registry-baseline',
|
normalizationMethod: 'registry-baseline',
|
||||||
confidence: averageConfidence * sampleSizeConfidenceMultiplier,
|
confidence: averageConfidence * sampleSizeConfidenceMultiplier,
|
||||||
|
|||||||
Reference in new issue
Block a user