Ë
    £�Djÿ]  ã                  óX  — d dl mZ d dlmZ d dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZmZmZ g d¢Zd#d„Zd$d„Zd%d„Z	 	 	 d&	 	 	 	 	 	 	 	 	 d'd„Z 	 d(d„Z!d„ Z"d„ Z#d„ Z$d„ Z%d„ Z&d„ Z'd„ Z(d„ Z)d„ Z*d„ Z+d„ Z,d„ Z-d„ Z.d„ Z/d „ Z0d!„ Z1d)d"„Z2y)*é    )Úannotations)ÚlrangeN)Ú	DataFrame)Úoffsets)Ú	to_offset)ÚLiteral)Ú_is_recarrayÚ_is_using_pandas)ÚValueWarning)ÚNDArray)Ú
array_likeÚ	bool_likeÚint_likeÚstring_like)ÚlagmatÚ	lagmat2dsÚ	add_trendÚduplication_matrixÚelimination_matrixÚcommutation_matrixÚvecÚvechÚunvecÚunvechÚfreq_to_periodc                óò  — t        |d«      }t        |dd¬«      }t        |dd¬«      }g d¢}|dk(  r| j                  «       S |d	k(  r|d
d }d}n#|dk(  s|dk(  r|d
d }|dk(  r|dd }d}n|dk(  rd}t        | «      rddlm} t        |«      ‚t        | d
«      }|rAt        | t        j                  «      rt        j                  | «      } n&| j                  «       } nt        j                  | «      } t        | «      }t        j                  t        j                   d|dz   t        j"                  ¬«      dz   «      }	t        j$                  |	«      }	|dk(  r	|	d
d
…df   }	d	|v �r!|rd„ }
| j'                  |
d«      }n<t        j(                  t        j                  | «      d¬«      }|dk(  }|| d   dk7  z  }|}t        j*                  |«      r¸|dk(  rž| j,                  dk(  rd}nyt        j                   | j.                  d   «      |   }t        | t        j                  «      r| j0                  }dj3                  |D �cg c]  }t5        |«      ‘Œ c}«      }d|› d�}|› d|› d�}t7        |«      ‚|dk(  r|dd
 }|	d
d
…dd
…f   }	|rdnd}|rEt        j                  |	| j8                  |¬«      }	|	| g} t        j:                  | d
d
|…   d¬«      } | S |	| g} t        j<                  | d
d
|…   «      } | S c c}w )aØ  
    Add a trend and/or constant to an array.

    Parameters
    ----------
    x : array_like
        Original array of data.
    trend : str {'n', 'c', 't', 'ct', 'ctt'}
        The trend to add.

        * 'n' add no trend.
        * 'c' add constant only.
        * 't' add trend only.
        * 'ct' add constant and linear trend.
        * 'ctt' add constant and linear and quadratic trend.
    prepend : bool
        If True, prepends the new data to the columns of X.
    has_constant : str {'raise', 'add', 'skip'}
        Controls what happens when trend is 'c' and a constant column already
        exists in x. 'raise' will raise an error. 'add' will add a column of
        1s. 'skip' will return the data without change. 'skip' is the default.

    Returns
    -------
    array_like
        The original data with the additional trend columns.  If x is a
        pandas Series or DataFrame, then the trend column names are 'const',
        'trend' and 'trend_squared'.

    See Also
    --------
    statsmodels.tools.tools.add_constant
        Add a constant column to an array.

    Notes
    -----
    Returns columns as ['ctt','ct','c'] whenever applicable. There is currently
    no checking for an existing trend.
    ÚprependÚtrend)ÚnÚcÚtÚctÚctt©ÚoptionsÚhas_constant)ÚraiseÚaddÚskip)Úconstr   Útrend_squaredr   r    Né   r   r"   r!   é   r#   )Úrecarray_exception)Údtypec                óv   — 	 t        j                  | «      dk(  xr t        j                  | dk7  «      S #  Y yxY w)Ng        F)ÚnpÚptpÚany)Úss    ú\C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels\tsa\tsatools.pyÚsafe_is_constz add_trend.<locals>.safe_is_const~   s6   € ð!ÜŸ6™6 !›9¨Ñ+Ò@´·±°q¸C±xÓ0@Ð@øð!Ù ús   ‚14 ´8©Úaxisr'   zx is constant.z, z3x contains one or more constant columns. Column(s) z are constant.z Adding a constant with trend='z' is not allowed.r)   éÿÿÿÿ©ÚindexÚcolumns)r   r   Úcopyr	   Ústatsmodels.tools.sm_exceptionsr.   ÚNotImplementedErrorr
   Ú
