
    'YHh,                     @   d 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mZ ddl
mZmZ ddlmZ ddlmZmZ dd	lmZ d
dgZd
dgZdZ eeed      Z ee      dd Z eee      Zd3dZd4dZd Zd Zd Z d Z!d Z"d Z#d Z$d
dgZ%d
dgZ&d
dgZ'g dZ(d
dgZ) e*de$fde#fde"fde"fg      Z+de&e'ffde&e(ffde%e'ffde%e)ffgZ,e,D ]  \  Z-Z.e.\  ZZ e/d        e/e-ee        eeed       Z0 eeed!      Z1e-dd" d#k(  r e+e-   e      Z2n e+e-   ee      Z2 e/e0dd$         e/e2dd$         ee0e2d        ee1e2e2d   z  d        d5d%Z3d6d&Z4d7d'Z5d( Z6d)ed)dfd*Z7 e3       Z8 ee8d         Z9e9ju                  d+      Z; e/e;d           ee8d         Z< ee8d   d,-      Z= e4e8d         Z> e4e8d   e8d   j                         z
        Z@ e/e<dd         e/e=dd         e/e>dd         e/e@dd         ed
dgd
gd.      Z e/ ee      dd/         e/ e	j                  ed              ej                  e        ej                           e7eee        ej                           e7eeed,0        ej                           eee<dd/  ej                   eEe<dd/             d)0        ej                           ej                  d1      ZG eeGe<dd/ d)0        ej                  d2      ZG eeGe<dd/  ej                   eEe<dd/             d,0       y)8ztrying to verify theoretical acf of arma

explicit functions for autocovariance functions of ARIMA(1,1), MA(1), MA(2)
plus 3 functions from nitime.utils

    N)assert_array_almost_equal)
regression)arma_generate_samplearma_impulse_response)
arma_acovfarma_acf)ARIMA)acfacovf)plot_acf      ?g333333皙? i  
   c                 H    ||dk(  rt        |       S |dk(  rt        |       S y )Nconstantlinear)detrend_meandetrend_linear)xkeys     ]/var/www/html/planif/env/lib/python3.12/site-packages/statsmodels/sandbox/tsa/example_arma.pydetrendr   %   s.    
{c:oA	ha   
    c                     t        j                  |       } |rEt        d      g|z  }|j                  t         j                         | | j                  |      |   z
  S | | j                  |      z
  S )z0Return x minus its mean along the specified axisN)npasarraysliceappendnewaxismean)r   axisinds      r   demeanr$   +   s]    


1AT{md"

2::166$<$$$qvvd|r   c                 (    | | j                         z
  S )zReturn x minus the mean(x))r!   r   s    r   r   r   4   s    qvvx<r   c                     | S )zReturn x: no detrending r&   s    r   detrend_noner)   8   s    Hr   c                    t        j                  t        |       t         j                        }t        j                  || d      }|d   |d   z  }| j                         ||j                         z  z
  }| ||z  |z   z
  S )z2Return y minus best fit line; 'linear' detrending )dtype   )bias)r   r,   )r   r   )r   arangelenfloat_covr!   )yr   Cbas        r   r   r   <   sk     			#a&		*A
q!!A	#qvA	1QVVX:A!a=r   c           	          t        | |      }t        d      D cg c]!  }t        j                  |d||z
   |||       # }}|S c c}w )z6add correlation of MA representation explicitely

    r   N)r   ranger   dot)armanobsirt	acovfexpls         r   acovf_explicitr?   E   sN     
r2	&B:?)DQ7DFR$Z0DID Es   &Ac                    | d    }|d   }d|dz  z   d|z  |z  z   d|dz  z
  z  g}|j                  d||z  z   ||z   z  d|dz  z
  z         t        d      D ]  }|d   }|j                  ||z          t        j                  |      S )Nr,   r         )r   r7   r   array)r9   r:   r5   r4   rho_lasts          r   acovf_arma11rH   M   s    
 
AA
1A q!tGAaCEMBq!tG$
%CJJ!A#!}bAg&'1X 2w

