
    M/Ph3x                        d Z ddlmZ ddlmZ ddlZddlZddlm	Z	m
Z
mZ ddlmZmZ ddlmZ erej        Znej        j        Zg dZd	Z	 d$dZd%dZd&dZd&dZd'dZd(dZd(dZd(dZd)dZej         ej         dZd Zd Z d*dZ!d*dZ"d Z#d+dZ$ eej                   Z%e%                    ddg           e%&                    d g            e%&                    d!g             G d" d#          Z'dS ),a  ARMA process and estimation with scipy.signal.lfilter

Notes
-----
* written without textbook, works but not sure about everything
  briefly checked and it looks to be standard least squares, see below

* theoretical autocorrelation function of general ARMA
  Done, relatively easy to guess solution, time consuming to get
  theoretical test cases, example file contains explicit formulas for
  acovf of MA(1), MA(2) and ARMA(1,1)

Properties:
Judge, ... (1985): The Theory and Practise of Econometrics

Author: josefpktd
License: BSD
    )NP_LT_2)AppenderN)linalgoptimizesignal)	Docstringremove_parameters)
array_like)	arma_acf
arma_acovfarma_generate_samplearma_impulse_responsearma2ararma2ma
deconvolve
lpol2index
index2lpolzThe model's autoregressive parameters (ar) indicate that the process
 is non-stationary. arma_acovf can only be used with stationary processes.
   c                 &   |t           j        j        n|}t          j        |          dk    r|g}|rrt	          |          }||xx         |z  cc<   t          |          }t          d          gt          |          z  }t          |dd          ||<   t          |          }nAt          |          }t          t          d          gt          j        |          z            }| ||          z  }	t          j	        || |	|          |         S )a  
    Simulate data from an ARMA.

    Parameters
    ----------
    ar : array_like
        The coefficient for autoregressive lag polynomial, including zero lag.
    ma : array_like
        The coefficient for moving-average lag polynomial, including zero lag.
    nsample : int or tuple of ints
        If nsample is an integer, then this creates a 1d timeseries of
        length size. If nsample is a tuple, creates a len(nsample)
        dimensional time series where time is indexed along the input
        variable ``axis``. All series are unless ``distrvs`` generates
        dependent data.
    scale : float
        The standard deviation of noise.
    distrvs : function, random number generator
        A function that generates the random numbers, and takes ``size``
        as argument. The default is np.random.standard_normal.
    axis : int
        See nsample for details.
    burnin : int
        Number of observation at the beginning of the sample to drop.
        Used to reduce dependence on initial values.

    Returns
    -------
    ndarray
        Random sample(s) from an ARMA process.

    Notes
    -----
    As mentioned above, both the AR and MA components should include the
    coefficient on the zero-lag. This is typically 1. Further, due to the
    conventions used in signal processing used in signal.lfilter vs.
    conventions in statistics for ARMA processes, the AR parameters should
    have the opposite sign of what you might expect. See the examples below.

    Examples
    --------
    >>> import numpy as np
    >>> np.random.seed(12345)
    >>> arparams = np.array([.75, -.25])
    >>> maparams = np.array([.65, .35])
    >>> ar = np.r_[1, -arparams] # add zero-lag and negate
    >>> ma = np.r_[1, maparams] # add zero-lag
    >>> y = sm.tsa.arma_generate_sample(ar, ma, 250)
    >>> model = sm.tsa.ARIMA(y, (2, 0, 2), trend='n').fit(disp=0)
    >>> model.params
    array([ 0.79044189, -0.23140636,  0.70072904,  0.40608028])
    Nr   )size)axis)
nprandomstandard_normalndimlisttupleslicelenr   lfilter)
armansamplescaledistrvsr   burninnewsizefsliceetas
             ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/arima_process.pyr   r   6   s   n ,3?bi''G	ww1) 9 w--..++W-VT400tv..d}rww'7'7788
