pyamr.core.regression package

Submodules

pyamr.core.regression.arima module

Classes:

ARIMAWrapper([estimator, evaluate])

class pyamr.core.regression.arima.ARIMAWrapper(estimator=None, evaluate=True)[source]

Bases: RegressionWrapper

Methods:

as_summary(**kwargs)

Returns a summary string (statsmodels inspired)

auto(endog[, exog, ic, trends, max_ar, ...])

Find the best model through bruteforce.

conf_int_forecast(predictions[, alpha])

Computes the out-sample confidence interval.

evaluate([alpha])

Generates a dictionary with the relevant attributes.

get_prediction([start, end, alpha])

Calls the predict function in ARIMA.

as_summary(**kwargs)[source]

Returns a summary string (statsmodels inspired)

auto(endog, exog=None, ic='bic', trends=['n', 'c'], max_ar=3, max_ma=3, max_d=1, warn='ignore', return_fits=False, converged=True, disp=0, verbose=0, **kwargs)[source]

Find the best model through bruteforce.

Note: It uses grid search through the base wrapper.

Parameters:
  • endog (array-like) – The endogenous variable (aka. time series data)

  • exog (array-like) – The exogenous variable (by default is the time t starting in 0)

  • ic (string) – The information criteria which should be any of the params which are set in the wrapper (see method _init_result).

  • max_ar (number) – The maximum autorregresive order to inspect.

  • max_ma (number) – The maximum moving average order to inspect.

  • max_d (number) – The maximum difference to inspect.

  • warn (string, options = {ignore, raise}) – Whether or not to ignore the warnings raised

  • return_fits (bool) – Whether or not to return all the fits. If false only the best model is returned. If true all the models and the best model are returned separately.

  • converged (bool) – Whether or not to return only converging models. If false all models are returned. If true, only those models that converged are returned.

conf_int_forecast(predictions, alpha=0.05)[source]

Computes the out-sample confidence interval.

Parameters:
  • predictions (array-like) – The predictions done by the model.

  • alpha (int) – The alpha to configure the confidence interval.

Return type:

array-like

evaluate(alpha=0.05)[source]

Generates a dictionary with the relevant attributes.

Note

There is an issue with the method conf_int(alpha). It can receive as input parameters a pandas series or a numpy array. We are using it with numpy array as input, hence when pandas series is passed the method _init_result gives an error.

Include ref to statsmodels:Arima and statsmodels:ArimaResults.

Parameters:

alpha (the confidence interval.)

Returns:

dictionary

Return type:

map with all the parameters.

get_prediction(start=None, end=None, alpha=0.05, **kwargs)[source]

Calls the predict function in ARIMA.

Note

The confidence intervals have been implemented using the following functions _forecast_conf_int() and _forecast_error(). These are implemented in statsmodels.ts.arima_model.ARIMAResults. As such, it produces the same confidence intervals than the method plot_predict().

Note

The parameter start refers to the original series. As such, if the series has been differenced (e.g. d=1) the first observation has been lost. In those cases, start has to be greater or equal to d.

Todo

  • Review the predictions when dynamic is set to true.

  • Review the confidence intervals (in and out sample)

  • Review the prediction intervals.

Parameters:
  • start (int (optional)) – The time t to start the prediction

  • end (int (optional)) – The time t to end the prediction

  • dynamic (bool) – The dynamic keyword affects in-sample prediction. If dynamic is false then the in-sample lagged values are used for prediction. if dynamic is true, then in-sample forecasts are used in place of lagged dependent variables. The first forecasted value is start.

  • alpha (float) – The alpha value when creating the norm point percent function to compute the confidence intervals for the forecast predictions.

  • typ (string-like, options = {linear, levels}) – Wether to predict the original levels (levels) or predict in terms of the differenced endogenous variables.

Return type:

matrix

pyamr.core.regression.pyarima module

Classes:

PyramidWrapper(**kwargs)

class pyamr.core.regression.pyarima.PyramidWrapper(**kwargs)[source]

Bases: BaseRegressionWrapper

Methods:

as_summary(**kwargs)

This method displays the summary.

auto(**kwargs)

This method finds the best arima.

fit(**kwargs)

This method fits the specified arima model.

from_instance(arima, **kwargs)

This method constructs a PyramidWrapper object from pyramid.ARIMA

get_prediction(**kwargs)

as_summary(**kwargs)[source]

This method displays the summary.

auto(**kwargs)[source]

This method finds the best arima.

@see pyrmid.arima.auto_arima

fit(**kwargs)[source]

This method fits the specified arima model.

