tseda.forecastability
tseda.forecastability — Forecast readiness scoring and leakage detection.
Classes
- ForecastabilityReport
Immutable result of
ForecastabilityScorer.score().- ForecastabilityScorer
Composite 0–100 forecastability scorer.
- LeakageReport
Immutable result of
LeakageDetector.check().- LeakageDetector
Temporal and target leakage detector for feature sets.
- class tseda.forecastability.ForecastabilityReport(score, sub_scores, recommended_model, recommended_diff, recommended_period, n_obs, pct_missing, pct_outlier, is_stationary, dominant_period)[source]
Bases:
objectImmutable forecastability assessment.
- Parameters:
- recommended_model
Suggested modelling approach:
"ARIMA","SARIMA","ETS","Prophet", or"ML".- Type:
- recommended_period
Dominant seasonal period detected, or
Noneif no seasonality found.- Type:
int or None
- dominant_period
Same as
recommended_period.- Type:
int or None
- __init__(score, sub_scores, recommended_model, recommended_diff, recommended_period, n_obs, pct_missing, pct_outlier, is_stationary, dominant_period)
- class tseda.forecastability.ForecastabilityScorer[source]
Bases:
objectAssess how forecastable a
TimeSeriesis.The scorer is stateless — calling
score()multiple times is safe.- score(ts, period)[source]
Return a
ForecastabilityReportwith an overall 0–100 score.- Parameters:
ts (TimeSeries)
period (int | None)
alpha (float)
- Return type:
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.scorer import ForecastabilityScorer
>>> rng = np.random.default_rng(1) >>> idx = pd.date_range("2020", periods=200, freq="D") >>> ts = TimeSeries(rng.standard_normal(200), index=idx) >>> r = ForecastabilityScorer().score(ts) >>> isinstance(r.score, float) True
- score(ts, *, period=None, alpha=0.05)[source]
Compute the forecastability score for ts.
- Parameters:
ts (TimeSeries) – Input series.
period (int, optional) – Seasonal period. When
Nonethe period is detected automatically via the FFT periodogram.alpha (float, optional) – Significance level used for stationarity and ACF tests. Default
0.05.
- Return type:
- Raises:
TypeError – If ts is not a
TimeSeries.ValueError – If period is given and is < 2, or ts has fewer than 4 obs.
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.scorer import ForecastabilityScorer >>> rng = np.random.default_rng(2) >>> idx = pd.date_range("2020", periods=365, freq="D") >>> n = 365 >>> seas = np.sin(2 * np.pi * np.arange(n) / 7) * 3 >>> ts = TimeSeries(seas + rng.standard_normal(n) * 0.5, index=idx) >>> r = ForecastabilityScorer().score(ts, period=7) >>> r.recommended_period 7
- class tseda.forecastability.LeakageReport(has_temporal_leakage, has_target_leakage, temporal_leakage_columns, target_leakage_columns, target_leakage_correlations, temporal_peak_lags, horizon, n_features, n_obs, warnings)[source]
Bases:
objectImmutable leakage detection result.
- Parameters:
- has_temporal_leakage
Trueif any feature shows stronger correlation with future target than with current / past target.- Type:
- has_target_leakage
Trueif any feature is correlated with the target at lag 0 above target_corr_threshold.- Type:
- target_leakage_correlations
Lag-0 Pearson correlation for each column in
target_leakage_columns.- Type:
dict of str → float
- temporal_peak_lags
For each feature column, the lag at which the cross-correlation with the target is maximised. Positive lag means feature correlates with future target.
- Type:
dict of str → int
- __init__(has_temporal_leakage, has_target_leakage, temporal_leakage_columns, target_leakage_columns, target_leakage_correlations, temporal_peak_lags, horizon, n_features, n_obs, warnings)
- Parameters:
- Return type:
None
- class tseda.forecastability.LeakageDetector[source]
Bases:
objectDetect temporal and target leakage in a feature set.
The detector is stateless.
- check(ts, horizon, features_df, target_corr_threshold)[source]
Return a
LeakageReport.- Parameters:
ts (TimeSeries)
horizon (int)
features_df (DataFrame | None)
target_corr_threshold (float)
- Return type:
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.leakage import LeakageDetector
Target leakage — a feature that is the target:
>>> rng = np.random.default_rng(0) >>> n = 80 >>> idx = pd.date_range("2020", periods=n, freq="D") >>> y = rng.standard_normal(n) >>> ts = TimeSeries(y, index=idx) >>> feat = pd.DataFrame({"target_copy": y}, index=idx) >>> r = LeakageDetector().check(ts, horizon=1, features_df=feat) >>> r.has_target_leakage True >>> "target_copy" in r.target_leakage_columns True
- check(ts, horizon, *, features_df=None, target_corr_threshold=0.95)[source]
Check features_df for leakage against target ts.
- Parameters:
ts (TimeSeries) – Target time series.
horizon (int) – Forecast horizon in time steps. Must be >= 1.
features_df (pandas.DataFrame, optional) – Feature matrix with the same
DatetimeIndexas ts, one column per feature. WhenNonethe report is empty with a warning.target_corr_threshold (float, optional) – Pearson r threshold above which a feature is flagged as target-leaking. Default
0.95.
- Return type:
- Raises:
TypeError – If ts is not a
TimeSeries.ValueError – If horizon < 1, target_corr_threshold ∉ (0, 1], or features_df has a different number of rows from ts.
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.leakage import LeakageDetector
>>> rng = np.random.default_rng(1) >>> n = 60 >>> idx = pd.date_range("2020", periods=n, freq="D") >>> ts = TimeSeries(rng.standard_normal(n), index=idx) >>> r = LeakageDetector().check(ts, horizon=3) >>> r.n_features 0
Forecastability Scorer
- class tseda.forecastability.scorer.ForecastabilityReport(score, sub_scores, recommended_model, recommended_diff, recommended_period, n_obs, pct_missing, pct_outlier, is_stationary, dominant_period)[source]
Bases:
objectImmutable forecastability assessment.
- Parameters:
- recommended_model
Suggested modelling approach:
"ARIMA","SARIMA","ETS","Prophet", or"ML".- Type:
- recommended_period
Dominant seasonal period detected, or
Noneif no seasonality found.- Type:
int or None
- dominant_period
Same as
recommended_period.- Type:
int or None
- __init__(score, sub_scores, recommended_model, recommended_diff, recommended_period, n_obs, pct_missing, pct_outlier, is_stationary, dominant_period)
- class tseda.forecastability.scorer.ForecastabilityScorer[source]
Bases:
objectAssess how forecastable a
TimeSeriesis.The scorer is stateless — calling
score()multiple times is safe.- score(ts, period)[source]
Return a
ForecastabilityReportwith an overall 0–100 score.- Parameters:
ts (TimeSeries)
period (int | None)
alpha (float)
- Return type:
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.scorer import ForecastabilityScorer
>>> rng = np.random.default_rng(1) >>> idx = pd.date_range("2020", periods=200, freq="D") >>> ts = TimeSeries(rng.standard_normal(200), index=idx) >>> r = ForecastabilityScorer().score(ts) >>> isinstance(r.score, float) True
- score(ts, *, period=None, alpha=0.05)[source]
Compute the forecastability score for ts.
- Parameters:
ts (TimeSeries) – Input series.
period (int, optional) – Seasonal period. When
Nonethe period is detected automatically via the FFT periodogram.alpha (float, optional) – Significance level used for stationarity and ACF tests. Default
0.05.
- Return type:
- Raises:
TypeError – If ts is not a
TimeSeries.ValueError – If period is given and is < 2, or ts has fewer than 4 obs.
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.scorer import ForecastabilityScorer >>> rng = np.random.default_rng(2) >>> idx = pd.date_range("2020", periods=365, freq="D") >>> n = 365 >>> seas = np.sin(2 * np.pi * np.arange(n) / 7) * 3 >>> ts = TimeSeries(seas + rng.standard_normal(n) * 0.5, index=idx) >>> r = ForecastabilityScorer().score(ts, period=7) >>> r.recommended_period 7
Leakage Detector
- class tseda.forecastability.leakage.LeakageReport(has_temporal_leakage, has_target_leakage, temporal_leakage_columns, target_leakage_columns, target_leakage_correlations, temporal_peak_lags, horizon, n_features, n_obs, warnings)[source]
Bases:
objectImmutable leakage detection result.
- Parameters:
- has_temporal_leakage
Trueif any feature shows stronger correlation with future target than with current / past target.- Type:
- has_target_leakage
Trueif any feature is correlated with the target at lag 0 above target_corr_threshold.- Type:
- target_leakage_correlations
Lag-0 Pearson correlation for each column in
target_leakage_columns.- Type:
dict of str → float
- temporal_peak_lags
For each feature column, the lag at which the cross-correlation with the target is maximised. Positive lag means feature correlates with future target.
- Type:
dict of str → int
- __init__(has_temporal_leakage, has_target_leakage, temporal_leakage_columns, target_leakage_columns, target_leakage_correlations, temporal_peak_lags, horizon, n_features, n_obs, warnings)
- Parameters:
- Return type:
None
- class tseda.forecastability.leakage.LeakageDetector[source]
Bases:
objectDetect temporal and target leakage in a feature set.
The detector is stateless.
- check(ts, horizon, features_df, target_corr_threshold)[source]
Return a
LeakageReport.- Parameters:
ts (TimeSeries)
horizon (int)
features_df (DataFrame | None)
target_corr_threshold (float)
- Return type:
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.leakage import LeakageDetector
Target leakage — a feature that is the target:
>>> rng = np.random.default_rng(0) >>> n = 80 >>> idx = pd.date_range("2020", periods=n, freq="D") >>> y = rng.standard_normal(n) >>> ts = TimeSeries(y, index=idx) >>> feat = pd.DataFrame({"target_copy": y}, index=idx) >>> r = LeakageDetector().check(ts, horizon=1, features_df=feat) >>> r.has_target_leakage True >>> "target_copy" in r.target_leakage_columns True
- check(ts, horizon, *, features_df=None, target_corr_threshold=0.95)[source]
Check features_df for leakage against target ts.
- Parameters:
ts (TimeSeries) – Target time series.
horizon (int) – Forecast horizon in time steps. Must be >= 1.
features_df (pandas.DataFrame, optional) – Feature matrix with the same
DatetimeIndexas ts, one column per feature. WhenNonethe report is empty with a warning.target_corr_threshold (float, optional) – Pearson r threshold above which a feature is flagged as target-leaking. Default
0.95.
- Return type:
- Raises:
TypeError – If ts is not a
TimeSeries.ValueError – If horizon < 1, target_corr_threshold ∉ (0, 1], or features_df has a different number of rows from ts.
Examples
>>> import numpy as np, pandas as pd >>> from tseda import TimeSeries >>> from tseda.forecastability.leakage import LeakageDetector
>>> rng = np.random.default_rng(1) >>> n = 60 >>> idx = pd.date_range("2020", periods=n, freq="D") >>> ts = TimeSeries(rng.standard_normal(n), index=idx) >>> r = LeakageDetector().check(ts, horizon=3) >>> r.n_features 0