isinstanceÚpdÚSeriesr   r1   Ú
asanyarrayÚlenÚvanderÚarangeÚfloat64ÚfliplrÚapplyr2   r3   ÚndimÚshaper<   ÚjoinÚstrÚ
ValueErrorr;   ÚconcatÚcolumn_stack)Úxr   r   r&   r<   Ú
trendorderr.   Ú	is_pandasÚnobsÚtrendarrr6   Ú	col_constÚptp0Úcol_is_constÚnz_constÚbase_errr    Ú
const_colsÚmsgÚorders                       r5   r   r   '   sè  € ôP ˜ Ó+€GÜ˜˜wÐ0LÔM€EÜØ�nÐ.Fô€Lò
 2€GØ�‚|Ø�v‰v‹xˆØ	�#ŠØ˜"˜1�+ˆØ‰
Ø	�$Š˜% 3š,Ø˜"˜1�+ˆØ�CŠ<Ø˜a �lˆGØ‰
Ø	�%ŠØˆ
ä�A„ÝFä!Ð"4Ó5Ð5ä   DÓ)€IÙÜ�aœŸ™Ô#Ü—‘˜Q“‰Aà—‘“‰Aä�M‰M˜!Óˆäˆq‹6€DÜ�y‰yÜ
�	‰	�!�T˜A‘X¤R§Z¡ZÔ0°*¸q±.ó€Hô �y‰y˜Ó"€HØ�‚|ØšA˜q˜D‘>ˆà
ˆe‚|Ùò!ð Ÿ™ ¨qÓ1‰Iä—6‘6œ"Ÿ-™-¨Ó*°Ô3ˆDØ 1™9ˆLØ# q¨¡t¨q¡yÑ1ˆHØ ˆIä�6‰6�)ÔØ˜wÒ&Ø—6‘6˜Q’;Ø/‘Hä Ÿi™i¨¯©°©
Ó3°IÑ>�GÜ! !¤R§\¡\Ô2Ø"#§)¡)˜Ø!%§¡¸GÖ+D°q¬C°­FÒ+DÓ!E�JàMØ%˜, nð6ð ð "˜
Ð"AÀ%ÀÐHYÐZ�Ü  “oÐ%Ø Ò'Ø! ! "˜+�Ø#¢A q¡r E™?�á‰A˜b€EÙÜ—<‘< °·±ÀÔIˆØ�qˆMˆÜ�I‰I�a™˜%˜‘j qÔ)ˆð
 €Hð �qˆMˆÜ�O‰O˜A™g ˜g™JÓ'ˆà€Hùò) ,Es   È?K4c                óî  — t        |d«      }t        |d«      }t        | dd¬«      } |€d}|dk  r| j                  d   |z   }| j                  dk(  r	| dd…df   } | dd…|f   }|d	u r|dz   }nk|d
u r| j                  d   }nW|dk  r| j                  d   |z   dz   }|| j                  d   kD  r)| j                  d   }t        j                  dt        «       |}t        ||d¬«      }t        |«      }t        || j                  d   «      }	|rE||v r!|j                  |j                  |«      «       n |	j                  |	j                  |«      «       t        j                  | |d…|f   || |d…|	f   f«      S )aˆ  
    Returns an array with lags included given an array.

    Parameters
    ----------
    x : array_like
        An array or NumPy ndarray subclass. Can be either a 1d or 2d array with
        observations in columns.
    col : int or None
        `col` can be an int of the zero-based column index. If it's a
        1d array `col` can be None.
    lags : int
        The number of lags desired.
    drop : bool
        Whether to keep the contemporaneous variable for the data.
    insert : bool or int
        If True, inserts the lagged values after `col`. If False, appends
        the data. If int inserts the lags at int.

    Returns
    -------
    array : ndarray
        Array with lags

    Examples
    --------

    >>> import statsmodels.api as sm
    >>> data = sm.datasets.macrodata.load()
    >>> data = data.data[['year','quarter','realgdp','cpi']]
    >>> data = sm.tsa.add_lag(data, 'realgdp', lags=2)

    Notes
    -----
    Trims the array both forward and backward, so that the array returned
    so that the length of the returned array is len(`X`) - lags. The lags are
    returned in increasing order, ie., t-1,t-2,...,t-lags
    ÚlagsÚdroprQ   r-   )rJ   Nr   r,   TFz<insert > number of variables, inserting at the last positionÚBoth)Útrim)r   r   r   rK   rJ   ÚwarningsÚwarnr   r   r   Úpopr;   r1   rP   )
