How to use the statsmodels.tools.validation.array_like function in statsmodels

To help you get started, we’ve selected a few statsmodels examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github statsmodels / statsmodels / statsmodels / tsa / ar_model.py View on Github external
def _resid(self, params):
        params = array_like(params, 'params', ndim=2)
        resid = self._y - self._x @ params
        return resid.squeeze()
github statsmodels / statsmodels / statsmodels / tsa / arima_model.py View on Github external
end : int
        Index of final in-sample observation
    """
    if exog is None:
        return None

    exog_start = 0
    exog_index = getattr(exog, 'index', None)
    exog_dates = isinstance(exog_index, pd.DatetimeIndex)
    endog_dates = isinstance(row_labels, pd.DatetimeIndex)
    date_adj = endog_dates and exog_dates and not dynamic
    if date_adj and row_labels.isin(exog_index).all():
        end_label = row_labels[end]
        exog_start = exog.index.get_loc(end_label) + 1

    exog = array_like(exog, 'exog', ndim=2)
    return exog[exog_start:]
github statsmodels / statsmodels / statsmodels / tsa / statespace / exponential_smoothing.py View on Github external
es_init._initialization_simple(
                    self.endog[:, 0], trend='add' if self.trend else None,
                    seasonal='add' if self.seasonal else None,
                    seasonal_periods=self.seasonal_periods))
        elif self.initialization_method == 'heuristic':
            initial_level, initial_trend, initial_seasonal = (
                es_init._initialization_heuristic(
                    self.endog[:, 0], trend='add' if self.trend else None,
                    seasonal='add' if self.seasonal else None,
                    seasonal_periods=self.seasonal_periods))
        elif self.initialization_method == 'known':
            initial_level = float_like(initial_level, 'initial_level')
            if self.trend:
                initial_trend = float_like(initial_trend, 'initial_trend')
            if self.seasonal:
                initial_seasonal = array_like(initial_seasonal,
                                              'initial_seasonal')

                if len(initial_seasonal) == self.seasonal_periods - 1:
                    initial_seasonal = np.r_[initial_seasonal,
                                             0 - np.sum(initial_seasonal)]

                if len(initial_seasonal) != self.seasonal_periods:
                    raise ValueError(
                        'Invalid length of initial seasonal values. Must be'
                        ' one of s or s-1, where s is the number of seasonal'
                        ' periods.')

        self._initial_level = initial_level
        self._initial_trend = initial_trend
        self._initial_seasonal = initial_seasonal
        self._initial_state = None
github statsmodels / statsmodels / statsmodels / tsa / arima_model.py View on Github external
def predict(self, params, start=None, end=None, exog=None, dynamic=False,
                **kwargs):
        if kwargs and 'typ' not in kwargs:
            raise TypeError('Unknown extra arguments')
        if not (hasattr(self, 'k_ar') and hasattr(self, 'k_trend')):
            raise RuntimeError('Model must be fit before calling predict')
        params = array_like(params, 'params')
        method = getattr(self, 'method', 'mle')  # do not assume fit
        # will return an index of a date
        start, end, out_of_sample, _ = (
            self._get_prediction_index(start, end, dynamic))

        if out_of_sample and (exog is None and self.k_exog > 0):
            raise ValueError("You must provide exog for ARMAX")

        endog = self.endog
        resid = self.geterrors(params)
        k_ar = self.k_ar

        # Adjust exog if exog has dates that align with endog
        row_labels = self.data.row_labels
        exog = _prediction_adjust_exog(exog, row_labels, dynamic, end)
        if out_of_sample != 0 and self.k_exog > 0:
github statsmodels / statsmodels / statsmodels / tsa / stattools.py View on Github external
.. [2] Hamilton, J.D.  "Time Series Analysis".  Princeton, 1994.

    .. [3] MacKinnon, J.G. 1994.  "Approximate asymptotic distribution functions for
        unit-root and cointegration tests.  `Journal of Business and Economic
        Statistics` 12, 167-76.

    .. [4] MacKinnon, J.G. 2010. "Critical Values for Cointegration Tests."  Queen's
        University, Dept of Economics, Working Papers.  Available at
        http://ideas.repec.org/p/qed/wpaper/1227.html

    Examples
    --------
    See example notebook
    """
    x = array_like(x, 'x')
    maxlag = int_like(maxlag, 'maxlag', optional=True)
    regression = string_like(regression, 'regression',
                             options=('c', 'ct', 'ctt', 'nc'))
    autolag = string_like(autolag, 'autolag', optional=True,
                          options=('aic', 'bic', 't-stat'))
    store = bool_like(store, 'store')
    regresults = bool_like(regresults, 'regresults')

    if regresults:
        store = True

    trenddict = {None: 'nc', 0: 'c', 1: 'ct', 2: 'ctt'}
    if regression is None or isinstance(regression, int):
        regression = trenddict[regression]
    regression = regression.lower()
    nobs = x.shape[0]
