
    M/Ph                     4   d dl mZmZmZmZ d dlmZmZ d dlZ	d dl
mZmZ d dlmZmZ d dlZd dlZd dlmZ d dlmZ d dlmZmZmZmZ d d	lmZ ee	j        ej        ej         f         Z!ee"ej#        f         Z$d
Z% G d de          Z& G d de&e          Z' G d de'          Z( G d de&          Z) G d de&e          Z* G d de*          Z+ G d de&e          Z, G d de,e*          Z- G d de,          Z. G d de,e'          Z/ G d d           Z0dS )!    )PD_LT_2_2_0Appenderis_int_indexto_numpy)ABCabstractmethodN)OptionalUnion)HashableSequence)qr)d_or_f)	bool_like
float_likerequired_int_likestring_like)freq_to_periodzstart is less than the first observation in the index. Values can only be created for observations after the start of the index.
c            
          e Zd ZdZdZedefd            Zede	e
         dej        fd            Ze	 ddede	e
         d	ee	e
                  dej        fd
            Zedefd            ZdefdZeedee
df         fd                        Zede	e
         dej        fd            Ze	 ddej        ded	ee	e
                  dej        fd            ZdefdZdedefdZdS )DeterministicTermz/Abstract Base Class for all Deterministic TermsFreturnc                     | j         S )z?Flag indicating whether the values produced are dummy variables)	_is_dummyselfs    ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/deterministic.pyis_dummyzDeterministicTerm.is_dummy*        ~    indexc                     dS )aR  
        Produce deterministic trends for in-sample fitting.

        Parameters
        ----------
        index : index_like
            An index-like object. If not an index, it is converted to an
            index.

        Returns
        -------
        DataFrame
            A DataFrame containing the deterministic terms.
        N r   r   s     r   	in_samplezDeterministicTerm.in_sample/         r   Nstepsforecast_indexc                     dS )a1  
        Produce deterministic trends for out-of-sample forecasts

        Parameters
        ----------
        steps : int
            The number of steps to forecast
        index : index_like
            An index-like object. If not an index, it is converted to an
            index.
        forecast_index : index_like
            An Index or index-like object to use for the forecasts. If
            provided must have steps elements.

        Returns
        -------
        DataFrame
            A DataFrame containing the deterministic terms.
        Nr!   )r   r%   r   r&   s       r   out_of_samplezDeterministicTerm.out_of_sample@   r$   r   c                     dS )z.A meaningful string representation of the termNr!   r   s    r   __str__zDeterministicTerm.__str__[   r$   r   c                 Z    t          |           j        f}t          || j        z             S N)type__name__hash_eq_attr)r   names     r   __hash__zDeterministicTerm.__hash___   s(    &*4jj&9%;D4=()))r   .c                     dS )z9tuple of attributes that are used for equality comparisonNr!   r   s    r   r0   zDeterministicTerm._eq_attrc   r$   r   c                     t          | t          j                  r| S 	 t          j        |           S # t          $ r t	          d          w xY w)Nz*index must be a pandas Index or index-like)
isinstancepdIndex	Exception	TypeErrorr   s    r   _index_likezDeterministicTerm._index_likeh   s]    eRX&& 	L	J8E??" 	J 	J 	JHIII	Js	   2 Ac                    |jt                               |          }t          |t          j                  sJ |j        d         |k    r!t          d|j        d          d| d          |S t          | t          j                  r%t          j        | d         dz   || j	                  S t          | t          j
                  rK| j	        Dt          j        | d         | j	        d	
          d         }t          j        || j	        |
          S t          | t          j                  rt          | t          j                  sJ 	 | j        }| j        }nA# t          $ r4 t!          |           dk    r| d         | d         z
  nd}| d         |z   }Y nw xY w|||z  z   }t          j        |||          S t#          |           rht%          j        t%          j        |           dk              r>t%          j        | d         dz   | d         |z   dz             }t          j        |          S ddl}|                    dt0          d	           | j        d         }	t          j        |	dz   |	|z   dz             S )zExtend the forecast indexNr   z(The number of values in forecast_index (z) must match steps (z).   periodsfreq   rA   r@   stepzOnly PeriodIndexes, DatetimeIndexes with a frequency set, RangesIndexes, and Index with a unit increment support extending. The index is set will contain the position relative to the data length.)
stacklevel)r   r;   r5   r6   r7   shape
ValueErrorPeriodIndexperiod_rangerA   DatetimeIndex
date_range
RangeIndexrF   stopAttributeErrorlenr   npalldiffarangewarningswarnUserWarning)
r   r%   r&   next_obsrF   startrO   idx_arrrV   nobss
             r   _extend_indexzDeterministicTerm._extend_indexq   s    %.::>JJNnbh77777#A&%// O&,Q/O OEJO O O   "!eR^,, 	%?b	Au5:    r/00 	%UZ5K}U2YUZKKKANH=
EJJJJr}-- 	%eR]33333)z
! ) ) )03E

QuRy59,,Ab	D() 4%<'D=48888%   	%RVBGENNa,?%@%@ 	%ib	AuRy5/@1/DEEG8G$$$"  	 	
 	
 	
 {1~}TAXte|a'7888s   E ;FFc                 R    |                                  dt          |           dz   S )Nz at 0x0x)r*   idr   s    r   __repr__zDeterministicTerm.__repr__   s'    ||~~ 6D 6 6 666r   otherc                     t          |t          |                     rW| j        }|j        }t          |          t          |          k    rdS t	          d t          ||          D                       S dS )NFc                      g | ]\  }}||k    S r!   r!   ).0abs      r   
<listcomp>z,DeterministicTerm.__eq__.<locals>.<listcomp>   s     CCC41aQCCCr   )r5   r-   r0   rQ   rS   zip)r   rb   own_attroth_attrs       r   __eq__zDeterministicTerm.__eq__   sp    eT$ZZ(( 	}H~H8}}H--uCC3x+B+BCCCDDD5r   r,   )r.   
__module____qualname____doc__r   propertyboolr   r   r   r   r6   	DataFramer#   intr	   r(   strr*   r2   tupler0   staticmethodr7   r;   r]   ra   objectrl   r!   r   r   r   r   $   s2       99 I$    X x1 bl    ^  
 8<	  ! !(!34	
 
   ^4 = = = = ^=*# * * * * H%#. H H H ^ XH J8H- J"( J J J \J  8<09 09x0909 !(!3409 
	09 09 09 \09d7# 7 7 7 7F t      r   r   c                       e Zd ZdZddededdfdZedefd	            Zedefd
            Z	ede
e         fd            Zdej        dej        fdZdefdZdS )TimeTrendDeterministicTermz:Abstract Base Class for all Time Trend Deterministic TermsTr   constantorderr   Nc                 Z    t          |d          | _        t          |d          | _        d S )Nrz   r{   )r   	_constantr   _order)r   rz   r{   s      r   __init__z#TimeTrendDeterministicTerm.__init__   s(    "8Z88'w77r   c                     | j         S )z+Flag indicating that a constant is included)r}   r   s    r   rz   z#TimeTrendDeterministicTerm.constant   r   r   c                     | j         S )zOrder of the time trendr~   r   s    r   r{   z TimeTrendDeterministicTerm.order        {r   c                     g }dddd}| j         r|                    d           t          d| j        dz             D ]:}||v r|                    ||                    "|                    d|            ;|S )Ntrendtrend_squaredtrend_cubed)r>   rB      constr>   ztrend**)r}   appendranger~   )r   columnstrend_namespowers       r   _columnsz#TimeTrendDeterministicTerm._columns   s    !o-HH> 	$NN7###1dkAo.. 	2 	2E##{5122220001111r   locsc                    t          | j                  | j        z   }t          j        |d|f          }t          j        d|ft                     }t          j        d| j        dz             |dt          | j                  d f<   ||z  }|S )Nr>   dtyper   )rs   r}   r~   rR   tilezerosrU   )r   r   ntermstermsr   s        r   
_get_termsz%TimeTrendDeterministicTerm._get_terms   s    T^$$t{2q&k**!VC000*,)At{Q*G*GaT^$$&&&'%r   c                     g }| j         r|                    d           | j        r |                    d| j        dz               |sdg}d                    |          }d| dS )NConstantzPowers 1 to r>   Empty,z
TimeTrend())r}   r   r~   join)r   r   	terms_strs      r   r*   z"TimeTrendDeterministicTerm.__str__   s}    > 	%LL$$$; 	;LL9a99::: 	IEHHUOO	(I((((r   Tr   )r.   rm   rn   ro   rq   rs   r   rp   rz   r{   listrt   r   rR   ndarrayr   r*   r!   r   r   ry   ry      s       DD8 8 8S 8 8 8 8 8 $    X s    X 
$s) 
 
 
 X
rz bj    	) 	) 	) 	) 	) 	) 	)r   ry   c            
           e Zd ZdZddededdf fdZed	edd fd
            Z	 e
ej        j                  deee         ej        f         dej        fd            Z e
ej        j                  	 ddedeee         ej        f         deee                  dej        fd            Zedeedf         fd            Z xZS )	TimeTrendao  
    Constant and time trend determinstic terms

    Parameters
    ----------
    constant : bool
        Flag indicating whether a constant should be included.
    order : int
        A non-negative int containing the powers to include (1, 2, ..., order).

    See Also
    --------
    DeterministicProcess
    Seasonality
    Fourier
    CalendarTimeTrend

    Examples
    --------
    >>> from statsmodels.datasets import sunspots
    >>> from statsmodels.tsa.deterministic import TimeTrend
    >>> data = sunspots.load_pandas().data
    >>> trend_gen = TimeTrend(True, 3)
    >>> trend_gen.in_sample(data.index)
    Tr   rz   r{   r   Nc                 L    t                                          ||           d S r,   )superr   )r   rz   r{   	__class__s      r   r   zTimeTrend.__init__   s#    5)))))r   r   c                 d    |                     d          }d}d|v rd}nd|v rd} | ||          S )aY  
        Create a TimeTrend from a string description.

        Provided for compatibility with common string names.

        Parameters
        ----------
        trend : {"n", "c", "t", "ct", "ctt"}
            The string representation of the time trend. The terms are:

            * "n": No trend terms
            * "c": A constant only
            * "t": Linear time trend only
            * "ct": A constant and a time trend
            * "ctt": A constant, a time trend and a quadratic time trend

        Returns
        -------
        TimeTrend
            The TimeTrend instance.
        cr   ttrB   tr>   rz   r{   
startswith)clsr   rz   r{   s       r   from_stringzTimeTrend.from_string  sO    . ##C((5==EEE\\EsHE2222r   r   c                    |                      |          }|j        d         }t          j        d|dz   t          j                  d d d f         }|                     |          }t          j        || j        |          S Nr   r>   r   r   r   )	r;   rH   rR   rU   doubler   r6   rr   r   )r   r   r\   r   r   s        r   r#   zTimeTrend.in_sample!  su       ''{1~yD1HBI666qqq$w?%%|E4=FFFFr   r%   r&   c                 >   |                      |          }|j        d         }|                     |||          }t          j        |dz   ||z   dz   t          j                  d d d f         }|                     |          }t          j        || j	        |          S r   )
r;   rH   r]   rR   rU   r   r   r6   rr   r   )r   r%   r   r&   r\   fcast_indexr   r   s           r   r(   zTimeTrend.out_of_sample+  s       ''{1~((~FFy4%<!#329EEEaaagN%%|E4=LLLLr   .c                     | j         | j        fS r,   )r}   r~   r   s    r   r0   zTimeTrend._eq_attr9  s    ~t{**r   r   r,   )r.   rm   rn   ro   rq   rs   r   classmethodrt   r   r   r   r#   r
   r   r   r6   r7   rr   r(   r	   rp   ru   r0   __classcell__r   s   @r   r   r      s        4* * *S * * * * * * * 3 3 3 3 3 [3< X)122G8H-rx78G	G G G 32G X-566
 8<	M MM Xh'12M !(!34	M
 
M M M 76M +%#. + + + X+ + + + +r   r   c            
       &   e Zd ZdZdZddededdfdZedefd	            Zedefd
            Z	e
deee         ej        ej        f         dd fd            Zedeedf         fd            ZdefdZedee         fd            Z eej        j                  deee         ej        f         dej        fd            Z eej        j                  	 ddedeee         ej        f         deee                  dej        fd            ZdS )Seasonalitya   
    Seasonal dummy deterministic terms

    Parameters
    ----------
    period : int
        The length of a full cycle. Must be >= 2.
    initial_period : int
        The seasonal index of the first observation. 1-indexed so must
        be in {1, 2, ..., period}.

    See Also
    --------
    DeterministicProcess
    TimeTrend
    Fourier
    CalendarSeasonality

    Examples
    --------
    Solar data has an 11-year cycle

    >>> from statsmodels.datasets import sunspots
    >>> from statsmodels.tsa.deterministic import Seasonality
    >>> data = sunspots.load_pandas().data
    >>> seas_gen = Seasonality(11)
    >>> seas_gen.in_sample(data.index)

    To start at a season other than 1

    >>> seas_gen = Seasonality(11, initial_period=4)
    >>> seas_gen.in_sample(data.index)
    Tr>   periodinitial_periodr   Nc                     t          |d          | _        t          |d          | _        |dk     rt          d          d| j        cxk    r|k    sn t          d          d S )Nr   r   rB   zperiod must be >= 2r>   z-initial_period must be in {1, 2, ..., period})r   _period_initial_periodrI   )r   r   r   s      r   r   zSeasonality.__init__c  s~    (::0, 
  
 A::2333D(2222F2222LMMM 32r   c                     | j         S )zThe period of the seasonalityr   r   s    r   r   zSeasonality.periodm       |r   c                     | j         S )z+The seasonal index of the first observation)r   r   s    r   r   zSeasonality.initial_periodr  s     ##r   r   c                 F   |                      |          }t          |t          j                  r|j        }n?t          |t          j                  r|j        r|j        n|j        }nt          d          |t          d          t          |          } | |          S )aF  
        Construct a seasonality directly from an index using its frequency.

        Parameters
        ----------
        index : {DatetimeIndex, PeriodIndex}
            An index with its frequency (`freq`) set.

        Returns
        -------
        Seasonality
            The initialized Seasonality instance.
        z,index must be a DatetimeIndex or PeriodIndexNz+index must have a freq or inferred_freq set)r   )