rQ   Úcolr_   r`   ÚinsertÚcontempÚins_idxÚndlagsÚ
first_colsÚ	last_colss
             r5   Úadd_lagrm   ª   s  € ôN �D˜&Ó!€DÜ�T˜6Ó"€DÜ�1�c Ô"€AØ
€{Øˆð ˆQ‚wØ�g‰g�a‰j˜3ÑˆØ‡v�v�‚{ØŠa�ˆg‰JˆØ’�3�‰i€Gà��~Ø˜‘'‰Ø	�5‰Ø—'‘'˜!‘*‰à�AŠ:Ø—W‘W˜Q‘Z &Ñ(¨1Ñ,ˆFØ�A—G‘G˜A‘JÒØ—W‘W˜Q‘ZˆFä�M‰Mð!äôð
 ˆä�G˜T¨Ô/€FÜ˜“€JÜ�w §¡¨¡
Ó+€IÙØ�*ÑØ�N‰N˜:×+Ñ+¨CÓ0Õ1à�M‰M˜)Ÿ/™/¨#Ó.Ô/Ü�?‰?˜A˜d™e ZÐ/Ñ0°&¸!¸D¹EÀ9Ð<LÑ:MÐNÓOÐOó    c                ó^  — t        |d«      }t        |d«      }| j                  dk(  rt        |«      dk(  r| j                  } n| j                  dkD  rt	        d«      ‚| j
                  d   }|dk(  r| | j                  d¬«      z
  }n}t        j                  t        j                  t        |«      «      |dz   ¬«      }t        j                  j                  |«      j                  | «      }| t        j                  ||«      z
  }| j                  dk(  rt        |«      dk(  r|j                  }|S )	a˜  
    Detrend an array with a trend of given order along axis 0 or 1.

    Parameters
    ----------
    x : array_like, 1d or 2d
        Data, if 2d, then each row or column is independently detrended with
        the same trendorder, but independent trend estimates.
    order : int
        The polynomial order of the trend, zero is constant, one is
        linear trend, two is quadratic trend.
    axis : int
        Axis can be either 0, observations by rows, or 1, observations by
        columns.

    Returns
    -------
    ndarray
        The detrended series is the residual of the linear regression of the
        data on the trend of given order.
    r]   r8   r-   r,   z0x.ndim > 2 is not implemented until it is neededr   r7   )ÚN)r   rJ   ÚintÚTr?   rK   Úmeanr1   rE   rF   ÚfloatÚlinalgÚpinvÚdot)rQ   r]   r8   rT   ÚresidÚtrendsÚbetas          r5   Údetrendr{   ú   sõ   € ô, �U˜GÓ$€EÜ�D˜&Ó!€Dà‡v�v�‚{”s˜4“y A’~Ø�C‰C‰Ø	
�‰�!ŠÜ!Ø>ó
ð 	
ð �7‰7�1‰:€DØ�‚zà�A—F‘F �F“NÑ"‰ä—‘œ2Ÿ9™9¤U¨4£[Ó1°U¸Q±YÔ?ˆÜ�y‰y�~‰~˜fÓ%×)Ñ)¨!Ó,ˆØ”B—F‘F˜6 4Ó(Ñ(ˆà‡v�v�‚{”s˜4“y A’~Ø—‘ˆà€Lrn   c           	     ó  — t        |d«      }t        |d«      }t        |ddd¬«      }t        |dd¬	«      }| }t        | d
dd¬«      } t	        |d«      xr |}|€dn|}|j                  «       }|r|dv rt        d«      ‚d}| j                  \  }}	|dv r|	}||k\  rt        d«      ‚t        j                  ||z   |	|dz   z  f«      }
t        dt        |dz   «      «      D ]%  }| |
||z
  ||z   |z
  …|	||z
  z  |	||z
  dz   z  …f<   Œ' |dv rd}n|dv r|}nt        d«      ‚|dv rt        |
«      }n|}|�r$|} t        | t        «      rQ| j                  D �cg c]  }t!        |«      ‘Œ }}t        t#        |«      «      | j                  d   k7  r!t        d«      ‚t!        | j$                  «      g}|D �cg c]  }t!        |«      ‘Œ }}t        |«      D ]>  }t!        |dz   «      }|j'                  |D �cg c]  }t!        |«      dz   |z   ‘Œ c}«       Œ@ t        |
d| | j(                  |¬«      }
|
j*                  |d }|dv r4||   }|j-                  |d¬«      }n|
||…|d…f   }|dk(  r|
||…d|…f   }|dk(  r|fS |S c c}w c c}w c c}w )a,	  
    Create 2d array of lags.

    Parameters
    ----------
    x : array_like
        Data; if 2d, observation in rows and variables in columns.
    maxlag : int
        All lags from zero to maxlag are included.
    trim : {'forward', 'backward', 'both', 'none', None}
        The trimming method to use.

        * 'forward' : trim invalid observations in front.
        * 'backward' : trim invalid initial observations.
        * 'both' : trim invalid observations on both sides.
        * 'none', None : no trimming of observations.
    original : {'ex','sep','in'}
        How the original is treated.

        * 'ex' : drops the original array returning only the lagged values.
        * 'in' : returns the original array and the lagged values as a single
          array.
        * 'sep' : returns a tuple (original array, lagged values). The original
                  array is truncated to have the same number of rows as
                  the returned lagmat.
    use_pandas : bool
        If true, returns a DataFrame when the input is a pandas
        Series or DataFrame.  If false, return numpy ndarrays.

    Returns
    -------
    lagmat : ndarray
        The array with lagged observations.
    y : ndarray, optional
        Only returned if original == 'sep'.

    Notes
    -----
    When using a pandas DataFrame or Series with use_pandas=True, trim can only
    be 'forward' or 'both' since it is not possible to consistently extend
    index values.

    Examples
    --------
    >>> from statsmodels.tsa.tsatools import lagmat
    >>> import numpy as np
    >>> X = np.arange(1,7).reshape(-1,2)
    >>> lagmat(X, maxlag=2, trim="forward", original='in')
    array([[ 1.,  2.,  0.,  0.,  0.,  0.],
       [ 3.,  4.,  1.,  2.,  0.,  0.],
       [ 5.,  6.,  3.,  4.,  1.,  2.]])

    >>> lagmat(X, maxlag=2, trim="backward", original='in')
    array([[ 5.,  6.,  3.,  4.,  1.,  2.],
       [ 0.,  0.,  5.,  6.,  3.,  4.],
       [ 0.,  0.,  0.,  0.,  5.,  6.]])

    >>> lagmat(X, maxlag=2, trim="both", original='in')
    array([[ 5.,  6.,  3.,  4.,  1.,  2.]])

    >>> lagmat(X, maxlag=2, trim="none", original='in')
    array([[ 1.,  2.,  0.,  0.,  0.,  0.],
       [ 3.,  4.,  1.,  2.,  0.,  0.],
       [ 5.,  6.,  3.,  4.,  1.,  2.],
       [ 0.,  0.,  5.,  6.,  3.,  4.],
       [ 0.,  0.,  0.,  0.,  5.,  6.]])
    ÚmaxlagÚ
use_pandasrb   T©ÚforwardÚbackwardÚbothÚnone©Úoptionalr%   Úoriginal)ÚexÚsepÚinr$   rQ   r-   N)rJ   r/   rƒ   )rƒ   r�   zEtrim cannot be 'none' or 'backward' when used on Series or DataFramesr   )r‡   rˆ   zmaxlag should be < nobsr,   )rƒ   r€   )r�   r‚   ztrim option not validzSColumns names must be distinct after conversion to string (if not already strings).z.L.r:   )rˆ   r‡   r7   rˆ   )r   r   r   r   r
   ÚlowerrN   rK   r1   ÚzerosÚrangerq   rD   r@   r   r<   rM   ÚsetÚnameÚextendr;   Úilocr`   )rQ   r}   rb   r†   r~   ÚorigrS   ÚdropidxrT   ÚnvarÚlmÚkÚstartobsÚstopobsr    Ú	x_columnsrf   r<   ÚlagÚlag_strr_   Úleadss                         r5   r   r   )  s
  € ôR �f˜hÓ'€FÜ˜: |Ó4€JÜØØØØ7ô	€Dô ˜8 ZÐ9LÔM€Hð €DÜ�1�c ¨Ô.€AÜ   tÓ,Ò;°€IØ�\‰6 t€DØ�:‰:‹<€DÙ�TÐ1Ñ1Üð#ó
