gendesign/backend/app/services/forecasting/__init__.py
Light1YT f87c5cb0b8 feat(forecasting): §9.6 key-rate sensitivity module (#951d, advisory)
Add rate_sensitivity.py: regress Δln(sales) on lagged Δ(key_rate) per segment,
gate (n≥8, r2≥0.1, slope<0), shrink toward EKB-wide prior (w=n/(n+10); EKB prior
bootstrapped from all-None spec, gate-fail→0.0 neutral), emit §9.6 explainability
phrase (X=100·(exp(β)−1), Y=lag, Z=most-sensitive Source-B bucket area floor).
Pure numpy helpers (ols_slope_r2, best_lag, shrink) DB-free + unit-tested on
synthetic series (real slope/lag recovery); compute_rate_sensitivity tested with
mocked PR1/PR2. Wrong-sign guarded 3 layers; degrades to insufficient-data /
EKB-wide form on thin/wrong-sign. ADVISORY until PR6 backtest — not wired into
any endpoint. 28 tests (forecasting/ total 141), ruff clean.

Follow-ups for PR6 backtest: validate lag selection out-of-sample (spurious-lag
on short windows), confirm _SHRINK_K + confidence thresholds before prod wiring.
2026-06-03 11:05:55 +05:00

63 lines
2.1 KiB
Python

"""Forecasting services — детерминированный форсайт-слой Site Finder v2.
#951 (Site Finder v2 / «GG-форсайт», EPIC 7 «Чувствительность к ключевой ставке»).
Этот пакет — фундамент data-independent логики прогноза: monthly макро-ряды,
классификатор режима ставки, лаговые помощники. Всё ДЕТЕРМИНИРОВАННО, БЕЗ LLM.
Слои (по PR):
• macro_series (#951b) — monthly макро-ряд + классификатор режима ставки (X-ось §9.6).
• sales_series (#951c) — monthly ряд продаж по сегменту (Y-ось §9.6).
• rate_sensitivity (#951d) — §9.6 чувствительность продаж к key_rate (CORE, ADVISORY).
• macro_coefficient (later PR) — §9.5 макро-коэффициент.
Источники данных:
• макро — таблица macro_indicator через reader site_finder/macro.py (reuse).
• продажи — objective_corpus_room_month / objective_lots (см. sales_series).
"""
from __future__ import annotations
from app.services.forecasting.macro_series import (
MonthlyMacro,
classify_regime,
get_monthly_macro,
is_confounded_window,
macro_at_lag,
)
from app.services.forecasting.rate_sensitivity import (
RateSensitivity,
best_lag,
compute_rate_sensitivity,
ols_slope_r2,
shrink,
)
from app.services.forecasting.sales_series import (
SalesSeries,
SegmentSpec,
build_sales_series,
fill_month_grid,
log_diff,
price_bucket_of,
room_area_bucket_of,
)
__all__ = [
"MonthlyMacro",
"RateSensitivity",
"SalesSeries",
"SegmentSpec",
"best_lag",
"build_sales_series",
"classify_regime",
"compute_rate_sensitivity",
"fill_month_grid",
"get_monthly_macro",
"is_confounded_window",
"log_diff",
"macro_at_lag",
"ols_slope_r2",
"price_bucket_of",
"room_area_bucket_of",
"shrink",
]