github statsmodels / statsmodels / statsmodels / tsa / stattools.py View on Github external
References
        ----------
        .. [1] Baum, C.F. (2004). ZANDREWS: Stata module to calculate
           Zivot-Andrews unit root test in presence of structural break,"
           Statistical Software Components S437301, Boston College Department
           of Economics, revised 2015.

        .. [2] Schwert, G.W. (1989). Tests for unit roots: A Monte Carlo
           investigation. Journal of Business & Economic Statistics, 7:
           147-159.

        .. [3] Zivot, E., and Andrews, D.W.K. (1992). Further evidence on the
           great crash, the oil-price shock, and the unit-root hypothesis.
           Journal of Business & Economic Studies, 10: 251-270.
        """
        x = array_like(x, 'x')
        trim = float_like(trim, 'trim')
        maxlag = int_like(maxlag, 'maxlag', optional=True)
        regression = string_like(regression, 'regression',
                                 options=('c', 't', 'ct'))
        autolag = string_like(autolag, 'autolag',
                              options=('AIC', 'BIC', 't-stat'), optional=True)
        if trim < 0 or trim > (1. / 3.):
            raise ValueError('trim value must be a float in range [0, 1/3)')
        nobs = x.shape[0]
        if autolag:
            adf_res = adfuller(x, maxlag=maxlag, regression='ct',
                               autolag=autolag)
            baselags = adf_res[2]
        elif maxlag:
            baselags = maxlag
        else:
github statsmodels / statsmodels / statsmodels / tsa / ar_model.py View on Github external
See Also
        --------
        statsmodels.base.model.LikelihoodModel.fit
            Base fit class with further details about options.

        Notes
        -----
        The parameters after `trend` are only used when method is 'mle'.

        References
        ----------
        .. [*] Jones, R.H. 1980 "Maximum likelihood fitting of ARMA models to
           time series with missing observations."  `Technometrics`.  22.3.
           389-95.
        """
        start_params = array_like(start_params, 'start_params', ndim=1,
                                  optional=True)
        method = method.lower()
        if method not in ['cmle', 'mle']:
            raise ValueError("Method %s not recognized" % method)
        self.method = method
        self.trend = trend
        self.transparams = transparams
        nobs = len(self.endog)  # overwritten if method is 'cmle'
        endog = self.endog
        # The parameters are no longer allowed to change in an instance
        fit_params = (maxlag, method, ic, trend)
        if self._fit_params is not None and self._fit_params != fit_params:
            raise RuntimeError(REPEATED_FIT_ERROR.format(*self._fit_params))
        if maxlag is None:
            maxlag = int(round(12 * (nobs / 100.) ** (1 / 4.)))
        k_ar = maxlag  # stays this if ic is None
github statsmodels / statsmodels / statsmodels / tsa / _bds.py View on Github external
epsilon : scalar, optional
        the threshold distance to use in calculating the heaviside indicators
    distance : scalar, optional
        if epsilon is omitted, specifies the distance multiplier to use when
        computing it

    Returns
    -------
    indicators : 2d array
        matrix of distance threshold indicators

    Notes
    -----
    Since this can be a very large matrix, use np.int8 to save some space.
    """
    x = array_like(x, 'x')

    if epsilon is not None and epsilon <= 0:
        raise ValueError("Threshold distance must be positive if specified."
                         " Got epsilon of %f" % epsilon)
    if distance <= 0:
        raise ValueError("Threshold distance must be positive."
                         " Got distance multiplier %f" % distance)

    # TODO: add functionality to select epsilon optimally
    # TODO: and/or compute for a range of epsilons in [0.5*s, 2.0*s]?
    #      or [1.5*s, 2.0*s]?
    if epsilon is None:
        epsilon = distance * x.std(ddof=1)

    return np.abs(x[:, None] - x) < epsilon
github statsmodels / statsmodels / statsmodels / tsa / stattools.py View on Github external
If True, then denominators for autocovariance is n-k, otherwise n.
    demean : bool, optional
        Flag indicating whether to demean x and y.

    Returns
    -------
    ndarray
        The estimated crosscovariance function.

    Notes
    -----
    This uses np.correlate which does full convolution. For very long time
    series it is recommended to use fft convolution instead.
    """
    x = array_like(x, 'x')
    y = array_like(y, 'y')
    unbiased = bool_like(unbiased, 'unbiased')
    demean = bool_like(demean, 'demean')

    n = len(x)
    if demean:
        xo = x - x.mean()
        yo = y - y.mean()
    else:
        xo = x
        yo = y
    if unbiased:
        xi = np.ones(n)
        d = np.correlate(xi, xi, 'full')
    else:
        d = n
    return (np.correlate(xo, yo, 'full') / d)[n - 1:]
github statsmodels / statsmodels / statsmodels / tsa / arima_model.py View on Github external
the constant. The number of observation in exog must match the
            value of steps.
        alpha : float
            The confidence intervals for the forecasts are (1 - alpha) %

        Returns
        -------
        forecast : array
            Array of out of sample forecasts
        stderr : array
            Array of the standard error of the forecasts.
        conf_int : array
            2d array of the confidence interval for the forecast
        """
        if exog is not None:
            exog = array_like(exog, 'exog', maxdim=2)
            if self.k_exog == 1 and exog.ndim == 1:
                exog = exog[:, None]
            elif exog.ndim == 1:
                if len(exog) != self.k_exog:
                    raise ValueError("1d exog given and len(exog) != k_exog")
                exog = exog[None, :]
            if exog.shape[0] != steps:
                raise ValueError("new exog needed for each step")
            if self.k_exog != exog.shape[1]:
                raise ValueError('exog must contain the same number of '
                                 'variables as in the estimated model.')
            # prepend in-sample exog observations
            if self.k_ar > 0:
                exog = np.vstack((self.model.exog[-self.k_ar:, self.k_trend:],
                                  exog))
        else: