
    Vfd:&                         d dl 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Z	 	 dd
ZddZ	 	 ddZ	 	 ddZ	 	 ddZdS )    N)arima_process)prefix_dtype_map)_get_epsilonapprox_fprime_cs)find_best_blas_type   )_arma_innovationszThe model's autoregressive parameters (ar_params) indicate that the process
 is non-stationary. The innovations algorithm cannot be used.
Fc           	         t          j        |           } | j        dk    }|r| dddf         } t          j        |g n|          }t          j        |g n|          }| j        \  }}t           j        d| f         }	t           j        d|f         }
|)t          | ||t          j        |          g          \  }}}t          |         }t          j        | |          } t          j        ||          }t          j        ||          } ||          	                                }t          t          |dz             }t          t          |dz             }t          t          |dz             }t          j        |	|
||          |z  } ||	|
|          \  }} ||||||          \  }}t          j        |          }t          j        |dk               sLt          j        |                                          r&t          j        |                                          st#          t$                    g }t'          |          D ]A}t          j         || dd|f         |||                    }|                    |           Bt          j        |          j        }|r||dddf         d	z  z  }|r|                                }||fS )
ay  
    Compute innovations using a given ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process, may be univariate or multivariate.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    normalize : bool, optional
        Whether or not to normalize the returned innovations. Default is False.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    innovations : ndarray
        Innovations (one-step-ahead prediction errors) for the given `endog`
        series with predictions based on the given ARMA process. If
        `normalize=True`, then the returned innovations have been "whitened" by
        dividing through by the square root of the mean square error.
    innovations_mse : ndarray
        Mean square error for the innovations.
    r   Ndtypearma_transformed_acovf_fastarma_innovations_algo_fastarma_innovations_filter)sigma2nobsr   g      ?)nparrayndim
atleast_1dshaper_r   r   asfortranarrayitemgetattrr	   r   
arma_acovfanyisfiniteall
ValueErrorNON_STATIONARY_ERRORrangeappendvstackTsqueeze)endog	ar_params	ma_paramsr   	normalizeprefixsqueezedr   k_endogarmar   _r   r   r   r   acovfacovf2thetavuiu_is                           Llib/python3.11/site-packages/statsmodels/tsa/innovations/arma_innovations.pyarma_innovationsr8      s   B HUOOEzQH aaagI$5bb9EEII$5bb9EEIKMD'	q9*}	B	q)|	B ~.Iy"(6*:*:;= =qV$E e5111E!)5999I!)5999IU6]]!!F #*6$AA#C #C!(6$@@"B "B%6$==? ? )"b17dD D DFLMJ//B
CCME6))$	9*/9 9HE1
A
q1u /E""&&((/A""$$/ -... 	A7^^  h..uQQQT{I/8%A A B B	
	!A 	Qqqq$wZ_  IIKKa4K    c                 R    t          | ||||          }t          j        |          S )a  
    Compute the log-likelihood of the given data assuming an ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    float
        The joint loglikelihood.
    )r'   r(   r   r*   )arma_loglikeobsr   sum)r&   r'   r(   r   r*   llf_obss         r7   arma_logliker>   n   s1    0 eyI%+F< < <G6'??r9   c                    t          j        |           } t          j        |g n|          }t          j        |g n|          }|)t          | ||t          j        |          g          \  }}}t          |         }t          j        | |          } t          j        ||          }t          j        ||          } ||                                          }t          t          |dz             } || |||          S )a  
    Compute the log-likelihood for each observation assuming an ARMA process.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive parameters.
    ma_params : ndarray, optional
        Moving average parameters.
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Array of loglikelihood values for each observation.
    Nr   arma_loglikeobs_fast)
r   r   r   r   r   ascontiguousarrayr   r   r   r	   )r&   r'   r(   r   r*   r   r/   funcs           r7   r;   r;      s    2 HUOOEI$5bb9EEII$5bb9EEI~.Iy"(6*:*:;= =qV$E e444E!)5999I!)5999IU6]]!!F$f/E&EFFD4y)V444r9   c                     	 |g n|}|g n|}t          |          t          |          	 	fd}t          j        |||f         }t          |ddt          |                    }t	          |||          S )a  
    Compute the score (gradient of the log-likelihood function).

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive coefficients, not including the zero lag.
    ma_params : ndarray, optional
        Moving average coefficients, not including the zero lag, where the sign
        convention assumes the coefficients are part of the lag polynomial on
        the right-hand-side of the ARMA definition (i.e. they have the same
        sign from the usual econometrics convention in which the coefficients
        are on the right-hand-side of the ARMA definition).
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Score, evaluated at the given parameters.

    Notes
    -----
    This is a numerical approximation, calculated using first-order complex
    step differentiation on the `arma_loglike` method.
    Nc                 d    t          | d          | z            | z   d                    S N)r>   paramsr&   pqs    r7   rB   zarma_score.<locals>.func   s7    E6"1":vaAgq1uvvOOOr9          @lenr   r   r   r   
r&   r'   r(   r   r*   rB   params0epsilonrH   rI   s
   `       @@r7   
arma_scorerP      s    D  'YI'YIIAIAP P P P P P P eIy&01G7Bc'll;;GGT7333r9   c                     	 |g n|}|g n|}t          |          t          |          	 	fd}t          j        |||f         }t          |ddt          |                    }t	          |||          S )a  
    Compute the score (gradient) per observation.

    Parameters
    ----------
    endog : ndarray
        The observed time-series process.
    ar_params : ndarray, optional
        Autoregressive coefficients, not including the zero lag.
    ma_params : ndarray, optional
        Moving average coefficients, not including the zero lag, where the sign
        convention assumes the coefficients are part of the lag polynomial on
        the right-hand-side of the ARMA definition (i.e. they have the same
        sign from the usual econometrics convention in which the coefficients
        are on the right-hand-side of the ARMA definition).
    sigma2 : ndarray, optional
        The ARMA innovation variance. Default is 1.
    prefix : str, optional
        The BLAS prefix associated with the datatype. Default is to find the
        best datatype based on given input. This argument is typically only
        used internally.

    Returns
    -------
    ndarray
        Score per observation, evaluated at the given parameters.

    Notes
    -----
    This is a numerical approximation, calculated using first-order complex
    step differentiation on the `arma_loglike` method.
    Nc                 d    t          | d          | z            | z   d                    S rE   )r;   rF   s    r7   rB   zarma_scoreobs.<locals>.func  s>    ufRaRj&1q5/%a!eff~/ / 	/r9   rJ   rK   rM   s
   `       @@r7   arma_scoreobsrS      s    D  'YI'YIIAIA/ / / / / / / eIy&01G7Bc'll;;GGT7333r9   )NNr   FN)NNr   N)numpyr   statsmodels.tsar    statsmodels.tsa.statespace.toolsr   statsmodels.tools.numdiffr   r   scipy.linalg.blasr    r	   r    r8   r>   r;   rP   rS    r9   r7   <module>r[      s       ) ) ) ) ) ) = = = = = = D D D D D D D D 1 1 1 1 1 1        DE-1\ \ \ \~   : CD(5 (5 (5 (5V >?-4 -4 -4 -4` AB.4 .4 .4 .4 .4 .4r9   