refactor: persist EWMA only on LLM failure, dedup EWMA helper
Audit items #7 and #5. #7 — BrentPricePredictor::generatePrediction previously wrote both an EWMA row and an LLM row to price_predictions on every run. The downstream OilSignal already prefers llm_with_context > llm > ewma, so the EWMA row was dead weight 95% of the time. Now we try LLM first; if it returns null (no API key, parse failure, etc.) we compute and persist EWMA as a real fallback. This also avoids redundant work on the success path. Updated the "stores both" test to "stores only LLM" — asserts no EWMA row is written when the provider succeeds. #5 — BrentPricePredictor and AnthropicPredictionProvider both had byte-identical computeEwma() methods with identical EWMA_ALPHA = 0.3 constants. Extracted to App\Services\Ewma::compute() and dropped both private methods + their alpha constants. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -12,8 +12,6 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
final class BrentPricePredictor
|
||||
{
|
||||
private const float EWMA_ALPHA = 0.3;
|
||||
|
||||
private const float EWMA_THRESHOLD_PCT = 1.5;
|
||||
|
||||
private const int EWMA_MAX_CONFIDENCE = 65;
|
||||
@@ -33,8 +31,10 @@ final class BrentPricePredictor
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate EWMA + LLM predictions, store them, and flag the latest
|
||||
* brent_prices row as having a prediction generated.
|
||||
* Try LLM first; persist EWMA only as a fallback when the LLM provider
|
||||
* returns null. The downstream OilSignal already prefers LLM
|
||||
* (llm_with_context > llm > ewma), so writing both rows on every run is
|
||||
* dead weight 95% of the time. EWMA still acts as the safety net.
|
||||
*/
|
||||
public function generatePrediction(): ?PricePrediction
|
||||
{
|
||||
@@ -48,25 +48,23 @@ final class BrentPricePredictor
|
||||
return null;
|
||||
}
|
||||
|
||||
$ewma = $this->generateEwmaPrediction($prices);
|
||||
|
||||
if ($ewma !== null) {
|
||||
PricePrediction::create($ewma->toArray());
|
||||
}
|
||||
|
||||
$llm = $this->provider->predict($prices);
|
||||
|
||||
if ($llm !== null) {
|
||||
PricePrediction::create($llm->toArray());
|
||||
$prices->first()->forceFill(['prediction_generated_at' => now()])->save();
|
||||
|
||||
return $llm;
|
||||
}
|
||||
|
||||
$result = $llm ?? $ewma;
|
||||
$ewma = $this->generateEwmaPrediction($prices);
|
||||
|
||||
if ($result !== null) {
|
||||
if ($ewma !== null) {
|
||||
PricePrediction::create($ewma->toArray());
|
||||
$prices->first()->forceFill(['prediction_generated_at' => now()])->save();
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $ewma;
|
||||
}
|
||||
|
||||
public function generateEwmaPrediction(Collection $prices): ?PricePrediction
|
||||
@@ -77,8 +75,8 @@ final class BrentPricePredictor
|
||||
return null;
|
||||
}
|
||||
|
||||
$ewma3 = $this->computeEwma(array_slice($chronological, -3));
|
||||
$ewma7 = $this->computeEwma(array_slice($chronological, -7));
|
||||
$ewma3 = Ewma::compute(array_slice($chronological, -3));
|
||||
$ewma7 = Ewma::compute(array_slice($chronological, -7));
|
||||
|
||||
$changePct = (($ewma3 - $ewma7) / $ewma7) * 100;
|
||||
|
||||
@@ -112,20 +110,6 @@ final class BrentPricePredictor
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float[] $prices Chronological (oldest first).
|
||||
*/
|
||||
private function computeEwma(array $prices): float
|
||||
{
|
||||
$ema = $prices[0];
|
||||
|
||||
foreach (array_slice($prices, 1) as $price) {
|
||||
$ema = self::EWMA_ALPHA * $price + (1 - self::EWMA_ALPHA) * $ema;
|
||||
}
|
||||
|
||||
return round($ema, 4);
|
||||
}
|
||||
|
||||
private function ewmaConfidence(float $changePct): int
|
||||
{
|
||||
$scaled = min($changePct / 5.0, 1.0) * self::EWMA_MAX_CONFIDENCE;
|
||||
|
||||
Reference in New Issue
Block a user