ð 	
ð
 €GØ—‘�J€Dˆ$Ø�=Ñ ØˆØ�‚~ÜÐ2Ó3Ð3Ü	�‰�4˜&‘= $¨&°1©*Ñ"5Ð6Ó	7€BÜ�1”c˜& 1™*“oÓ&ò ˆð ð 	Ø�‰
�D˜6‘M AÑ%Ð%Ø�˜‘
Ñ˜T V¨a¡Z°!¡^Ñ4Ð4ð	6ò	
ðð Ð"Ñ"Ø‰Ø	Ð%Ñ	%Ø‰äÐ0Ó1Ð1àÐ#Ñ#Ü�b“'‰àˆâØˆÜ�aœÔ#Ø)*¯©Ö3 Aœ˜Q�Ð3ˆIÐ3Ü”3�y“>Ó" a§g¡g¨a¡jÒ0Ü ð0óð ô
 ˜QŸV™V›˜ˆIØ'0Ö1 ”3�s•8Ð1ˆÐ1Ü˜“=ò 	NˆCÜ˜# ™'“lˆGØ�N‰NÀ)ÖL¸3œC ›H uÑ,¨wÓ6ÒLÕMð	Nô �r˜(˜7�|¨1¯7©7¸GÔDˆØ�w‰w�x�yÐ!ˆØ�}Ñ$Ø˜‘OˆEØ—9‘9˜Y¨Q�9Ó/‰Dà�(˜7Ð" G¡HÐ,Ñ-ˆØ�uÒØ�x Ð'¨¨'¨Ð1Ñ2ˆEà�5ÒØ�Uˆ{Ðàˆùò3 4ùò 2ùò Ms   ÅI=Æ0JÇ/J
c           	     óÜ  — t        |d«      }t        |dd¬«      }t        |ddd¬«      }|€|}t        ||«      }t        | d«      }| j                  d	k(  r"|rt        j                  | «      } n3| dd…df   } n)| j                  d
k(  s| j                  dkD  rt        d«      ‚| j                  \  }}	|r¬|rªt        | j                  dd…d
f   ||dd¬«      }
|
j                  dd…d|d	z   …f   g}t        d	|	«      D ]J  }t        | j                  dd…|f   ||dd¬«      }
|j                  |
j                  dd…||d	z   …f   «       ŒL t        j                  |d	¬«      S |rt        j                  | «      } t        | dd…d
f   ||d¬«      dd…d|d	z   …f   g}t        d	|	«      D ]3  }|j                  t        | dd…|f   ||d¬«      dd…||d	z   …f   «       Œ5 t        j                   |«      S )a©  
    Generate lagmatrix for 2d array, columns arranged by variables.

    Parameters
    ----------
    x : array_like
        Data, 2d. Observations in rows and variables in columns.
    maxlag0 : int
        The first variable all lags from zero to maxlag are included.
    maxlagex : {None, int}
        The max lag for all other variables all lags from zero to maxlag are
        included.
    dropex : int
        Exclude first dropex lags from other variables. For all variables,
        except the first, lags from dropex to maxlagex are included.
    trim : str
        The trimming method to use.

        * 'forward' : trim invalid observations in front.
        * 'backward' : trim invalid initial observations.
        * 'both' : trim invalid observations on both sides.
        * 'none' : no trimming of observations.
    use_pandas : bool
        If true, returns a DataFrame when the input is a pandas
        Series or DataFrame.  If false, return numpy ndarrays.

    Returns
    -------
    ndarray
        The array with lagged observations, columns ordered by variable.

    Notes
    -----
    Inefficient implementation for unequal lags, implemented for convenience.
    Úmaxlag0ÚmaxlagexT)r…   rb   r   r„   Nr,   r   r-   z'Only supports 1 and 2-dimensional data.r‰   )rb   r†   r~   r7   )rb   r†   )r   r   Úmaxr
   rJ   rA   r   rN   rK   r   r�   rŒ   ÚappendrO   r1   rC   rP   )rQ   r�   rž   Údropexrb   r~   r}   rS   rT   r“   r_   Úlagslir•   s                r5   r   r   À  s  € ôL �w 	Ó*€GÜ˜ *°tÔ<€HÜØØØØ7ô	€Dð ÐØˆÜ�˜(Ó#€FÜ   DÓ)€Ià‡v�v�‚{ÙÜ—‘˜Q“‰Aà’!�T�'‘
‰AØ	
�‰�1Š˜Ÿ™ š
ÜÐBÓCÐCà—‘�J€Dˆ$á‘ZÜØ�F‰F’1�a�4‰L˜& t°dÀtô
ˆð —)‘)šA˜} ¨1¡˜}Ð,Ñ-Ð.ˆÜ�q˜$“ò 	?ˆAÜØ—‘’q˜!�t‘˜f¨4¸$È4ôˆDð �M‰M˜$Ÿ)™)¢A v°¸1±Ð'<Ð$<Ñ=Õ>ð		?ô
 �y‰y˜ aÔ(Ð(Ù	Ü�M‰M˜!Óˆô 	ˆq’�A�‰w˜ T°DÔ9º!¸]¸wÈ¹{¸]Ð:JÑKð€Fô �1�d‹^ò 
ˆØ�‰Ü�1’Q˜�T‘7˜F¨¸Ô=Ú�6˜H q™LÐ(Ð(ñõ	
ð
ô �?‰?˜6Ó"Ð"rn   c                ó$   — | j                  d«      S )NÚF)Úravel©Úmats    r5   r   r     s   € Ø�9‰9�S‹>Ðrn   c                ó\   — | j                   j                  t        t        | «      «      «      S ©N)rr   ÚtakeÚ_triu_indicesrD   r¦   s    r5   r   r     s   € à�5‰5�:‰:”m¤C¨£HÓ-Ó.Ð.rn   c                óB   — t        j                  | «      \  }}|| z  |z   S r©   )r1   Útril_indices©r   ÚrowsÚcolss      r5   Ú_tril_indicesr±   #  ó"   € Ü—‘ Ó#�J€Dˆ$Ø�!‰8�d‰?Ðrn   c                óB   — t        j                  | «      \  }}|| z  |z   S r©   )r1   Útriu_indicesr®   s      r5   r«   r«   (  r²   rn   c                óB   — t        j                  | «      \  }}|| z  |z   S r©   )r1   Údiag_indicesr®   s      r5   Ú_diag_indicesr·   -  r²   rn   c                ó    — t        t        j                  t        | «      «      «      }||z  t        | «      k(  sJ ‚| j	                  ||fd¬«      S )Nr¤   ©r]   )rq   r1   ÚsqrtrD   Úreshape)Úvr•   s     r5   r   r   2  sA   € ÜŒB�G‰G”C˜“F‹OÓ€AØˆq‰5”C˜“FŠ?ÑØ�9‰9�a˜�V 3ˆ9Ó'Ð'rn   c           	     óR  — ddt        j                  ddt        | «      z  z   «      z   z  }t        t        j                  |«      «      }t        j
                  ||f«      }| |t        j                  |«      <   ||j                  z   }|t        j                  |«      xx   dz  cc<   |S )Ng      à?r9   r,   é   r-   )	r1   rº   rD   rq   Úroundr‹   r´   rr   r¶   )r¼   r¯   Úresults      r5   r   r   8  s‹   € à�"”r—w‘w˜q 1¤s¨1£v¡:™~Ó.Ñ.Ñ/€DÜŒr�x‰x˜‹~Ó€Dä�X‰X�t˜T�lÓ#€FØ$%€FŒ2�?‰?˜4Ó Ñ!Ø�f—h‘hÑ€Fð Œ2�?‰?˜4Ó Ó! QÑ&Ó!à€Mrn   c                óè   — t        | d«      } t        j                  | | dz   z  dz  «      }t        j                  |D �cg c]  }t	        |«      j                  «       ‘Œ c}«      j                  S c c}w )z’
    Create duplication matrix D_n which satisfies vec(S) = D_n vech(S) for
    symmetric matrix S

    Returns
    -------
    D_n : ndarray
    r   r,   r-   )r   r1   ÚeyeÚarrayr   r¥   rr   )r   ÚtmprQ   s      r5   r   r   G  sY   € ô 	��CÓ€AÜ
