Soothsayer.AR (Soothsayer v0.6.1)

View Source

Auto-regression (AR) component functionality.

Handles network building, feature engineering, and weight extraction for AR models. Supports both linear AR and deep AR-Net architectures with configurable hidden layers.

Summary

Functions

Builds the AR component layer(s).

Builds AR input tensor for prediction given training data and prediction dates.

Creates the Axon input node for the AR component.

Creates lagged input features and corresponding targets for AR training.

Extracts raw AR layer weights from a fitted model.

Functions

build_component(input, arg2)

@spec build_component(Axon.t() | nil, map()) :: Axon.t()

Builds the AR component layer(s).

Supports both linear AR (single dense layer) and deep AR-Net (multiple hidden layers with ReLU activation followed by linear output).

Parameters

Returns

An Axon layer when AR is enabled, Axon.constant(0) otherwise.

build_input(training_data, prediction_dates, lags)

@spec build_input(map(), list(), non_neg_integer()) :: Nx.Tensor.t()

Builds AR input tensor for prediction given training data and prediction dates.

For each prediction date, looks up the previous lags values from the training data to use as AR features. Returns zeros for dates that don't have enough history.

Parameters

  • training_data - Map with :dates (list of dates) and :y_normalized (list of values)
  • prediction_dates - List of dates to build AR inputs for
  • lags - Number of lagged values to include

Returns

A tensor of shape {n_predictions, lags} with AR features.

build_network_input(arg1)

@spec build_network_input(map()) :: Axon.t() | nil

Creates the Axon input node for the AR component.

Parameters

  • config - Model configuration map with :ar key.

Returns

An Axon input node when AR is enabled, nil otherwise.

create_lagged_inputs(y, lags)

@spec create_lagged_inputs(Nx.Tensor.t(), non_neg_integer()) ::
  {Nx.Tensor.t(), Nx.Tensor.t()}

Creates lagged input features and corresponding targets for AR training.

Given a time series y and number of lags, creates sliding windows where each window contains lags consecutive values, and the target is the next value.

Parameters

  • y - A 1D tensor of time series values.
  • lags - Number of lagged values to use as features.

Returns

A tuple {lagged, targets} where:

  • lagged - Tensor of shape {n_samples, lags} with lagged features
  • targets - Tensor of shape {n_samples, 1} with target values

Examples

iex> y = Nx.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
iex> {lagged, targets} = Soothsayer.AR.create_lagged_inputs(y, 3)
iex> Nx.shape(lagged)
{2, 3}

get_weights(model)

@spec get_weights(Soothsayer.Model.t()) :: %{
  required(String.t()) => %{kernel: Nx.Tensor.t(), bias: Nx.Tensor.t()}
}

Extracts raw AR layer weights from a fitted model.

For linear AR models, returns the output layer weights. For deep AR-Net models, returns all layer weights including hidden layers.

Parameters

Returns

A map of layer names to weight structs containing :kernel and :bias tensors.

Examples

iex> weights = Soothsayer.AR.get_weights(fitted_model)
%{"ar_dense_out" => %{kernel: #Nx.Tensor<...>, bias: #Nx.Tensor<...>}}