r;   r5   r6   rJ   rA   rL   inferred_freqr9   rI   r   )r   r   rA   r   s       r   
from_indexzSeasonality.from_indexw  s    " &&eR^,, 	L:DDr/00 	L!&D5::1DDDJKKK<JKKK%%s&!!!!r   .c                     | j         | j        fS r,   )r   r   r   s    r   r0   zSeasonality._eq_attr  s    |T111r   c                     d| j          dS )NzSeasonality(period=r   r   r   s    r   r*   zSeasonality.__str__  s    4T\4444r   c                 |    | j         }g }t          d|dz             D ]}|                    d| d| d           |S )Nr>   s(r   r   )r   r   r   )r   r   r   is       r   r   zSeasonality._columns  sY    q&1*%% 	/ 	/ANN---F---....r   c                    |                      |          }|j        d         }| j        }t          j        ||f          }| j        dz
  }t          |          D ]}||z   |z  }d||d ||f<   t          j        || j	        |          S Nr   r>   r   )
r;   rH   r   rR   r   r   r   r6   rr   r   )r   r   r\   r   termoffsetr   cols           r   r#   zSeasonality.in_sample  s       ''{1~xv''%)v 	% 	%Av:'C#$DFC  |D$-uEEEEr   r%   r&   c                 H   |                      |          }|                     |||          }|j        d         }| j        }t	          j        ||f          }| j        dz
  }t          |          D ]}	||z   |	z   |z  }
d||	d ||
f<   t          j	        || j
        |          S r   )r;   r]   rH   r   rR   r   r   r   r6   rr   r   )r   r%   r   r&   r   r\   r   r   r   r   col_locs              r   r(   zSeasonality.out_of_sample  s       ''((~FF{1~x((%)v 	) 	)Af}q(F2G'(DFG#$$|D$-{KKKKr   )r>   r,   )r.   rm   rn   ro   r   rs   r   rp   r   r   r   r
   r   r   r6   rL   rJ   r   ru   r0   rt   r*   r   r   r   r   r#   r7   rr   r(   r	   r!   r   r   r   r   >  sK          D IN Ns NC N N N N N     X $ $ $ $ X$ "(8,b.>NO"	" " " ["8 2%#. 2 2 2 X25 5 5 5 5 $s)    X X)122F8H-rx78F	F F F 32F X-566
 8<	L LL Xh'12L !(!34	L
 
L L L 76L L Lr   r   c                   b    e Zd ZdZdeddfdZedefd            Zdej	        dej	        fdZ
dS )	FourierDeterministicTermz7Abstract Base Class for all Fourier Deterministic Termsr{   r   Nc                 0    t          |d          | _        d S Nr   )r   r~   )r   r{   s     r   r   z!FourierDeterministicTerm.__init__  s    'w77r   c                     | j         S )z'The order of the Fourier terms includedr   r   s    r   r{   zFourierDeterministicTerm.order  r   r   r   c                 v   dt           j        z  |                    t           j                  z  }t          j        |j        d         d| j        z  f          }t          | j                  D ]K}t          t           j	        t           j
        f          D ]#\  }} ||dz   |z            |d d d|z  |z   f<   $L|S )NrB   r   r>   )rR   piastyper   emptyrH   r~   r   	enumeratesincos)r   r   r   r   jfuncs         r   r   z#FourierDeterministicTerm._get_terms  s    25y4;;ry111$*Q-T[9::t{## 	; 	;A$bfbf%566 ; ;4&*dAET>&:&:aaaQl##;r   )r.   rm   rn   ro   rs   r   rp   r{   rR   r   r   r!   r   r   r   r     s        AA8c 8d 8 8 8 8 s    Xrz bj      r   r   c            
           e Zd ZdZdZdedef fdZedefd            Z	ede
e         fd            Z eej        j                  d	eee         ej        f         dej        fd
            Z eej        j                  	 dded	eee         ej        f         deee                  dej        fd            Zedeedf         fd            ZdefdZ xZS )Fouriera  
    Fourier series deterministic terms

    Parameters
    ----------
    period : int
        The length of a full cycle. Must be >= 2.
    order : int
        The number of Fourier components to include. Must be <= 2*period.

    See Also
    --------
    DeterministicProcess
    TimeTrend
    Seasonality
    CalendarFourier

    Notes
    -----
    Both a sine and a cosine term are included for each i=1, ..., order

    .. math::

       f_{i,s,t} & = \sin\left(2 \pi i \times \frac{t}{m} \right)  \\
       f_{i,c,t} & = \cos\left(2 \pi i \times \frac{t}{m} \right)

    where m is the length of the period.

    Examples
    --------
    Solar data has an 11-year cycle

    >>> from statsmodels.datasets import sunspots
    >>> from statsmodels.tsa.deterministic import Fourier
    >>> data = sunspots.load_pandas().data
    >>> fourier_gen = Fourier(11, order=2)
    >>> fourier_gen.in_sample(data.index)
    Fr   r{   c                     t                                          |           t          |d          | _        d| j        z  | j        k    rt          d          d S )Nr   rB   z2 * order must be <= period)r   r   r   r   r~   rI   )r   r   r{   r   s      r   r   zFourier.__init__  sV    !&(33t{?T\)):;;; *)r   r   c                     | j         S )zThe period of the Fourier termsr   r   s    r   r   zFourier.period  r   r   c           
          | j         }t          |                                          }g }t          d| j        dz             D ]%}dD ] }|                    | d| d| d           !&|S )Nr>   r   r   (r   r   )r   r   stripr   r~   r   )r   r   
