
    "g                         d dl 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	 dggdgdggdgddgd	ggd
gddgdgd	dgdggdZ
d Zd Z ej                  dej                  z        Zd Zd Zd Z G d de      Zy)    N)HermiteE)	factorial)rv_continuous)   r   )r      )r   r   )r      )r   r   )r      )r   r   )r	   r   )r   r   r   r	   c                 l    | dk  rt        d| z        	 t        |    S # t        $ r t        d      w xY w)a<  
    Return all non-negative integer solutions of the diophantine equation

            n*k_n + ... + 2*k_2 + 1*k_1 = n   (1)

    Parameters
    ----------
    n : int
        the r.h.s. of Eq. (1)

    Returns
    -------
    partitions : list
        Each solution is itself a list of the form `[(m, k_m), ...]`
        for non-zero `k_m`. Notice that the index `m` is 1-based.

    Examples:
    ---------
    >>> _faa_di_bruno_partitions(2)
    [[(1, 2)], [(2, 1)]]
    >>> for p in _faa_di_bruno_partitions(4):
    ...     assert 4 == sum(m * k for (m, k) in p)
    r   z+Expected a positive integer; got %s insteadz'Higher order terms not yet implemented.)
ValueError_faa_di_bruno_cacheKeyErrorNotImplementedError)ns    `/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/distributions/edgeworth.py_faa_di_bruno_partitionsr      sL    0 	1uFJKKM"1%% M ""KLLMs    3c           	         |dk  rt        d|z        t        |       |k  rt        |d|dt        |       d      d}t        |      D ]q  }t        d |D              }d|dz
  z  t	        |dz
        z  }|D ]<  \  }}|t        j                  | |dz
     t	        |      z  |      t	        |      z  z  }> ||z  }s |t	        |      z  }|S )	au  Compute n-th cumulant given moments.

    Parameters
    ----------
    momt : array_like
        `momt[j]` contains `(j+1)`-th moment.
        These can be raw moments around zero, or central moments
        (in which case, `momt[0]` == 0).
    n : int
        which cumulant to calculate (must be >1)

    Returns
    -------
    kappa : float
        n-th cumulant.
    r   z,Expected a positive integer. Got %s instead.z-th cumulant requires z moments, only got .        c              3   &   K   | ]	  \  }}|  y wN .0mks      r   	<genexpr>z(cumulant_from_moments.<locals>.<genexpr>P   s     "fq!"   )r   lenr   sumr   nppower)momtr   kappaprtermr   r   s           r   cumulant_from_momentsr(   8   s    " 	1uG!KLL
4y1}+,aT< = 	=E%a( """a!e}yQ// 	KFQBHHT!a%[9Q<7;ilJJD	K 
Yq\EL    r   c                 H    t        j                  | dz   dz        t        z  S )Nr   g       @)r!   exp_norm_pdf_Cxs    r   	_norm_pdfr/   [   s     661a4%){**r)   c                 ,    t        j                  |       S r   specialndtrr-   s    r   	_norm_cdfr4   ^   s    <<?r)   c                 .    t        j                  |        S r   r1   r-   s    r   _norm_sfr6   a   s    <<r)   c                   <     e Zd ZdZd fd	Zd Zd Zd Zd Z xZ	S )ExpandedNormalav  Construct the Edgeworth expansion pdf given cumulants.

    Parameters
    ----------
    cum : array_like
        `cum[j]` contains `(j+1)`-th cumulant: cum[0] is the mean,
        cum[1] is the variance and so on.

    Notes
    -----
    This is actually an asymptotic rather than convergent series, hence
    higher orders of the expansion may or may not improve the result.
    In a strongly non-Gaussian case, it is possible that the density
    becomes negative, especially far out in the tails.

    Examples
    --------
    Construct the 4th order expansion for the chi-square distribution using
    the known values of the cumulants:

    >>> import matplotlib.pyplot as plt
    >>> from scipy import stats
    >>> from scipy.special import factorial
    >>> df = 12
    >>> chi2_c = [2**(j-1) * factorial(j-1) * df for j in range(1, 5)]
    >>> edgw_chi2 = ExpandedNormal(chi2_c, name='edgw_chi2', momtype=0)

    Calculate several moments:
    >>> m, v = edgw_chi2.stats(moments='mv')
    >>> np.allclose([m, v], [df, 2 * df])
    True

    Plot the density function:
    >>> mu, sigma = df, np.sqrt(2*df)
    >>> x = np.linspace(mu - 3*sigma, mu + 3*sigma)
    >>> fig1 = plt.plot(x, stats.chi2.pdf(x, df=df), 'g-', lw=4, alpha=0.5)
    >>> fig2 = plt.plot(x, stats.norm.pdf(x, mu, sigma), 'b--', lw=4, alpha=0.5)
    >>> fig3 = plt.plot(x, edgw_chi2.pdf(x), 'r-', lw=2)
    >>> plt.show()

    References
    ----------
    .. [*] E.A. Cornish and R.A. Fisher, Moments and cumulants in the
         specification of distributions, Revue de l'Institut Internat.
         de Statistique. 5: 307 (1938), reprinted in
         R.A. Fisher, Contributions to Mathematical Statistics. Wiley, 1950.
    .. [*] https://en.wikipedia.org/wiki/Edgeworth_series
    .. [*] S. Blinnikov and R. Moessner, Expansions for nearly Gaussian
        distributions, Astron. Astrophys. Suppl. Ser. 130, 193 (1998)
    c                    t        |      dk  rt        d      | j                  |      \  | _        | _        | _        t        | j                        | _        | j                  j                  dkD  rt        | j                  dd         | _	        nd | _	        t        j                  | j                  j                               }|| j                  z
  | j
                  z  }|t        j                  |      dk(  t        j                  |      dk  z     j                         rd|z  }t!        j"                  |t$               |j'                  |dd       t)        | T  d	i | y )
Nr   z"At least two cumulants are needed.r   c                      y)Nr   r   r-   s    r   <lambda>z)ExpandedNormal.__init__.<locals>.<lambda>   s    r)   r   r	   zPDF has zeros at %s )namemomtyper   )r   r   _compute_coefs_pdf_coef_mu_sigmar   	_herm_pdfsize	_herm_cdfr!   real_if_closerootsimagabsanywarningswarnRuntimeWarningupdatesuper__init__)selfcumr<   kwdsr&   mesg	__class__s         r   rO   zExpandedNormal.__init__   s   s8a<ABB,0,C,CC,H)
DHdk!$**-::??Q%tzz!"~o6DN)DN T^^1134\T[[(bggajAo"&&)a-01557)A-DMM$/T !# 	$ 4 r)   c                     || j                   z
  | j                  z  }| j                  |      t        |      z  | j                  z  S r   )r@   rA   rB   r/   rP   r.   ys      r   _pdfzExpandedNormal._pdf   s9    \T[[(~~a 9Q</$++==r)   c                     || j                   z
  | j                  z  }t        |      | j                  |      t	        |      z  z   S r   )r@   rA   r4   rD   r/   rV   s      r   _cdfzExpandedNormal._cdf   s>    \T[[(!q!IaL01 	2r)   c                     || j                   z
  | j                  z  }t        |      | j                  |      t	        |      z  z
  S r   )r@   rA   r6   rD   r/   rV   s      r   _sfzExpandedNormal._sf   s>    \T[[(q!IaL01 	2r)   c                 X   |d   t        j                  |d         }}t        j                  |      }t        |      D ]  \  }}||xx   |d   |z  z  cc<    t        j                  |j
                  dz  dz
        }d|d<   t        |j
                  dz
        D ]  }t        |dz         D ]v  }	||dz   z  }
|	D ]?  \  }}|
t        j                  ||dz      t        |dz         z  |      t        |      z  z  }
A t        d |	D              }||dz   d|z  z   xx   |
z  cc<   x  |||fS )Nr   r   r      g      ?r   c              3   &   K   | ]	  \  }}|  y wr   r   r   s      r   r   z4ExpandedNormal._compute_coefs_pdf.<locals>.<genexpr>   s     *fq!*r   )r!   sqrtasarray	enumeratezerosrC   ranger   r"   r   r    )rP   rQ   musigmalamjlcoefsr%   r'   r   r   r&   s                 r   r>   z!ExpandedNormal._compute_coefs_pdf   s>   FBGGCFOEjjocN 	 DAqFc!faiF	  xx1q()Qsxx!|$ 	*A-ac2 *qs| RFQBHHS1X	!A#%>BYq\QQDR***QUQqS[!T)!*	* Rr)   )zEdgeworth expanded normal)
__name__
__module____qualname____doc__rO   rX   rZ   r\   r>   __classcell__)rT   s   @r   r8   r8   e   s"    1d!*>2
2
r)   r8   )rJ   numpyr!   numpy.polynomial.hermite_er   scipy.specialr   scipy.statsr   r2   r   r   r(   r`   pir,   r/   r4   r6   r8   r   r)   r   <module>rv      s      / # %  H:HvhHvv&1Hvv&662BVHM	O MDD bggag+f] fr)   