Parameters:
  • endog

  • exog

  • missing

  • hasconst

Returns:

object

Return type:

A PyramidWrapper object.

from_instance(arima, **kwargs)[source]

This method constructs a PyramidWrapper object from pyramid.ARIMA

get_prediction(**kwargs)[source]

pyamr.core.regression.pyarima2 module

Classes:

PyramidWrapper([estimator, evaluate])

class pyamr.core.regression.pyarima2.PyramidWrapper(estimator=None, evaluate=True)[source]

Bases: RegressionWrapper

Methods:

as_summary(**kwargs)

This method displays the summary.

auto(**kwargs)

This method finds the best arima.

evaluate([alpha])

This method set all the variables into this class.

from_instance(arima, **kwargs)

This method constructs a PyramidWrapper object from pyramid.ARIMA

get_prediction([start, end, alpha])

resid()

Get the residual

as_summary(**kwargs)[source]

This method displays the summary.

auto(**kwargs)[source]

This method finds the best arima.

@see pyrmid.arima.auto_arima

evaluate(alpha=0.05)[source]

This method set all the variables into this class.

@see: statsmodels.Arima @see: statsmodels.ArimaResults

Parameters:

alpha

Return type:

series

from_instance(arima, **kwargs)[source]

This method constructs a PyramidWrapper object from pyramid.ARIMA

get_prediction(start=None, end=None, alpha=0.05, **kwargs)[source]
resid()[source]

Get the residual

pyamr.core.regression.sarimax module

Classes:

SARIMAXWrapper([estimator, evaluate])

Description

class pyamr.core.regression.sarimax.SARIMAXWrapper(estimator=None, evaluate=True)[source]

Bases: RegressionWrapper

Description

Methods:

as_summary(**kwargs)

This method creates a summary string.

auto(endog[, exog, ic, max_ar, max_ma, ...])

This method finds the best arima through bruteforce.

evaluate([alpha])

This method creates a dictioanry with the relevant parameters.

get_prediction([start, end, alpha])

This method calls the get prediction function in ARIMA.

load(**kwargs)

This method loads the information.

save(**kwargs)

This method saves the information.

as_summary(**kwargs)[source]

This method creates a summary string.

auto(endog, exog=None, ic='bic', max_ar=3, max_ma=3, max_d=1, max_P=0, max_D=0, max_Q=0, list_s=[12], warn='ignore', trends=['n', 'c', 't', 'ct'], return_fits=False, verbose=0, converged=True, **kwargs)[source]

This method finds the best arima through bruteforce.

Note: It uses grid search through the base wrapper.

Parameters:
  • endog (array-like) – The endogenous variable (aka. time series data)

  • exog (array-like) – The exogenous variable (by default is the time t starting in 0)

  • ic (string) – The information criteria which should be any of the params which are set in the wrapper (see method _init_result).

  • max_ar (number) – The maximum autorregresive order to inspect.

  • max_ma (number) – The maximum moving average order to inspect.

  • max_d (number) – The maximum difference to inspect.

  • max_P (number)

  • max_D (number)

  • max_Q (number)

  • list_s (array-like)

  • warn (string, options = {ignore, raise}) – Whether or not to ignore the warnings raised

  • return_fits (bool) – Whether or not to return all the fits. If false only the best model is returned. If true all the models and the best model are returned separately.

  • converged (bool) – Whether or not to return only converging models. If false all models are returned. If true, only those models that converged are returned.

evaluate(alpha=0.05)[source]

This method creates a dictioanry with the relevant parameters.

@see: statsmodels.Sarimax @see: statsmodels.SarimaxResults

Parameters:

alpha (significance level)

Returns:

dictionary

Return type:

dictionary with the parameters.

get_prediction(start=None, end=None, alpha=0.05, **kwargs)[source]

This method calls the get prediction function in ARIMA.

Parameters:
  • start (int (optional)) – The time t to start the prediction

  • end (int (optional)) – The time t to end the prediction

  • dynamic (bool) – The dynamic keyword affects in-sample prediction. If dynamic is false then the in-sample lagged values are used for prediction. if dynamic is true, then in-sample forecasts are used in place of lagged dependent variables. The first forecasted value is start.

  • alpha (float) – The alpha value when creating the norm point percent function to compute the confidence intervals for the forecast predictions.

  • typ (string-like, options = {linear, levels}) – Wether to predict the original levels (levels) or predict in terms of the differenced endogenous variables.

Return type:

matrix

load(**kwargs)[source]

This method loads the information.

save(**kwargs)[source]

This method saves the information.

