Models

class BaseModel(name, horizon=5, delay=5)[source]

Bases: object

Abstract class representing the base specification of a model.

:param : param str name: the name of the method.

# Attributes: # :attr: fit: fit model to data using X (N observations x M features) and y (N observations) # :attr: predict: regression. Predict # :attr: fit:

abstract fit(X, y=None, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

abstract fit_transform(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

abstract predict(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

ARIMA

class ARIMA(name, p=5, d=1, q=0)[source]

arima is parameterized by \(p\), the number of autoregressive terms, \(d\), the number of differences needed for stationarity and \(q\), the number of lagged forecast errors in the forecasting equation:

\[\begin{equation} \hat{x_t} = \alpha + \beta_1 x_{t-1} +...+ \beta_p x_{t-p} - \theta_1 e_{t-1} - \theta_q e_{t-q} \end{equation}\]

where \(\beta\) represent autoregressive terms, \(\theta\) represent moving average terms, and \(\alpha\) is a bias/intercept term. Potential uses include autoregression, imputation, and fault detection. base implementation from statsmodels.tsa.arima.model.arima

Parameters:
  • name (string) – generic name to save and load this model.

  • p (int) – number of autoregressive terms \(x_{t-1} + ... + x_{t-p}\)

  • d (int) – differencing factor between autoregressive terms and moving average terms.

  • q (int) – number of moving average terms. \(q\) in the equation above.

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

KNN

class KNN(name, n_neighbors=10)[source]

K nearest neighbors wrapper around implementation from sklearn.neighbors.KNeighborsRegressor

Parameters:
  • name (string) – the name of the method.

  • n_neighbors (int) – number of neighbors to use.

fit(X, y, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

LSTM

class LSTM(name, batch_size=10, train_iters=10, hidden_size=64, layer_dim=1, learning_rate=0.2)[source]
Parameters:
  • number_of_features – number of input features

  • hidden_dim – hidden size of the RNN

  • n_layers – number of layers in RNN

  • batch_size – batch size

  • seq_len – TODO: specify this better

  • output_size – Output dimension

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

class LSTM_Model(input_dim, batch_size, seq_len, layer_dim, output_dim, hidden_dim, dropout=0.1)[source]

LSTM wrapper around LSTM implementation Kuguoglu 2021

Parameters:
  • number_of_features – number of input features

  • hidden_dim – hidden size of the RNN

  • n_layers – number of layers in RNN

  • batch_size – batch size

  • seq_len – TODO: specify this better

  • output_size – Output dimension

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

RNN

class RNN(input_size, batch_size, seq_len, output_size, hidden_dim, n_layers)[source]

Bases: Module

RNN wrapper around RNN implementation Kuguoglu 2021

Parameters:
  • number_of_features – number of input features

  • hidden_dim – hidden size of the RNN

  • n_layers – number of layers in RNN

  • batch_size – batch size

  • seq_len – TODO: specify this better

  • output_size – Output dimension

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class VanillaRNN(name, train_iters=10, num_layers=3, hidden_size=1, batch_size=5, learning_rate=0.01)[source]

Bases: BaseModel

fit(X, **kwargs)[source]

Fit hyperparameters using Maximum Likelihood Estimation

X (2d torch.tensor): training data in 2d array form (nsamples,nfeatures) y (1d torch.tensor): training data in list form, (nsameples) for each response

fit_transform(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

predict(X, **kwargs)[source]
Args
param X 2d torch.tensor:

training data in 2d array form (nsamples,nfeatures)

VRAE

class VRAE(name, batch_size=1, hidden_size=90, hidden_layer_depth=1, latent_length=20, learning_rate=0.005, block='LSTM', train_iters=5, dropout_rate=0.0, optimizer_name='Adam', loss_fn='MSELoss', cuda=False, print_every=100, clip=True, max_grad_norm=5, dload='.')[source]

Variational recurrent auto-encoder. This module is used for dimensionality reduction of timeseries

Built using help from timeseries-clustering-vae(Lodaya2021)

Parameters:
  • sequence_length – length of the input sequence

  • number_of_features – number of input features

  • hidden_size – hidden size of the RNN

  • hidden_layer_depth – number of layers in RNN

  • latent_length – latent vector length

  • batch_size – number of timeseries in a single batch

  • learning_rate – the learning rate of the module

  • block – GRU/LSTM to be used as a basic building block

  • n_epochs – Number of iterations/epochs

  • dropout_rate – The probability of a node being dropped-out

  • optimizer – ADAM/ SGD optimizer to reduce the loss function

  • loss – SmoothL1Loss / MSELoss / ReconLoss / any custom loss which inherits from _Loss class

  • cuda (boolean) – to be run on GPU or not

  • print_every – The number of iterations after which loss should be printed

  • clip (boolean) – Gradient clipping to overcome explosion

  • max_grad_norm – The grad-norm to be clipped

  • dload – Download directory where models are to be dumped

compute_loss(X, X_true)[source]

Given input tensor, forward propagate, compute the loss, and backward propagate. Represents the lifecycle of a single iteration

Parameters:

X (tensor) – Input

Returns:

total loss, reconstruction loss, kl-divergence loss and original input

fit(X, save=False, **kwargs)[source]

Calls _train function over a fixed number of epochs, specified by n_epochs

Parameters:
  • X (dataset) – np.array

  • save (bool) – If true, dumps the trained model parameters as pickle file at dload directory

Returns:

fit_transform(dataset, save=False, **kwargs)[source]

Combines the fit and transform functions above :param dataset: Dataset on which fit and transform have to be performed :param bool save: If true, dumps the model and latent vectors as pickle file :return: latent vectors for input dataset

forward(x)[source]

Forward propagation which involves one pass from inputs to encoder to lambda to decoder

:param x:input tensor

Returns:

the decoded output, latent vector

load(PATH)[source]

Loads the model’s parameters from the path mentioned :param PATH: Should contain pickle file :return: None

predict(X, save=False, **kwargs)[source]

Given input dataset, creates dataloader, runs dataloader on _batch_transform Prerequisite is that model has to be fit :param dataset: input dataset who’s latent vectors are to be obtained :param bool save: If true, dumps the latent vector dataframe as a pickle file :return:

reconstruct(X, save=False)[source]

Given input dataset, creates dataloader, runs dataloader on _batch_reconstruct Prerequisite is that model has to be fit :param dataset: input dataset who’s output vectors are to be obtained :param bool save: If true, dumps the output vector dataframe as a pickle file :return:

save(file_name)[source]

Pickles the model parameters to be retrieved later :param file_name: the filename to be saved as,`dload` serves as the download directory :return: None