
    M/PhP&                         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        | d          } | 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.
    WC)requirementsr   Ndtypearma_transformed_acovf_fastarma_innovations_algo_fastarma_innovations_filter)sigma2nobsr   g      ?)nprequirendim
atleast_1dshaper_r   arrayr   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                           l/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/innovations/arma_innovations.pyarma_innovationsr;      s   B JuD111E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         r:   arma_loglikerA   n   s1    0 eyI%+F< < <G6'??r<   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   r2   funcs           r:   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4r<   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)rA   paramsr)   pqs    r:   rE   zarma_score.<locals>.func   s7    E6"1":vaAgq1uvvOOOr<          @lenr   r   r   r   
r)   r*   r+   r   r-   rE   params0epsilonrK   rL   s
   `       @@r:   
arma_scorerS      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3r<   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 rH   )r>   rI   s    r:   rE   zarma_scoreobs.<locals>.func  s>    ufRaRj&1q5/%a!eff~/ / 	/r<   rM   rN   rP   s
   `       @@r:   arma_scoreobsrV      s    D  'YI'YIIAIA/ / / / / / / eIy&01G7Bc'll;;GGT7333r<   )NNr   FN)NNr   N)numpyr   statsmodels.tsar    statsmodels.tsa.statespace.toolsr   statsmodels.tools.numdiffr   r   scipy.linalg.blasr    r	   r#   r;   rA   r>   rS   rV    r<   r:   <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 .4r<   