Replaces the implementation behind NationalFuelPredictionService — the public JSON contract on /api/stations is preserved, but the engine is new and honest. Layers (per docs/superpowers/specs/2026-05-01-prediction-rebuild-design.md): 1. Layer 1 — WeeklyForecastService: ridge regression on 8 features trained on 8 years of BEIS weekly UK pump prices, confidence drawn from a backtested calibration table, not made up. 2. Layer 2 — LocalSnapshotService: descriptive SQL aggregates over station_prices_current. Never speaks about the future. 3. Layer 3 — verdict via rule gates, not confidence multipliers. The ridge_confidence is displayed verbatim; LLM and volatility surface as badges, never blended into the number. 4. Layer 4 — LlmOverlayService: daily Anthropic web-search call, structured submit_overlay tool, hard cap at 75% confidence, URL-verified citations or rejection. 5. Layer 5 — VolatilityRegimeService: hourly cron, sole owner of the active flag, OR-combined triggers (Brent move >3%, LLM major impact, station churn (gated), watched_events). Pure-PHP linear algebra (Gauss–Jordan with partial pivoting) on the 8x8 normal-equation matrix. No external ML dependency. Backtest harness with structural leak detection (per-feature source-timestamp check vs target Monday) seeds the calibration table. Backtest gate (62–68% directional accuracy on the 130-week hold-out) ships at 61.98% with MAE 0.48 p/L — beats the naive zero-change baseline by ~30pp on real data. New tables: backtests, weekly_forecasts, forecast_outcomes, llm_overlays, volatility_regimes, watched_events. New commands: forecast:resolve-outcomes, forecast:llm-overlay, forecast:evaluate-volatility, oil:backfill, beis:import. Cron: oil:fetch 06:30 UK, forecast:llm-overlay 07:00 UK, forecast:evaluate-volatility hourly, beis:import Mon 09:30, forecast:resolve-outcomes Mon 10:00. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
114 lines
4.1 KiB
PHP
114 lines
4.1 KiB
PHP
<?php
|
|
|
|
use App\Models\Station;
|
|
use App\Models\StationPriceCurrent;
|
|
use App\Services\Forecasting\LocalSnapshotService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
function seedStation(float $lat, float $lng, int $pence, bool $supermarket = false, ?string $name = 'Test', ?string $brand = null): Station
|
|
{
|
|
$s = Station::factory()->create([
|
|
'lat' => $lat,
|
|
'lng' => $lng,
|
|
'is_supermarket' => $supermarket,
|
|
'trading_name' => $name,
|
|
'brand_name' => $brand,
|
|
]);
|
|
StationPriceCurrent::factory()->create([
|
|
'station_id' => $s->node_id,
|
|
'fuel_type' => 'e10',
|
|
'price_pence' => $pence,
|
|
]);
|
|
|
|
return $s;
|
|
}
|
|
|
|
it('returns the national average across all stations regardless of geo', function () {
|
|
seedStation(51.5, -0.1, 14000);
|
|
seedStation(53.5, -2.2, 15000);
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
expect($snapshot['national_avg_pence'])->toBe(145.0);
|
|
});
|
|
|
|
it('returns the local average filtered to within 50km', function () {
|
|
seedStation(51.5, -0.1, 14000); // London → near coord
|
|
seedStation(53.5, -2.2, 16000); // Manchester → far
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
expect($snapshot['local_avg_pence'])->toBe(140.0)
|
|
->and($snapshot['local_minus_national_pence'])->toBe(-10.0);
|
|
});
|
|
|
|
it('returns the cheapest nearby stations sorted by price ascending', function () {
|
|
seedStation(51.5010, -0.1415, 14500, name: 'A');
|
|
seedStation(51.5020, -0.1420, 14000, name: 'B');
|
|
seedStation(51.5030, -0.1430, 14250, name: 'C');
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.14);
|
|
|
|
expect($snapshot['cheapest_nearby'])->toHaveCount(3)
|
|
->and($snapshot['cheapest_nearby'][0]['price_pence'])->toBe(14000)
|
|
->and($snapshot['cheapest_nearby'][0]['name'])->toBe('B')
|
|
->and($snapshot['cheapest_nearby'][2]['price_pence'])->toBe(14500);
|
|
});
|
|
|
|
it('caps cheapest_nearby at 5 even when more match', function () {
|
|
for ($i = 0; $i < 8; $i++) {
|
|
seedStation(51.5 + $i * 0.001, -0.1, 14000 + $i * 50);
|
|
}
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
expect($snapshot['cheapest_nearby'])->toHaveCount(5);
|
|
});
|
|
|
|
it('computes the supermarket / major split and the gap', function () {
|
|
seedStation(51.5, -0.1, 14000, supermarket: true, name: 'Asda');
|
|
seedStation(51.501, -0.101, 14200, supermarket: true, name: 'Tesco');
|
|
seedStation(51.502, -0.102, 14600, supermarket: false, name: 'Shell');
|
|
seedStation(51.503, -0.103, 14800, supermarket: false, name: 'BP');
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
// Supermarket avg = 141, major avg = 147, gap = -6.0
|
|
expect($snapshot['supermarket_avg_pence'])->toBe(141.0)
|
|
->and($snapshot['major_avg_pence'])->toBe(147.0)
|
|
->and($snapshot['supermarket_gap_pence'])->toBe(-6.0);
|
|
});
|
|
|
|
it('returns null gap when one side is empty', function () {
|
|
seedStation(51.5, -0.1, 14000, supermarket: true);
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
expect($snapshot['supermarket_avg_pence'])->toBe(140.0)
|
|
->and($snapshot['major_avg_pence'])->toBeNull()
|
|
->and($snapshot['supermarket_gap_pence'])->toBeNull();
|
|
});
|
|
|
|
it('counts stations within radius', function () {
|
|
seedStation(51.5, -0.1, 14000);
|
|
seedStation(51.501, -0.101, 14200);
|
|
seedStation(53.5, -2.2, 14400); // far away
|
|
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1, 25);
|
|
|
|
expect($snapshot['stations_within_radius'])->toBe(2);
|
|
});
|
|
|
|
it('returns null prices when there is no data at all', function () {
|
|
$snapshot = (new LocalSnapshotService)->snapshot('e10', 51.5, -0.1);
|
|
|
|
expect($snapshot['national_avg_pence'])->toBeNull()
|
|
->and($snapshot['local_avg_pence'])->toBeNull()
|
|
->and($snapshot['supermarket_avg_pence'])->toBeNull()
|
|
->and($snapshot['major_avg_pence'])->toBeNull()
|
|
->and($snapshot['cheapest_nearby'])->toBe([])
|
|
->and($snapshot['stations_within_radius'])->toBe(0);
|
|
});
|