
    !g                        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jF                  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       Z0y)!    )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y)DeterministicTermz/Abstract Base Class for all Deterministic TermsFreturnc                     | j                   S )z?Flag indicating whether the values produced are dummy variables)	_is_dummyselfs    Z/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/deterministic.pyis_dummyzDeterministicTerm.is_dummy*        ~~    indexc                      y)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                      y)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                      y)z.A meaningful string representation of the termNr!   r   s    r   __str__zDeterministicTerm.__str__[   r$   r   c                 ^    t        |       j                  f}t        || j                  z         S N)type__name__hash_eq_attr)r   names     r   __hash__zDeterministicTerm.__hash___   s(    &*4j&9&9%;D4==())r   .c                      y)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   sF    eRXX&L	J88E?" 	JHII	Js	   3 Ac                    |dt         j                  |      }t        |t        j                        sJ |j
                  d   |k7  rt        d|j
                  d    d| d      |S t        | t        j                        r(t        j                  | d   dz   || j                        S t        | t        j                        rV| j                  Jt        j                  | d   | j                  d	
      d   }t        j                  || j                  |
      S t        | t        j                        rUt        | t        j                        sJ 	 | j                  }| j                  }|||z  z   }t        j                  |||      S t#        |       ret%        j&                  t%        j(                  |       dk(        r:t%        j*                  | d   dz   | d   |z   dz         }t        j                  |      S ddl}|j/                  dt0        d	       | j
                  d   }	t        j                  |	dz   |	|z   dz         S # t        $ r' t!        |       dkD  r| d   | d   z
  nd}| d   |z   }Y 	w xY w)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:    %.::>JNnbhh777##A&%/ &,,Q/00DUG2O  "!eR^^,??b	Au5::  r//0UZZ5K}}U2YUZZKANH==

EJJr}}-eR]]333)zz


 4%<'D==488% RVVBGGENa,?%@iib	AuRy5/@1/DEG88G$$"  	 	
 {{1~}}TAXte|a'788+ " )03E
QuRy59,Ab	D()s   I ,I21I2c                 B    | j                         dt        |       dz   S )Nz at 0x0x)r*   idr   s    r   __repr__zDeterministicTerm.__repr__   s     ||~&D" 666r   otherc                     t        |t        |             r[| j                  }|j                  }t        |      t        |      k7  ryt	        t        ||      D cg c]
  \  }}||k(   c}}      S yc c}}w )NF)r5   r-   r0   rQ   rS   zip)r   rb   own_attroth_attrabs         r   __eq__zDeterministicTerm.__eq__   sc    eT$Z(}}H~~H8}H-3x+BC41aQCDD Ds   A2
r,   )r.   
__module____qualname____doc__r   propertyboolr   r   r   r   r6   	DataFramer#   intr	   r(   strr*   r2   tupler0   staticmethodr7   r;   r]   ra   objectri   r!   r   r   r   r   $   s   9 I$   x1 bll    
 8<	 ! !(!34	
 
 4 = = =*# * H%#. H  H J8H- J"(( J J  8<09xx0909 !(!3409 
	09 09d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y)TimeTrendDeterministicTermz:Abstract Base Class for all Time Trend Deterministic Termsconstantorderr   Nc                 H    t        |d      | _        t        |d      | _        y )Nrw   rx   )r   	_constantr   _order)r   rw   rx   s      r   __init__z#TimeTrendDeterministicTerm.__init__   s    "8Z8'w7r   c                     | j                   S )z+Flag indicating that a constant is included)rz   r   s    r   rw   z#TimeTrendDeterministicTerm.constant   r   r   c                     | j                   S )zOrder of the time trendr{   r   s    r   rx   z TimeTrendDeterministicTerm.order        {{r   c                     g }dddd}| j                   r|j                  d       t        d| j                  dz         D ]/  }||v r|j                  ||          |j                  d|        1 |S )Ntrendtrend_squaredtrend_cubed)r>   rB      constr>   ztrend**)rz   appendranger{   )r   columnstrend_namespowers       r   _columnsz#TimeTrendDeterministicTerm._columns   sw    !o-H>>NN7#1dkkAo. 	2E#{51201		2
 r   locsc                 8   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   )rp   rz   r{   rR   tilezerosrU   )r   r   ntermstermsr   s        r   
