
    >h                        S SK JrJrJrJr  S SKJrJr  S SKr	S SK
JrJr  S SKJrJr  S SKrS SKrS SKJr  S SKJr  S SKJrJrJrJr  S S	KJr  \\	R                  \R>                  \R@                  4   r!\\"\RF                  4   r$S
r% " S S\5      r& " S S\&\5      r' " S S\'5      r( " S S\&5      r) " S S\&\5      r* " S S\*5      r+ " S S\&\5      r, " S S\,\*5      r- " S S\,5      r. " S S\,\'5      r/ " S S 5      r0g)!    )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            
          \ rS rSrSrSr\S\4S j5       r\	S\
\   S\R                  4S j5       r\	 SS	\S\
\   S
\\
\      S\R                  4S jj5       r\	S\4S j5       rS\4S jr\\	S\\S4   4S j5       5       r\S\
\   S\R0                  4S j5       r\ SS\R0                  S	\S
\\
\      S\R0                  4S jj5       rS\4S jrS\S\4S jrSrg)DeterministicTerm$   z/Abstract Base Class for all Deterministic TermsFreturnc                     U R                   $ )z?Flag indicating whether the values produced are dummy variables)	_is_dummyselfs    pC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/tsa/deterministic.pyis_dummyDeterministicTerm.is_dummy*        ~~    indexc                     g)z
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_sampleDeterministicTerm.in_sample/       r    Nstepsforecast_indexc                     g)a  
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_sampleDeterministicTerm.out_of_sample@   r'   r    c                     g)z.A meaningful string representation of the termNr#   r   s    r   __str__DeterministicTerm.__str__[   r'   r    c                 \    [        U 5      R                  4n[        XR                  -   5      $ N)type__name__hash_eq_attr)r   names     r   __hash__DeterministicTerm.__hash___   s&    &*4j&9&9%;D==())r    .c                     g)z9tuple of attributes that are used for equality comparisonNr#   r   s    r   r5   DeterministicTerm._eq_attrc   r'   r    c                     [        U [        R                  5      (       a  U $  [        R                  " U 5      $ ! [         a    [	        S5      ef = f)Nz*index must be a pandas Index or index-like)
isinstancepdIndex	Exception	TypeErrorr!   s    r   _index_likeDeterministicTerm._index_likeh   sI    eRXX&&L	J88E?" 	JHII	Js	   9 Ac                 $   Ubj  [         R                  U5      n[        U[        R                  5      (       d   eUR
                  S   U:w  a  [        SUR
                  S    SU S35      eU$ [        U [        R                  5      (       a%  [        R                  " U S   S-   XR                  S9$ [        U [        R                  5      (       aR  U R                  bE  [        R                  " U S   U R                  S	S
9S   n[        R                  " X0R                  US
9$ [        U [        R                  5      (       aV  [        U [        R                  5      (       d   e U R                  nU R                  nXTU-  -   n[        R                  " XVUS9$ [#        U 5      (       an  [$        R&                  " [$        R(                  " U 5      S:H  5      (       a<  [$        R*                  " U S   S-   U S   U-   S-   5      n[        R                  " U5      $ SSKnUR/                  S[0        S	S9  U R
                  S   n	[        R                  " U	S-   X-   S-   5      $ ! [         a(    [!        U 5      S:  a  U S   U S   -
  OSnU S   U-   n GNf = f)zExtend the forecast indexNr   z(The number of values in forecast_index (z) must match steps (z).   periodsfreq   rI   rH   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   rB   r<   r=   r>   shape
ValueErrorPeriodIndexperiod_rangerI   DatetimeIndex
date_range
RangeIndexrN   stopAttributeErrorlenr   npalldiffarangewarningswarnUserWarning)
r!   r(   r)   next_obsrN   startrW   idx_arrr^   nobss
             r   _extend_indexDeterministicTerm._extend_indexq   sE    %.::>JNnbhh7777##A&%/ &,,Q/00DUG2O  "!eR^^,,??b	Au::  r//00UZZ5K}}U2YUZZKANH==

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


 %<'D==488%  RVVBGGENa,?%@%@iib	AuRy5/@1/DEG88G$$"  	 	
 {{1~}}TAXt|a'788+ " )03E
QuRy59,Ab	D()s   $I .JJc                 B    U R                  5       S[        U 5      S 3-   $ )Nz at 0x0x)r.   idr   s    r   __repr__DeterministicTerm.__repr__   s     ||~&D" 666r    otherc                     [        U[        U 5      5      (       a\  U R                  nUR                  n[        U5      [        U5      :w  a  g[	        [        X#5       VVs/ s H	  u  pEXE:H  PM     snn5      $ gs  snnf )NF)r<   r2   r5   rY   r[   zip)r   rl   own_attroth_attrabs         r   __eq__DeterministicTerm.__eq__   sf    eT$Z((}}H~~H8}H-3x+BC+B41+BCDD Ds   A8
r#   r1   )r3   
__module____qualname____firstlineno____doc__r   propertyboolr   r   r   r   r=   	DataFramer%   intr	   r+   strr.   r7   tupler5   staticmethodr>   rB   re   rj   objectrs   __static_attributes__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                       \ rS rSrSrSS\S\SS4S jjr\S\4S j5       r	\S\4S	 j5       r
\S\\   4S
 j5       rS\R                  S\R                  4S jrS\4S jrSrg)TimeTrendDeterministicTerm   z:Abstract Base Class for all Time Trend Deterministic Termsconstantorderr   Nc                 H    [        US5      U l        [        US5      U l        g )Nr   r   )r   	_constantr   _order)r   r   r   s      r   __init__#TimeTrendDeterministicTerm.__init__   s    "8Z8'w7r    c                     U R                   $ )z+Flag indicating that a constant is included)r   r   s    r   r   #TimeTrendDeterministicTerm.constant   r   r    c                     U R                   $ )zOrder of the time trendr   r   s    r   r    TimeTrendDeterministicTerm.order        {{r    c                     / nSSSS.nU R                   (       a  UR                  S5        [        SU R                  S-   5       H1  nX2;   a  UR                  X#   5        M  UR                  SU 35        M3     U$ )Ntrendtrend_squaredtrend_cubed)rF   rJ      constrF   ztrend**)r   appendranger   )r   columnstrend_namespowers       r   _columns#TimeTrendDeterministicTerm._columns   sn    !o-H>>NN7#1dkkAo.E#{1201	 /
 r    locsc                 6   [        U R                  5      U R                  -   n[        R                  " USU45      n[        R
                  " SU4[         S9n[        R                  " SU R                  S-   5      US[        U R                  5      S 24'   X4-  nU$ )NrF   dtyper   )r|   r   r   rZ   tilezerosr]   )r   r   ntermstermsr   s        r   
_get_terms%TimeTrendDeterministicTerm._get_terms   s{    T^^$t{{2q&k*!VC0*,))At{{Q*GaT^^$&&'r    c                     / nU R                   (       a  UR                  S5        U R                  (       a!  UR                  SU R                  S-    35        U(       d  S/nSR                  U5      nSU S3$ )NConstantzPowers 1 to rF   Empty,z
TimeTrend())r   r   r   join)r   r   	terms_strs      r   r.   "TimeTrendDeterministicTerm.__str__   sc    >>LL$;;LL<a'89:IEHHUO	I;a((r    r   r   Tr   )r3   ru   rv   rw   rx   rz   r|   r   ry   r   r   listr}   r   rZ   ndarrayr   r.   r   r#   r    r   r   r      s    D8 8S 8 8 $   s   
$s) 
 
rzz bjj 	) 	)r    r   c            
         ^  \ rS rSrSrSS\S\SS4U 4S jjjr\S\	SS 4S	 j5       r
\" \R                  R                  5      S
\\\   \R$                  4   S\R&                  4S j5       r\" \R(                  R                  5       SS\S
\\\   \R$                  4   S\\\      S\R&                  4S jj5       r\S\\S4   4S j5       rSrU =r$ )	TimeTrend   a  
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)
r   r   r   Nc                 $   > [         TU ]  X5        g r1   )superr   )r   r   r   	__class__s      r   r   TimeTrend.__init__   s    )r    r   c                 V    UR                  S5      nSnSU;   a  SnOSU;   a  SnU " X#S9$ )a  
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   ttrJ   trF   r   r   
startswith)clsr   r   r   s       r   from_stringTimeTrend.from_string  s<    . ##C(5=EE\EH22r    r!   c                     U R                  U5      nUR                  S   n[        R                  " SUS-   [        R                  S9S S 2S 4   nU R                  U5      n[        R                  " X@R                  US9$ Nr   rF   r   r   r!   )	rB   rP   rZ   r]   doubler   r=   r{   r   )r   r!   rd   r   r   s        r   r%   TimeTrend.in_sample!  sg       '{{1~yyD1HBII6q$w?%||E==FFr    r(   r)   c                 ,   U R                  U5      nUR                  S   nU R                  X!U5      n[        R                  " US-   XA-   S-   [        R
                  S9S S 2S 4   nU R                  U5      n[        R                  " XpR                  US9$ r   )
rB   rP   re   rZ   r]   r   r   r=   r{   r   )r   r(   r!   r)   rd   fcast_indexr   r   s           r   r+   TimeTrend.out_of_sample+  s       '{{1~((~Fyy4<!#3299EagN%||E==LLr    .c                 2    U R                   U R                  4$ r1   r   r   s    r   r5   TimeTrend._eq_attr9  s    ~~t{{**r    r#   r   r1   )r3   ru   rv   rw   rx   rz   r|   r   classmethodr}   r   r   r   r%   r
   r   r   r=   r>   r{   r+   r	   ry   r~   r5   r   __classcell__r   s   @r   r   r      s6   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            
       4   \ rS rSrSrSrSS\S\SS4S jjr\S\4S	 j5       r	\S\4S
 j5       r
\S\\\   \R                   \R"                  4   SS 4S j5       r\S\\S4   4S j5       rS\4S jr\S\\   4S j5       r\" \R6                  R                  5      S\\\   \R8                  4   S\R:                  4S j5       r\" \R<                  R                  5       SS\S\\\   \R8                  4   S\\\      S\R:                  4S jj5       rSr g)Seasonalityi>  a  
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                     [        US5      U l        [        US5      U l        US:  a  [        S5      eSU R                  s=::  a  U::  d  O  [        S5      eg )Nr   r   rJ   zperiod must be >= 2rF   z-initial_period must be in {1, 2, ..., period})r   _period_initial_periodrQ   )r   r   r   s      r   r   Seasonality.__init__c  s\    (:0, 
 A:233D((2F2LMM 3r    c                     U R                   $ )zThe period of the seasonalityr   r   s    r   r   Seasonality.periodm       ||r    c                     U R                   $ )z+The seasonal index of the first observation)r   r   s    r   r   Seasonality.initial_periodr  s     ###r    r!   c                 b   U R                  U5      n[        U[        R                  5      (       a  UR                  nOT[        U[        R
                  5      (       a*  UR                  (       a  UR                  OUR                  nO[        S5      eUc  [        S5      e[        U5      nU " US9$ )z
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   )
rB   r<   r=   rR   rI   rT   inferred_freqr@   rQ   r   )r   r!   rI   r   s       r   
from_indexSeasonality.from_indexw  s    " &eR^^,,::Dr//00!&5::1D1DDJKK<JKK%&!!r    .c                 2    U R                   U R                  4$ r1   )r   r   r   s    r   r5   Seasonality._eq_attr  s    ||T1111r    c                 "    SU R                    S3$ )NzSeasonality(period=r   r   r   s    r   r.   Seasonality.__str__  s    $T\\N!44r    c                 ~    U R                   n/ n[        SUS-   5       H  nUR                  SU SU S35        M     U$ )NrF   s(r   r   )r   r   r   )r   r   r   is       r   r   Seasonality._columns  sE    q&1*%ANNRs!F81-. &r    c                 (   U R                  U5      nUR                  S   nU R                  n[        R                  " X#45      nU R
                  S-
  n[        U5       H  nXe-   U-  nSXFS U2U4'   M     [        R                  " X@R                  US9$ Nr   rF   r   )
rB   rP   r   rZ   r   r   r   r=   r{   r   )r   r!   rd   r   termoffsetr   cols           r   r%   Seasonality.in_sample  s       '{{1~xx'%%)vA:'C#$DFC   ||D--uEEr    r(   r)   c                 R   U R                  U5      nU R                  X!U5      nUR                  S   nU R                  n[        R
                  " X45      nU R                  S-
  n[        U5       H  n	XX-   U	-   U-  n
SXyS U2U
4'   M     [        R                  " XpR                  US9$ r   )rB   re   rP   r   rZ   r   r   r   r=   r{   r   )r   r(   r!   r)   r   rd   r   r   r   r   col_locs              r   r+   Seasonality.out_of_sample  s       '((~F{{1~xx(%%)vA}q(F2G'(DFG#$  ||D--{KKr    )r   r   )rF   r1   )!r3   ru   rv   rw   rx   r   r|   r   ry   r   r   r   r
   r   r   r=   rT   rR   r   r~   r5   r}   r.   r   r   r   r   r%   r>   r{   r+   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                   |    \ rS rSrSrS\SS4S jr\S\4S j5       rS\	R                  S\	R                  4S	 jrS
rg)FourierDeterministicTermi  z7Abstract Base Class for all Fourier Deterministic Termsr   r   Nc                 &    [        US5      U l        g Nr   )r   r   )r   r   s     r   r   !FourierDeterministicTerm.__init__  s    'w7r    c                     U R                   $ )z'The order of the Fourier terms includedr   r   s    r   r   FourierDeterministicTerm.order  r   r    r   c                    S[         R                  -  UR                  [         R                  5      -  n[         R                  " UR
                  S   SU R                  -  45      n[        U R                  5       HP  n[        [         R                  [         R                  45       H   u  pEU" US-   U-  5      US S 2SU-  U-   4'   M"     MR     U$ )NrJ   r   rF   )rZ   piastyper   emptyrP   r   r   	enumeratesincos)r   r   r   r   jfuncs         r   r   #FourierDeterministicTerm._get_terms  s    255y4;;ryy11$**Q-T[[9:t{{#A$bffbff%56&*AET>&:aQl# 7 $ r    r   )r3   ru   rv   rw   rx   r|   r   ry   r   rZ   r   r   r   r#   r    r   r   r     sN    A8c 8d 8 s  rzz bjj r    r   c            
         ^  \ rS rSrSrSrS\S\4U 4S jjr\	S\4S j5       r
\	S\\   4S	 j5       r\" \R                   R                  5      S
\\\   \R*                  4   S\R,                  4S j5       r\" \R.                  R                  5       SS\S
\\\   \R*                  4   S\\\      S\R,                  4S jj5       r\	S\\S4   4S j5       rS\4S jrSrU =r$ )Fourieri  a%  
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U ]  U5        [        US5      U l        SU R                  -  U R                  :  a  [        S5      eg )Nr   rJ   z2 * order must be <= period)r   r   r   r   r   rQ   )r   r   r   r   s      r   r   Fourier.__init__  sC    !&(3t{{?T\\):;; *r    r   c                     U R                   $ )zThe period of the Fourier termsr   r   s    r   r   Fourier.period  r   r    c           
          U R                   n[        U5      R                  5       n/ n[        SU R                  S-   5       H&  nS H  nUR                  U SU SU S35        M     M(     U$ )NrF   r  r  (r   r   )r   r   stripr   r   r   )r   r   
fmt_periodr   r   typs         r   r   Fourier._columns  sk    F^))+
q$++/*A%#as!J<q9: & + r    r!   c                     U R                  U5      nUR                  S   nU R                  [        R                  " U5      U R
                  -  5      n[        R                  " X1U R                  S9$ Nr   r!   r   )	rB   rP   r   rZ   r]   r   r=   r{   r   )r   r!   rd   r   s       r   r%   Fourier.in_sample  sU       '{{1~		$$,, >?||EFFr    r(   r)   c                    U R                  U5      nU R                  X!U5      nUR                  S   nU R                  [        R
                  " XUU-   5      U R                  -  5      n[        R                  " XdU R                  S9$ r  )
rB   re   rP   r   rZ   r]   r   r=   r{   r   )r   r(   r!   r)   r   rd   r   s          r   r+   Fourier.out_of_sample  sl       '((~F{{1~		$u = LM||EdmmLLr    .c                 2    U R                   U R                  4$ r1   r   r   r   s    r   r5   Fourier._eq_attr,  s    ||T[[((r    c                 <    SU R                    SU R                   S3$ )NzFourier(period=, order=r   r  r   s    r   r.   Fourier.__str__0  s     ht{{m1EEr    r   r1   )r3   ru   rv   rw   rx   r   floatr|   r   ry   r   r   r}   r   r   r   r%   r
   r   r   r=   r>   r{   r+   r	   r~   r5   r.   r   r   r   s   @r   r  r    s^   %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 Fr    r  c            	       <   \ rS rSrSrS\SS4S jr\S\4S j5       rS\	\
R                  \
R                  4   S\R                  4S	 jr\
R                  \
R                  44S\
R                   S
\	\\\S4   4   S\	\
R                  \
R                  4   4S jjrSrg)CalendarDeterministicTermi4  z4Abstract Base Class for calendar deterministic termsrI   r   Nc                      [         R                  " SUSS9nUR                  U l        g ! [         a    [	        S5      ef = f)Nz
2020-01-01rF   rK   z freq is not understood by pandas)r=   rU   rI   _freqrQ   )r   rI   r!   s      r   r   "CalendarDeterministicTerm.__init__7  sB    	AMM,T1EEDJ 	A?@@	As	   '* A c                 .    U R                   R                  $ z(The frequency of the deterministic termsr$  freqstrr   s    r   rI   CalendarDeterministicTerm.freq>       zz!!!r    r!   c                 b   [        U[        R                  5      (       a  UR                  5       nXR	                  U R
                  5      R                  5       -
  nUR	                  U R
                  5      nUS-   R                  5       UR                  5       -
  n[        U5      [        U5      -  $ )NrF   )r<   r=   rR   to_timestamp	to_periodr$  r   )r   r!   deltar   gaps        r   _compute_ratio(CalendarDeterministicTerm._compute_ratioC  s     eR^^,,&&(E

3@@BB__TZZ(Av##%(99#..r    allowed.c                    [        U[        5      (       a  U4n[        X5      (       d  [        U5      S:X  a  SUS   R                  -   nODSR	                  S US S  5       5      n[        U5      S:  a  US-  nUS	US   R                  -   -  n[        U 5      R                   S
U 3n[        U5      e[        U[        R                  [        R                  45      (       d   eU$ )NrF   za r   z, c              3   8   #    U  H  oR                   v   M     g 7fr1   )r3   ).0rq   s     r   	<genexpr>>CalendarDeterministicTerm._check_index_type.<locals>.<genexpr>[  s     )Kl**ls   rE   rJ   r   z and z! terms can only be computed from )	r<   r2   rY   r3   r   r@   r=   rT   rR   )r   r!   r3  allowed_typesmsgs        r   _check_index_type+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Dr    )r$  )r3   ru   rv   rw   rx   r}   r   ry   rI   r
   r=   rT   rR   rZ   r   r1  r>   r2   r~   r;  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            
         ^  \ rS rSrSrS\S\SS4U 4S jjr\S\	\   4S j5       r
\" \R                  R                  5      S	\\\   \R$                  4   S\R&                  4S
 j5       r\" \R(                  R                  5       SS\S	\\\   \R$                  4   S\\\      S\R&                  4S jj5       r\S\\S4   4S j5       rS\4S jrSrU =r$ )CalendarFourierih  a  
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)
rI   r   r   Nc                 p   > [         TU ]  U5        [        R                  X5        [        US5      U l        g r   )r   r   r   r   r   )r   rI   r   r   s      r   r   CalendarFourier.__init__  s,     ))$6'w7r    c           
          / n[        SU R                  S-   5       H:  nS H1  nUR                  U SU SU R                  R                   S35        M3     M<     U$ )NrF   r  r  z,freq=r   )r   r   r   r$  r)  )r   r   r   r  s       r   r   CalendarFourier._columns  s[    q$++/*A%#as&1C1C0DAFG & + r    r!   c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R
                  " X1U R                  S9$ Nr  )rB   r;  r1  r   r=   r{   r   )r   r!   ratior   s       r   r%   CalendarFourier.in_sample  sW       '&&u-##E*&||EFFr    r(   r)   c                 N   U R                  U5      nU R                  X!U5      nU R                  U5        [        U[        R
                  [        R                  45      (       d   eU R                  U5      nU R                  U5      n[        R                  " XdU R                  S9$ rD  )rB   re   r;  r<   r=   rT   rR   r1  r   r{   r   )r   r(   r!   r)   r   rE  r   s          r   r+   CalendarFourier.out_of_sample  s       '((~F{++(8(8"..'IJJJJ##K0&||EdmmLLr    .c                 F    U R                   R                  U R                  4$ r1   r$  r)  r   r   s    r   r5   CalendarFourier._eq_attr  s    zz!!4;;..r    c                 P    SU R                   R                   SU R                   S3$ )NzFourier(freq=r  r   rJ  r   s    r   r.   CalendarFourier.__str__  s&    tzz112(4;;-qIIr    r   r1   )r3   ru   rv   rw   rx   r}   r|   r   ry   r   r   r   r   r%   r
   r   r   r=   r>   r{   r+   r	   r~   r5   r.   r   r   r   s   @r   r>  r>  h  sC   .`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 Jr    r>  c            
       $  ^  \ rS rSrSrSr\(       a  SSSSS.SSS	.S
S
S.SSS.SSSS.S.rO SSSS.SS0S
S
S.SSSS.SSSS.SS
0SSS.S.rS\S\SS4U 4S jjr	\
S\4S j5       r\
S\4S j5       rS\\R                  \R                   4   S\R$                  4S jrS\\R                  \R                   4   S\R$                  4S  jrS\\R                  \R                   4   S\R$                  4S! jrS\\R                  \R                   4   S\R$                  4S" jrS\\R                  \R                   4   S\R$                  4S# jr\
S\\   4S$ j5       r\" \R8                  R                  5      S\\\   \R>                  4   S\R@                  4S% j5       r\" \RB                  R                  5       S-S&\"S\\\   \R>                  4   S'\#\\      S\R@                  4S( jj5       r!\
S\$\S)4   4S* j5       r%S\4S+ jr&S,r'U =r($ ).CalendarSeasonalityi  aN  
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   rU  rV  r   )MSM      )rY  QrZ  )WrT  r]  AY)rS  rT  rU  rU  )rY  ME)rY  ra  QEra  )ra  rb  )r^  rT  r]  r_  r`  rb  YErI   r   r   Nc           	        > [        5       nUR                  " U R                  R                  5        Vs/ s H  n[	        UR                  5       5      PM     sn6   [        U R                  R                  5       5      n[        US[        U5      SS9n[        USUSS9nXR                  U   ;  a  [        SU SU S35      e[        TU ])  U5        X l        U R                  R                  R                  S5      S	   U l        g s  snf )
NrI   F)optionslowerr   zThe combination of freq=z and period=z is not supported.-r   )setupdate
_supportedvaluesr   keysr~   r   rQ   r   r   r   r$  r)  split	_freq_str)r   rI   r   freq_optionsvalperiod_optionsr   s         r   r   CalendarSeasonality.__init__  s    !$*.//*@*@*BC*B3d388:*BC	
 t3356&%"5U
 HnE
 v..*4& 1 !35  	++11#6q9# Ds   #Dc                 .    U R                   R                  $ r'  r(  r   s    r   rI   CalendarSeasonality.freq  r+  r    c                     U R                   $ )zThe full periodr   r   s    r   r   CalendarSeasonality.period  r   r    r!   c                    U R                   R                  S;   a  UR                  SUR                  -  -   $ U R                   R                  S:X  a  UR                  $ [        R
                  " SSS9R                  R                  5       nUR                  nUR                  U5      R                  5       (       d  [        S5      eU$ )NrX  rW  rT  z2000-1-1
   )rH   z=freq is B but index contains days that are not business days.)
r$  r)  hour	dayofweekr=   bdate_rangeuniqueisinr[   rQ   )r   r!   bdayslocs       r   _weekly_to_loc"CalendarSeasonality._weekly_to_loc"  s     ::+::U__ 444ZZ3&??"NN:r:DDKKME//C88E?&&((   Jr    c                     UR                   $ r1   )ry  r$   s     r   _daily_to_loc!CalendarSeasonality._daily_to_loc3  s     zzr    c                 &    UR                   S-
  S-  $ )NrF   r   )monthr$   s     r   _quarterly_to_loc%CalendarSeasonality._quarterly_to_loc8  s     a1$$r    c                 r    U R                   R                  S;   a  UR                  S-
  $ UR                  S-
  $ )N)rZ  ra  rY  rF   )r$  r)  r  quarterr$   s     r   _annual_to_loc"CalendarSeasonality._annual_to_loc=  s4     ::!22;;?"==1$$r    c                    U R                   S:X  a  U R                  U5      nOUU R                   S:X  a  U R                  U5      nO3U R                   S;   a  U R                  U5      nOU R	                  U5      nU R
                  U R                      U R                     n[        R                  " UR                  S   U45      nSU[        R                  " UR                  S   5      U4'   U$ )NrT  r^  )r]  rb  r   rF   )r   r  r  r  r  rj  rn  rZ   r   rP   r]   )r   r!   r   
full_cycler   s        r   r   CalendarSeasonality._get_termsE  s     <<3%%e,D\\S &&u-D\\[())%0D&&u-D__T\\24>>B
$**Q-4501bii

1&,-r    c           
          / nU R                   U R                     U R                     n[        U5       H5  nUR	                  SU R                   SUS-    SU R                   S35        M7     U$ )Nr   =rF   z	, period=r   )rj  r   rn  r   r   )r   r   countr   s       r   r   CalendarSeasonality._columnsU  sh    -dnn=uANNT^^$Aa!eWIdll^1E  r    c                     U R                  U5      nU R                  U5      nU R                  U5      n[        R                  " X!U R
                  S9$ rD  )rB   r;  r   r=   r{   r   )r   r!   r   s      r   r%   CalendarSeasonality.in_sample_  sG       '&&u-&||EFFr    r(   r)   c                 ,   U R                  U5      nU R                  X!U5      nU R                  U5        [        U[        R
                  [        R                  45      (       d   eU R                  U5      n[        R                  " XTU R                  S9$ rD  )
rB   re   r;  r<   r=   rT   rR   r   r{   r   )r   r(   r!   r)   r   r   s         r   r+   !CalendarSeasonality.out_of_samplei  sy       '((~F{++(8(8"..'IJJJJ,||EdmmLLr    .c                 2    U R                   U R                  4$ r1   )r   rn  r   s    r   r5   CalendarSeasonality._eq_attrw  s    ||T^^++r    c                 "    SU R                    S3$ )NzSeasonal(freq=r   )rn  r   s    r   r.   CalendarSeasonality.__str__{  s    /q11r    )rn  r   r1   ))r3   ru   rv   rw   rx   r   r   rj  r}   r   ry   rI   r   r
   r=   rT   rR   rZ   r   r  r  r  r  r   r   r   r   r   r%   r   r   r>   r{   r+   r|   r	   r~   r5   r.   r   r   r   s   @r   rO  rO    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 2 2r    rO  c                     ^  \ rS rSrSr  SSS.S\S\S\S\\	\\
4      S	S4
U 4S
 jjjjr\S	\\   4S j5       r\ SS\S\S\\	\\
4      S	S 4S jj5       rS\	\R"                  \R$                  4   S\R(                  S	\R*                  4S jr\" \R2                  R                  5      S\	\\   \R8                  4   S	\R*                  4S j5       r\" \R:                  R                  5       SS\S\	\\   \R8                  4   S\\\      S	\R*                  4S jj5       r\S	\\S4   4S j5       rS	\4S jr Sr!U =r"$ )CalendarTimeTrendi  a^  
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_periodrI   r   r   r  r   c                   > [         TU ]  U5        [        R                  XUS9  SU l        Ub4  [        R
                  " USU R                  S9nUR                  S   U l        Uc  S U l	        g [        U5      U l	        g )Nr   r   rF   rG   )
r   r   r   _ref_i8r=   rS   r$  asi8r}   _base_period)r   rI   r   r   r  prr   s         r   r   CalendarTimeTrend.__init__  sw     	"++5 	, 	
 "adjjIB771:DL$/$7DS=Mr    c                     U R                   $ )zThe base period)r  r   s    r   r  CalendarTimeTrend.base_period  s        r    r   c                 X    UR                  S5      nSnSU;   a  SnOSU;   a  SnU " XXSS9$ )aM  
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   rJ   r   rF   r  r   )r   rI   r   r  r   r   s         r   r   CalendarTimeTrend.from_string  s?    F ##C(5=EE\E45BBr    r!   rE  c                 f   [        U[        R                  5      (       a  UR                  U R                  5      nUR
                  nX0R                  -
  S-   nUR                  [        R                  5      U-   nUS S 2S 4   nU R                  U5      n[        R                  " XPR                  US9$ )NrF   r   )r<   r=   rT   r.  r$  r  r  r   rZ   r   r   r{   r   )r   r!   rE  index_i8timer   s         r   _termsCalendarTimeTrend._terms  s     eR--..OODJJ/E::ll*Q.ryy)E1AtG}%||E==FFr    c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  X5      $ r1   )rB   r;  r1  r  )r   r!   rE  s      r   r%   CalendarTimeTrend.in_sample  sC       '&&u-##E*{{5((r    r(   r)   c                    U R                  U5      nU R                  X!U5      nU R                  U5        [        U[        R
                  [        R                  45      (       d   eU R                  U5      nU R                  XE5      $ r1   )	rB   re   r;  r<   r=   rR   rT   r1  r  )r   r(   r!   r)   r   rE  s         r   r+   CalendarTimeTrend.out_of_sample  su       '((~F{++8H8H'IJJJJ##K0{{;..r    .c                     U R                   U R                  U R                  R                  4nU R                  b  XR                  4-  nU$ r1   )r   r   r$  r)  r  )r   attrs     r   r5   CalendarTimeTrend._eq_attr  sJ     NNKKJJ&

 (&&((Dr    c                     [         R                  U 5      nSUS S -   SU R                  R                   S3-   nU R                  b  US S SU R                   S3-   nU$ )NCalendarrE   z, freq=r   zbase_period=)r   r.   r$  r)  r  )r   values     r   r.   CalendarTimeTrend.__str__&  sl    *2248U3BZ'GDJJ4F4F3Gq*II(#2J<0A0A/B!!DDEr    )r  r  r   r1   )#r3   ru   rv   rw   rx   r}   rz   r|   r	   r
   DateLiker   ry   r  r   r   r=   rT   rR   rZ   r   r{   r  r   r   r%   r   r   r>   r+   r~   r5   r.   r   r   r   s   @r   r  r    s   3p 	N 7;NN N 	N eCM23N 
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    r  c                      \ rS rSrSrSSSSSSSS.S\\\   \R                  4   S	\
\\\4      S
\S\S\S\S\\   S\4S jjr\S\R                  4S j5       r\S\\   4S j5       rS\\R(                     S\\R(                     4S jrS\R(                  S\R(                  4S jr\" \R0                  R                  5      S\R(                  4S j5       r\" \R2                  R                  5       S'S\S\
\\\   \R                  4      S\R(                  4S jj5       rS\R4                  S\\R6                  \R8                  4   4S jrS\S\S\R(                  4S jrS\R4                  S\R4                  S\R(                  4S jrS \S!\ S\R4                  4S" jr!S\\"\#\ 4   S\\"\#\ 4   S\R(                  4S# jr$S(S$ jr%S% r&S&r'g))DeterministicProcessi.  a\  
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   r   r   seasonalfourieradditional_termsdropr!   r   r   r   r  r  r  r  c                l   [        U[        R                  5      (       d  [        R                  " U5      nXl        / U l        SU l        S U l        U R                  5         [        USSS9n[        US5      =U l
        n[        US5      U l        [        US5      =U l        n[        US5      U l        [        U5      nS U l        [        US	5      U l        Xpl        U(       d  U(       a$  U R                  R'                  [)        X45      5        U(       a  U(       a  [+        S
5      eU(       d  U(       a"  Uc  Uc  [-        U R                  5      =U l        nU(       a1  [        US5      nU R                  R'                  [1        U5      5        O:U(       a3  [        US5      nUc   eU R                  R'                  [3        X&S95        U HX  n	[        U	[4        5      (       d  [7        S5      eXR                  ;  a  U R                  R'                  U	5        MO  [+        S5      e   X l        S U l        g )NFr   T)optionalr   r   r  r  r  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.)r<   r=   r>   _index_deterministic_terms_extendable_index_freq_validate_indexr   r   r   r   r   	_seasonal_fourierr~   _cached_in_sample_drop_additional_termsr   r   rQ   r   r   r   r  r   r@   _retain_cols)
r   r!   r   r   r   r  r  r  r  r   s
             r   r   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%%%%%,,WV-KL$Dd$566+  444))006 !  % 6:r    r   c                     U R                   $ )zThe index of the process)r  r   s    r   r!   DeterministicProcess.index  r   r    c                     U R                   $ )z/The deterministic terms included in the process)r  r   s    r   r   DeterministicProcess.terms  s     (((r    r   c                    S nU R                    H5  n[        U[        [        45      (       d  M   U=(       d    UR                  nM7     UcV  SnU HN  nXDR
                  S   :H  R                  5       UR
                  S   S:g  -  nU=(       d    UR                  5       nMP     Un[        U R                   5       HC  u  psUR                  nU(       a   U(       a  X   R
                  S S 2SS 24   X'   U=(       d    UnME     U$ )NFr   rF   )
r  r<   r   r  r   ilocr[   anyr  r   )	r   r   	has_constdtermr   	const_col
drop_firstr   r   s	            r   _adjust_dummies$DeterministicProcess._adjust_dummies  s    $(	..E%)->!?@@%7	 / I!YYq\1668DIIaLA<MN	%8	  
!$";";<HA~~HJ 8==AB/#/xJ = r    c                 R   [         R                  " US:H  SS9n[         R                  " U5      (       a  UR                  S S 2U) 4   nUR	                  SS9UR                  SS9:H  n[         R                  " U5      S:  a&  X3R                  5       -  nUR                  S S 2U) 4   nU$ )Nr   axisrF   )rZ   r[   r  r  maxminsum
duplicated)r   r   all_zerois_constantsurplus_constss        r   _remove_zeros_ones'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                    U R                   b  U R                   $ U R                  nU R                  (       d8  [        R                  " [
        R                  " UR                  S   S45      US9$ / nU R                   H#  nUR                  UR                  U5      5        M%     U R                  U5      n[        R                  " USS9nU R                  U5      nU R                  (       Gau  [        U5      n[        USSS9nUS   nUS   n[
        R                   " [
        R"                  " U5      5      n	U	S   UR                  S   -  [
        R$                  " [&        5      R(                  -  n
[+        [
        R,                  " X:  5      5      nUR.                  U-  nS/nSn[1        SUR                  S   5       HR  n[
        R2                  R5                  US US-   2S US-   24   5      nUU:  a  UR                  U5        UnUU:X  d  MR    O   [7        U5      U:X  a  UR8                  S S 2U4   nO*UR8                  S S 2[
        R:                  " US U 5      4   nUR<                  U l        X@l         U$ )	Nr   rA   rF   r  rT)modepivotingrE   ) r  r  r  r=   r{   rZ   r   rP   r   r%   r  concatr  r  r   r   absdiagfinfor   epsr|   r  Tr   linalgmatrix_rankrY   r  sortr   r  )r   r!   	raw_termsr   r   	terms_arrresr  pabs_diagtolrankrpxkeep	last_rankr   	curr_ranks                    r   r%   DeterministicProcess.in_sample  s   !!-)))((<<%++a.!)< =UKK	--DT^^E23 . ((3	 ii	:''.::: IYS48CAABAvvbggaj)H1+	 22RXXe_5H5HHCrvvhn-.D##	/C3DI1iooa01II11#gAgwQw6F2GH	y(KKN )I$ 2 4yD 

1d7+

1bggah&7#78!MM!&r    r(   r)   c                 @   [        US5      nU R                  (       a  U R                  c  U R                  5         U R                  nU R
                  (       d8  [        R                  " [        R                  " UR                  S   S45      US9$ / nU R
                   H$  nUR                  UR                  XU5      5        M&     [        R                  " USS9nU R                  c   eUR                  S   [        U R                  5      :w  a  X`R                     nU$ )Nr(   r   rA   rF   r  )r   r  r  r%   r  r  r=   r{   rZ   r   rP   r   r+   r  rY   )r   r(   r)   r!   r  r   r   s          r   r+   "DeterministicProcess.out_of_sample  s     "%1::$++3NN((<<%++a.!)< =UKK	--DT//nMN . ii	:  ,,,;;q>S!2!233++,Er    rW   c                     U R                   n[        U[        R                  5      (       a"  [        R                  " US   XR
                  S9$ [        R                  " US   XR                  S9$ )Nr   )endrI   )rb   r  rI   )r  r<   r=   rR   rS   rI   rU   r  )r   rW   r!   s      r   _extend_time_index'DeterministicProcess._extend_time_index1  sR     eR^^,,??58JJGG}}58<L<LMMr    rb   c                    U R                   n[        U5      n[        U[        R                  5      (       d	  U(       d   eXS   :  a  [        [        5      e[        U[        R                  5      (       a  UR                  nO5[        U5      S:  a$  [        R                  " U5      R                  5       OSnUS:w  a  XS   -
  U-  S:w  a  [        SU S35      eU(       a+  [        R                  " [        R                  " X5      5      nO[        R                  " XUS9nUS   U R                   S   ::  a!  U R                  5       nUR                  U   nU$ US   U R                   S   :  ao  US   U-   nUS   U:w  aA  [        R                  " XUS9n	U R!                  U	R"                  S   U	S9n
U
R                  U   $ U R!                  UR"                  S   US9$ X`R                   S   :*  nXk   nXk)    nU R                  5       R                  U   nU R!                  UR"                  S   US9n[        R$                  " X/SS	9$ )
Nr   rF   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.rM   rE   )r)   )r(   r)   r  )r  r   r<   r=   rV   rQ   START_BEFORE_INDEX_ERRrN   rY   rZ   r\   r  r>   r]   r%   r  r+   rP   r  )r   rb   rW   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_index,DeterministicProcess._range_from_range_index:  s   %e,%//>AA8344eR]]++zzH/25zA~rwwu~))+1Hq=uQx/8;A>xj I* * 
 hhryy56GmmEh?G2;$++b/)(I!g.IQZ$++b/)rX-JqzZ'mmJ8D((1c(Jwww''%%gmmA&6w%OO;;r?2.#N3)--m<%%#))!,=N & 
 yy.3!<<r    c                    U R                   n[        U R                   [        R                  5      (       ap  [        U[        R                  5      (       a  UR                  U R                  S9n[        U[        R                  5      (       a  UR                  U R                  S9nXS   :  a  [        [        5      eX R                   S   ::  a  U R                  5       R                  X $ U R                  U5      nXDUS   :     nU R                  UR                  S   U5      nXS   :  a  UR                  X $ [        R                  " U R                  5       U/SS9nUR                  X $ )N)rI   r   rE   r  )r  r<   r=   rR   	Timestampr.  r  rQ   r	  r%   r  r  r+   rP   r  )r   rb   rW   r!   r  oos_idxr  boths           r   _range_from_time_index+DeterministicProcess._range_from_time_indexe  s,    dkk2>>22%..T-=-=>$--~~4+;+;~<8344;;r?">>#''33))$/E"I-.  q!17;AJ775&&yy$..*C0q9xx##r    r  r6   c                    US:  a  [        U S35      eXR                  R                  S   :  a  U R                  U   $ XR                  R                  S   S-
  -
  S-   nU R                  n[        U R                  [        R
                  5      (       a6  [        R                  " US   U R                  US9nUS   R                  5       $ [        R                  " US   U R                  US9nUS   $ )Nr   z must be non-negative.rF   rE   rK   )
rQ   r  rP   r<   r=   rR   rS   r  r-  rU   )r   r  r6   add_periodsr!   r  drs          r   _int_to_timestamp&DeterministicProcess._int_to_timestampz  s    19v%;<==;;$$Q'';;u%%{{003a781<dkk2>>22b	 0 0+B b6&&((]]"ID,,k
 "vr    c                 t   U R                   (       d  [        S5      e[        U R                  5      [        R
                  4;   d  [        U R                  5      (       a.  [        US5      n[        US5      nUS-  nU R                  X5      $ [        U[        [        R                  45      (       a  U R                  US5      nO[        R                  " U5      n[        U[        [        R                  45      (       a  U R                  US5      nO[        R                  " U5      nU R                  X5      $ )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.
rb   rW   rF   )r  r@   r2   r  r=   rV   r   r   r  r<   r|   rZ   integerr   r  r  )r   rb   rW   s      r   r   DeterministicProcess.range  s    *    00L4M4M%eW5E$T62DAID//<<ec2::.//**5':ELL'EdS"**-..))$7D<<%D**577r    c                    [        U R                  [        R                  5      (       a#  U R                  R                  U l        SU l        g [        U R                  [        R                  5      (       aL  U R                  R                  =(       d    U R                  R                  U l        U R
                  S LU l        g [        U R                  [        R                  5      (       a  SU l        g [        U R                  5      (       aV  U R                  S   S:H  =(       a7    [        R                  " [        R                  " U R                  5      S:H  5      U l        g g )NTr   rF   )r<   r  r=   rR   rI   r  r  rT   r   rV   r   rZ   r[   r\   r   s    r   r  $DeterministicProcess._validate_index  s    dkk2>>22#{{//D#DR%5%566#{{//L4;;3L3LD#//t;DR]]33#D$++&&#{{1~2  rvv$)8D 'r    c                     [        UU R                  U R                  U R                  U R                  U R
                  U R                  U R                  S9$ )a  
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
r  )r  r   r   r   r  r  r  r  r$   s     r   applyDeterministicProcess.apply  sG     $<<^^++^^MM!33	
 		
r    )r  r  r   r  r  r  r  r  r  r   r   r  r  r1   )r   N)(r3   ru   rv   rw   rx   r
   r   r   r=   r>   r	   r   r|   rz   r   r   ry   r!   r   r   r{   r  r  r   r%   r+   r  rT   rR   r  r  r  r}   r   IntLiker  r   r  r(  r   r#   r    r   r  r  .  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    r  )1statsmodels.compat.pandasr   r   r   r   abcr   r   datetimedttypingr	   r
   collections.abcr   r   numpyrZ   pandasr=   scipy.linalgr   statsmodels.iolib.summaryr   statsmodels.tools.validationr   r   r   r   statsmodels.tsa.tsatoolsr   r  
datetime64r  r|   r#  r*  r	  r   r   r   r   r   r  r"  r>  rO  r  r  r#   r    r   <module>r8     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    