�&‰&��a˜!‘e‘ Ñ!Ó
"€CÜ�8‰8°Ö4¨1”V˜A“Y—_‘_Õ&Ò4Ó5×7Ñ7Ð7ùÒ4s   ¾ A/c                ó¼   — t        | d«      } t        t        j                  t        j                  | | f«      «      «      }t        j
                  | | z  «      |dk7     S )z�
    Create the elimination matrix L_n which satisfies vech(M) = L_n vec(M) for
    any matrix M

    Parameters
    ----------

    Returns
    -------
    r   r   )r   r   r1   ÚtrilÚonesrÂ   )r   Úvech_indicess     r5   r   r   U  sK   € ô 	��CÓ€AÜ”r—w‘wœrŸw™w¨¨1 v›Ó/Ó0€LÜ�6‰6�!�a‘%‹=˜¨Ñ*Ñ+Ð+rn   c                óú   — t        | d«      } t        |d«      }t        j                  | |z  «      }t        j                  | |z  «      j	                  | |fd¬«      }|j                  |j                  «       d¬«      S )z½
    Create the commutation matrix K_{p,q} satisfying vec(A') = K_{p,q} vec(A)

    Parameters
    ----------
    p : int
    q : int

    Returns
    -------
    K : ndarray (pq x pq)
    ÚpÚqr¤   r¹   r   r7   )r   r1   rÂ   rF   r»   rª   r¥   )rÊ   rË   ÚKÚindicess       r5   r   r   e  sl   € ô 	��CÓ€AÜ��CÓ€Aä