1T6 88C=r   c                     | d    }| d    }t        j                  d      }d|dz  z   |dz  z   |d<   | ||z  z   |d<   | |d<   |S )Nr,   rA   r   r   r   zeros)r:   b1b2rE   s       r   	acovf_ma2rN   `   sd     Q%B
Q%B
((2,C"a%i"a%CFcBrEkCFSCFJr   c                 ^    | d    }t        j                  d      }d|dz  z   |d<   | |d<   |S )Nr,   r   rA   r   rJ   )r:   r4   rE   s      r   	acovf_ma1rP   o   s=     
AA
((2,C!Q$hCFRCFJr   gg        )r   r   g333333?ma1ma2arma11ar1)r;   )lagsrA   r:      c                    t        j                  g d      }t         j                  j                  | |dz        }t        j                  |       }t        |      }t        |      D ]-  }||   t        j                  |d | d d d   |d |       z   ||<   / t        ||       D ]-  }||   t        j                  |||z
  | d d d   |      z   ||<   / |||fS )N)g@gQ|g|?5^:@g oŏg      ?)sizescalerC   )r   rD   randomnormalrK   r/   r7   r8   )NsigmatapsvuPls          r   ar_generatorrc      s    
 8867D
		aucz2A
AD	A1X 4tbffQrU4R4[$r(33!41QZ 3tbffQqs1Xdd^T22!3a:r   c                     | j                   |   }t        j                  j                  | d|z  dz
  |      }t        j                  j                  ||j	                         z  |      j
                  d| }||z  S )zReturns the autocorrelation of signal s at all lags. Adheres to the
definition r(k) = E{s(n)s*(n-k)} where E{} is the expectation operator.
rA   r,   )nr"   )r"   N)shaper   fftifft	conjugatereal)sr"   r\   Ssxxs        r   autocorrrn      sg     	
A


1!AD
)A
&&++aoD+
1
6
6r
:Cq5Lr   c                     t        j                  | ||      t        j                  |       t        j                  |      z  | j                  d   z  z  S )zReturns the correlation between two ndarrays, by calling np.correlate in
'same' mode and normalizing the result by the std of the arrays and by
their lengths. This results in a correlation = 1 for an auto-correlationrC   )r   	correlatestdrf   )r   r2   modes      r   	norm_corrrs      sC    
 \\!Ad#ffQiq	!1772;/1 3r   c                 *     | j                   ||fi |S )a9  
    call signature::

        acorr(x, normed=True, detrend=detrend_none, usevlines=True,
              maxlags=10, **kwargs)

    Plot the autocorrelation of *x*.  If *normed* = *True*,
    normalize the data by the autocorrelation at 0-th lag.  *x* is
    detrended by the *detrend* callable (default no normalization).

    Data are plotted as ``plot(lags, c, **kwargs)``

    Return value is a tuple (*lags*, *c*, *line*) where:

      - *lags* are a length 2*maxlags+1 lag vector

      - *c* is the 2*maxlags+1 auto correlation vector

      - *line* is a :class:`~matplotlib.lines.Line2D` instance
        returned by :meth:`plot`

    The default *linestyle* is None and the default *marker* is
    ``'o'``, though these can be overridden with keyword args.
    The cross correlation is performed with
    :func:`numpy.correlate` with *mode* = 2.

    If *usevlines* is *True*, :meth:`~matplotlib.axes.Axes.vlines`
    rather than :meth:`~matplotlib.axes.Axes.plot` is used to draw
    vertical lines from the origin to the acorr.  Otherwise, the
    plot style is determined by the kwargs, which are
    :class:`~matplotlib.lines.Line2D` properties.

    *maxlags* is a positive integer detailing the number of lags
    to show.  The default value of *None* will return all
    :math:`2 \mathrm{len}(x) - 1` lags.

    The return value is a tuple (*lags*, *c*, *linecol*, *b*)
    where

    - *linecol* is the
      :class:`~matplotlib.collections.LineCollection`

    - *b* is the *x*-axis.

    .. seealso::

        :meth:`~matplotlib.axes.Axes.plot` or
        :meth:`~matplotlib.axes.Axes.vlines`
           For documentation on valid kwargs.

    **Example:**

    :func:`~matplotlib.pyplot.xcorr` above, and
    :func:`~matplotlib.pyplot.acorr` below.

    **Example:**

    .. plot:: mpl_examples/pylab_examples/xcorr_demo.py
    )xcorr)selfr   kwargss      r   pltacorrrx      s    x 4::a%f%%r   Tc                 H   t        |      }|t        |      k7  rt        d       |t        j                  |            } |t        j                  |            }t        j                  ||d      }	|rC|	t        j
                  t        j                  ||      t        j                  ||      z        z  }	||dz
  }||k\  s|dk  rt        d|z        t        j                  | |dz         }
|	|dz
  |z
  ||z    }	|ra | j                  |
dg|	fi |} | j                  di |}|j                  dd	       |j                  d
d        | j                  |
|	fi |}n<|j                  dd	       |j                  d
d        | j                  |
|	fi |\  }d}|
|	||fS )a  
    call signature::

        def xcorr(self, x, y, normed=True, detrend=detrend_none,
          usevlines=True, maxlags=10, **kwargs):

    Plot the cross correlation between *x* and *y*.  If *normed* =
    *True*, normalize the data by the cross correlation at 0-th
    lag.  *x* and y are detrended by the *detrend* callable
    (default no normalization).  *x* and *y* must be equal length.

    Data are plotted as ``plot(lags, c, **kwargs)``

    Return value is a tuple (*lags*, *c*, *line*) where:

      - *lags* are a length ``2*maxlags+1`` lag vector

      - *c* is the ``2*maxlags+1`` auto correlation vector

      - *line* is a :class:`~matplotlib.lines.Line2D` instance
         returned by :func:`~matplotlib.pyplot.plot`.

    The default *linestyle* is *None* and the default *marker* is
    'o', though these can be overridden with keyword args.  The
    cross correlation is performed with :func:`numpy.correlate`
    with *mode* = 2.

    If *usevlines* is *True*:

       :func:`~matplotlib.pyplot.vlines`
       rather than :func:`~matplotlib.pyplot.plot` is used to draw
       vertical lines from the origin to the xcorr.  Otherwise the
       plotstyle is determined by the kwargs, which are
       :class:`~matplotlib.lines.Line2D` properties.

       The return value is a tuple (*lags*, *c*, *linecol*, *b*)
       where *linecol* is the
       :class:`matplotlib.collections.LineCollection` instance and
       *b* is the *x*-axis.

    *maxlags* is a positive integer detailing the number of lags to show.
    The default value of *None* will return all ``(2*len(x)-1)`` lags.

    **Example:**

    :func:`~matplotlib.pyplot.xcorr` above, and
    :func:`~matplotlib.pyplot.acorr` below.

    **Example:**

    .. plot:: mpl_examples/pylab_examples/xcorr_demo.py
    zx and y must be equal lengthrA   )rr   Nr,   z.maxlags must be None or strictly positive < %dr   markero	linestyleNoner(   )r/   
ValueErrorr   r   rp   sqrtr8   r.   vlinesaxhline
setdefaultplot)rv   r   r2   normedr   	usevlinesmaxlagsrw   NxcrU   r5   r4   ds                 r   pltxcorrr     s   p 
QB	3q6z788

1A

1A
Q"A	RWWRVVAq\BFF1aL011q&"}! )+-. / 	/ 99gXgai(D	"Q$w,r'z"A DKKqc1//DLL"6"(C(+v.DIIdA(( 	(C(+v.TYYtQ)&)Aq=r   )   r   r   F)unbiasedi     )r         )N)r   )i   r   )rC   )valid)H__doc__numpyr   numpy.testingr   matplotlib.pyplotpyplotpltstatsmodelsr   statsmodels.tsa.arima_processr   r   r   r   statsmodels.tsa.arima.modelr	   statsmodels.tsa.stattoolsr
   r   statsmodels.graphics.tsaplotsr   r9   r:   modr   x_acfx_irr   r$   r   r)   r   r?   rH   rN   rP   rT   ar0rQ   rR   ma0dict	comparefncasesr   argsprintmyacovfmyacfothacovfrc   rn   rs   rx   r   arrvsarmafitresacf1acovf1bacf2r!   acf2myule_walkerr   figurer.   r/   subplotaxr(   r   r   <module>r      s    3  " U > - 0 2$Z#Y 	R&AsR$!&	 
4j	2h	3i	2h
				< 		 	 #s		c
	C:	c
	
  >GAtFB	"I	!RRb)GR"%E!ud{9Q<#9Q<B'	'"1+	(2A, gx3eXhqk%9"=#>*&3<&| !%l"[D 	 U1X
hhx c!f
58}
a5
)aq%(--/)* d3Bi  gcrl  d3Bi  eCRj  #tseS1 c!fSbk  jQ# $ 	 

 Qq  


 QqE " 


 d3Bi3tCRy>2d C 


S[[ T#2Y$ 'S[[ T#2Y			#d3Bi.1U Cr   