''w'''
'C>"b#D111&99    
   c                    |Lt          j        t          j        |           t          j        |          t          j        |                    }t          |           dz
  }t          |          dz
  }t	          ||          dz   }|j        dk     rt          d          ||cxk    rdk    r n nt          j        ||          }||d<   |S |dk    rPt          j        t          j        t          j	        |                               dk    rt          t                    t          | ||          }	t          j        ||f|          }
t          j        |df|          }t          j        ||          }| |d|dz   <   t          |          D ]}|d|dz            ddd         |
|d|dz   f<   |
|d||z
  fxx         ||dz   |         z  cc<   |t          j        |||dz            |	dt	          |dz   |z
  d                             z  ||<   t          j        t	          ||          |          }	 t           j                            |
|          dddf         |d|<   n,# t           j        j        $ r t          t                    w xY w||k    rct#          j        dg| |d|         ddd                   }t#          j        dg| t          j        ||z
  |          |          d         ||d<   |d|         S )	a[  
    Theoretical autocovariances of stationary ARMA processes

    Parameters
    ----------
    ar : array_like, 1d
        The coefficients for autoregressive lag polynomial, including zero lag.
    ma : array_like, 1d
        The coefficients for moving-average lag polynomial, including zero lag.
    nobs : int
        The number of terms (lags plus zero lag) to include in returned acovf.
    sigma2 : float
        Variance of the innovation term.

    Returns
    -------
    ndarray
        The autocovariance of ARMA process given by ar, ma.

    See Also
    --------
    arma_acf : Autocorrelation function for ARMA processes.
    acovf : Sample autocovariance estimation.

    References
    ----------
    .. [*] Brockwell, Peter J., and Richard A. Davis. 2009. Time Series:
        Theory and Methods. 2nd ed. 1991. New York, NY: Springer.
    Nr   r   z'Must have positive innovation variance.)dtypelags)zi)r   common_typearrayr   maxreal
ValueErrorzerosabsrootsNONSTATIONARY_ERRORr   rangedotr   solveLinAlgErrorr   lfilticr    )r!   r"   nobssigma2r.   pqmout	ma_coeffsAbtmp_arkacovfr2   s                   r*   r   r      s"   < }rx||RXb\\28F;K;KLLB!AB!AAq		AA{QBCCC 	A{{{{{{{{{ht5)))A
	
Q26"&"..//144,--- BQ'''I 	!Qu%%%A
!Qu%%%AXau%%%FF7QU7O1XX P P AE+DDbD1!YAY,	!QQY,61q5A+..r!a!e)}i8M#q1uqy1:M:M8M.NOOO!HSq\\///E.IOOAq))!!!Q$/bqb		9  . . .,---. axx^QCU3A3Z"%566NCRXdQhe444
 
 

abb	 $<s    /I0 0)Jc                 :    t          | ||          }||d         z  S )a  
    Theoretical autocorrelation function of an ARMA process.

    Parameters
    ----------
    ar : array_like
        Coefficients for autoregressive lag polynomial, including zero lag.
    ma : array_like
        Coefficients for moving-average lag polynomial, including zero lag.
    lags : int
        The number of terms (lags plus zero lag) to include in returned acf.

    Returns
    -------
    ndarray
        The autocorrelations of ARMA process given by ar and ma.

    See Also
    --------
    arma_acovf : Autocovariances from ARMA processes.
    acf : Sample autocorrelation function estimation.
    acovf : Sample autocovariance function estimation.
    r   )r   )r!   r"   r0   rL   s       r*   r   r      s$    0 r2t$$E58r+   c                 (   t          j        |          }t          | ||dz             }d|d<   t          d|dz             D ]O}|d|         }t	          j        t	          j        |dd                   |dd                   d         ||dz
  <   P|S )ay  
    Theoretical partial autocorrelation function of an ARMA process.

    Parameters
    ----------
    ar : array_like, 1d
        The coefficients for autoregressive lag polynomial, including zero lag.
    ma : array_like, 1d
        The coefficients for moving-average lag polynomial, including zero lag.
    lags : int
        The number of terms (lags plus zero lag) to include in returned pacf.

    Returns
    -------
    ndarrray
        The partial autocorrelation of ARMA process given by ar and ma.

    Notes
    -----
    Solves yule-walker equation for each lag order up to nobs lags.

    not tested/checked yet
    r   r/         ?r      Nr1   )r   r8   r   r<   r   r>   toeplitz)r!   r"   r0   apacfacovrK   rs          r*   	arma_pacfrU      s    2 HTNNEB***DE!H1dQh H H!H|FOAcrcF$;$;QqrrUCCBGa!eLr+   c                 6   t          j        || ||          \  }}t          j        |          dz  t          j        dt          j        z            z  }t          j        t          j        |                    rddl} |j	        dt          d           ||fS )a0  
    Periodogram for ARMA process given by lag-polynomials ar and ma.

    Parameters
    ----------
    ar : array_like
        The autoregressive lag-polynomial with leading 1 and lhs sign.
    ma : array_like
        The moving average lag-polynomial with leading 1.
    worN : {None, int}, optional
        An option for scipy.signal.freqz (read "w or N").
        If None, then compute at 512 frequencies around the unit circle.
        If a single integer, the compute at that many frequencies.
        Otherwise, compute the response at frequencies given in worN.
    whole : {0,1}, optional
        An options for scipy.signal.freqz/
        Normally, frequencies are computed from 0 to pi (upper-half of
        unit-circle.  If whole is non-zero compute frequencies from 0 to 2*pi.

    Returns
    -------
    w : ndarray
        The frequencies.
    sd : ndarray
        The periodogram, also known as the spectral density.

    Notes
    -----
    Normalization ?

    This uses signal.freqz, which does not use fft. There is a fft version
    somewhere.
    )worNwholerP   r   Nz7Warning: nan in frequency response h, maybe a unit root)