�‰ˆq�1‰u‹€AÜ�i‰i˜˜A™Ó×&Ñ&¨¨1 v°SÐ&Ó9€GØ�6‰6�'—-‘-“/¨ˆ6Ó*Ð*rn   c           	     ó  — t        j                  | dz  «      }t        j                  | dz  «      }t        dt        | «      «      D ]8  }||   }t        |«      D ]  }||xx   ||||z
  dz
     z  z  cc<   Œ |d| |d| Œ: |S )zÁ
    Transforms params to induce stationarity/invertability.

    Parameters
    ----------
    params : array_like
        The AR coefficients

    Reference
    ---------
    Jones(1980)
    r-   r,   N)r1   ÚtanhrŒ   rD   )ÚparamsÚ	newparamsrÄ   ÚjÚaÚkiters         r5   Ú_ar_transparamsrÕ   z  s—   € ô —‘˜ ™
Ó#€IÜ
�'‰'�&˜1‘*Ó
€CÜ�1”c˜&“kÓ"ò  ˆØ�a‰LˆÜ˜1“Xò 	7ˆEØ�‹J˜!˜i¨¨E©	°A©Ñ6Ñ6Ñ6ŒJð	7à˜B˜Q˜ˆ	�"�1‰ð	 ð
 Ðrn   c                ó,  — | j                  «       } | j                  «       }t        t        | «      dz
  dd«      D ]?  }| |   }t        |«      D ]"  }| |   || ||z
  dz
     z  z   d|dz  z
  z  ||<   Œ$ |d| | d| ŒA dt        j                  | «      z  }|S )z�
    Inverse of the Jones reparameterization

    Parameters
    ----------
    params : array_like
        The transformed AR coefficients
    r,   r   r9   r-   N)r=   rŒ   rD   r1   Úarctanh)rÐ   rÄ   rÒ   rÓ   rÔ   Ú
