U
    Kvf                     @  s*  d dl mZ d dlmZmZmZ d dlZd dlZd dlZ	d dl
mZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlm  mZ d dlm  mZ d dlm Z  dZ!d	Z"ej#Z$ej%Z%d
d Z&dd Z'dddddZ(G dd dej)Z*G dd dej+Z,G dd dej-Z.e/e.e, dS )    )annotations)is_float_indexis_int_indexis_numeric_dtypeN)
DatetimeIndexIndexPeriodPeriodIndex
RangeIndexSeries	Timestamp
date_rangeperiod_rangeto_datetime)	to_offset)
PandasData)ValueWarninga  
    %(model)s

    Parameters
    ----------
    %(params)s
    dates : array_like, optional
        An array-like object of datetime objects. If a pandas object is given
        for endog or exog, it is assumed to have a DateIndex.
    freq : str, optional
        The frequency of the time-series. A Pandas offset or 'B', 'D', 'W',
        'M', 'A', or 'Q'. This is optional if dates are given.
    %(extra_params)s
    %(extra_sections)szTimeseries model base classc              
   C  s  |}|}t |ttf}t|}t |t}t|}t|}|rt | ttj	fr| dk rh|  |krh||  } n\| |d krz|j
}|j}	W n  tk
r   |j}|j}	Y nX || d |	  }
t||
|	d}|s2|r2|s2t | ttj	fr2| dk r|  |kr||  } n*| |d kr2tt|d t| d }|r:|tkrHt}nt}t | ttj	fr| dk r|  |k r|||   } n@| t|d kr||d t| d |jd}|d } n||  } nv|tkrt| |jd}nt| }||d kr:||d ||jd}|d |ks2||d t|d |jd}|d } |rL|| }n^|sX|rz||   W n4 ttfk
r } ztt|W 5 d}~X Y nX | }n
|| }||k	}t |tr|jd }n|}||d|d  |fS )	a  
    Get the location of a specific key in an index

    Parameters
    ----------
    key : label
        The key for which to find the location if the underlying index is
        a DateIndex or a location if the underlying index is a RangeIndex
        or an Index with an integer dtype.
    index : pd.Index
        The index to search.

    Returns
    -------
    loc : int
        The location of the key
    index : pd.Index
        The index including the key; this is a copy of the original index
        unless the index had to be expanded to accommodate `key`.
    index_was_expanded : bool
        Whether or not the index was expanded to accommodate `key`.

    Notes
    -----
    If `key` is past the end of of the given index, and the index is either
    an Index with an integral dtype or a date index, this function extends
    the index up to and including key, and then returns the location in the
    new index.
    r      )startstopstep)r   Zperiodsfreq)r   r   endr   N)
isinstancer	   r   r   r
   typelenintnpintegerr   r   AttributeError_startZ_stepr   Zaranger   r   r   r   r   get_loc
IndexError
ValueErrorKeyErrorstrslicer   )keyindex
base_index
date_index	int_indexrange_indexZindex_classnobsZbase_index_startZbase_index_stepr   Zindex_fnZdate_keyloceindex_was_expandedr    r3   B/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/tsa/base/tsa_model.pyget_index_loc5   s    


  





  


r5   c              
   C  s   zt | |\}}}W n tk
r } zz~t| ttjfsD|| }n t|trV|j}t|tj	r|j
tkrxt|}n|d }t|tjs |d|d  }d}W n   |Y nX W 5 d}~X Y nX |||fS )a  
    Get the location of a specific key in an index or model row labels

    Parameters
    ----------
    key : label
        The key for which to find the location if the underlying index is
        a DateIndex or is only being used as row labels, or a location if
        the underlying index is a RangeIndex or a NumericIndex.
    index : pd.Index
        The index to search.
    row_labels : pd.Index
        Row labels to search if key not found in index

    Returns
    -------
    loc : int
        The location of the key
    index : pd.Index
        The index including the key; this is a copy of the original index
        unless the index had to be expanded to accommodate `key`.
    index_was_expanded : bool
        Whether or not the index was expanded to accommodate `key`.

    Notes
    -----
    This function expands on `get_index_loc` by first trying the given
    base index (or the model's index if the base index was not given) and
    then falling back to try again with the model row labels as the base
    index.
    r   Nr   F)r5   r&   r   r   r   r    r#   r(   r   ZndarrayZdtypeboolZargmaxnumbersIntegral)r)   r*   