_get_termsz%TimeTrendDeterministicTerm._get_terms   s}    T^^$t{{2q&k*!VC0*,))At{{Q*GaT^^$&&'%r   c                     g }| j                   r|j                  d       | j                  r!|j                  d| j                  dz           |sdg}dj                  |      }d| dS )NConstantzPowers 1 to r>   Empty,z
TimeTrend())rz   r   r{   join)r   r   	terms_strs      r   r*   z"TimeTrendDeterministicTerm.__str__   sc    >>LL$;;LL<a'89:IEHHUO	I;a((r   Tr   )r.   rj   rk   rl   rn   rp   r|   rm   rw   rx   listrq   r   rR   ndarrayr   r*   r!   r   r   rv   rv      s    D8 8S 8 8 $   s   
$s) 
 
rzz bjj 	) 	)r   rv   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)
    rw   rx   r   Nc                 &    t         |   ||       y r,   )superr|   )r   rw   rx   	__class__s      r   r|   zTimeTrend.__init__   s    5)r   r   c                 V    |j                  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>   rw   rx   
startswith)clsr   rw   rx   s       r   from_stringzTimeTrend.from_string  s>    . ##C(5=EE\EHE22r   r   c                    | j                  |      }|j                  d   }t        j                  d|dz   t        j                        d d d f   }| j                  |      }t        j                  || j                  |      S Nr   r>   r   r   r   )	r;   rH   rR   rU   doubler   r6   ro   r   )r   r   r\   r   r   s        r   r#   zTimeTrend.in_sample!  si       '{{1~yyD1HBII6q$w?%||E4==FFr   r%   r&   c                 :   | j                  |      }|j                  d   }| j                  |||      }t        j                  |dz   ||z   dz   t        j
                        d d d f   }| j                  |      }t        j                  || j                  |      S r   )
r;   rH   r]   rR   rU   r   r   r6   ro   r   )r   r%   r   r&   r\   fcast_indexr   r   s           r   r(   zTimeTrend.out_of_sample+  s       '{{1~((~Fyy4%<!#3299EagN%||E4==LLr   .c                 2    | j                   | j                  fS r,   )rz   r{   r   s    r   r0   zTimeTrend._eq_attr9  s    ~~t{{**r   r   r,   )r.   rj   rk   rl   rn   rp   r|   classmethodrq   r   r   r   r#   r
   r   r   r6   r7   ro   r(   r	   rm   rr   r0   __classcell__r   s   @r   r   r      s1   4* *S * * 3 3 3 3< ))112G8H-rxx78G	G 3G --556
 8<	MM Xh'12M !(!34	M
 