invarcoefss         r5   Ú_ar_invtransparamsrÙ   ‘  s¸   € ð �[‰[‹]€FØ
�+‰+‹-€CÜ”3�v“; ‘? A rÓ*ò ˆØ�1‰IˆÜ˜1“Xò 	ˆEØ  ™-¨!¨f°Q¸±YÀ±]Ñ.CÑ*CÑCØ�A˜‘F‘
ñˆC�ŠJð	ð ˜˜!�Wˆˆr�‰
ðð ”R—Z‘Z Ó'Ñ'€JØÐrn   c           	     óª  — dt        j                  |  «      z
  dt        j                  |  «      z   z  j                  «       }dt        j                  |  «      z
  dt        j                  |  «      z   z  j                  «       }t        dt	        | «      «      D ]8  }||   }t        |«      D ]  }||xx   ||||z
  dz
     z  z  cc<   Œ |d| |d| Œ: |S )zÒ
    Transforms params to induce stationarity/invertability.

    Parameters
    ----------
    params : ndarray
        The ma coeffecients of an (AR)MA model.

    Reference
    ---------
    Jones(1980)
    r,   N)r1   Úexpr=   rŒ   rD   )rÐ   rÑ   rÄ   rÒ   ÚbrÔ   s         r5   Ú_ma_transparamsrÝ   §  s×   € ð ”b—f‘f˜f˜W“oÑ%¨!¬b¯f©f°f°W«oÑ*=Ñ>×DÑDÓF€IØ”—‘˜�w“Ñ A¬¯©°¨w«Ñ$7Ñ8×
>Ñ
>Ó
@€Cô �1”c˜&“kÓ"ò  ˆØ�a‰LˆÜ˜1“Xò 	7ˆEØ�‹J˜!˜i¨¨E©	°A©Ñ6Ñ6Ñ6ŒJð	7à˜B˜Q˜ˆ	�"�1‰ð	 ð
 Ðrn   c                ó  — | j                  «       }t        t        | «      dz
  dd«      D ]?  }| |   }t        |«      D ]"  }| |   || ||z
  dz
     z  z
  d|dz  z
  z  ||<   Œ$ |d| | d| ŒA t        j                  d| z
  d| z   z  «       }|S )z�
    Inverse of the Jones reparameterization

    Parameters
    ----------
    params : ndarray
        The transformed MA coefficients
    r,   r   r9   r-   N)r=   rŒ   rD   r1   Úlog)ÚmacoefsrÄ   rÒ   rÜ   rÔ   Ú
