timeseries_forecast package
Submodules
timeseries_forecast.AR_NN_models module
- class timeseries_forecast.AR_NN_models.AR_quantile_neural_network(tau=0.5, loss_type='quantile', **kwargs)
Bases:
base_neural_network
Use a tensorflow to train an autoregressive quantile neural network. The loss seeks to minimize the weighted residual based on the quantile which causes predicitions to estimate the expectation of the quantile.
- Parameters:
tau (float, optional) –
Determines the quantile [0.0-1.0] that the network will seek to follow. The default is 0.5.
Example: 0.5 will result in following the median of the data where as 0.95 will attempt to cover 95% of the data.
loss_type (str, optional) – Determines the loss function to use while training the neural network. The default is ‘quantile’. Accepts strings for any of the standard tensorflow loss functions.
tau – The percentile to use in a quantile regression. The default is 0.5.
loss_type – Any tensorflow loss or ‘quantile’. The default is ‘quantile’.
- Return type:
None.
- load_model(filename)
load the model weights and config values
timeseries_forecast.GP_model module
- class timeseries_forecast.GP_model.PhysicsKernel_CrackGrowth(initial_crack_length, periodic_load=0.05, C_constraint=None, m_constraint=None, batch_shape=torch.Size([]), **kwargs)
Bases:
Mean
This object serves as an example of including a physics function in the Gaussian Process. Pytorch will optimize the registered parameters including the defined constraints.
- Parameters:
initial_crack_length (float) – Initial crack size to be inserted as a constant in Paris equation.
periodic_load (float, optional) – Cyclic load causing crack propagation. The default is 0.05.
batch_shape (int, optional) – Size of batches to train on in SGD. The default is torch.Size().
- Return type:
None.
- property C
- property m
- forward(x)
Using the Paris equation for crack propgation under a cyclic load.
- Parameters:
x (cycles of fatigue) –
- Returns:
at
- Return type:
crack size at time t
- class timeseries_forecast.GP_model.GPModel(train_x, train_y, lr=0.1, model_type='basic', mean_function='constant', likelihood_function='gaussian', lengthscale_constraint=None, outputscale_constraint=None, periodic_period_constraint=None, periodic_lengthscale_constraint=None, likelihood_noise_constraint=0.0001, use_cholesky=True, periodic=False, n_mixtures=4, base_kernel='RBF', train_auto_stop=True, auto_stop_tol=1e-05, early_stoppage_iterations=100)
Bases:
object
Using gpytorch as the backend solver, this object constructs a Gaussian Process of varying levels of complexity, which includes incorporation of physics objects to both resolve the covaraince and calibrate the model inputs.
The main goal here is bring scikit ease of use to the powerful scaling and flexibility of gpytorch.
- Parameters:
train_x (np.array) –
train_y (np.array) –
lr (float, optional) – Learning rate used in SGD. The default is 0.1.
model_type (str, optional) – basic or spectral_mixing. The default is ‘basic’.
mean_function (str, optional) – ‘constant’,’linear’, or custom object such as the ‘PhysicsKernel_CrackGrowth’. The default is ‘constant’.
likelihood_function (str, optional) – Only supports gaussian at the moment. The default is ‘gaussian’.
lengthscale_constraint (float, optional) – Constraint used in the lengthscale of the kernel. The default is None.
outputscale_constraint (float, optional) – Constraint used in the prediction output. The default is None.
periodic_period_constraint (float, optional) – Constraint used in the period of the periodic kernel if present. The default is None.
periodic_lengthscale_constraint (float, optional) – Constraint used in the lengthscale of the periodic kernel if present. The default is None.
likelihood_noise_constraint (float, optional) – Constraint used in the magnitude of the noise adder to the covariance matrix. The default is 1e-4.
use_cholesky (bool, optional) – Use cholesky decompisition during training. The default is True.
periodic (bool, optional) – If true add a periodic kernel to the base_kernel. The default is False.
n_mixtures (int, optional) – The default is 4.
base_kernel (str, optional) – Base kernel to use in the covariance matrix. The default is ‘RBF’.
train_auto_stop (bool, optional) – If true, stop training after early_stoppage_iterations of no improvement. The default is True.
auto_stop_tol (float, optional) – If absolute change in loss is below this value, stop traning. The default is 1e-5.
early_stoppage_iterations (int, optional) – If during training the loss does not improve within this many iterations, stop training. The default is 100.
- Return type:
None.
- train(training_iter)
- Return type:
None
- Parameters:
training_iter (int) – Number of training iterations to perform
- Return type:
None
- update_training_data(newX, newy, replace=False)
Update the training data stored within the object. When training, the GP will use this data to refine the fitting parameters. Can be used to refine existing models instead of retraining from scratch.
- Return type:
None
- Parameters:
newX (np.array) –
newy (np.array) –
replace (bool, optional) – Append to the existing training set or replace. The default is False.
- Return type:
None
- get_estimate(X, uncertainty='confidence')
- Return type:
object
- Parameters:
X (np.array) –
uncertainty (str, optional) – Either sets the uncertainty to be based on the confidence (95%) of the mean prediction or the prediction interval (95%), which bounds were we would expect to see a newly measured value. The default is ‘confidence’.
- Returns:
observed_pred –
gpytorch object that includes the attributes/methods:
confidence_region mean variance
- Example:
observed_pred = GP.get_estimate(X, uncertainty=’confidence’)
# Get upper and lower confidence bounds lower, upper = observed_pred.confidence_region()
# Get the expectation mean = observed_pred.mean.numpy()
# Get the variance mean = observed_pred.variance.numpy()
- Return type:
object
- plot(X, y=None, uncertainty='confidence')
Generate figures of the GP performance with the X,y data.
- Return type:
None
- Parameters:
X (np.array) –
y (np.array, optional) – The default is None.
uncertainty (str, optional) – confidence or prediction. The default is ‘confidence’.
- Return type:
None
- print_parameters()
Print to screen the raw GP parameters, the constraints on each of these parameters, and the transformed actual values of these parameters.
- Return type:
None.
timeseries_forecast.parametric module
- class timeseries_forecast.parametric.ts_model(volatility_model=False, **kwargs)
Bases:
object
Provides a pass through interface for AutoARIMA or ARCH models.
Users should see the following links for full API inputs to these functions. This object currently provides no additional value for ARIMA.
Main functionality is the additional of AutoARCH grid searching.
- AutoARIMA documentation:
- Arch documentation:
- Parameters:
volatility_model (bool, optional) – Determines if an ARIMA or ARCH model should be used. Default is False, which uses an ARIMA model.
Examples
In this example, an ARIMA model is automatically selected that best fits the provided ‘y’ data. The automation performs a parallized grid search with the max AR lags of 8, max MA lags of 3, and 1 level of signal differencing is used.
The predict function will start from the end of the training data and forecast n_periods into the future.
from twinstat.timeseries_forecast.parametric import ts_model TS = ts_model(max_p =8, max_q=3, D=1, n_jobs=-1, stepwise=False) TS.model.fit(y) TS.model.summary() TS.model.predict(n_periods=5)
- plot(series, lags=40)
Some useful diagnostic plots to determine what the proper AR and MA terms would be for the provided data.
Note that the a decaying ACF will determine MA and a decarying PACF will determine AR. Users may want to increase the number of lags shown, to assess if a season lag needs to be included in the models.
Users are recommended to see a time series text to fully understand how to use these plots.
- Parameters:
series (np.array) – Data to plot.
lags (int, optional) – Number of lags to include in the plot. The default is 40.
- Returns:
None.
rtype:
None
- auto_arch(series, max_p=3, max_o=0, max_q=3, n_jobs=1, **kwargs)
Perform a grid search to find best ARCH model. Performance criteria based on the Bayesian Information Criterion (BIC)
- Return type:
None
- Parameters:
series (np.array) – Data to fit.
max_p (int, optional) – max AR terms to use in the fitting. The default is 3.
max_o (int, optional) – max asymmetric innovation terms to use in the fitting. The default is 0.
max_q (int, optional) – max MA terms to use in the fitting. The default is 3.
n_jobs (int, optional) – Number of parallel processes to use. -1 will attempt to use all available CPUs. The default is 1.
rescale (bool, optional) – Internally rescale data to improve convergence.
- Return type:
None