fmt_periodr   r   typs         r   r   zFourier._columns  s    F^^))++
q$+/** 	; 	;A% ; ;#9999J999::::;r   r   c                     |                      |          }|j        d         }|                     t          j        |          | j        z            }t          j        ||| j                  S Nr   r   r   )	r;   rH   r   rR   rU   r   r6   rr   r   )r   r   r\   r   s       r   r#   zFourier.in_sample  s[       ''{1~	$$, >??|EFFFFr   Nr%   r&   c                    |                      |          }|                     |||          }|j        d         }|                     t	          j        |||z             | j        z            }t          j        ||| j	                  S r   )
r;   r]   rH   r   rR   rU   r   r6   rr   r   )r   r%   r   r&   r   r\   r   s          r   r(   zFourier.out_of_sample  sz       ''((~FF{1~	$u = = LMM|EdmLLLLr   .c                     | j         | j        fS r,   r   r~   r   s    r   r0   zFourier._eq_attr,  s    |T[((r   c                 (    d| j          d| j         dS )NzFourier(period=, order=r   r   r   s    r   r*   zFourier.__str__0  s    EEEt{EEEEr   r,   )r.   rm   rn   ro   r   floatrs   r   rp   r   r   rt   r   r   r   r#   r
   r   r   r6   r7   rr   r(   r	   ru   r0   r*   r   r   s   @r   r   r     s       % %L I<u <S < < < < < <     X $s)    X X)122G8H-rx78G	G G G 32G X-566
 8<	
M 
M
M Xh'12
M !(!34	
M
 

M 
M 
M 76
M )%#. ) ) ) X)F F F F F F F F Fr   r   c            	           e Zd ZdZdeddfdZedefd            Zdee	j
        e	j        f         dej        fdZe	j
        e	j        ffde	j        d	eeeed
f         f         dee	j
        e	j        f         fdZdS )CalendarDeterministicTermz4Abstract Base Class for calendar deterministic termsrA   r   Nc                     	 t          j        d|d          }|j        | _        d S # t          $ r t	          d          w xY w)Nz
2020-01-01r>   rC   z freq is not understood by pandas)r6   rM   rA   _freqrI   )r   rA   r   s      r   r   z"CalendarDeterministicTerm.__init__7  sY    	AM,T1EEEEDJJJ 	A 	A 	A?@@@	As	   #' Ac                     | j         j        S z(The frequency of the deterministic termsr   freqstrr   s    r   rA   zCalendarDeterministicTerm.freq>       z!!r   r   c                    t          |t          j                  r|                                }||                    | j                                                  z
  }|                    | j                  }|dz                                   |                                z
  }t          |          t          |          z  S )Nr>   )r5   r6   rJ   to_timestamp	to_periodr   r   )r   r   deltar   gaps        r   _compute_ratioz(CalendarDeterministicTerm._compute_ratioC  s     eR^,, 	)&&((E
33@@BBB__TZ((Av##%%(9(99#..r   allowed.c                    t          |t                    r|f}t          ||          st          |          dk    rd|d         j        z   }nRd                    d |d d         D                       }t          |          dk    r|dz  }|d	|d         j        z   z  }t          |           j         d
| }t          |          t          |t          j        t          j        f          sJ |S )Nr>   za r   z, c              3   $   K   | ]}|j         V  d S r,   )r.   )re   rf   s     r   	<genexpr>z>CalendarDeterministicTerm._check_index_type.<locals>.<genexpr>[  s$      )K)K!*)K)K)K)K)K)Kr   r=   rB   r   z and z! terms can only be computed from )	r5   r-   rQ   r.   r   r9   r6   rL   rJ   )r   r   r  allowed_typesmsgs        r   _check_index_typez+CalendarDeterministicTerm._check_index_typeM  s    gt$$ 	!jG%)) 	!7||q   $wqz': : $		)K)Kgcrcl)K)K)K K Kw<<!##!S(M72;+?!??::& # # # #  C.. %""2BN!CDDDDDr   )r.   rm   rn   ro   rt   r   rp   rA   r
   r6   rL   rJ   rR   r   r  r7   r-   ru   r  r!   r   r   r   r   4  s	       >>AS AT A A A A "c " " " X"/2+R^;</	/ / / / N2
 x tU49--. 
r/	0     r   r   c            
           e Zd ZdZdededdf fdZedee         fd            Z	 e
ej        j                  deee         ej        f         dej        fd	            Z e
ej        j                  	 dd
edeee         ej        f         deee                  dej        fd            Zedeedf         fd            ZdefdZ xZS )CalendarFouriera  
    Fourier series deterministic terms based on calendar time

    Parameters
    ----------
    freq : str
        A string convertible to a pandas frequency.
    order : int
        The number of Fourier components to include. Must be <= 2*period.

    See Also
    --------
    DeterministicProcess
    CalendarTimeTrend
    CalendarSeasonality
    Fourier

    Notes
    -----
    Both a sine and a cosine term are included for each i=1, ..., order

    .. math::

       f_{i,s,t} & = \sin\left(2 \pi i \tau_t \right)  \\
       f_{i,c,t} & = \cos\left(2 \pi i \tau_t \right)

    where m is the length of the period and :math:`\tau_t` is the frequency
    normalized time.  For example, when freq is "D" then an observation with
    a timestamp of 12:00:00 would have :math:`\tau_t=0.5`.

    Examples
    --------
    Here we simulate irregularly spaced hourly data and construct the calendar
    Fourier terms for the data.

    >>> import numpy as np
    >>> import pandas as pd
    >>> base = pd.Timestamp("2020-1-1")
    >>> gen = np.random.default_rng()
    >>> gaps = np.cumsum(gen.integers(0, 1800, size=1000))
    >>> times = [base + pd.Timedelta(gap, unit="s") for gap in gaps]
    >>> index = pd.DatetimeIndex(pd.to_datetime(times))

    >>> from statsmodels.tsa.deterministic import CalendarFourier
    >>> cal_fourier_gen = CalendarFourier("D", 2)
    >>> cal_fourier_gen.in_sample(index)
    rA   r{   r   Nc                     t                                          |           t                              | |           t          |d          | _        d S r   )r   r   r   r   r~   )r   rA   r{   r   s      r   r   zCalendarFourier.__init__  sF     ))$666'w77r   c           
          g }t          d| j        dz             D ]/}dD ]*}|                    | d| d| j        j         d           +0|S )Nr>   r   r   z,freq=r   )r   r~   r   r   r   )r   r   r   r   s       r   r   zCalendarFourier._columns  sw    q$+/** 	H 	HA% H H#FFFF1CFFFGGGGHr   r   c                     |                      |          }|                     |          }|                     |          }|                     |          }t	          j        ||| j                  S Nr   )r;   r  r  r   r6   rr   r   )r   r   ratior   s       r   r#   zCalendarFourier.in_sample  sg       ''&&u--##E**&&|EFFFFr   r%   r&   c                 `   |                      |          }|                     |||          }|                     |           t          |t          j        t          j        f          sJ |                     |          }|                     |          }t	          j	        ||| j
                  S r  )r;   r]   r  r5   r6   rL   rJ   r  r   rr   r   )r   r%   r   r&   r   r  r   s          r   r(   zCalendarFourier.out_of_sample  s       ''((~FF{++++(8".'IJJJJJ##K00&&|EdmLLLLr   .c                 (    | j         j        | j        fS r,   r   r   r~   r   s    r   r0   zCalendarFourier._eq_attr  s    z!4;..r   c                 2    d| j         j         d| j         dS )NzFourier(freq=r   r   r  r   s    r   r*   zCalendarFourier.__str__  s"    Itz1II4;IIIIr   r,   )r.   rm   rn   ro   rt   rs   r   rp   r   r   r   r   r#   r
   r   r   r6   r7   rr   r(   r	   ru   r0   r*   r   r   s   @r   r  r  h  s       . .`8S 8 8 8 8 8 8 8 8
 $s)    X X)122G8H-rx78G	G G G 32G X-566
 8<	M MM Xh'12M !(!34	M
 
M M M 76M /%#. / / / X/J J J J J J J J Jr   r  c            
           e Zd ZdZdZerddddddddd	d	d
ddd
dddddZn ddddddid	d	ddddddddddd	iddddZdededdf fdZe	defd            Z
e	defd            Zdeej        ej        f         dej        fdZdeej        ej        f         dej        fdZdeej        ej        f         dej        fd Zdeej        ej        f         dej        fd!Zdeej        ej        f         dej        fd"Ze	dee         fd#            Z eej        j                  deee         ej        f         dej        fd$            Z eej         j                  	 d+d%e!deee         ej        f         d&e"ee                  dej        fd'            Z e	de#ed(f         fd)            Z$defd*Z% xZ&S ),CalendarSeasonalitya  
    Seasonal dummy deterministic terms based on calendar time

    Parameters
    ----------
    freq : str
        The frequency of the seasonal effect.
    period : str
        The pandas frequency string describing the full period.

    See Also
    --------
    DeterministicProcess
    CalendarTimeTrend
    CalendarFourier
    Seasonality

    Examples
    --------
    Here we simulate irregularly spaced data (in time) and hourly seasonal
    dummies for the data.

    >>> import numpy as np
    >>> import pandas as pd
    >>> base = pd.Timestamp("2020-1-1")
    >>> gen = np.random.default_rng()
    >>> gaps = np.cumsum(gen.integers(0, 1800, size=1000))
    >>> times = [base + pd.Timedelta(gap, unit="s") for gap in gaps]
    >>> index = pd.DatetimeIndex(pd.to_datetime(times))

    >>> from statsmodels.tsa.deterministic import CalendarSeasonality
    >>> cal_seas_gen = CalendarSeasonality("H", "D")
    >>> cal_seas_gen.in_sample(index)
    T         )BDhH   r  r  r   )MSM      )r"  Qr#  )Wr  r&  AY)r  r  r  r  )r"  ME)r"  r*  QEr*  )r*  r+  )r'  r  r&  r(  r)  r+  YErA   r   r   Nc                    t                      } |j        d | j                                        D               t	          | j                                                  }t          |dt	          |          d          }t          |d|d          }|| j        |         vrt          d| d| d          t                      	                    |           || _
        | j        j                            d	          d
         | _        d S )Nc                 P    g | ]#}t          |                                          $S r!   )r   keys)re   vals     r   rh   z0CalendarSeasonality.__init__.<locals>.<listcomp>  s(    CCC3d388::CCCr   rA   F)optionslowerr   zThe combination of freq=z and period=z is not supported.-r   )setupdate
_supportedvaluesru   r/  r   rI   r   r   r   r   r   split	_freq_str)r   rA   r   freq_optionsperiod_optionsr   s        r   r   zCalendarSeasonality.__init__  s+   !$CC$/*@*@*B*BCCC	
 	
 t335566&%"5"5U
 
 
 HnE
 
 
 tv...54 5 5 5 5 5   	+11#66q9r   c                     | j         j        S r   r   r   s    r   rA   zCalendarSeasonality.freq  r   r   c                     | j         S )zThe full periodr   r   s    r   r   zCalendarSeasonality.period  r   r   r   c                 H   | j         j        dv r|j        d|j        z  z   S | j         j        dk    r|j        S t	          j        dd          j                                        }|j        }|                    |                                          st          d          |S )Nr!  r   r  z2000-1-1
   )r@   z=freq is B but index contains days that are not business days.)
r   r   hour	dayofweekr6   bdate_rangeuniqueisinrS   rI   )r   r   bdayslocs       r   _weekly_to_locz"CalendarSeasonality._weekly_to_loc"  s     :++:U_ 444Z3&&?"N:r:::DKKMME/C88E??&&((     Jr   c                     |j         S r,   )r@  r"   s     r   _daily_to_locz!CalendarSeasonality._daily_to_loc3  s     zr   c                     |j         dz
  dz  S )Nr>   r   )monthr"   s     r   _quarterly_to_locz%CalendarSeasonality._quarterly_to_loc8  s     a1$$r   c                 F    | j         j        dv r
|j        dz
  S |j        dz
  S )N)r#  r*  r"  r>   )r   r   rK  quarterr"   s     r   _annual_to_locz"CalendarSeasonality._annual_to_loc=  s.     :!222;?"=1$$r   c                    | j         dk    r|                     |          }nU| j         dk    r|                     |          }n4| j         dv r|                     |          }n|                     |          }| j        | j                  | j                 }t          j        |j	        d         |f          }d|t          j
        |j	        d                   |f<   |S )Nr  r'  )r&  r+  r   r>   )r   rI  rG  rL  rO  r6  r9  rR   r   rH   rU   )r   r   r   
full_cycler   s        r   r   zCalendarSeasonality._get_termsE  s     <3%%e,,DD\S  &&u--DD\[(())%00DD&&u--D_T\24>B
$*Q-45501bi
1&&,-r   c           
          g }| j         | j                 | j                 }t          |          D ].}|                    d| j         d|dz    d| j         d           /|S )Nr   =r>   z	, period=r   )r6  r   r9  r   r   )r   r   countr   s       r   r   zCalendarSeasonality._columnsU  sy    -dn=u 	 	ANNET^EEa!eEEdlEEE    r   c                     |                      |          }|                     |          }|                     |          }t          j        ||| j                  S r  )r;   r  r   r6   rr   r   )r   r   r   s      r   r#   zCalendarSeasonality.in_sample_  sT       ''&&u--&&|EFFFFr   r%   r&   c                 6   |                      |          }|                     |||          }|                     |           t          |t          j        t          j        f          sJ |                     |          }t	          j        ||| j	                  S r  )
r;   r]   r  r5   r6   rL   rJ   r   rr   r   )r   r%   r   r&   r   r   s         r   r(   z!CalendarSeasonality.out_of_samplei  s       ''((~FF{++++(8".'IJJJJJ,,|EdmLLLLr   .c                     | j         | j        fS r,   )r   r9  r   s    r   r0   zCalendarSeasonality._eq_attrw  s    |T^++r   c                     d| j          dS )NzSeasonal(freq=r   )r9  r   s    r   r*   zCalendarSeasonality.__str__{  s    11111r   r,   )'r.   rm   rn   ro   r   r   r6  rt   r   rp   rA   r   r
   r6   rL   rJ   rR   r   rG  rI  rL  rO  r   r   r   r   r   r#   r   r   r7   rr   r(   rs   r	   ru   r0   r*   r   r   s   @r   r  r    s       ! !F I  
qvF;;##""$$,,
 


 qv..r##"A.."A..)1%%
 

:S :# :$ : : : : : :, "c " " " X"     X2+R^;<	   "2+R^;<	   
%2+R^;<%	% % % %
%2+R^;<%	% % % %2+R^;<	     $s)    X X)122G8H-rx78G	G G G 32G X-566
 8<	M MM Xh'12M !(!34	M
 
M M M 76M ,%#. , , , X,2 2 2 2 2 2 2 2 2r   r  c                   d    e Zd ZdZ	 	 ddddededed	eeee	f                  d
df
 fdZ
ed
ee         fd            Ze	 ddeded	eeee	f                  d
d fd            Zdeej        ej        f         dej        d
ej        fdZ eej        j                  deee         ej        f         d
ej        fd            Z eej        j                  	 ddedeee         ej        f         deee                  d
ej        fd            Zed
eedf         fd            Zd
efdZ xZ S )CalendarTimeTrenda
  
    Constant and time trend determinstic terms based on calendar time

    Parameters
    ----------
    freq : str
        A string convertible to a pandas frequency.
    constant : bool
        Flag indicating whether a constant should be included.
    order : int
        A non-negative int containing the powers to include (1, 2, ..., order).
    base_period : {str, pd.Timestamp}, default None
        The base period to use when computing the time stamps. This value is
        treated as 1 and so all other time indices are defined as the number
        of periods since or before this time stamp. If not provided, defaults
        to pandas base period for a PeriodIndex.

    See Also
    --------
    DeterministicProcess
    CalendarFourier
    CalendarSeasonality
    TimeTrend

    Notes
    -----
    The time stamp, :math:`\tau_t`, is the number of periods that have elapsed
    since the base_period. :math:`\tau_t` may be fractional.

    Examples
    --------
    Here we simulate irregularly spaced hourly data and construct the calendar
    time trend terms for the data.

    >>> import numpy as np
    >>> import pandas as pd
    >>> base = pd.Timestamp("2020-1-1")
    >>> gen = np.random.default_rng()
    >>> gaps = np.cumsum(gen.integers(0, 1800, size=1000))
    >>> times = [base + pd.Timedelta(gap, unit="s") for gap in gaps]
    >>> index = pd.DatetimeIndex(pd.to_datetime(times))

    >>> from statsmodels.tsa.deterministic import CalendarTimeTrend
    >>> cal_trend_gen = CalendarTimeTrend("D", True, order=1)
    >>> cal_trend_gen.in_sample(index)

    Next, we normalize using the first time stamp

    >>> cal_trend_gen = CalendarTimeTrend("D", True, order=1,
    ...                                   base_period=index[0])
    >>> cal_trend_gen.in_sample(index)
    Tr   Nbase_periodrA   rz   r{   r\  r   c                "   t                                          |           t                              | ||           d| _        |.t	          j        |d| j                  }|j        d         | _        |d nt          |          | _	        d S )Nr   r   r>   r?   )
r   r   ry   _ref_i8r6   rK   r   asi8rt   _base_period)r   rA   rz   r{   r\  prr   s         r   r   zCalendarTimeTrend.__init__  s     	"++85 	, 	
 	
 	
 "adjIIIB71:DL$/$7DDS=M=Mr   c                     | j         S )zThe base period)r`  r   s    r   r\  zCalendarTimeTrend.base_period  s       r   r   c                 h    |                     d          }d}d|v rd}nd|v rd} | ||||          S )a  
        Create a TimeTrend from a string description.

        Provided for compatibility with common string names.

        Parameters
        ----------
        freq : str
            A string convertible to a pandas frequency.
        trend : {"n", "c", "t", "ct", "ctt"}
            The string representation of the time trend. The terms are:

            * "n": No trend terms
            * "c": A constant only
            * "t": Linear time trend only
            * "ct": A constant and a time trend
            * "ctt": A constant, a time trend and a quadratic time trend
        base_period : {str, pd.Timestamp}, default None
            The base period to use when computing the time stamps. This value
            is treated as 1 and so all other time indices are defined as the
            number of periods since or before this time stamp. If not
            provided, defaults to pandas base period for a PeriodIndex.

        Returns
        -------
        TimeTrend
            The TimeTrend instance.
        r   r   r   rB   r   r>   r[  r   )r   rA   r   r\  rz   r{   s         r   r   zCalendarTimeTrend.from_string  sT    F ##C((5==EEE\\Es45kBBBBr   r   r  c                 P   t          |t          j                  r|                    | j                  }|j        }|| j        z
  dz   }|                    t          j	                  |z   }|d d d f         }| 
                    |          }t          j        || j        |          S )Nr>   r   )r5   r6   rL   r  r   r_  r^  r   rR   r   r   rr   r   )r   r   r  index_i8timer   s         r   _termszCalendarTimeTrend._terms  s     eR-.. 	0OODJ//E:dl*Q.ry))E1AAAtG}%%|E4=FFFFr   c                     |                      |          }|                     |          }|                     |          }|                     ||          S r,   )r;   r  r  rg  )r   r   r  s      r   r#   zCalendarTimeTrend.in_sample  sQ       ''&&u--##E**{{5%(((r   r%   r&   c                 *   |                      |          }|                     |||          }|                     |           t          |t          j        t          j        f          sJ |                     |          }|                     ||          S r,   )	r;   r]   r  r5   r6   rJ   rL   r  rg  )r   r%   r   r&   r   r  s         r   r(   zCalendarTimeTrend.out_of_sample  s       ''((~FF{++++8H'IJJJJJ##K00{{;...r   .c                 \    | j         | j        | j        j        f}| j        || j        fz  }|S r,   )r}   r~   r   r   r`  )r   attrs     r   r0   zCalendarTimeTrend._eq_attr  s=     NKJ&

 (T&((Dr   c                     t                               |           }d|d d         z   d| j        j         dz   }| j        |d d         d| j         dz   }|S )NCalendarr=   z, freq=r   zbase_period=)ry   r*   r   r   r`  )r   values     r   r*   zCalendarTimeTrend.__str__&  sl    *22488U3B3Z'*IDJ4F*I*I*II(#2#J!D0A!D!D!DDEr   r   r,   )!r.   rm   rn   ro   rt   rq   rs   r	   r
   DateLiker   rp   r\  r   r   r6   rL   rJ   rR   r   rr   rg  r   r   r#   r   r   r7   r(   ru   r0   r*   r   r   s   @r   rZ  rZ    s       3 3p 	N 7;N N NN N 	N eCM23N 
N N N N N N$ !Xc] ! ! ! X! 
 7;	(C (C(C (C eCM23	(C
 
(C (C (C [(CTG2+R^;<GEGZG	G G G G X)122)8H-rx78)	) ) ) 32) X-566
 8<	/ // Xh'12/ !(!34	/
 
/ / / 76/ %#.    X        r   rZ  c                      e Zd ZdZdddddddddeee         ej        f         de	ee
ef                  d	ed
edededee         defdZedej        fd            Zedee         fd            Zdeej                 deej                 fdZdej        dej        fdZ eej        j                  dej        fd            Z eej        j                  	 d%dede	eee         ej        f                  dej        fd            Zdej        deej        ej        f         fdZdededej        fdZdej        dej        dej        fdZded edej        fd!Z dee!e"ef         dee!e"ef         dej        fd"Z#d&d#Z$d$ Z%dS )'DeterministicProcessa  
    Container class for deterministic terms.

    Directly supports constants, time trends, and either seasonal dummies or
    fourier terms for a single cycle. Additional deterministic terms beyond
    the set that can be directly initialized through the constructor can be
    added.

    Parameters
    ----------
    index : {Sequence[Hashable], pd.Index}
        The index of the process. Should usually be the "in-sample" index when
        used in forecasting applications.
    period : {float, int}, default None
        The period of the seasonal or fourier components. Must be an int for
        seasonal dummies. If not provided, freq is read from index if
        available.
    constant : bool, default False
        Whether to include a constant.
    order : int, default 0
        The order of the tim trend to include. For example, 2 will include
        both linear and quadratic terms. 0 exclude time trend terms.
    seasonal : bool = False
        Whether to include seasonal dummies
    fourier : int = 0
        The order of the fourier terms to included.
    additional_terms : Sequence[DeterministicTerm]
        A sequence of additional deterministic terms to include in the process.
    drop : bool, default False
        A flag indicating to check for perfect collinearity and to drop any
        linearly dependent terms.

    See Also
    --------
    TimeTrend
    Seasonality
    Fourier
    CalendarTimeTrend
    CalendarSeasonality
    CalendarFourier

    Notes
    -----
    See the notebook `Deterministic Terms in Time Series Models
    <../examples/notebooks/generated/deterministics.html>`__ for an overview.

    Examples
    --------
    >>> from statsmodels.tsa.deterministic import DeterministicProcess
    >>> from pandas import date_range
    >>> index = date_range("2000-1-1", freq="M", periods=240)

    First a determinstic process with a constant and quadratic time trend.

    >>> dp = DeterministicProcess(index, constant=True, order=2)
    >>> dp.in_sample().head(3)
                const  trend  trend_squared
    2000-01-31    1.0    1.0            1.0
    2000-02-29    1.0    2.0            4.0
    2000-03-31    1.0    3.0            9.0

    Seasonal dummies are included by setting seasonal to True.

    >>> dp = DeterministicProcess(index, constant=True, seasonal=True)
    >>> dp.in_sample().iloc[:3,:5]
                const  s(2,12)  s(3,12)  s(4,12)  s(5,12)
    2000-01-31    1.0      0.0      0.0      0.0      0.0
    2000-02-29    1.0      1.0      0.0      0.0      0.0
    2000-03-31    1.0      0.0      1.0      0.0      0.0

    Fourier components can be used to alternatively capture seasonal patterns,

    >>> dp = DeterministicProcess(index, constant=True, fourier=2)
    >>> dp.in_sample().head(3)
                const  sin(1,12)  cos(1,12)  sin(2,12)  cos(2,12)
    2000-01-31    1.0   0.000000   1.000000   0.000000        1.0
    2000-02-29    1.0   0.500000   0.866025   0.866025        0.5
    2000-03-31    1.0   0.866025   0.500000   0.866025       -0.5

    Multiple Seasonalities can be captured using additional terms.

    >>> from statsmodels.tsa.deterministic import Fourier
    >>> index = date_range("2000-1-1", freq="D", periods=5000)
    >>> fourier = Fourier(period=365.25, order=1)
    >>> dp = DeterministicProcess(index, period=3, constant=True,
    ...                           seasonal=True, additional_terms=[fourier])
    >>> dp.in_sample().head(3)
                const  s(2,3)  s(3,3)  sin(1,365.25)  cos(1,365.25)
    2000-01-01    1.0     0.0     0.0       0.000000       1.000000
    2000-01-02    1.0     1.0     0.0       0.017202       0.999852
    2000-01-03    1.0     0.0     1.0       0.034398       0.999408
    NFr   r!   r   rz   r{   seasonalfourieradditional_termsdropr   r   rz   r{   rs  rt  ru  rv  c                   t          |t          j                  st          j        |          }|| _        g | _        d| _        d | _        |                                  t          |dd          }t          |d          x| _
        }t          |d          | _        t          |d          x| _        }t          |d          | _        t          |          }d | _        t          |d	          | _        || _        |s|r(| j                            t)          ||                     |r|rt+          d
          |s|r||t-          | j                  x| _        }|r8t          |d          }| j                            t1          |                     n?|r=t          |d          }|J | j                            t3          ||                     |D ]X}	t          |	t4                    st7          d          |	| j        vr| j                            |	           Jt+          d          || _        d | _        d S )NFr   T)optionalrz   r{   rs  rt  rv  zseasonal and fourier can be initialized through the constructor since these will be necessarily perfectly collinear. Instead, you can pass additional components using the additional_terms input.)r{   zJAll additional terms must be instances of subsclasses of DeterministicTermzuOne or more terms in additional_terms has been added through the parameters of the constructor. Terms must be unique.)r5   r6   r7   _index_deterministic_terms_extendable_index_freq_validate_indexr   r   r}   r   r~   	_seasonal_fourierru   _cached_in_sample_drop_additional_termsr   r   rI   r   r   r   r   r   r9   _retain_cols)
r   r   r   rz   r{   rs  rt  ru  rv  r   s
             r   r   zDeterministicProcess.__init__  s    %** 	$HUOOE=?! FHt<<<$-h
$C$CC'w77$-h
$C$CC)'9== !122!%tV,,
!1 	Iu 	I%,,Yx-G-GHHH 	 	H  
  	I 	IV^~(6t7G(H(HHv 	M&vx88F%,,[-@-@AAAA 	M11F%%%%,,WV7-K-K-KLLL$ 	 	Dd$566 +   4444)006666 !  
 6:r   r   c                     | j         S )zThe index of the process)ry  r   s    r   r   zDeterministicProcess.index  r   r   c                     | j         S )z/The deterministic terms included in the process)rz  r   s    r   r   zDeterministicProcess.terms  s     ((r   r   c                    d }| j         D ]'}t          |t          t          f          r	|p|j        }(|Rd}|D ]M}||j        d         k                                    |j        d         dk    z  }|p|                                }N|}t          | j                   D ]0\  }}|j	        }|r|r||         j        d d dd f         ||<   |p|}1|S )NFr   r>   )
rz  r5   r   rZ  rz   ilocrS   anyr   r   )	r   r   	has_constdtermr   	const_col
drop_firstr   r   s	            r   _adjust_dummiesz$DeterministicProcess._adjust_dummies  s   $(	. 	8 	8E%)->!?@@ 8%7	I 9 9!TYq\16688DIaLA<MN	%8		
!$";<< 	0 	0HAu~H 0J 0 8=ABB/a#/xJJr   c                 d   t          j        |dk    d          }t          j        |          r|j        d d | f         }|                    d          |                    d          k    }t          j        |          dk    r)||                                z  }|j        d d | f         }|S )Nr   axisr>   )rR   rS   r  rF  maxminsum
duplicated)r   r   all_zerois_constantsurplus_constss        r   _remove_zeros_onesz'DeterministicProcess._remove_zeros_ones  s    6%1*1---6( 	,Iaaa(l+EiiQi''599!9+<+<<6+""(;+A+A+C+CCNIaaa.01Er   c                    | j         | j         S | j        }| j        s5t          j        t          j        |j        d         df          |          S g }| j        D ]*}|                    |	                    |                     +| 
                    |          }t          j        |d          }|                     |          }| j        rt          |          }t          |dd          }|d         }|d         }t          j        t          j        |                    }	|	d         |j        d         z  t          j        t&                    j        z  }
t+          t          j        |	|
k                        }|j        |z  }dg}d}t1          d|j        d                   D ]X}t
          j                            |d |dz   d |dz   f                   }||k    r|                    |           |}||k    r nYt7          |          |k    r|j        d d |f         }n+|j        d d t          j        |d |                   f         }|j        | _        || _         |S )	Nr   r:   r>   r  rT)modepivotingr=   ) r  ry  rz  r6   rr   rR   r   rH   r   r#   r  concatr  r  r   r   absdiagfinfor   epsrs   r  Tr   linalgmatrix_rankrQ   r  sortr   r  )r   r   	raw_termsr   r   	terms_arrresr  pabs_diagtolrankrpxkeep	last_rankr   	curr_ranks                    r   r#   zDeterministicProcess.in_sample  sV   !-))( 	L<%+a.!)< = =UKKKK	- 	4 	4DT^^E223333((33	 i	:::''..: 	9 IYS4888CAABAvbgajj))H1+	 22RXe__5HHCrvhn--..D#	/C3DI1ioa011  I11#gAgwQw6F2GHH	y((KKNNN )I$$E %4yyD  
111d7+
111bgah&7&7#78!M!&r   r%   r&   c                    t          |d          }| j        r| j        |                                  | j        }| j        s5t          j        t          j	        |j
        d         df          |          S g }| j        D ],}|                    |                    |||                     -t          j        |d          }| j        J |j
        d         t          | j                  k    r|| j                 }|S )Nr%   r   r:   r>   r  )r   r  r  r#   ry  rz  r6   rr   rR   r   rH   r   r(   r  rQ   )r   r%   r&   r   r  r   r   s          r   r(   z"DeterministicProcess.out_of_sample  s    "%11: 	$+3NN( 	L<%+a.!)< = =UKKKK	- 	O 	ODT//unMMNNNN i	::: ,,,;q>S!23333$+,Er   rO   c                     | j         }t          |t          j                  r"t          j        |d         ||j                  S t          j        |d         || j                  S )Nr   )endrA   )rZ   r  rA   )ry  r5   r6   rJ   rK   rA   rM   r|  )r   rO   r   s      r   _extend_time_indexz'DeterministicProcess._extend_time_index1  s[     eR^,, 	H?58EJGGGG}58D<LMMMMr   rZ   c                    | j         }t          |          }t          |t          j                  s|sJ ||d         k     rt          t                    t          |t          j                  r|j        }n;t          |          dk    r&t          j
        |                                          nd}|dk    r%||d         z
  |z  dk    rt          d| d          |r(t          j        t          j        ||                    }nt          j        |||          }|d         | j         d         k    r#|                                 }|j        |         }|S |d         | j         d         k    r|d         |z   }|d         |k    rFt          j        |||          }	|                     |	j        d         |	          }
|
j        |         S |                     |j        d         |          S || j         d         k    }||         }||          }|                                 j        |         }|                     |j        d         |          }t          j        ||gd	          S )
Nr   r>   z,The step of the index is not 1 (actual step=zM). start must be in the sequence that would have been generated by the index.rE   r=   )r&   )r%   r&   r  )ry  r   r5   r6   rN   rI   START_BEFORE_INDEX_ERRrF   rQ   rR   rT   r  r7   rU   r#   rF  r(   rH   r  )r   rZ   rO   r   is_int64_indexidx_stepnew_idxr#   
next_valuetmpoosin_sample_locin_sample_idxout_of_sample_idxin_sample_exogoos_exogs                   r   _range_from_range_indexz,DeterministicProcess._range_from_range_index:  sm   %e,,%//A>AAA583444eR]++ 	EzHH/25zzA~~rwu~~))+++1Hq==uuQx/8;AA*x * * *  
  	@hry5566GGmE4h???G2;$+b/))((I!g.IQZ$+b/))rX-JqzZ''mJ8DDD((1c(JJww''%%gmA&6w%OOO4;r?2.#]N3))-m<%%#)!,=N & 
 
 y.(3!<<<<r   c                    | j         }t          | j         t          j                  rjt          |t          j                  r|                    | j                  }t          |t          j                  r|                    | j                  }||d         k     rt          t                    || j         d         k    r!| 	                                j
        ||         S |                     |          }|||d         k             }|                     |j        d         |          }||d         k    r|j
        ||         S t          j        | 	                                |gd          }|j
        ||         S )N)rA   r   r=   r  )ry  r5   r6   rJ   	Timestampr  r|  rI   r  r#   rF  r  r(   rH   r  )r   rZ   rO   r   r  oos_idxr  boths           r   _range_from_time_indexz+DeterministicProcess._range_from_time_indexe  sV    dk2>22 	=%.. ?T-=>>$-- =~~4+;~<<5834444;r?"">>##'d
33))$//'E"I-.  q!17;;GAJ75:&&y$..**C0q999xd
##r   rn  r1   c                    |dk     rt          | d          || j        j        d         k     r| j        |         S || j        j        d         dz
  z
  dz   }| j        }t          | j        t          j                  r<t	          j        |d         | j        |          }|d                                         S t	          j	        |d         | j        |          }|d         S )Nr   z must be non-negative.r>   r=   rC   )
rI   ry  rH   r5   r6   rJ   rK   r|  r   rM   )r   rn  r1   add_periodsr   ra  drs          r   _int_to_timestampz&DeterministicProcess._int_to_timestampz  s    199<<<===4;$Q''';u%%t{03a781<dk2>22 	)b	 0+  B b6&&(((]"ID,k
 
 
 "vr   c                 j   | j         st          d          t          | j                  t          j        fv st          | j                  r;t          |d          }t          |d          }|dz  }|                     ||          S t          |t          t          j        f          r|                     |d          }nt	          j        |          }t          |t          t          j        f          r|                     |d          }nt	          j        |          }|                     ||          S )a  
        Deterministic terms spanning a range of observations

        Parameters
        ----------
        start : {int, str, dt.datetime, pd.Timestamp, np.datetime64}
            The first observation.
        stop : {int, str, dt.datetime, pd.Timestamp, np.datetime64}
            The final observation. Inclusive to match most prediction
            function in statsmodels.

        Returns
        -------
        DataFrame
            A data frame of deterministic terms
        zThe index in the deterministic process does not support extension. Only PeriodIndex, DatetimeIndex with a frequency, RangeIndex, and integral Indexes that start at 0 and have only unit differences can be extended when producing out-of-sample forecasts.
rZ   rO   r>   )r{  r9   r-   ry  r6   rN   r   r   r  r5   rs   rR   integerr  r  r  )r   rZ   rO   s      r   r   zDeterministicProcess.range  s"   *  	    000L4M4M0%eW55E$T622DAID//t<<<ec2:.// 	(**5'::EEL''EdS"*-.. 	&))$77DD<%%D**5$777r   c                    t          | j        t          j                  r| j        j        | _        d| _        d S t          | j        t          j                  r-| j        j        p| j        j        | _        | j        d u| _        d S t          | j        t          j	                  r	d| _        d S t          | j                  rG| j        d         dk    o.t          j        t          j        | j                  dk              | _        d S d S )NTr   r>   )r5   ry  r6   rJ   rA   r|  r{  rL   r   rN   r   rR   rS   rT   r   s    r   r}  z$DeterministicProcess._validate_index  s    dk2>22 	#{/D#DR%566 	#{/L4;3LD#/t;DR]33 	#D$+&& 	#{1~2  rv$$)8 8D	 	r   c           
      v    t          || j        | j        | j        | j        | j        | j        | j                  S )ap  
        Create an identical determinstic process with a different index

        Parameters
        ----------
        index : index_like
            An index-like object. If not an index, it is converted to an
            index.

        Returns
        -------
        DeterministicProcess
            The deterministic process applied to a different index
        rr  )rq  r   r}   r~   r~  r  r  r  r"   s     r   applyzDeterministicProcess.apply  sB     $<^+^M!3	
 	
 	
 		
r   r,   )r   N)&r.   rm   rn   ro   r
   r   r   r6   r7   r	   r   rs   rq   r   r   rp   r   r   r   rr   r  r  r   r#   r(   r  rL   rJ   r  r  r  rt   r  IntLikero  r   r}  r  r!   r   r   rq  rq  .  sX       [ [B /38:=; =; =;Xh'12=; ucz*+	=;
 =; =; =; =; ##45=; =; =; =; =;~ rx    X )t-. ) ) ) X)T",%7 D<N    &	 	 	 	 	 	 X)122&2< & & & 32&P X-566 IM  !x'928'C!DE 
	   76(NlN 
r/	0N N N N)=S )= )= )= )= )= )=V$\$)+$	$ $ $ $*s # ",    "+8Wh+,+8 GXs*++8 
	+8 +8 +8 +8Z   
 
 
 
 
r   rq  )1statsmodels.compat.pandasr   r   r   r   abcr   r   datetimedttypingr	   r
   collections.abcr   r   numpyrR   pandasr6   scipy.linalgr   statsmodels.iolib.summaryr   statsmodels.tools.validationr   r   r   r   statsmodels.tsa.tsatoolsr   r  
datetime64ro  rs   r  r  r  r   ry   r   r   r   r   r   r  r  rZ  rq  r!   r   r   <module>r     s              $ # # # # # # #     " " " " " " " " . . . . . . . .               , , , , , ,            4 3 3 3 3 3blBM9:
RZ
  K K K K K K K K\/) /) /) /) /)!2C /) /) /)dW+ W+ W+ W+ W+* W+ W+ W+tCL CL CL CL CL# CL CL CLL    0#   (YF YF YF YF YF& YF YF YFx1 1 1 1 1 13 1 1 1h]J ]J ]J ]J ]J/1I ]J ]J ]J@t2 t2 t2 t2 t23 t2 t2 t2nl l l l l13M l l l^p
 p
 p
 p
 p
 p
 p
 p
 p
 p
r   