pyamr.core.regression.theilsens module

Classes:

TheilSensWrapper(**kwargs)

class pyamr.core.regression.theilsens.TheilSensWrapper(**kwargs)[source]

Bases: BaseRegressionWrapper

Methods:

as_summary([alpha])

This method displays the summary.

fit(**kwargs)

This method....

get_prediction([exog, start, end])

This method...

as_summary(alpha=0.05)[source]

This method displays the summary.

fit(**kwargs)[source]

This method….

Parameters:
  • x

  • y

  • alpha

Returns:

object

Return type:

A KendallWrapper objects.

get_prediction(exog=None, start=None, end=None, **kwargs)[source]

This method…

Parameters:
  • exog

  • start

  • end

  • kwargs

pyamr.core.regression.wbase module

Classes:

BaseWrapper(**kwargs)

class pyamr.core.regression.wbase.BaseWrapper(**kwargs)[source]

Bases: object

Methods:

as_series([flabel, label])

This method returns a series with all the information.

as_summary()

This method displays the final summary.

attrs()

This method returns all the defined attributes as tuples.

fargs(kwargs, function)

This method returns elements in kwargs which are function inputs.

fit(**kwargs)

This method performs the fit.

from_list_dataframe(wrapper_list, **kwargs)

This methods creates a dataframe summary from a list.

grid_search(grid_params)

This method computes grid search.

grid_search_dataframe(grid_params, **kwargs)

This method computes grid search and stores results in a dataframe.

load(fname)

This method loads the wrapper.

methods()

This method returns all the defined methods as tuples.

save(fname)

This method saves the wrapper.

as_series(flabel=True, label=None)[source]

This method returns a series with all the information.

Parameters:

label (label to concatenate to index name (e.g. trend to label-trend).)

Returns:

series

Return type:

pandas series with the results, configuration and model.

as_summary()[source]

This method displays the final summary.

attrs()[source]

This method returns all the defined attributes as tuples.

fargs(kwargs, function)[source]

This method returns elements in kwargs which are function inputs.

fit(**kwargs)[source]

This method performs the fit.

from_list_dataframe(wrapper_list, **kwargs)[source]

This methods creates a dataframe summary from a list.

Parameters:
  • wrapper_list (list with wrapper objects.)

  • flabel (if include the class _name in the index.)

Returns:

summary

Return type:

pandas dataframe.

This method computes grid search.

It creates all the possible combinations combining the arguments passed. Such arguments should be for the constructor and the fit methods. The results are stored in an array with the corresponding wrapper instances.

Notes: The wrappers need to have the method fit implemented.

Parameters:
  • con_kwargs (arguments to be passed to the constructor method.)

  • fit_kwargs (arguments to be passed to the fit method.)

Returns:

summary

Return type:

summary with all the elements.

grid_search_dataframe(grid_params, **kwargs)[source]

This method computes grid search and stores results in a dataframe.

Parameters:
  • con_kwargs (arguments to be passed to the constructor method.)

  • fit_kwargs (arguments to be passed to the fit method.)

Returns:

summary

Return type:

summary with all the elements.

load(fname)[source]

This method loads the wrapper.

methods()[source]

This method returns all the defined methods as tuples.

save(fname)[source]

This method saves the wrapper.

pyamr.core.regression.wls module

Classes:

PredictionResult(mean, cilo, ciup, pstd, ...)

WLSWrapper([estimator, evaluate])

The description...

class pyamr.core.regression.wls.PredictionResult(mean, cilo, ciup, pstd, pilo, piup, time, nobs, endog)[source]

Bases: object

class pyamr.core.regression.wls.WLSWrapper(estimator=None, evaluate=True)[source]

Bases: RegressionWrapper

The description…

Methods:

as_summary(**kwargs)

This method creates a summary string.

evaluate([alpha])

This method set all the variables into this class.

fit(endog[, exog, trend, weights, W])

This method computes the WLS.

get_prediction([start, end, alpha])

This method predicts using the model.

line(x)

This method returns arrays to plot line and confidence intervals.

pred_int([start, end])

This method computes the prediction intervals

as_summary(**kwargs)[source]

This method creates a summary string.

evaluate(alpha=0.05)[source]

This method set all the variables into this class.

Notes:
  • if instead of having the attribute series it is desired to set each element of the series as an attribute, just used the following statement: setattr(self, name, value).

  • Note the difference between the resid and wresid since they will also have different statistical properties. Furthermore, there is a vector with normalized residuals resid_pearson.

@see: statsmodels.WLS @see: pyAMR.core.regression.RegressionResultsWrapper