stacklevel)r   freqzr   r9   sqrtpianyisnanwarningswarnRuntimeWarning)r!   r"   rW   rX   whsdr_   s           r*   arma_periodogramre     s    D <BT777DAq	a"'!be),,	,B	vbhqkk 
H	
 	
 	
 	

 b5Lr+   d   c                 `    t          j        |          }d|d<   t          j        || |          S )al  
    Compute the impulse response function (MA representation) for ARMA process.

    Parameters
    ----------
    ar : array_like, 1d
        The auto regressive lag polynomial.
    ma : array_like, 1d
        The moving average lag polynomial.
    leads : int
        The number of observations to calculate.

    Returns
    -------
    ndarray
        The impulse response function with nobs elements.

    Notes
    -----
    This is the same as finding the MA representation of an ARMA(p,q).
    By reversing the role of ar and ma in the function arguments, the
    returned result is the AR representation of an ARMA(p,q), i.e

    ma_representation = arma_impulse_response(ar, ma, leads=100)
    ar_representation = arma_impulse_response(ma, ar, leads=100)

    Fully tested against matlab

    Examples
    --------
    AR(1)

    >>> arma_impulse_response([1.0, -0.8], [1.], leads=10)
    array([ 1.        ,  0.8       ,  0.64      ,  0.512     ,  0.4096    ,
            0.32768   ,  0.262144  ,  0.2097152 ,  0.16777216,  0.13421773])

    this is the same as

    >>> 0.8**np.arange(10)
    array([ 1.        ,  0.8       ,  0.64      ,  0.512     ,  0.4096    ,
            0.32768   ,  0.262144  ,  0.2097152 ,  0.16777216,  0.13421773])

    MA(2)

    >>> arma_impulse_response([1.0], [1., 0.5, 0.2], leads=10)
    array([ 1. ,  0.5,  0.2,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ])

    ARMA(1,2)

    >>> arma_impulse_response([1.0, -0.8], [1., 0.5, 0.2], leads=10)
    array([ 1.        ,  1.3       ,  1.24      ,  0.992     ,  0.7936    ,
            0.63488   ,  0.507904  ,  0.4063232 ,  0.32505856,  0.26004685])
    rO   r   )r   r8   r   r    )r!   r"   leadsimpulses       r*   r   r   =  s/    l huooGGAJ>"b'***r+   c                 &    t          | ||          S )a  
    A finite-lag approximate MA representation of an ARMA process.

    Parameters
    ----------
    ar : ndarray
        The auto regressive lag polynomial.
    ma : ndarray
        The moving average lag polynomial.
    lags : int
        The number of coefficients to calculate.

    Returns
    -------
    ndarray
        The coefficients of AR lag polynomial with nobs elements.

    Notes
    -----
    Equivalent to ``arma_impulse_response(ma, ar, leads=100)``
    rh   r   r!   r"   r0   s      r*   r   r   x      , !Rt4444r+   c                 &    t          || |          S )a  
    A finite-lag AR approximation of an ARMA process.

    Parameters
    ----------
    ar : array_like
        The auto regressive lag polynomial.
    ma : array_like
        The moving average lag polynomial.
    lags : int
        The number of coefficients to calculate.

    Returns
    -------
    ndarray
        The coefficients of AR lag polynomial with nobs elements.

    Notes
    -----
    Equivalent to ``arma_impulse_response(ma, ar, leads=100)``
    rk   rl   rm   s      r*   r   r     rn   r+      r!   c                    fd}|Bt           j        dt          j        dz
            z  t          j        |dz
            f         }n|}t	          j        ||| d          }t          j        |d                   }	t           j        d|	ddz
           f         f}
t           j        d|	dz
  d         f         }|
||fS )a  
    Find arma approximation to ar process.

    This finds the ARMA(p,q) coefficients that minimize the integrated
    squared difference between the impulse_response functions (MA
    representation) of the AR and the ARMA process. This does not  check
    whether the MA lag polynomial of the ARMA process is invertible, neither
    does it check the roots of the AR lag polynomial.

    Parameters
    ----------
    ar_des : array_like
        The coefficients of original AR lag polynomial, including lag zero.
    p : int
        The length of desired AR lag polynomials.
    q : int
        The length of desired MA lag polynomials.
    n : int
        The number of terms of the impulse_response function to include in the
        objective function for the approximation.
    mse : str, 'ar'
        Not used.
    start : ndarray
        Initial values to use when finding the approximation.

    Returns
    -------
    ar_app : ndarray
        The coefficients of the AR lag polynomials of the approximation.
    ma_app : ndarray
        The coefficients of the MA lag polynomials of the approximation.
    res : tuple
        The result of optimize.leastsq.

    Notes
    -----
    Extension is possible if we want to match autocovariance instead
    of impulse response function.
    c                     t           j        d| d dz
           f         t           j        d| dz
  d          f         }}t          ||          }||z
  S )Nr   )r   r_r   )armaar_desr!   r"   	ar_approxnrC   s        r*   	msear_errzar2arma.<locals>.msear_err  sX    q$wQw-'("%4A=0@*AB)"b!44		!!r+   Ngr   i  )maxfevr   )r   rs   onesr8   r   leastsq
atleast_1d)ru   rC   rD   rw   msestartrx   arma0resarma_appar_appma_apps    ` `        r*   ar2armar     s    \" " " " " "
 }dRWQU^^+RXa!e__<=

9eVD
A
A
AC}SV$$HeAx!a%(()+FU1hq1uww''(F63r+   )r!   r"   c                     t          j                    5  t          j        dt                     t	          | d          } ddd           n# 1 swxY w Y   t          j        |           d         }| |         }||fS )a4  
    Remove zeros from lag polynomial

    Parameters
    ----------
    ar : array_like
        coefficients of lag polynomial

    Returns
    -------
    coeffs : ndarray
        non-zero coefficients of lag polynomial
    index : ndarray
        index (lags) of lag polynomial with non-zero elements
    ignorer!   Nr   )r_   catch_warningssimplefilterComplexWarningr
   r   nonzero)r!   indexcoeffss      r*   r   r     s      
	 	"	" " "h777D!!" " " " " " " " " " " " " " " JrNN1EYF5=s   +AAAc                 \    t          |          }t          j        |dz             }| ||<   |S )a3  
    Expand coefficients to lag poly

    Parameters
    ----------
    coeffs : ndarray
        non-zero coefficients of lag polynomial
    index : ndarray
        index (lags) of lag polynomial with non-zero elements

    Returns
    -------
    ar : array_like
        coefficients of lag polynomial
    r   )r5   r   r8   )r   r   rw   r!   s       r*   r   r     s.      	E

A	!a%BBuIIr+   c                     ddl m} t          j        |          }t          j         || |z              ||dz             z
   ||           z
            S )a@  MA representation of fractional integration

    .. math:: (1-L)^{-d} for |d|<0.5  or |d|<1 (?)

    Parameters
    ----------
    d : float
        fractional power
    n : int
        number of terms to calculate, including lag zero

    Returns
    -------
    ma : ndarray
        coefficients of lag polynomial
    r   gammalnr   scipy.specialr   r   arangeexp)drw   r   js       r*   	lpol_fimar     s]    $ &%%%%%
	!A6''!a%..771q5>>1GGAJJ>???r+   c                     ddl m} t          j        |          }t          j         ||  |z              ||dz             z
   ||            z
             }d|d<   |S )a  AR representation of fractional integration

    .. math:: (1-L)^{d} for |d|<0.5  or |d|<1 (?)

    Parameters
    ----------
    d : float
        fractional power
    n : int
        number of terms to calculate, including lag zero

    Returns
    -------
    ar : ndarray
        coefficients of lag polynomial

    Notes:
    first coefficient is 1, negative signs except for first term,
    ar(L)*x_t
    r   r   r   r   )r   rw   r   r   r!   s        r*   	lpol_fiarr   3  sq    , &%%%%%
	!A
&!a771q5>>1GGQBKK?
@
@	@BBqEIr+   c                 $    dgdg| dz
  z  z   dgz   S )zreturn coefficients for seasonal difference (1-L^s)

    just a trivial convenience function

    Parameters
    ----------
    s : int
        number of periods in season

    Returns
    -------
    sdiff : list, length s+1
    r   r   r1    )ss    r*   
lpol_sdiffr   R  s"     3!A"%%r+   c           	      0   t          j        |           } t          j        |          }t          |           }t          |          }||k    r|g }| }n|||z
  dz   }t          j        |t                    }d|d<   t          j        | ||          }t          j        ||d          }t          |           t          |          k     rEt          j        | t          j        t          |          t          |           z
            f          } | |z
  }||fS )a  Deconvolves divisor out of signal, division of polynomials for n terms

    calculates den^{-1} * num

    Parameters
    ----------
    num : array_like
        signal or lag polynomial
    denom : array_like
        coefficients of lag polynomial (linear filter)
    n : None or int
        number of terms of quotient

    Returns
    -------
    quot : ndarray
        quotient or filtered series
    rem : ndarray
        remainder

    Notes
    -----
    If num is a time series, then this applies the linear filter den^{-1}.
    If both num and den are both lag polynomials, then this calculates the
    quotient polynomial for n terms and also returns the remainder.

    This is copied from scipy.signal.signaltools and added n as optional
    parameter.
    Nr   r   full)mode)	r   r|   r   r8   floatr   r    convolveconcatenate)	numdenrw   NDquotreminput
num_approxs	            r*   r   r   c  s    < -

C
-

CCACA1uu9A	AE""a~c3.._S$V<<<
s88c*oo%%.#rxJ#c((0J'K'K!LMMCJ9r+   r"   NotesExamplesc                   &   e Zd ZdZddZedd            Zedd            Zed d            Zd Z	d	 Z
d
 Z e eej        g d                    d d            Z e eej        ddg                    d d            Z e eej        ddg                    d d            Z e eej        g d                    d d            Z e eej        ddg                    d d            Z e eej        ddg                    d d            Z e eej        ddg                    d d            Zed             Zed             Zed             Zed             Zd!dZ e e e!                    	 d"d            Z"dS )#ArmaProcessaF	  
    Theoretical properties of an ARMA process for specified lag-polynomials.

    Parameters
    ----------
    ar : array_like
        Coefficient for autoregressive lag polynomial, including zero lag.
        Must be entered using the signs from the lag polynomial representation.
        See the notes for more information about the sign.
    ma : array_like
        Coefficient for moving-average lag polynomial, including zero lag.
    nobs : int, optional
        Length of simulated time series. Used, for example, if a sample is
        generated. See example.

    Notes
    -----
    Both the AR and MA components must include the coefficient on the
    zero-lag. In almost all cases these values should be 1. Further, due to
    using the lag-polynomial representation, the AR parameters should
    have the opposite sign of what one would write in the ARMA representation.
    See the examples below.

    The ARMA(p,q) process is described by

    .. math::

        y_{t}=\phi_{1}y_{t-1}+\ldots+\phi_{p}y_{t-p}+\theta_{1}\epsilon_{t-1}
               +\ldots+\theta_{q}\epsilon_{t-q}+\epsilon_{t}

    and the parameterization used in this function uses the lag-polynomial
    representation,

    .. math::

        \left(1-\phi_{1}L-\ldots-\phi_{p}L^{p}\right)y_{t} =
            \left(1+\theta_{1}L+\ldots+\theta_{q}L^{q}\right)\epsilon_{t}

    Examples
    --------
    ARMA(2,2) with AR coefficients 0.75 and -0.25, and MA coefficients 0.65 and 0.35

    >>> import statsmodels.api as sm
    >>> import numpy as np
    >>> np.random.seed(12345)
    >>> arparams = np.array([.75, -.25])
    >>> maparams = np.array([.65, .35])
    >>> ar = np.r_[1, -arparams] # add zero-lag and negate
    >>> ma = np.r_[1, maparams] # add zero-lag
    >>> arma_process = sm.tsa.ArmaProcess(ar, ma)
    >>> arma_process.isstationary
    True
    >>> arma_process.isinvertible
    True
    >>> arma_process.arroots
    array([1.5-1.32287566j, 1.5+1.32287566j])
    >>> y = arma_process.generate_sample(250)
    >>> model = sm.tsa.ARIMA(y, (2, 0, 2), trend='n').fit(disp=0)
    >>> model.params
    array([ 0.79044189, -0.23140636,  0.70072904,  0.40608028])

    The same ARMA(2,2) Using the from_coeffs class method

    >>> arma_process = sm.tsa.ArmaProcess.from_coeffs(arparams, maparams)
    >>> arma_process.arroots
    array([1.5-1.32287566j, 1.5+1.32287566j])
    Nrf   c                 D   |t          j        dg          }|t          j        dg          }t          j                    5  t          j        dt
                     t          |d          | _        t          |d          | _        d d d            n# 1 swxY w Y   | j        dd           | _	        | j        dd          | _
        t           j                            | j                  | _        t           j                            | j                  | _        || _        d S )NrO   r   r!   r"   r   )r   r4   r_   r   r   r   r
   r!   r"   arcoefsmacoefs
polynomial
PolynomialarpolymapolyrA   )selfr!   r"   rA   s       r*   __init__zArmaProcess.__init__  s&   :3%B:3%B$&& 	+ 	+!(N;;; T**DG T**DG	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ |wqrr{m..tw77m..tw77			s   ABBBc                    |Vt          |          rGt          j        j        j                            |          }|j        dd         |j        d         z  }ng }|Vt          |          rGt          j        j        j                            |          }|j        dd         |j        d         z  }ng } | t          j        d|f         t          j        d|f         |          S )a  
        Create ArmaProcess from AR and MA polynomial roots.

        Parameters
        ----------
        maroots : array_like, optional
            Roots for the MA polynomial
            1 + theta_1*z + theta_2*z^2 + ..... + theta_n*z^n
        arroots : array_like, optional
            Roots for the AR polynomial
            1 - phi_1*z - phi_2*z^2 - ..... - phi_n*z^n
        nobs : int, optional
            Length of simulated time series. Used, for example, if a sample
            is generated.

        Returns
        -------
        ArmaProcess
            Class instance initialized with arcoefs and macoefs.

        Examples
        --------
        >>> arroots = [.75, -.25]
        >>> maroots = [.65, .35]
        >>> arma_process = sm.tsa.ArmaProcess.from_roots(arroots, maroots)
        >>> arma_process.isstationary
        True
        >>> arma_process.isinvertible
        True
        Nr   r   rA   )r   r   r   r   	fromrootscoefrs   )clsmarootsarrootsrA   r   r   r   r   s           r*   
from_rootszArmaProcess.from_roots  s    @ 3w<<]-8BB7KKFk!""oA6GGG3w<<]-8BB7KKFk!""oA6GGG s25G$beAwJ&7dCCCCr+   c                     |g n|}|g n|} | t           j        dt          j        |           f         t           j        dt          j        |          f         |          S )a	  
        Create ArmaProcess from an ARMA representation.

        Parameters
        ----------
        arcoefs : array_like
            Coefficient for autoregressive lag polynomial, not including zero
            lag. The sign is inverted to conform to the usual time series
            representation of an ARMA process in statistics. See the class
            docstring for more information.
        macoefs : array_like
            Coefficient for moving-average lag polynomial, excluding zero lag.
        nobs : int, optional
            Length of simulated time series. Used, for example, if a sample
            is generated.

        Returns
        -------
        ArmaProcess
            Class instance initialized with arcoefs and macoefs.

        Examples
        --------
        >>> arparams = [.75, -.25]
        >>> maparams = [.65, .35]
        >>> arma_process = sm.tsa.ArmaProcess.from_coeffs(ar, ma)
        >>> arma_process.isstationary
        True
        >>> arma_process.isinvertible
        True
        Nr   r   )r   rs   asarray)r   r   r   rA   s       r*   from_coeffszArmaProcess.from_coeffs   sp    B  ""W""WsE!bj))))*E!RZ((()
 
 
 	
r+   c                 D    |p|j         } | |j        |j        |          S )a  
        Create an ArmaProcess from the results of an ARIMA estimation.

        Parameters
        ----------
        model_results : ARIMAResults instance
            A fitted model.
        nobs : int, optional
            If None, nobs is taken from the results.

        Returns
        -------
        ArmaProcess
            Class instance initialized from model_results.

        See Also
        --------
        statsmodels.tsa.arima.model.ARIMA
            The models class used to create the ArmaProcess
        r   )rA   polynomial_reduced_arpolynomial_reduced_ma)r   model_resultsrA   s      r*   from_estimationzArmaProcess.from_estimationI  s9    , )})s//
 
 
 	
r+   c                    t          || j                  r)| j        |j        z  j        }| j        |j        z  j        }nw	 |\  }}t
          j                            |          }t
          j                            |          }| j        |z  j        }| j        |z  j        }n#  t          d          xY w|                     ||| j	                  S )NzOther type is not a valid typer   )

isinstance	__class__r   r   r   r   r   r   	TypeErrorrA   )r   othr!   r"   arothmaoth	arpolyoth	mapolyoths           r*   __mul__zArmaProcess.__mul__f  s    c4>** 	B+
*0B+
*0BBB"uM44U;;	M44U;;	kI-3kI-3B @AAA~~b"49~555s    A!B" "B3c           
          d}|                     | j                                        | j                                        | j        t          t          |                               S )Nz&ArmaProcess({0}, {1}, nobs={2}) at {3})formatr!   tolistr"   rA   hexid)r   msgs     r*   __repr__zArmaProcess.__repr__u  sL    6zzGNNdgnn..	3r$xx==
 
 	
r+   c                     d                     | j                                        | j                                                  S )NzArmaProcess
AR: {}
MA: {})r   r!   r   r"   r   s    r*   __str__zArmaProcess.__str__{  s7    ,33GNNdgnn..
 
 	
r+   )r!   r"   rB   c                 L    |p| j         }t          | j        | j        |          S )Nr   )rA   r   r!   r"   r   rA   s     r*   rL   zArmaProcess.acovf  s'     ty$'476666r+   r!   r"   c                 L    |p| j         }t          | j        | j        |          S Nr/   )rA   r   r!   r"   r   r0   s     r*   acfzArmaProcess.acf  s'     tyt4444r+   c                 L    |p| j         }t          | j        | j        |          S r   )rA   rU   r!   r"   r   s     r*   pacfzArmaProcess.pacf  s'     ty$'5555r+   )r!   r"   rW   rX   c                 L    |p| j         }t          | j        | j        |          S )N)rW   )rA   re   r!   r"   r   s     r*   periodogramzArmaProcess.periodogram  s)      tyt<<<<r+   c                 L    |p| j         }t          | j        | j        |          S )Nrk   )rA   r   r!   r"   )r   rh   s     r*   impulse_responsezArmaProcess.impulse_response  s'    "$TWdgUCCCCr+   c                 L    |p| j         }t          | j        | j        |          S r   )r0   r   r!   r"   r   s     r*   r   zArmaProcess.arma2ma  '     tytwd3333r+   c                 L    |p| j         }t          | j        | j        |          S r   )r0   r   r!   r"   r   s     r*   r   zArmaProcess.arma2ar  r   r+   c                 4    | j                                         S )z&Roots of autoregressive lag-polynomial)r   r:   r   s    r*   r   zArmaProcess.arroots       {  """r+   c                 4    | j                                         S )z&Roots of moving average lag-polynomial)r   r:   r   s    r*   r   zArmaProcess.maroots  r   r+   c                 h    t          j        t          j        | j                  dk              rdS dS )z
        Arma process is stationary if AR roots are outside unit circle.

        Returns
        -------
        bool
             True if autoregressive roots are outside unit circle.
        rO   TF)r   allr9   r   r   s    r*   isstationaryzArmaProcess.isstationary  s1     6"&&&,-- 	45r+   c                 h    t          j        t          j        | j                  dk              rdS dS )z
        Arma process is invertible if MA roots are outside unit circle.

        Returns
        -------
        bool
             True if moving average roots are outside unit circle.
        r   TF)r   r   r9   r   r   s    r*   isinvertiblezArmaProcess.isinvertible  s1     6"&&&*++ 	45r+   Fc                 f   | j         }| j        }| j        }|ssd|t          j        |          dk              z  |t          j        |          dk     <   t          j        j                            |          }|j        |j        d         z  }|r"| 	                    | j
        || j                  S ||fS )a  
        Make MA polynomial invertible by inverting roots inside unit circle.

        Parameters
        ----------
        retnew : bool
            If False (default), then return the lag-polynomial as array.
            If True, then return a new instance with invertible MA-polynomial.

        Returns
        -------
        manew : ndarray
           A new invertible MA lag-polynomial, returned if retnew is false.
        wasinvertible : bool
           True if the MA lag-polynomial was already invertible, returned if
           retnew is false.
        armaprocess : new instance of class
           If retnew is true, then return a new instance with invertible
           MA-polynomial.
        rO   r   r   r   )r   r"   r   r   r9   r   r   r   r   r   r!   rA   )r   retnewprmainv
invertiblepnews         r*   invertrootszArmaProcess.invertroots  s    , \&
 	-!$r"&**q.'9!9BrvbzzA~=+55b99DI	!,E 	%>>$'5ty>AAA*$$r+   rO   r   c           	      B    t          | j        | j        |||||          S )N)r   r&   )r   r!   r"   )r   r#   r$   r%   r   r&   s         r*   generate_samplezArmaProcess.generate_sample  s.     $GTWgugD
 
 
 	
r+   )NNrf   N)F)rf   rO   Nr   r   )#__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r   r   r	   r   rL   r   r   rU   r   re   r   r   r   r   r   propertyr   r   r   r   r   str_generate_sample_docr   r   r+   r*   r   r     s\       B BJ    -D -D -D [-D^ &
 &
 &
 [&
P 
 
 
 [
86 6 6
 
 

 
 

 X
 24J4J4JKKLL7 7 7 ML7 X 04,??@@5 5 5 A@5 X	 1D$<@@AA6 6 6 BA6 X$&C&C&C	
 	
 
= = = 
= X 5 =d|LLMMD D D NMD X$>>??4 4 4 @?4 X$>>??4 4 4 @?4 # # X# # # X#   X   X!% !% !% !%F Xcc&''((CD
 
 
 )(
 
 
r+   r   )r   Nr   r   )r,   r   N)r,   )Nr   )rf   )rp   r!   N)rp   r   )(r  statsmodels.compat.numpyr   statsmodels.compat.pandasr   r_   numpyr   scipyr   r   r   statsmodels.tools.docstringr   r	   statsmodels.tools.validationr
   r   
exceptions__all__r;   r   r   r   rU   re   r   r   r   r   
_arma_docsr   r   r   r   r   r   r  replace_blockr   r   r+   r*   <module>r     s   $ - , , , , , . . . . . .      * * * * * * * * * * D D D D D D D D 3 3 3 3 3 3
 2&NN]1N
 
 
  <=G: G: G: G:TK K K K\   8       F- - - -`8+ 8+ 8+ 8+v5 5 5 525 5 5 54; ; ; ;| O7?;;
  0  ,@ @ @ @2   >& & &"/ / / /d !y!5!=>>   & &d| 4 4 4  " "7B / / /  " ":r 2 2 2]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
 ]
r+   