M 7M +%#. + +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j4                  j                        d
eee   ej6                  f   dej8                  fd       Z eej:                  j                        	 dded
eee   ej6                  f   deee      dej8                  fd       Zy)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)
    Tperiodinitial_periodr   Nc                     t        |d      | _        t        |d      | _        |dk  rt        d      d| j                  cxk  r|k  st        d       t        d      y )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  sm    (:0, 
 A:233D((2F2LMM 3LMM 3r   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                 H   | j                  |      }t        |t        j                        r|j                  }nJt        |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 PeriodIndexz+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^^,::Dr//0!&5::1D1DDJKK<JKK%&!!r   .c                 2    | j                   | j                  fS r,   )r   r   r   s    r   r0   zSeasonality._eq_attr  s    ||T1111r   c                 "    d| j                    dS )NzSeasonality(period=r   r   r   s    r   r*   zSeasonality.__str__  s    $T\\N!44r   c                 z    | j                   }g }t        d|dz         D ]  }|j                  d| d| d        |S )Nr>   s(r   r   )r   r   r   )r   r   r   is       r   r   zSeasonality._columns  sJ    q&1*% 	/ANNRs!F81-.	/r   c                 .   | j                  |      }|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   ro   r   )r   r   r\   r   termoffsetr   cols           r   r#   zSeasonality.in_sample  s       '{{1~xxv'%%)v 	%Av:'C#$DFC 	% ||D$--uEEr   r%   r&   c                 Z   | j                  |      }| j                  |||      }|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   ro   r   )r   r%   r   r&   r   r\   r   r   r   r   col_locs              r   r(   zSeasonality.out_of_sample  s       '((~F{{1~xx(%%)v 	)Af}q(F2G'(DFG#$	) ||D$--{KKr   )r>   r,   )r.   rj   rk   rl   r   rp   r|   rm   r   r   r   r
   r   r   r6   rL   rJ   r   rr   r0   rq   r*   r   r   r   r   r#   r7   ro   r(   r	   r!   r   r   r   r   >  s    D INs NC N N    $ $ $ "(8,b.>.>NO"	" "8 2%#. 2 25 5 $s)   ))112F8H-rxx78F	F 3F --556
 8<	LL Xh'12L !(!34	L
 
L 7Lr   r   c                   n    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
y)	FourierDeterministicTermz7Abstract Base Class for all Fourier Deterministic Termsrx   r   Nc                 &    t        |d      | _        y Nr   )r   r{   )r   rx   s     r   r|   z!FourierDeterministicTerm.__init__  s    'w7r   c                     | j                   S )z'The order of the Fourier terms includedr   r   s    r   rx   zFourierDeterministicTerm.order  r   r   r   c                    dt         j                  z  |j                  t         j                        z  }t        j                  |j
                  d   d| j                  z  f      }t        | j                        D ]N  }t        t         j                  t         j                  f      D ]   \  }} ||dz   |z        |d d d|z  |z   f<   " P |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    255y4;;ryy11$**Q-T[[9:t{{# 	;A$bffbff%56 ;4&*AET>&:aQl#;	; r   )r.   rj   rk   rl   rp   r|   rm   rx   rR   r   r   r!   r   r   r   r     sN    A8c 8d 8 s  rzz bjj 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   rx   c                     t         |   |       t        |d      | _        d| j                  z  | j                  kD  rt        d      y )Nr   rB   z2 * order must be <= period)r   r|   r   r   r{   rI   )r   r   rx   r   s      r   r|   zFourier.__init__  sC    !&(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        |      j                         }g }t        d| j                  dz         D ]#  }dD ]  }|j                  | 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  su    F^))+