Parameters:

alpha (the confidence interval)

Returns:

dictionary

Return type:

map with all the parameters.

fit(endog, exog=None, trend='n', weights=None, W=None, **kwargs)[source]

This method computes the WLS.

@see statsmodels.regression.linear_model.WLS

Parameters:
  • endog (array-like) – The endogenous variable (aka. time series data)

  • exog (array-like) – The exogenous variable (by default is the time t starting in 0)

  • trend (str-like, options = {c, n}) – Wether to add a constant or not.

  • weights (array-like (optional)) – The weights for the weighted least square regression. If weights and W are both not None, the W instance will be used to transform the weights variables.

  • W (object-like (optional)) – The instance to transform the weights. It must implement the function ‘weights’.

  • kwargs (dict-like) – The rest of the arguments to pass to the __init__ and fit methods of the class statsmodels.WLS (see xxx)

Returns:

object

Return type:

OLSWrapper object.

get_prediction(start=None, end=None, alpha=0.05, **kwargs)[source]

This method predicts using the model.

Todo

Note that wls_predict_std has weights (those used in WLS) as an input parameter. However, these are not passed to the function. Those weights are available for insample predictions but not for forecasting.

Parameters:
  • start (int (optional)) – The time t to start the prediction

  • end (int (optional)) – The time t to end the prediction

  • kwargs – The arguments to pass to the method get_prediction of the class statsmodels.WLS (see xxx)

Return type:

time, prediction mean, prediction confidence interval

line(x)[source]

This method returns arrays to plot line and confidence intervals.

pred_int(start=None, end=None, **kwargs)[source]

This method computes the prediction intervals

Parameters:
  • start (int (optional)) – The time t to start the prediction

  • end (int (optional)) – The time t to end the prediction

Return type:

the standard prediction error and the prediction intervals

pyamr.core.regression.wlsarma module

Classes:

WLSARMAWrapper(**kwargs)

class pyamr.core.regression.wlsarma.WLSARMAWrapper(**kwargs)[source]

Bases: BaseRegressionWrapper

Methods:

as_summary(**kwargs)

This method displays the summary.

fit(endog[, exog, weights, wls_kwargs, ...])

This method...

get_prediction([start, end, ptype])

This method computes the prediction.

as_summary(**kwargs)[source]

This method displays the summary.

fit(endog, exog=None, weights=None, wls_kwargs={}, arima_kwargs=None, sarimax_kwargs=None, **kwargs)[source]

This method…

Parameters:
  • endog

  • exog

  • wls_kwargs

  • arima_kwargs

  • sarimax_kwargs

  • kwargs

get_prediction(start=None, end=None, ptype='combined', **kwargs)[source]

This method computes the prediction.

Note: Correct for ARIMA. The arima is trained with n samples (e.g. 24) and then we ask for start=50 and end=74 hance for some reason arima becomes crazy and return a larger array. Therefore, we predict for indices None (begining) till the maxium number to predict which is fshape-start.

Parameters:
  • start (start of prediction (as in wls))

  • end (end of preediction (as in wls))

  • type (string (combined, wls, arma))

pyamr.core.regression.wreg module

Classes:

RegressionWrapper([estimator, evaluate])

Description...

class pyamr.core.regression.wreg.RegressionWrapper(estimator=None, evaluate=True)[source]

Bases: BaseWrapper

Description…

Methods:

conf_int_insample(predictions[, alpha])

Computes the in-sample confidence interval.

fit(**kwargs)

This method performs the fit.

resid()

Get the residual

conf_int_insample(predictions, alpha=0.05)[source]

Computes the in-sample confidence interval.

Parameters:
  • predictions (array-like) – The predictions done by the model.

  • alpha (int) – The alpha to configure the confidence interval.

Return type:

array-like

fit(**kwargs)[source]

This method performs the fit.

Parameters:

kwargs (dict-like) – The arguments that will be passed to the method.

resid()[source]

Get the residual

pyamr.core.regression.wregression module

Classes:

BaseRegressionWrapper(**kwargs)

class pyamr.core.regression.wregression.BaseRegressionWrapper(**kwargs)[source]

Bases: BaseWrapper

Methods:

conf_int_insample(forecast[, resid, alpha])

This function computes a basic confidence interval.

conf_int_insample(forecast, resid=None, alpha=0.05)[source]

This function computes a basic confidence interval.

Note: It might not be the adecuate way of computing it.

Parameters:
  • forecast (the forecasted values.)

  • alpha (the alpha value selected.)

Returns:

  • cilo

  • ciup

Module contents