o
    0Gf                     @   s   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d Zdd Zedej Zdd Zdd Zdd ZG d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                 C   s6   | dk r
t d|  zt|  W S  ty   td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)n r   Clib/python3.10/site-packages/statsmodels/distributions/edgeworth.py_faa_di_bruno_partitions   s   
r   c                 C   s   |dk r
t d| t| |k rt d||t| f d}t|D ]6}tdd |D }d|d  t|d  }|D ]\}}|t| |d  t| |t| 9 }q:||7 }q!|t|9 }|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.z0%s-th cumulant requires %s moments, only got %s.        c                 s       | ]\}}|V  qd S Nr   .0mkr   r   r   	<genexpr>P       z(cumulant_from_moments.<locals>.<genexpr>)r	   lenr   sumr   nppower)Zmomtr   Zkappaprtermr   r   r   r   r   cumulant_from_moments8   s   *
r"   r   c                 C   s   t | d  d t S )Nr   g       @)r   Zexp_norm_pdf_Cxr   r   r   	_norm_pdf[   s   r&   c                 C   s
   t | S r   specialZndtrr$   r   r   r   	_norm_cdf^   s   
r)   c                 C   s   t |  S r   r'   r$   r   r   r   _norm_sfa   s   r*   c                       sB   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Z  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                    s   t |dk r
td| |\| _| _| _t| j| _| jjdkr-t| jdd   | _	ndd | _	t
| j }|| j | j }|t
|dkt
|dk @   r^d| }t|t ||dd	 t jd
i | d S )Nr   z"At least two cumulants are needed.r   c                 S   s   dS )Nr   r   r$   r   r   r   <lambda>   s    z)ExpandedNormal.__init__.<locals>.<lambda>r   r   zPDF has zeros at %s )nameZmomtyper   )r   r	   _compute_coefs_pdfZ_coef_mu_sigmar   	_herm_pdfsize	_herm_cdfr   Zreal_if_closerootsimagabsanywarningswarnRuntimeWarningupdatesuper__init__)selfcumr.   kwdsr    Zmesg	__class__r   r   r>      s    
$zExpandedNormal.__init__c                 C   s(   || j  | j }| |t| | j S r   )r0   r1   r2   r&   r?   r%   yr   r   r   _pdf   s   zExpandedNormal._pdfc                 C   s*   || j  | j }t|| |t|  S r   )r0   r1   r)   r4   r&   rD   r   r   r   _cdf      zExpandedNormal._cdfc                 C   s*   || j  | j }t|| |t|  S r   )r0   r1   r*   r4   r&   rD   r   r   r   _sf   rH   zExpandedNormal._sfc              
   C   s  |d t |d }}t |}t|D ]\}}||  |d |   < qt |jd d }d|d< t|jd D ]E}t|d D ]<}	||d  }
|	D ]\}}|
t ||d  t	|d  |t	| 9 }
qMt
dd |	D }||d d|    |
7  < qCq;|||fS )	Nr   r   r      g      ?r   c                 s   r   r   r   r   r   r   r   r      r   z4ExpandedNormal._compute_coefs_pdf.<locals>.<genexpr>)r   sqrtZasarray	enumerateZzerosr3   ranger   r   r   r   )r?   r@   ZmuZsigmaZlamjlZcoefsr   r!   r   r   r    r   r   r   r/      s   
.
z!ExpandedNormal._compute_coefs_pdf)r,   )
__name__
__module____qualname____doc__r>   rF   rG   rI   r/   __classcell__r   r   rB   r   r+   e   s    2r+   )r9   Znumpyr   Znumpy.polynomial.hermite_er   Zscipy.specialr   Zscipy.statsr   r(   r
   r   r"   rK   Zpir#   r&   r)   r*   r+   r   r   r   r   <module>   s$    	
""