row_labelsr0   r2   r1   r3   r3   r4   get_index_label_loc   s(     

r:   F"tuple[int, int, int, Index | None]returnc	                 C  s  zt | ||j\} }	}
W n tk
r4   tdY nX |dkrPt| t|d }zt |||j\}}}W n tk
r   tdY nX t| tr| j} t|tr|jd }|| d }|| k rt	d|dk	rt|t|kst	dt|t
s
|s
tjdtdd	 t|}nj|rt|st|jdk	rL|
sL|sL|j| |d  }n&|sbtjd
tdd	 tjdtdd	 n
|r~d}|dk	r|d |_|d |_||_nd|_d|_d|_t||d  d}||8 }| |||fS )a)
  
    Get the location of a specific key in an index or model row labels

    Parameters
    ----------
    start : label
        The key at which to start prediction. Depending on the underlying
        model's index, may be an integer, a date (string, datetime object,
        pd.Timestamp, or pd.Period object), or some other object in the
        model's row labels.
    end : label
        The key at which to end prediction (note that this key will be
        *included* in prediction). Depending on the underlying
        model's index, may be an integer, a date (string, datetime object,
        pd.Timestamp, or pd.Period object), or some other object in the
        model's row labels.
    nobs : int
    base_index : pd.Index

    index : pd.Index, optional
        Optionally an index to associate the predicted results to. If None,
        an attempt is made to create an index for the predicted results
        from the model's index or model's row labels.
    silent : bool, optional
        Argument to silence warnings.

    Returns
    -------
    start : int
        The index / observation location at which to begin prediction.
    end : int
        The index / observation location at which to end in-sample
        prediction. The maximum value for this is nobs-1.
    out_of_sample : int
        The number of observations to forecast after the end of the sample.
    prediction_index : pd.Index or None
        The index associated with the prediction results. This index covers
        the range [start, end + out_of_sample]. If the model has no given
        index and no given row labels (i.e. endog/exog is not Pandas), then
        this will be None.

    Notes
    -----
    The arguments `start` and `end` behave differently, depending on if
    they are integer or not. If either is an integer, then it is assumed
    to refer to a *location* in the index, not to an index value. On the
    other hand, if it is a date string or some other type of object, then
    it is assumed to refer to an index *value*. In all cases, the returned
    `start` and `end` values refer to index *locations* (so in the former
    case, the given location is validated and returned whereas in the
    latter case a location is found that corresponds to the given index
    value).

    This difference in behavior is necessary to support `RangeIndex`. This
    is because integers for a RangeIndex could refer either to index values
    or to index locations in an ambiguous way (while for `NumericIndex`,
    since we have required them to be full indexes, there is no ambiguity).
    zYThe `start` argument could not be matched to a location related to the index of the data.Nr   zWThe `end` argument could not be matched to a location related to the index of the data.z)Prediction must have `end` after `start`.zeInvalid `index` provided in prediction. Must have length consistent with `start` and `end` arguments.zBecause the model data (`endog`, `exog`) were not given as Pandas objects, the prediction output will be Numpy arrays, and the given `index` argument will only be used internally.   
stacklevelzmNo supported index is available. Prediction results will be given with an integer index beginning at `start`.zNo supported index is available. In the next version, calling this method in a model without a supported index will result in an exception.r   r   )r:   r9   r&   maxr   r   r(   r   r   r%   r   warningswarnr   r   FutureWarningZpredict_startZpredict_endZpredict_dates)r   r   r/   r+   r*   silent
index_noneindex_generateddata_Z	start_oosZ	end_indexZend_oosZprediction_indexZout_of_sampler3   r3   r4   get_prediction_index  s    H  
  




	