invmacoefss         r5   Ú_ma_invtransparamsrâ   À  s¹   € ð �,‰,‹.€CÜ”3�w“< !Ñ# Q¨Ó+ò ˆØ�A‰JˆÜ˜1“Xò 	ˆEØ! %™.¨1¨w°q¸5±yÀ1±}Ñ/EÑ+EÑEØ�A˜‘F‘
ñˆC�ŠJð	ð ˜"˜1�gˆ��‰ðô —&‘&˜!˜g™+¨!¨g©+Ñ6Ó7Ð7€JØÐrn   c           
     ó¾   — t        |d«      }| d| } t        j                  t        |dd«      D �cg c]  }t        j                  | ||z
  «      d   ‘Œ  c}«      S c c}w )a“  
    Returns the successive differences needed to unintegrate the series.

    Parameters
    ----------
    x : array_like
        The original series
    d : int
        The number of differences of the differenced series.

    Returns
    -------
    y : array_like
        The increasing differences from 0 to d-1 of the first d elements
        of x.

    See Also
    --------
    unintegrate
    ÚdNr   r9   )r   r1   ÚasarrayrŒ   Údiff)rQ   rä   Úis      r5   Úunintegrate_levelsrè   Õ  sT   € ô* 	��CÓ€AØ	ˆ"ˆ1ˆ€AÜ�:‰:´U¸1¸aÀ³_ÖE°”r—w‘w˜q ! a¡%Ó(¨Ó+ÒEÓFÐFùÒEs   °#Ac                ó  — t        |«      dd }t        |«      dkD  rC|j                  d«      }t        t	        j
                  t        j                  || f   «      |«      S |d   }t	        j
                  t        j                  || f   «      S )ay  
    After taking n-differences of a series, return the original series

    Parameters
    ----------
    x : array_like
        The n-th differenced series
    levels : list
        A list of the first-value in each differenced series, for
        [first-difference, second-difference, ..., n-th difference]

    Returns
    -------
    y : array_like
        The original series de-differenced

    Examples
    --------
    >>> x = np.array([1, 3, 9., 19, 8.])
    >>> levels = unintegrate_levels(x, 2)
    >>> levels
    array([ 1.,  2.])
    >>> unintegrate(np.diff(x, 2), levels)
    array([  1.,   3.,   9.,  19.,   8.])
    Nr,   r9   r   )ÚlistrD   re   Úunintegrater1   ÚcumsumÚr_)rQ   ÚlevelsÚx0s      r5   rë   rë   ï  so   € ô4 �&‹\™!ˆ_€FÜ
ˆ6ƒ{�Q‚Ø�Z‰Z˜‹^ˆÜœ2Ÿ9™9¤R§U¡U¨2¨q¨5¡\Ó2°FÓ;Ð;Ø	�‰€BÜ�9‰9”R—U‘U˜2˜q˜5‘\Ó"Ð"rn   c                óÊ  — t        | t        j                  «      st        | «      } t        | t        j                  «      sJ ‚| j                  j                  «       } d}| dv s| j                  |«      ry| dk(  s| j                  d«      ry| dk(  s| j                  d«      ry	| d
k(  s| j                  d«      ry| dk(  ry| dk(  ry| dk(  ryt        dj                  | «      «      ‚)a$  
    Convert a pandas frequency to a periodicity

    Parameters
    ----------
    freq : str or offset
        Frequency to convert

    Returns
    -------
    int
        Periodicity of freq

    Notes
    -----
    Annual maps to 1, quarterly maps to 4, monthly to 12, weekly to 52.
    )zA-zAS-zY-zYS-zYE-)ÚAÚYr,   ÚQ)zQ-ÚQSÚQEé   ÚM)zM-ÚMSÚMEé   ÚWzW-é4   ÚDé   ÚBé   ÚHé   zDfreq {} not understood. Please report if you think this is in error.)	r@   r   Ú
DateOffsetr   Ú	rule_codeÚupperÚ
startswithrN   Úformat)ÚfreqÚyearly_freqss     r5   r   r     s×   € ô$ �dœG×.Ñ.Ô/Ü˜‹ˆÜ�dœG×.Ñ.Ô/Ñ/Ø�>‰>×ÑÓ!€Dà4€LØˆzÑ˜TŸ_™_¨\Ô:ØØ	�Š˜Ÿ™Ð(:Ô;ØØ	�Š˜Ÿ™Ð(:Ô;ØØ	�Š˜Ÿ™¨Ô-ØØ	�ŠØØ	�ŠØØ	�ŠØäð&ß&,¡f¨T£ló
ð 	
rn   )r    Fr)   )Nr,   FT)r,   r   )r€   r‡   F)
r}   rq   rb   z.Literal['forward', 'backward', 'both', 'none']r†   zLiteral['ex', 'sep', 'in']r~   ÚboolÚreturnzKNDArray | DataFrame | tuple[NDArray, NDArray] | tuple[DataFrame, DataFrame])Nr   r€   F)r  zstr | offsets.DateOffsetr  rq   )3Ú
__future__r   Ústatsmodels.compat.pythonr   rc   Únumpyr1   ÚpandasrA   r   Úpandas.tseriesr   Úpandas.tseries.frequenciesr   Útypingr   Ústatsmodels.tools.datar	   r
   r>   r   Ústatsmodels.tools.typingr   Ústatsmodels.tools.validationr   r   r   r   Ú__all__r   rm   r{   r   r   r   r   r±   r«   r·   r   r   r   r   r   rÕ   rÙ   rÝ   râ   rè   rë   r   © rn   r5   ú<module>r     sü   ðÝ "å ,ã ã Û Ý Ý "Ý 0Ý ç AÝ 8Ý ,÷ó ò€ó@óFMPó`,ðb AJØ04Ø!ð	TØðTà?ðTð 0ðTð ð	Tð
 [óTðp EJóT#ònò/òò
ò
ò
(òò8ò,ò +ò*ò.ò,ò2ò*Gò4#ôD*
rn   