
    M/Ph                         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dS )    N)HermiteE)	factorial)rv_continuous)   r   )r      )r   r   )r      )r   r   )r      )r   r   )r	   r   )r   r   r   r	   c                     | 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    c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/distributions/edgeworth.py_faa_di_bruno_partitionsr      sb    0 	1uuFJKKKM"1%% M M M ""KLLLMs	   ' Ac           	         |dk     rt          d|z            t          |           |k     r%t          |d|dt          |           d          d}t          |          D ]}t          d |D                       }d|dz
  z  t	          |dz
            z  }|D ]F\  }}|t          j        | |dz
           t	          |          z  |          t	          |          z  z  }G||z  }|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   | ]	\  }}|V  
d S 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!   " 	1uuG!KLLL
4yy1}}+,11aaaT< = = 	=E%a((  """""""a!e}yQ/// 	K 	KFQBHT!a%[9Q<<7;;illJJDD	Yq\\ELr   r   c                 H    t          j        | dz   dz            t          z  S )Nr   g       @)r!   exp_norm_pdf_Cxs    r   	_norm_pdfr.   [   s!    61a4%){**r   c                 *    t          j        |           S r   specialndtrr,   s    r   	_norm_cdfr3   ^   s    <??r   c                 ,    t          j        |            S r   r0   r,   s    r   _norm_sfr5   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)
    Edgeworth expanded normalc                    t          |          dk     rt          d          |                     |          \  | _        | _        | _        t          | j                  | _        | j        j        dk    r#t          | j        dd                     | _	        nd | _	        t          j        | j                                                  }|| j        z
  | j        z  }|t          j        |          dk    t          j        |          dk     z                                           rd|z  }t!          j        |t$                     |                    |dd            t)                      j        d	i | d S )
Nr   z"At least two cumulants are needed.r   c                     dS )Nr   r   r,   s    r   <lambda>z)ExpandedNormal.__init__.<locals>.<lambda>   s    r 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__   sS   s88a<<ABBB,0,C,CC,H,H)
DHdk!$*--:?Q%tz!""~o66DNN)\DN T^113344\T[(bgajjAo"&))a-015577 	0)A-DM$///T !# # 	$ 	$ 	$  4     r   c                     || j         z
  | j        z  }|                     |          t          |          z  | j        z  S r   )r@   rA   rB   r.   rP   r-   ys      r   _pdfzExpandedNormal._pdf   s8    \T[(~~a  9Q<</$+==r   c                     || j         z
  | j        z  }t          |          |                     |          t	          |          z  z   S r   )r@   rA   r3   rD   r.   rV   s      r   _cdfzExpandedNormal._cdf   sA    \T[(!q!!IaLL01 	2r   c                     || j         z
  | j        z  }t          |          |                     |          t	          |          z  z
  S r   )r@   rA   r5   rD   r.   rV   s      r   _sfzExpandedNormal._sf   sA    \T[(q!!IaLL01 	2r   c                    |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 ]}	||dz   z  }
|	D ]I\  }}|
t          j        ||dz            t          |dz             z  |          t          |          z  z  }
Jt          d |	D                       }||dz   d|z  z   xx         |
z  cc<   |||fS )Nr   r   r      g      ?r   c              3       K   | ]	\  }}|V  
d S 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   sr   FBGCFOOEjoocNN 	  	 DAqFFFc!faiFFFFx1q())Qsx!|$$ 	* 	*A-ac22 * *qs| R RFQBHS1X	!A#%>BBYq\\QQDD*******QUQqS[!!!T)!!!!* Rr   )r8   )
__name__
__module____qualname____doc__rO   rX   rZ   r\   r>   __classcell__)rT   s   @r   r7   r7   e   s        1 1d! ! ! ! ! !*> > >2 2 2
2 2 2
      r   r7   )rJ   numpyr!   numpy.polynomial.hermite_er   scipy.specialr   scipy.statsr   r1   r   r   r(   r`   pir+   r.   r3   r5   r7   r   r   r   <module>rv      s^        / / / / / / # # # # # # % % % % % %       H:HvhHvv&1Hvv&662BVHM	O O M M MD  D bgag+ + +    f f f f f] f f f f fr   