rJ   c                      s|   e Zd Zeeeedd Zd fdd	ZdddZ	dd	d
Z
dddZdddddZdd Zdd ZeeeddZ  ZS )TimeSeriesModel )modelparamsZextra_paramsZextra_sectionsNnonec                   s*   t  j||fd|i| | || d S )Nmissing)super__init___init_dates)selfendogZexogdatesr   rP   kwargs	__class__r3   r4   rR     s    zTimeSeriesModel.__init__c                 C  s  |dk	r|}n| j j}|dkr.|dk	r.tdd}|dk	rt|ttfszbt|}t|stt	|stt|d t
r|tdt|tr|j}t|}t|tstd|}W n   |dk	rtdY nX t|ttfr|dkr"|jdkr"|j}|dk	r"d}|dk	r"tjd	| td
d |dk	r4t|}|dkr^|jdkr^|dk	rtdnp|dk	r|jdkrt|d |d |d}|s||std|}n$|dk	r|s|j|kstdn|dk	rtd|dk	}t|ttf}t|t}	t|}
t|t}|r$|jdk	nd}tt| jjd }|
rL||nd}|rz
|j}W n tk
r|   |j}Y nX nd}|r|s|s|stjdtd
d |r|stjdtd
d |r|stjdtd
d d}|r|r|p|
r|p|}|r|}n|}d}|| _|| _ |dk| _!|
oF| oF| | _"|oT| | _#| j#rh| jjnd| _$|| _%| j#r| jnd| j _&| j#r| jj'nd| j _dS )a  
        Initialize dates

        Parameters
        ----------
        dates : array_like, optional
            An array like object containing dates.
        freq : str, tuple, datetime.timedelta, DateOffset or None, optional
            A frequency specification for either `dates` or the row labels from
            the endog / exog data.

        Notes
        -----
        Creates `self._index` and related attributes. `self._index` is always
        a Pandas index, and it is always NumericIndex, DatetimeIndex, or
        PeriodIndex.

        If Pandas objects, endog / exog may have any type of index. If it is
        an NumericIndex with values 0, 1, ..., nobs-1 or if it is (coerceable to)
        a DatetimeIndex or PeriodIndex *with an associated frequency*, then it
        is called a "supported" index. Otherwise it is called an "unsupported"
        index.

        Supported indexes are standardized (i.e. a list of date strings is
        converted to a DatetimeIndex) and the result is put in `self._index`.

        Unsupported indexes are ignored, and a supported NumericIndex is
        generated and put in `self._index`. Warnings are issued in this case
        to alert the user if the returned index from some operation (e.g.
        forecasting) is different from the original data's index. However,
        whenever possible (e.g. purely in-sample prediction), the original
        index is returned.

        The benefit of supported indexes is that they allow *forecasting*, i.e.
        it is possible to extend them in a reasonable way. Thus every model
        must have an underlying supported index, even if it is just a generated
        NumericIndex.
        Nz,Frequency provided without associated index.Fr   zNumeric index givenzCould not coerce to date indexz2Non-date index index provided to `dates` argument.TzMNo frequency information was provided, so inferred frequency %s will be used.r>   r?   zYNo frequency information was provided with date index and no frequency could be inferred.r   r   zEThe given frequency argument could not be matched to the given index.zBThe given frequency argument is incompatible with the given index.zKGiven index could not be coerced to dates but `freq` argument was provided.zLAn unsupported index was provided and will be ignored when e.g. forecasting.z|A date index has been provided, but it has no associated frequency information and so will be ignored when e.g. forecasting.zeA date index has been provided, but it is not monotonic and so will be ignored when e.g. forecasting.)(rH   r9   r%   r   r   r	   r   Zasarrayr   r   floatr   valuesr   r   r   inferred_freqrB   rC   r   r   r   equalsr   r
   rangerU   shapeZis_monotonic_increasingr!   is_monotonic_index_index_generated_index_noneZ_index_int64Z_index_datesZ_index_freqZ_index_inferred_freqrV   Zfreqstr)rT   rV   r   r*   r\   ra   Zresampled_indexZ	has_indexr,   Zperiod_indexr-   r.   Zhas_freq	incrementZis_incrementr`   rG   Zvalid_indexr3   r3   r4   rS     s    *







	

  



	
zTimeSeriesModel._init_datesc                 C  s   |dkr| j }t||S )a9  
        Get the location of a specific key in an index

        Parameters
        ----------
        key : label
            The key for which to find the location if the underlying index is
            a DateIndex or a location if the underlying index is a RangeIndex
            or an NumericIndex.
        base_index : pd.Index, optional
            Optionally the base index to search. If None, the model's index is
            searched.

        Returns
        -------
        loc : int
            The location of the key
        index : pd.Index
            The index including the key; this is a copy of the original index
            unless the index had to be expanded to accommodate `key`.
        index_was_expanded : bool
            Whether or not the index was expanded to accommodate `key`.

        Notes
        -----
        If `key` is past the end of of the given index, and the index is either
        an NumericIndex or a date index, this function extends the index up to
        and including key, and then returns the location in the new index.
        N)ra   r5   rT   r)   r+   r3   r3   r4   _get_index_loc  s    zTimeSeriesModel._get_index_locc                 C  s   |dkr| j }t||| jjS )a|  
        Get the location of a specific key in an index or model row labels

        Parameters
        ----------
        key : label
            The key for which to find the location if the underlying index is
            a DateIndex or is only being used as row labels, or a location if
            the underlying index is a RangeIndex or an NumericIndex.
        base_index : pd.Index, optional
            Optionally the base index to search. If None, the model's index is
            searched.

        Returns
        -------
        loc : int
            The location of the key
        index : pd.Index
            The index including the key; this is a copy of the original index
            unless the index had to be expanded to accommodate `key`.
        index_was_expanded : bool
            Whether or not the index was expanded to accommodate `key`.

        Notes
        -----
        This method expands on `_get_index_loc` by first trying the given
        base index (or the model's index if the base index was not given) and
        then falling back to try again with the model row labels as the base
        index.
        N)ra   r:   rH   r9   re   r3   r3   r4   _get_index_label_loc  s    z$TimeSeriesModel._get_index_label_locFr;   r<   c                 C  s,   t | j}t|||| j||| j| j| jd	S )a
  
        Get the location of a specific key in an index or model row labels

        Parameters
        ----------
        start : label
            The key at which to start prediction. Depending on the underlying
            model's index, may be an integer, a date (string, datetime object,
            pd.Timestamp, or pd.Period object), or some other object in the
            model's row labels.
        end : label
            The key at which to end prediction (note that this key will be
            *included* in prediction). Depending on the underlying
            model's index, may be an integer, a date (string, datetime object,
            pd.Timestamp, or pd.Period object), or some other object in the
            model's row labels.
        index : pd.Index, optional
            Optionally an index to associate the predicted results to. If None,
            an attempt is made to create an index for the predicted results
            from the model's index or model's row labels.
        silent : bool, optional
            Argument to silence warnings.

        Returns
        -------
        start : int
            The index / observation location at which to begin prediction.
        end : int
            The index / observation location at which to end in-sample
            prediction. The maximum value for this is nobs-1.
        out_of_sample : int
            The number of observations to forecast after the end of the sample.
        prediction_index : pd.Index or None
            The index associated with the prediction results. This index covers
            the range [start, end + out_of_sample]. If the model has no given
            index and no given row labels (i.e. endog/exog is not Pandas), then
            this will be None.

        Notes
        -----
        The arguments `start` and `end` behave differently, depending on if
        they are integer or not. If either is an integer, then it is assumed
        to refer to a *location* in the index, not to an index value. On the
        other hand, if it is a date string or some other type of object, then
        it is assumed to refer to an index *value*. In all cases, the returned
        `start` and `end` values refer to index *locations* (so in the former
        case, the given location is validated and returned whereas in the
        latter case a location is found that corresponds to the given index
        value).

        This difference in behavior is necessary to support `RangeIndex`. This
        is because integers for a RangeIndex could refer either to index values
        or to index locations in an ambiguous way (while for `NumericIndex`,
        since we have required them to be full indexes, there is no ambiguity).
        )r+   r*   rE   rF   rG   rH   )r   rU   rJ   ra   rc   rb   rH   )rT   r   r   r*   rE   r/   r3   r3   r4   _get_prediction_index  s    8
z%TimeSeriesModel._get_prediction_indexc                 C  s   | j jS N)rH   xnames)rT   r3   r3   r4   _get_exog_namesP  s    zTimeSeriesModel._get_exog_namesc                 C  s   t |ts|g}|| j_d S ri   )r   listrH   rj   )rT   valsr3   r3   r4   _set_exog_namesS  s    
zTimeSeriesModel._set_exog_namesz%The names of the exogenous variables.)NNNrO   )NN)N)N)NF)__name__
__module____qualname___tsa_doc
_model_doc_generic_params_missing_param_doc__doc__rR   rS   rf   rg   rh   rk   rn   propertyZ
exog_names__classcell__r3   r3   rX   r4   rK     s0          
 k
#
#ErK   c                      s   e Zd Zd fdd	Z  ZS )TimeSeriesModelResults      ?c                   s   |j | _ t |||| d S ri   )rH   rQ   rR   )rT   rM   rN   Znormalized_cov_paramsZscalerX   r3   r4   rR   c  s    zTimeSeriesModelResults.__init__)rz   )ro   rp   rq   rR   rx   r3   r3   rX   r4   ry   b  s   ry   c                   @  s8   e Zd Zi ZeejjeZddiZ	eejj
e	Z
dS )TimeSeriesResultsWrapperZpredictrV   N)ro   rp   rq   _attrswrapZunion_dictsbaseZLikelihoodResultsWrapperZ_wrap_attrsZ_methodsZ_wrap_methodsr3   r3   r3   r4   r{   h  s     r{   )NFFNN)0
__future__r   Zstatsmodels.compat.pandasr   r   r   r7   rB   Znumpyr   Zpandasr   r   r   r	   r
   r   r   r   r   r   Zpandas.tseries.frequenciesr   Zstatsmodels.base.datar   Zstatsmodels.base.modelr~   rM   Zstatsmodels.base.wrapperwrapperr}   Zstatsmodels.tools.sm_exceptionsr   rr   rs   Z_model_params_docrt   ru   r5   r:   rJ   ZLikelihoodModelrK   ZLikelihoodModelResultsry   ZResultsWrapperr{   Zpopulate_wrapperr3   r3   r3   r4   <module>   sD   0  N      /    