Skip to main content

module neuralforecast.utils

Global Variables

  • AirPassengers
  • unique_id
  • ds
  • y
  • airline1_dummy
  • airline2_dummy

function generate_series

generate_series(
    n_series: int,
    freq: str = 'D',
    min_length: int = 50,
    max_length: int = 500,
    n_temporal_features: int = 0,
    n_static_features: int = 0,
    equal_ends: bool = False,
    seed: int = 0
) → DataFrame
Generate Synthetic Panel Series. Generates n_series of frequency freq of different lengths in the interval [min_length, max_length]. If n_temporal_features > 0, then each serie gets temporal features with random values. If n_static_features > 0, then a static dataframe is returned along the temporal dataframe. If equal_ends == True then all series end at the same date. Args:
  • n_series (int): Number of series for synthetic panel.
  • freq (str, optional): Frequency of the data, panda’s available frequencies. Defaults to “D”.
  • min_length (int, optional): Minimal length of synthetic panel’s series. Defaults to 50.
  • max_length (int, optional): Maximal length of synthetic panel’s series. Defaults to 500.
  • n_temporal_features (int, optional): Number of temporal exogenous variables for synthetic panel’s series. Defaults to 0.
  • n_static_features (int, optional): Number of static exogenous variables for synthetic panel’s series. Defaults to 0.
  • equal_ends (bool, optional): If True, series finish in the same date stamp ds. Defaults to False.
  • seed (int, optional): Random seed for reproducibility. Defaults to 0.
Returns:
  • pd.DataFrame: Synthetic panel with columns [unique_id, ds, y] and exogenous.

function time_features_from_frequency_str

time_features_from_frequency_str(freq_str: str) → List[TimeFeature]
Returns a list of time features that will be appropriate for the given frequency string. Args:
  • freq_str (str): Frequency string of the form [multiple][granularity] such as “12H”, “5min”, “1D” etc.
Returns:
  • List[TimeFeature]: List of time features appropriate for the frequency.

function augment_calendar_df

augment_calendar_df(df, freq='H')
Augment a dataframe with calendar features based on frequency. Frequency mappings:
  • Q - [month]
  • M - [month]
  • W - [Day of month, week of year]
  • D - [Day of week, day of month, day of year]
  • B - [Day of week, day of month, day of year]
  • H - [Hour of day, day of week, day of month, day of year]
  • T - [Minute of hour*, hour of day, day of week, day of month, day of year]
  • S - [Second of minute, minute of hour, hour of day, day of week, day of month, day of year]
*minute returns a number from 0-3 corresponding to the 15 minute period it falls into. Args:
  • df (pd.DataFrame): DataFrame to augment with calendar features.
  • freq (str, optional): Frequency string for determining which features to add. Defaults to “H”.
Returns:
  • Tuple[pd.DataFrame, List[str]]: Tuple of (augmented DataFrame, list of feature column names).

function get_indexer_raise_missing

get_indexer_raise_missing(idx: Index, vals: List[str]) → List[int]
Get index positions for values, raising error if any are missing. Args:
  • idx (pd.Index): Index to search in.
  • vals (List[str]): Values to find indices for.
Returns:
  • List[int]: List of index positions.
Raises:
  • ValueError: If any values are missing from the index.

function add_conformal_distribution_intervals

add_conformal_distribution_intervals(
    model_fcsts: <built-in function array>,
    cs_df: ~DFType,
    model: str,
    cs_n_windows: int,
    n_series: int,
    horizon: int,
    level: Optional[List[Union[int, float]]] = None,
    quantiles: Optional[List[float]] = None
) → Tuple[<built-in function array>, List[str]]
Add conformal intervals based on conformal scores using distribution strategy. This strategy creates forecast paths based on errors and calculates quantiles using those paths. Args:
  • model_fcsts (np.array): Model forecasts array.
  • cs_df (DFType): DataFrame containing conformal scores.
  • model (str): Model name.
  • cs_n_windows (int): Number of conformal score windows.
  • n_series (int): Number of series.
  • horizon (int): Forecast horizon.
  • level (Optional[List[Union[int, float]]], optional): Confidence levels for prediction intervals. Defaults to None.
  • quantiles (Optional[List[float]], optional): Quantiles for prediction intervals. Defaults to None.
Returns:
  • Tuple[np.array, List[str]]: Tuple of (forecasts with intervals, column names).

function add_conformal_error_intervals

add_conformal_error_intervals(
    model_fcsts: <built-in function array>,
    cs_df: ~DFType,
    model: str,
    cs_n_windows: int,
    n_series: int,
    horizon: int,
    level: Optional[List[Union[int, float]]] = None,
    quantiles: Optional[List[float]] = None
) → Tuple[<built-in function array>, List[str]]
Add conformal intervals based on conformal scores using error strategy. This strategy creates prediction intervals based on absolute errors. Args:
  • model_fcsts (np.array): Model forecasts array.
  • cs_df (DFType): DataFrame containing conformal scores.
  • model (str): Model name.
  • cs_n_windows (int): Number of conformal score windows.
  • n_series (int): Number of series.
  • horizon (int): Forecast horizon.
  • level (Optional[List[Union[int, float]]], optional): Confidence levels for prediction intervals. Defaults to None.
  • quantiles (Optional[List[float]], optional): Quantiles for prediction intervals. Defaults to None.
Returns:
  • Tuple[np.array, List[str]]: Tuple of (forecasts with intervals, column names).

function get_prediction_interval_method

get_prediction_interval_method(method: str)
Get the prediction interval method function by name. Args:
  • method (str): Name of the prediction interval method.
Returns:
  • Callable: The corresponding method function.
Raises:
  • ValueError: If the method is not supported.

function level_to_quantiles

level_to_quantiles(level: List[Union[int, float]]) → List[float]
Convert a list of confidence levels to quantiles. Args:
  • level (List[Union[int, float]]): List of confidence levels (e.g., [80, 90]).
Returns:
  • List[float]: List of corresponding quantiles.

function quantiles_to_level

quantiles_to_level(quantiles: List[float]) → List[Union[int, float]]
Convert a list of quantiles to confidence levels. Args:
  • quantiles (List[float]): List of quantiles (e.g., [0.1, 0.5, 0.9]).
Returns:
  • List[Union[int, float]]: List of corresponding confidence levels.

class TimeFeature

method __init__

__init__()

class SecondOfMinute

Second of minute encoded as value between [-0.5, 0.5].

method __init__

__init__()

class MinuteOfHour

Minute of hour encoded as value between [-0.5, 0.5].

method __init__

__init__()

class HourOfDay

Hour of day encoded as value between [-0.5, 0.5].

method __init__

__init__()

class DayOfWeek

Day of week encoded as value between [-0.5, 0.5].

method __init__

__init__()

class DayOfMonth

Day of month encoded as value between [-0.5, 0.5].

method __init__

__init__()

class DayOfYear

Day of year encoded as value between [-0.5, 0.5].

method __init__

__init__()

class MonthOfYear

Month of year encoded as value between [-0.5, 0.5].

method __init__

__init__()

class WeekOfYear

Week of year encoded as value between [-0.5, 0.5].

method __init__

__init__()

class PredictionIntervals

Class for storing prediction intervals metadata information.

method __init__

__init__(n_windows: int = 2, method: str = 'conformal_distribution')
Initialize PredictionIntervals. Args:
  • n_windows (int, optional): Number of windows to evaluate. Defaults to 2.
  • method (str, optional): One of the supported methods for the computation of prediction intervals: conformal_error or conformal_distribution. Defaults to “conformal_distribution”.