Update: 2026-07-06 19:27:17

This commit is contained in:
Hamza-Ayed
2026-07-06 19:27:17 +03:00
parent 3c9a3dbef8
commit 5725fb36f4
2 changed files with 42 additions and 5 deletions
@@ -2,6 +2,7 @@ import { PricingTier, RideSample, RegressionResult } from './types';
import {
twoStageRegression,
robustMultipleLinearRegression,
simpleDistanceModel,
calcRMSE,
calcRSquared,
detectMinimumFare,
@@ -78,17 +79,29 @@ export function analyzeTier(tier: PricingTier): PricingTier {
price: s.price,
}));
// Run both models
const modelA = twoStageRegression(input); // Stage 1: flag fall | Stage 2: km + min
const modelB = robustMultipleLinearRegression(input); // Current robust approach
// Run all three models
const modelA = twoStageRegression(input); // Stage 1: flag fall | Stage 2: km + min
const modelB = robustMultipleLinearRegression(input); // Iterative outlier removal
const modelC = simpleDistanceModel(input); // distance-only: price = k × dist
const best = selectBestModel(modelA, modelB, samples);
if (!best) {
// Distance-only fallback: if it's within 10% of the best model, prefer it
let finalModel = best;
if (finalModel && modelC) {
const rmseBest = evalRMSE(finalModel.model, samples);
const rmseDist = evalRMSE(modelC, samples);
if (rmseDist <= rmseBest * 1.10) {
finalModel = { model: modelC, name: 'distance-only' };
}
}
if (!finalModel) {
tier.regression = null;
return tier;
}
const { model: mlrResult, name: modelName } = best;
const { model: mlrResult, name: modelName } = finalModel;
// Compute final metrics using the winning model
const actualPrices = samples.map(s => s.price);