q$++/* 	;A% ;#as!J<q9:;	; r   r   c                     | j                  |      }|j                  d   }| j                  t        j                  |      | j
                  z        }t        j                  ||| j                        S Nr   r   r   )	r;   rH   r   rR   rU   r   r6   ro   r   )r   r   r\   r   s       r   r#   zFourier.in_sample  sW       '{{1~		$$,, >?||EFFr   r%   r&   c                    | j                  |      }| j                  |||      }|j                  d   }| j                  t	        j
                  |||z         | j                  z        }t        j                  ||| j                        S r   )
r;   r]   rH   r   rR   rU   r   r6   ro   r   )r   r%   r   r&   r   r\   r   s          r   r(   zFourier.out_of_sample  sr       '((~F{{1~		$u = LM||EdmmLLr   .c                 2    | 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     ht{{m1EEr   r,   )r.   rj   rk   rl   r   floatrp   r|   rm   r   r   rq   r   r   r   r#   r
   r   r   r6   r7   ro   r(   r	   rr   r0   r*   r   r   s   @r   r   r     sX   %L I<u <S <    $s)   ))112G8H-rxx78G	G 3G --556
 8<	
M
M Xh'12
M !(!34	
M
 

M 7
M )%#. ) )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y)CalendarDeterministicTermz4Abstract Base Class for calendar deterministic termsrA   r   Nc                     	 t        j                  d|d      }|j                  | _        y # 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  sB    	AMM,T1EEDJ 	A?@@	As	   ), Ac                 .    | j                   j                  S z(The frequency of the deterministic termsr   freqstrr   s    r   rA   zCalendarDeterministicTerm.freq>       zz!!!r   r   c                 Z   t        |t        j                        r|j                         }||j	                  | j
                        j                         z
  }|j	                  | j
                        }|dz   j                         |j                         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@@BB__TZZ(Av##%(99#..r   allowed.c                    t        |t              r|f}t        ||      st        |      dk(  rd|d   j                  z   }nCdj	                  d |d d D              }t        |      dkD  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   4   K   | ]  }|j                     y wr,   )r.   ).0rg   s     r   	<genexpr>z>CalendarDeterministicTerm._check_index_type.<locals>.<genexpr>[  s     )K!**)Ks   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':': : $		)Kgcrl)K Kw<!#!S(M72;+?+?!??:&&''H /#  C. %""2"2BNN!CDDDr   )r.   rj   rk   rl   rq   r|   rm   rA   r
   r6   rL   rJ   rR   r   r  r7   r-   rr   r	  r!   r   r   r   r   4  s    >AS AT A "c " "/2++R^^;</	/ NN2
xx 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   rx   r   Nc                 r    t         |   |       t        j                  | |       t        |d      | _        y r   )r   r|   r   r   r{   )r   rA   rx   r   s      r   r|   zCalendarFourier.__init__  s.     ))$6'w7r   c           
          g }t        d| j                  dz         D ]7  }dD ]0  }|j                  | d| d| j                  j                   d       2 9 |S )Nr>   r   r   z,freq=r   )r   r{   r   r   r   )r   r   r   r   s       r   r   zCalendarFourier._columns  si    q$++/* 	HA% H#as&1C1C0DAFGH	H r   r   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }t	        j
                  ||| j                        S Nr   )r;   r	  r  r   r6   ro   r   )r   r   ratior   s       r   r#   zCalendarFourier.in_sample  sY       '&&u-##E*&||EFFr   r%   r&   c                 L   | j                  |      }| j                  |||      }| j                  |       t        |t        j
                  t        j                  f      sJ | j                  |      }| j                  |      }t	        j                  ||| j                        S r  )r;   r]   r	  r5   r6   rL   rJ   r  r   ro   r   )r   r%   r   r&   r   r  r   s          r   r(   zCalendarFourier.out_of_sample  s       '((~F{++(8(8"..'IJJJ##K0&||EdmmLLr   .c                 F    | j                   j                  | j                  fS r,   r   r   r{   r   s    r   r0   zCalendarFourier._eq_attr  s    zz!!4;;..r   c                 P    d| j                   j                   d| j                   dS )NzFourier(freq=r   r   r  r   s    r   r*   zCalendarFourier.__str__  s&    tzz112(4;;-qIIr   r,   )r.   rj   rk   rl   rq   rp   r|   rm   r   r   r   r   r#   r
   r   r   r6   r7   ro   r(   r	   rr   r0   r*   r   r   s   @r   r  r  h  s=   .`8S 8 8 8
 $s)   ))112G8H-rxx78G	G 3G --556
 8<	MM Xh'12M !(!34	M
 
M 7M /%#. / /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j6                  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                  | j                  j                         D cg c]  }t	        |j                                c}  t        | j                  j                               }t        |dt        |      d      }t        |d|d      }|| j                  |   vrt        d| d| d      t        | )  |       || _        | j                  j                  j                  d      d	   | _        y c c}w )
NrA   F)optionslowerr   zThe combination of freq=z and period=z is not supported.-r   )setupdate
_supportedvaluesr   keysrr   r   rI   r   r|   r   r   r   split	_freq_str)r   rA   r   freq_optionsvalperiod_optionsr   s         r   r|   zCalendarSeasonality.__init__  s    !$*.//*@*@*BC3d388:C	
 t3356&%"5U
 HnE
 tv..*4& 1 !35  	++11#6q9# Ds    D	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                    | j                   j                  dv r|j                  d|j                  z  z   S | j                   j                  dk(  r|j                  S t	        j
                  dd      j                  j                         }|j                  }|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ZZ3&??"NN:r:DDKKME//C88E?&&(   Jr   c                     |j                   S r,   )r=  r"   s     r   _daily_to_locz!CalendarSeasonality._daily_to_loc3  s     zz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                 n    | j                   j                  dv r|j                  dz
  S |j                  dz
  S )N)r!  r(  r   r>   )r   r   rH  quarterr"   s     r   _annual_to_locz"CalendarSeasonality._annual_to_loc=  s4     ::!22;;?"==1$$r   c                    | j                   dk(  r| j                  |      }nR| j                   dk(  r| j                  |      }n1| j                   dv r| j                  |      }n| j	                  |      }| j
                  | j                      | j                     }t        j                  |j                  d   |f      }d|t        j                  |j                  d         |f<   |S )Nr  r%  )r$  r)  r   r>   )r   rF  rD  rI  rL  r1  r5  rR   r   rH   rU   )r   r   r   
full_cycler   s        r   r   zCalendarSeasonality._get_termsE  s     <<3%%e,D\\S &&u-D\\[())%0D&&u-D__T\\24>>B
$**Q-4501bii

1&,-r   c           
          g }| j                   | j                     | j                     }t        |      D ]4  }|j	                  d| j                   d|dz    d| j                   d       6 |S )Nr   =r>   z	, period=r   )r1  r   r5  r   r   )r   r   countr   s       r   r   zCalendarSeasonality._columnsU  sm    -dnn=u 	ANNT^^$Aa!eWIdll^1E	 r   c                     | j                  |      }| j                  |      }| j                  |      }t        j                  ||| j
                        S r  )r;   r	  r   r6   ro   r   )r   r   r   s      r   r#   zCalendarSeasonality.in_sample_  sI       '&&u-&||EFFr   r%   r&   c                 *   | j                  |      }| j                  |||      }| j                  |       t        |t        j
                  t        j                  f      sJ | j                  |      }t	        j                  ||| j                        S r  )
r;   r]   r	  r5   r6   rL   rJ   r   ro   r   )r   r%   r   r&   r   r   s         r   r(   z!CalendarSeasonality.out_of_samplei  sz       '((~F{++(8(8"..'IJJJ,||EdmmLLr   .c                 2    | j                   | j                  fS r,   )r   r5  r   s    r   r0   zCalendarSeasonality._eq_attrw  s    ||T^^++r   c                 "    d| j                    dS )NzSeasonal(freq=r   )r5  r   s    r   r*   zCalendarSeasonality.__str__{  s    /q11r   r,   )'r.   rj   rk   rl   r   r   r1  rq   r|   rm   rA   r   r
   r6   rL   rJ   rR   r   rD  rF  rI  rL  r   r   r   r   r   r#   r   r   r7   ro   r(   rp   r	   rr   r0   r*   r   r   s   @r   r  r    s   !F I qvF;#"$,

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

:S :# :$ :, "c " "   2++R^^;<	"2++R^^;<	
%2++R^^;<%	%
%2++R^^;<%	%2++R^^;<	  $s)   ))112G8H-rxx78G	G 3G --556
 8<	MM Xh'12M !(!34	M
 
M 7M ,%#. , ,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j0                  j                        deee   ej6                  f   dej(                  fd       Z eej8                  j                        	 ddedeee   ej6                  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)
    Nbase_periodrA   rw   rx   rY  r   c                   t         |   |       t        j                  | ||       d| _        |6t	        j
                  |d| j                        }|j                  d   | _        |d | _	        y t        |      | _	        y )Nr   r   r>   r?   )
r   r|   rv   _ref_i8r6   rK   r   asi8rq   _base_period)r   rA   rw   rx   rY  prr   s         r   r|   zCalendarTimeTrend.__init__  sy     	"++85 	, 	
 "adjjIB771:DL$/$7DS=Mr   c                     | j                   S )zThe base period)r]  r   s    r   rY  zCalendarTimeTrend.base_period  s        r   r   c                 Z    |j                  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>   rX  r   )r   rA   r   rY  rw   rx   s         r   r   zCalendarTimeTrend.from_string  sC    F ##C(5=EE\E45kBBr   r   r  c                 d   t        |t        j                        r|j                  | j                        }|j
                  }|| j                  z
  dz   }|j                  t        j                        |z   }|d d d f   }| j                  |      }t        j                  || j                  |      S )Nr>   r   )r5   r6   rL   r   r   r\  r[  r   rR   r   r   ro   r   )r   r   r  index_i8timer   s         r   _termszCalendarTimeTrend._terms  s     eR--.OODJJ/E::dll*Q.ryy)E1AtG}%||E4==FFr   c                     | j                  |      }| j                  |      }| j                  |      }| j                  ||      S r,   )r;   r	  r  rd  )r   r   r  s      r   r#   zCalendarTimeTrend.in_sample  sE       '&&u-##E*{{5%((r   r%   r&   c                 
   | j                  |      }| j                  |||      }| j                  |       t        |t        j
                  t        j                  f      sJ | j                  |      }| j                  ||      S r,   )	r;   r]   r	  r5   r6   rJ   rL   r  rd  )r   r%   r   r&   r   r  s         r   r(   zCalendarTimeTrend.out_of_sample  sv       '((~F{++8H8H'IJJJ##K0{{;..r   .c                     | j                   | j                  | j                  j                  f}| j                  || j                  fz  }|S r,   )rz   r{   r   r   r]  )r   attrs     r   r0   zCalendarTimeTrend._eq_attr  sL     NNKKJJ&

 (T&&((Dr   c                     t         j                  |       }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=)rv   r*   r   r   r]  )r   values     r   r*   zCalendarTimeTrend.__str__&  sl    *2248U3BZ'GDJJ4F4F3Gq*II(#2J<0A0A/B!!DDEr   r   r,   )!r.   rj   rk   rl   rq   rn   rp   r	   r
   DateLiker|   rm   rY  r   r   r6   rL   rJ   rR   r   ro   rd  r   r   r#   r   r   r7   r(   rr   r0   r*   r   r   s   @r   rW  rW    s   3p 	N 7;NN N 	N eCM23N 
N$ !Xc] ! ! 
 7;	(C(C (C eCM23	(C
 
(C (CTG2++R^^;<GEGZZG	G ))112)8H-rxx78)	) 3) --556
 8<	// Xh'12/ !(!34	/
 
/ 7/ %#.   r   rW  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j0                  j                        	 d%dede	eee   ej                  f      dej&                  fd       Zdej2                  deej4                  ej6                  f   fdZdededej&                  fdZdej2                  dej2                  dej&                  fdZded edej2                  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%y)'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   rw   rx   seasonalfourieradditional_termsdropr   r   rw   rx   rp  rq  rr  rs  c                   t        |t        j                        st        j                  |      }|| _        g | _        d| _        d | _        | j                          t        |dd      }t        |d      x| _
        }t        |d      | _        t        |d      x| _        }t        |d      | _        t        |      }d | _        t        |d	      | _        || _        |s|r%| j                  j'                  t)        ||             |r|rt+        d
      |s|r ||t-        | j                        x| _        }|r1t        |d      }| j                  j'                  t1        |             n8|r6t        |d      }|J | j                  j'                  t3        ||             |D ]Q  }	t        |	t4              st7        d      |	| j                  vr| j                  j'                  |	       Ht+        d       || _        d | _        y )NFr   T)optionalrw   rx   rp  rq  rs  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.)rx   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   rz   r   r{   	_seasonal_fourierrr   _cached_in_sample_drop_additional_termsr   r   rI   r   r   r   r   r   r9   _retain_cols)
r   r   r   rw   rx   rp  rq  rr  rs  r   s
             r   r|   zDeterministicProcess.__init__  s    %*HHUOE=?! FHt<$-h
$CC'w7$-h
$CC)'9= !12!%tV,
!1u%%,,Yx-GHH 
 V^~(6t7G7G(HHv&vx8F%%,,[-@A1F%%%%%,,WV7-KL$ 	Dd$56+  4444))006 ! 	 6:r   r   c                     | j                   S )zThe index of the process)rv  r   s    r   r   zDeterministicProcess.index  r   r   c                     | j                   S )z/The deterministic terms included in the process)rw  r   s    r   r   zDeterministicProcess.terms  s     (((r   r   c                    d }| j                   D ])  }t        |t        t        f      s|xs |j                  }+ |Pd}|D ]I  }||j
                  d   k(  j                         |j
                  d   dk7  z  }|xs |j                         }K |}t        | j                         D ]6  \  }}|j                  }|r|r||   j
                  d d dd f   ||<   |xs |}8 |S )NFr   r>   )
rw  r5   r   rW  rw   ilocrS   anyr   r   )	r   r   	has_constdtermr   	const_col
drop_firstr   r   s	            r   _adjust_dummiesz$DeterministicProcess._adjust_dummies  s    $(	.. 	8E%)->!?@%7		8 I 9!TYYq\1668DIIaLA<MN	%8	9 
!$";";< 	0HAu~~HJ 8==AB/a#/xJ	0 r   c                 T   t        j                  |dk(  d      }t        j                  |      r|j                  d d | f   }|j	                  d      |j                  d      k(  }t        j                  |      dkD  r'||j                         z  }|j                  d d | f   }|S )Nr   axisr>   )rR   rS   r  rC  maxminsum
duplicated)r   r   all_zerois_constantsurplus_constss        r   _remove_zeros_onesz'DeterministicProcess._remove_zeros_ones  s    66%1*1-66(IIa(l+EiiQi'599!9+<<66+"(;+A+A+CCNIIa.01Er   c                    | j                   | j                   S | j                  }| j                  s9t        j                  t        j                  |j                  d   df      |      S g }| j                  D ]"  }|j                  |j                  |             $ | j                  |      }t        j                  |d      }| j                  |      }| j                  rot        |      }t        |dd      }|d   }|d   }t        j                   t        j"                  |            }	|	d   |j                  d   z  t        j$                  t&              j(                  z  }
t+        t        j,                  |	|
kD              }|j.                  |z  }dg}d}t1        d|j                  d         D ]O  }t
        j2                  j5                  |d |dz   d |dz   f         }||kD  r|j                  |       |}||k(  sO n t7        |      |k(  r|j8                  d d |f   }n)|j8                  d d t        j:                  |d |       f   }|j<                  | _        || _         |S )	Nr   r:   r>   r  rT)modepivotingr=   ) r}  rv  rw  r6   ro   rR   r   rH   r   r#   r  concatr  r~  r   r   absdiagfinfor   epsrp   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  s!   !!-)))((<<%++a.!)< =UKK	-- 	4DT^^E23	4 ((3	 ii	:''.:: IYS48CAABAvvbggaj)H1+	 22RXXe_5H5HHCrvvhn-.D##	/C3DI1iooa01 II11#gAgwQw6F2GH	y(KKN )I$ 4yD 

1d7+

1bggah&7#78!MM!&r   r%   r&   c                 ,   t        |d      }| j                  r| j                  | j                          | j                  }| j
                  s9t        j                  t        j                  |j                  d   df      |      S g }| j
                  D ]$  }|j                  |j                  |||             & t        j                  |d      }| j                  J |j                  d   t        | j                        k7  r|| j                     }|S )Nr%   r   r:   r>   r  )r   r~  r  r#   rv  rw  r6   ro   rR   r   rH   r   r(   r  rQ   )r   r%   r&   r   r  r   r   s          r   r(   z"DeterministicProcess.out_of_sample  s     "%1::$++3NN((<<%++a.!)< =UKK	-- 	ODT//unMN	O ii	:  ,,,;;q>S!2!2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   )rv  r5   r6   rJ   rK   rA   rM   ry  )r   rO   r   s      r   _extend_time_indexz'DeterministicProcess._extend_time_index1  sS     eR^^,??58EJJGG}}58D<L<LMMr   rZ   c                    | j                   }t        |      }t        |t        j                        s|sJ ||d   k  rt        t              t        |t        j                        r|j                  }n3t        |      dkD  r#t        j                  |      j                         nd}|dk7  r||d   z
  |z  dk7  rt        d| d      |r*t        j                  t        j                  ||            }nt        j                  |||      }|d   | j                   d   k  r!| j                         }|j                  |   }|S |d   | j                   d   kD  rw|d   |z   }|d   |k7  rGt        j                  |||      }	| j!                  |	j"                  d   |	      }
|
j                  |   S | j!                  |j"                  d   |      S || j                   d   k  }||   }||    }| j                         j                  |   }| 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  )rv  r   r5   r6   rN   rI   START_BEFORE_INDEX_ERRrF   rQ   rR   rT   r  r7   rU   r#   rC  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:  s$   %e,%/>AA58344eR]]+zzH/25zA~rwwu~))+1Hq=uuQx/8;A>xj I* * 
 hhryy56GmmE4h?G2;$++b/)(I!g.IQZ$++b/)rX-JqzZ'mmJ8D((1c(Jwww''%%gmmA&6w%OO4;;r?2.#]N3)--m<%%#))!,=N & 
 yy.(3!<<r   c                    | j                   }t        | j                   t        j                        rlt        |t        j                        r|j                  | j                        }t        |t        j                        r|j                  | j                        }||d   k  rt        t              || j                   d   k  r| j                         j                  || S | j                  |      }|||d   kD     }| j                  |j                  d   |      }||d   k\  r|j                  || S t        j                  | j                         |gd      }|j                  || S )N)rA   r   r=   r  )rv  r5   r6   rJ   	Timestampr   ry  rI   r  r#   rC  r  r(   rH   r  )r   rZ   rO   r   r  oos_idxr  boths           r   _range_from_time_indexz+DeterministicProcess._range_from_time_indexe  s1    dkk2>>2%.T-=-=>$-~~4+;+;~<583444;;r?">>#''d33))$/'E"I-.  q!17;GAJ775&&yy$..*C0q9xxd##r   rk  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
                        r8t	        j                  |d   | j                  |      }|d   j                         S t	        j                  |d   | j                  |      }|d   S )Nr   z must be non-negative.r>   r=   rC   )
rI   rv  rH   r5   r6   rJ   rK   ry  r   rM   )r   rk  r1   add_periodsr   r^  drs          r   _int_to_timestampz&DeterministicProcess._int_to_timestampz  s    19v%;<==4;;$$Q'';;u%%t{{003a781<dkk2>>2b	 0 0+B b6&&((]]"ID,,k
 "vr   c                 H   | j                   st        d      t        | j                        t        j
                  fv st        | j                        r/t        |d      }t        |d      }|dz  }| j                  ||      S t        |t        t        j                  f      r| j                  |d      }nt	        j                  |      }t        |t        t        j                  f      r| j                  |d      }nt	        j                  |      }| 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>   )rx  r9   r-   rv  r6   rN   r   r   r  r5   rp   rR   integerr  r  r  )r   rZ   rO   s      r   r   zDeterministicProcess.range  s    *    00L4M%eW5E$T62DAID//t<<ec2::./**5':ELL'EdS"**-.))$7D<<%D**5$77r   c                    t        | j                  t        j                        r#| j                  j                  | _        d| _        y t        | j                  t        j                        rG| j                  j                  xs | j                  j                  | _        | j
                  d u| _        y t        | j                  t        j                        rd| _        y t        | j                        rO| j                  d   dk(  xr5 t        j                  t        j                  | j                        dk(        | _        y y )NTr   r>   )r5   rv  r6   rJ   rA   ry  rx  rL   r   rN   r   rR   rS   rT   r   s    r   rz  z$DeterministicProcess._validate_index  s    dkk2>>2#{{//D#DR%5%56#{{//L4;;3L3LD#//t;DR]]3#D$++&#{{1~2  rvv$)8D 'r   c           
          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
        ro  )rn  r   rz   r{   r{  r|  r  r~  r"   s     r   applyzDeterministicProcess.apply  sG     $<<^^++^^MM!33	
 		
r   r,   )r   N)&r.   rj   rk   rl   r
   r   r   r6   r7   r	   r   rp   rn   r   r|   rm   r   r   r   ro   r  r  r   r#   r(   r  rL   rJ   r  r  r  rq   r  IntLikerl  r   rz  r  r!   r   r   rn  rn  .  s   [B /38:=;Xh'12=; ucz*+	=;
 =; =; =; =; ##45=; =;~ rxx   )t-. ) )T",,%7 D<N &	 	 	 ))112&2<< & 3&P --556 IM !x'9288'C!DE 
	 7(NllN 
r/	0N)=S )= )= )=V$\\$)+$	$*s # ",, "+8Wh+,+8 GXs*++8 
	+8Z
r   rn  )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  
datetime64rl  rp   r  r  r  r   rv   r   r   r   r   r   r  r  rW  rn  r!   r   r   <module>r     s%    $  " .    ,  4bllBMM9:
RZZ
  K K\/)!2C /)dW+* W+tCL# CLL0# (YF& YFx1 13 1h]J/1I ]J@t23 t2nl13M l^p
 p
r   