# `Soothsayer.AR`
[🔗](https://github.com/georgeguimaraes/soothsayer/blob/v0.6.3/lib/soothsayer/ar.ex#L1)

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.

# `build_component`

```elixir
@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

  * `input` - Axon input node from `build_network_input/1`.
  * `config` - Model configuration map.

## Returns

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

# `build_input`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@spec get_weights(Soothsayer.Model.t()) :: %{
  required(String.t()) =&gt; %{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

  * `model` - A fitted `Soothsayer.Model` struct with AR enabled.

## 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<...>}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
