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,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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user