
    "gP&                         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y)    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.
c           	      V   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                  ||      } ||      j                         }t        t        |dz         }t        t        |dz         }t        t        |dz         }t        j                  |	|
||	      |z  } ||	|
|      \  }} ||||||      \  }}t        j                  |      }t        j                  |d
k        sFt        j                   |      j#                         r#t        j                   |      j#                         st%        t&              g }t)        |      D ]8  }t        j                   || dd|f   |||            }|j+                  |       : t        j,                  |      j.                  }|r||dddf   dz  z  }|r|j1                         }||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                           i/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/tsa/innovations/arma_innovations.pyarma_innovationsr;      s   B JJuD1EzzQHagI$5b9EII$5b9EIKKMD'	q9*}	B	q)|	B ~.Iy"((6*:;=qV$E e51E!!)59I!!)59I6]!F #*6$AA#C!(6$@@"B%6$==? ))"b17dDFLMJ/B
CME6)$	9*/9HE1
A
q1uE"&&(A""$-.. 	A7^ hh.uQT{I/8%A B	 			!A	Qq$wZ_ IIKa4K    c                 L    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   s'    0 eyI%+F<G66'?r<   c                    t        j                  |       } t        j                  |g n|      }t        j                  |g n|      }|&t        | ||t        j                  |      g      \  }}}t        |   }t        j
                  | |      } t        j                  ||      }t        j                  ||      } ||      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.
    r   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 HHUOEI$5b9EII$5b9EI~.Iy"((6*:;=qV$E  e4E!!)59I!!)59I6]!F$f/E&EFDy)V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                 >    t        | d  | z    | z   d        S N)rA   paramsr)   pqs    r:   rE   zarma_score.<locals>.func   s.    E6"1:vaAq1uvOOr<          @lenr   r   r   r   
r)   r*   r+   r   r-   rE   params0epsilonrK   rL   s
   `       @@r:   
arma_scorerS      sp    D  'YI'YIIAIAP eeIy&01G7Bc'l;GGT7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                 >    t        | d  | z    | z   d        S rH   )r>   rI   s    r:   rE   zarma_scoreobs.<locals>.func  s3    ufRaj&1q5/%a!ef~/ 	/r<   rM   rN   rP   s
   `       @@r:   arma_scoreobsrV      so    D  'YI'YIIAIA/ eeIy&01G7Bc'l;GGT7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^      sb     ) = D 1   DE-1\~: CD(5V >?-4` AB.4r<   