Support ratio profile metrics
This commit is contained in:
1 parent
02538ae300
commit
3460a93eaf
4 files changed
+210
-19
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { FEATURE_SCHEMA_VERSION, METRICS } from '@persona-lenz/shared';
|
||||
import { FEATURE_SCHEMA_VERSION, VECTOR_METRICS } from '@persona-lenz/shared';
|
||||
import { FeatureExtractorService } from './feature-extractor.service';
|
||||
|
||||
function createMatchPayload(overrides: Record<string, unknown> = {}) {
|
||||
@@ -98,7 +98,7 @@ describe('FeatureExtractorService', () => {
|
||||
soloKills: 2,
|
||||
});
|
||||
expect(feature.vector).toEqual(
|
||||
METRICS.map((metric) => feature.normalized[metric.id]),
|
||||
VECTOR_METRICS.map((metric) => feature.normalized[metric.id]),
|
||||
);
|
||||
expect(feature.vector).toHaveLength(12);
|
||||
expect(feature.featureSchemaVersion).toBe(FEATURE_SCHEMA_VERSION);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
FEATURE_SCHEMA_VERSION,
|
||||
METRICS,
|
||||
VECTOR_METRICS,
|
||||
type MetricDefinition,
|
||||
type Role,
|
||||
} from '@persona-lenz/shared';
|
||||
@@ -227,7 +228,7 @@ export class FeatureExtractorService {
|
||||
patch: resolvePatch(info.gameVersion),
|
||||
features,
|
||||
normalized,
|
||||
vector: METRICS.map((metric) => normalized[metric.id]),
|
||||
vector: VECTOR_METRICS.map((metric) => normalized[metric.id]),
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
normalizationMethod: 'registry-baseline',
|
||||
confidence: this.resolveConfidence(role, gameDurationSec, laneOpponent),
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { FEATURE_SCHEMA_VERSION, METRICS } from '@persona-lenz/shared';
|
||||
import {
|
||||
FEATURE_SCHEMA_VERSION,
|
||||
METRICS,
|
||||
VECTOR_METRICS,
|
||||
type MetricDefinition,
|
||||
} from '@persona-lenz/shared';
|
||||
import type { PlayerMatchFeature } from '../global/database/schemas';
|
||||
import { ProfileBuilderService } from './profile-builder.service';
|
||||
import {
|
||||
buildProfileMetricValues,
|
||||
ProfileBuilderService,
|
||||
} from './profile-builder.service';
|
||||
|
||||
function createFeature(
|
||||
overrides: Partial<PlayerMatchFeature> = {},
|
||||
@@ -85,7 +93,7 @@ describe('ProfileBuilderService', () => {
|
||||
);
|
||||
}
|
||||
expect(profile.vector).toEqual(
|
||||
METRICS.map((metric) => profile.profile[metric.id]),
|
||||
VECTOR_METRICS.map((metric) => profile.profile[metric.id]),
|
||||
);
|
||||
expect(profile.championPool).toEqual([
|
||||
{ championId: 103, games: 1, wins: 1 },
|
||||
@@ -106,4 +114,131 @@ describe('ProfileBuilderService', () => {
|
||||
}),
|
||||
).toThrow('Cannot build a profile without match features.');
|
||||
});
|
||||
|
||||
it('aggregates ratio metrics from raw numerator and denominator counts', () => {
|
||||
const metrics: MetricDefinition[] = [
|
||||
{
|
||||
id: 'scalarMetric',
|
||||
label: 'Scalar metric',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'scalar',
|
||||
includeInVector: true,
|
||||
},
|
||||
{
|
||||
id: 'rareEventRate',
|
||||
label: 'Rare event rate',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'ratio',
|
||||
includeInVector: false,
|
||||
},
|
||||
];
|
||||
const firstFeature = createFeature({
|
||||
features: {
|
||||
rareEventRateNumerator: 1,
|
||||
rareEventRateDenominator: 2,
|
||||
},
|
||||
normalized: { scalarMetric: 0.8 },
|
||||
confidence: 1,
|
||||
});
|
||||
const secondFeature = createFeature({
|
||||
features: {
|
||||
rareEventRateNumerator: 2,
|
||||
rareEventRateDenominator: 3,
|
||||
},
|
||||
normalized: { scalarMetric: 0.2 },
|
||||
confidence: 0.5,
|
||||
});
|
||||
|
||||
const profile = buildProfileMetricValues(
|
||||
[firstFeature, secondFeature],
|
||||
metrics,
|
||||
);
|
||||
|
||||
expect(profile.scalarMetric).toBeCloseTo((0.8 + 0.2 * 0.5) / 1.5);
|
||||
expect(profile.rareEventRate).toBeCloseTo(3 / 5);
|
||||
});
|
||||
|
||||
it('builds profile vectors from included metrics while retaining insight metrics', () => {
|
||||
const actualShared = jest.requireActual<
|
||||
typeof import('@persona-lenz/shared')
|
||||
>('@persona-lenz/shared');
|
||||
const metrics: MetricDefinition[] = [
|
||||
{
|
||||
id: 'scalarMetric',
|
||||
label: 'Scalar metric',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'scalar',
|
||||
includeInVector: true,
|
||||
},
|
||||
{
|
||||
id: 'vectorRate',
|
||||
label: 'Vector rate',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'ratio',
|
||||
includeInVector: true,
|
||||
},
|
||||
{
|
||||
id: 'insightRate',
|
||||
label: 'Insight rate',
|
||||
direction: 'higherIsBetter',
|
||||
kind: 'ratio',
|
||||
includeInVector: false,
|
||||
},
|
||||
];
|
||||
|
||||
let profile: ReturnType<ProfileBuilderService['buildProfile']> | undefined;
|
||||
|
||||
jest.isolateModules(() => {
|
||||
jest.doMock('@persona-lenz/shared', () => ({
|
||||
...actualShared,
|
||||
FEATURE_SCHEMA_VERSION: 99,
|
||||
METRICS: metrics,
|
||||
VECTOR_METRICS: metrics.filter((metric) => metric.includeInVector),
|
||||
}));
|
||||
const { ProfileBuilderService: MockedProfileBuilderService } =
|
||||
jest.requireActual<typeof import('./profile-builder.service')>(
|
||||
'./profile-builder.service',
|
||||
);
|
||||
const serviceWithMockedRegistry = new MockedProfileBuilderService();
|
||||
const firstFeature = createFeature({
|
||||
features: {
|
||||
vectorRateNumerator: 1,
|
||||
vectorRateDenominator: 2,
|
||||
insightRateNumerator: 2,
|
||||
insightRateDenominator: 4,
|
||||
},
|
||||
normalized: { scalarMetric: 0.8 },
|
||||
confidence: 1,
|
||||
});
|
||||
const secondFeature = createFeature({
|
||||
features: {
|
||||
vectorRateNumerator: 3,
|
||||
vectorRateDenominator: 4,
|
||||
insightRateNumerator: 1,
|
||||
insightRateDenominator: 1,
|
||||
},
|
||||
normalized: { scalarMetric: 0.2 },
|
||||
confidence: 0.5,
|
||||
});
|
||||
|
||||
profile = serviceWithMockedRegistry.buildProfile({
|
||||
features: [firstFeature, secondFeature],
|
||||
isSeed: false,
|
||||
puuid: 'target-puuid',
|
||||
queueId: 420,
|
||||
});
|
||||
});
|
||||
|
||||
jest.dontMock('@persona-lenz/shared');
|
||||
|
||||
expect(profile).toBeDefined();
|
||||
expect(profile?.profile.scalarMetric).toBeCloseTo((0.8 + 0.2 * 0.5) / 1.5);
|
||||
expect(profile?.profile.vectorRate).toBeCloseTo(4 / 6);
|
||||
expect(profile?.profile.insightRate).toBeCloseTo(3 / 5);
|
||||
expect(profile?.vector).toEqual([
|
||||
profile?.profile.scalarMetric,
|
||||
profile?.profile.vectorRate,
|
||||
]);
|
||||
expect(profile?.featureSchemaVersion).toBe(99);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
import {
|
||||
FEATURE_SCHEMA_VERSION,
|
||||
METRICS,
|
||||
VECTOR_METRICS,
|
||||
type MetricDefinition,
|
||||
type Role,
|
||||
} from '@persona-lenz/shared';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
@@ -11,6 +13,7 @@ type ProfileSourceFeature = Pick<
|
||||
PlayerMatchFeature,
|
||||
| 'championId'
|
||||
| 'confidence'
|
||||
| 'features'
|
||||
| 'gameCreation'
|
||||
| 'normalized'
|
||||
| 'patch'
|
||||
@@ -27,6 +30,69 @@ export interface BuildProfileParams {
|
||||
|
||||
const ROLE_ORDER: Role[] = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'UTILITY'];
|
||||
|
||||
function toFiniteMetricValue(value: number | undefined): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
|
||||
}
|
||||
|
||||
function aggregateScalarMetric(
|
||||
features: readonly ProfileSourceFeature[],
|
||||
metricId: string,
|
||||
safeTotalConfidence: number,
|
||||
): number {
|
||||
return (
|
||||
features.reduce(
|
||||
(sum, feature) =>
|
||||
sum +
|
||||
toFiniteMetricValue(feature.normalized[metricId]) * feature.confidence,
|
||||
0,
|
||||
) / safeTotalConfidence
|
||||
);
|
||||
}
|
||||
|
||||
function aggregateRatioMetric(
|
||||
features: readonly ProfileSourceFeature[],
|
||||
metricId: string,
|
||||
): number {
|
||||
const numeratorKey = `${metricId}Numerator`;
|
||||
const denominatorKey = `${metricId}Denominator`;
|
||||
const numerator = features.reduce(
|
||||
(sum, feature) => sum + toFiniteMetricValue(feature.features[numeratorKey]),
|
||||
0,
|
||||
);
|
||||
const denominator = features.reduce(
|
||||
(sum, feature) =>
|
||||
sum + toFiniteMetricValue(feature.features[denominatorKey]),
|
||||
0,
|
||||
);
|
||||
|
||||
if (denominator <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return numerator / denominator;
|
||||
}
|
||||
|
||||
export function buildProfileMetricValues(
|
||||
features: readonly ProfileSourceFeature[],
|
||||
metrics: readonly MetricDefinition[],
|
||||
): 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> = {};
|
||||
|
||||
for (const metric of metrics) {
|
||||
profile[metric.id] =
|
||||
metric.kind === 'ratio'
|
||||
? aggregateRatioMetric(features, metric.id)
|
||||
: aggregateScalarMetric(features, metric.id, safeTotalConfidence);
|
||||
}
|
||||
|
||||
return profile;
|
||||
}
|
||||
|
||||
function isRole(value: string): value is Role {
|
||||
return ROLE_ORDER.includes(value as Role);
|
||||
}
|
||||
@@ -42,18 +108,7 @@ export class ProfileBuilderService {
|
||||
(sum, feature) => sum + feature.confidence,
|
||||
0,
|
||||
);
|
||||
const safeTotalConfidence = Math.max(totalConfidence, Number.EPSILON);
|
||||
|
||||
const profile = Object.fromEntries(
|
||||
METRICS.map((metric) => [
|
||||
metric.id,
|
||||
params.features.reduce(
|
||||
(sum, feature) =>
|
||||
sum + (feature.normalized[metric.id] ?? 0) * feature.confidence,
|
||||
0,
|
||||
) / safeTotalConfidence,
|
||||
]),
|
||||
);
|
||||
const profile = buildProfileMetricValues(params.features, METRICS);
|
||||
|
||||
const roleCounts = new Map<Role, number>();
|
||||
const championPool = new Map<
|
||||
@@ -106,7 +161,7 @@ export class ProfileBuilderService {
|
||||
left.championId - right.championId,
|
||||
),
|
||||
profile,
|
||||
vector: METRICS.map((metric) => profile[metric.id]),
|
||||
vector: VECTOR_METRICS.map((metric) => profile[metric.id]),
|
||||
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||
normalizationMethod: 'registry-baseline',
|
||||
confidence: averageConfidence * sampleSizeConfidenceMultiplier,
|
||||
|
||||
Reference in new issue
Block a user