ó
áp7]c           @` s=  d  d l  m Z m Z m Z 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 j Z i d g g d 6d g d g g d 6d g d d g d g g d 6d g d d g d g d d g d g g d	 6Z d
 „  Z d „  Z e j d e j ƒ Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d S(   i    (   t   divisiont   print_functiont   absolute_importN(   t   HermiteE(   t	   factorial(   t   rv_continuousi   i   i   i   c         C` sO   |  d k  r t  d |  ƒ ‚ n  y t |  SWn t k
 rJ t d ƒ ‚ n Xd S(   sN   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: a list of solutions of (1). 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)

    i   s+   Expected a positive integer; got %s insteads'   Higher order terms not yet implemented.N(   t
   ValueErrort   _faa_di_bruno_cachet   KeyErrort   NotImplementedError(   t   n(    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   _faa_di_bruno_partitions   s    c         C` s	  | d k  r t  d | ƒ ‚ n  t |  ƒ | k  rS t  d | | t |  ƒ f ƒ ‚ n  d } x™ t | ƒ D]‹ } t d „  | Dƒ ƒ } d | d t | d ƒ } xF | D]> \ } } | t j |  | d t | ƒ | ƒ t | ƒ 9} q¥ W| | 7} qf W| t | ƒ 9} | S(   sw  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: integer
        which cumulant to calculate (must be >1)

    Returns
    -------
    kappa: float
        n-th cumulant.

    i   s,   Expected a positive integer. Got %s instead.s0   %s-th cumulant requires %s moments, only got %s.g        c         s` s   |  ] \ } } | Vq d  S(   N(    (   t   .0t   mt   k(    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pys	   <genexpr>S   s    iÿÿÿÿ(   R   t   lenR   t   sumR   t   npt   power(   t   momtR
   t   kappat   pt   rt   termR   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   cumulant_from_moments:   s    6c         C` s   t  j |  d d ƒ t S(   Ni   g       @(   R   t   expt   _norm_pdf_C(   t   x(    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt	   _norm_pdf^   s    c         C` s   t  j |  ƒ S(   N(   t   specialt   ndtr(   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt	   _norm_cdfa   s    c         C` s   t  j |  ƒ S(   N(   R   R   (   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   _norm_sfd   s    t   ExpandedNormalc           B` s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sr  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.misc 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.
    .. [*] http://en.wikipedia.org/wiki/Edgeworth_series
    .. [*] S. Blinnikov and R. Moessner, Expansions for nearly Gaussian
        distributions, Astron. Astrophys. Suppl. Ser. 130, 193 (1998)

    s   Edgeworth expanded normalc         K` s<  t  | ƒ d k  r! t d ƒ ‚ n  |  j | ƒ \ |  _ |  _ |  _ t |  j ƒ |  _ |  j j d k r€ t |  j d ƒ |  _	 n d „  |  _	 t
 j |  j j ƒ  ƒ } | |  j |  j } | t
 j | ƒ d k t
 j | ƒ d k  @j ƒ  rd | } t j | t ƒ n  | j i | d 6d d	 6ƒ t t |  ƒ j |   d  S(
   Ni   s"   At least two cumulants are needed.i   c         S` s   d S(   Ng        (    (   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   <lambda>¤   t    i    i   s   PDF has zeros at %s t   namet   momtype(   R   R   t   _compute_coefs_pdft   _coeft   _mut   _sigmaR   t	   _herm_pdft   sizet	   _herm_cdfR   t   real_if_closet   rootst   imagt   abst   anyt   warningst   warnt   RuntimeWarningt   updatet   superR!   t   __init__(   t   selft   cumR$   t   kwdsR   t   mesg(    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyR7   œ   s    !2
c         C` s2   | |  j  |  j } |  j | ƒ t | ƒ |  j S(   N(   R(   R)   R*   R   (   R8   R   t   y(    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   _pdf±   s    c         C` s5   | |  j  |  j } t | ƒ |  j | ƒ t | ƒ S(   N(   R(   R)   R   R,   R   (   R8   R   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   _cdfµ   s    	c         C` s5   | |  j  |  j } t | ƒ |  j | ƒ t | ƒ S(   N(   R(   R)   R    R,   R   (   R8   R   R<   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   _sfº   s    	c      	   C` sY  | d t  j | d ƒ } } t  j | ƒ } x2 t | ƒ D]$ \ } } | | c | d | <q: Wt  j | j d d ƒ } d | d <xÃ t | j d ƒ D]® } x¥ t | d ƒ D]“ }	 | | d }
 xJ |	 D]B \ } } |
 t  j | | d t	 | d ƒ | ƒ t	 | ƒ 9}
 qÌ Wt
 d „  |	 Dƒ ƒ } | | d d | c |
 7<q± Wqš W| | | f S(   Ni    i   i   i   g      ð?i   c         s` s   |  ] \ } } | Vq d  S(   N(    (   R   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pys	   <genexpr>Í   s    (   R   t   sqrtt   asarrayt	   enumeratet   zerosR+   t   rangeR   R   R   R   (   R8   R9   t   mut   sigmat   lamt   jt   lt   coeft   sR   R   R   R   R   (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyR&   ¿   s    
:$(   t   __name__t
   __module__t   __doc__R7   R=   R>   R?   R&   (    (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyR!   h   s   3			(   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (   t
   __future__R    R   R   R2   t   numpyR   t   numpy.polynomial.hermite_eR   t   statsmodels.compat.scipyR   t   scipy.statsR   t   scipy.specialR   R   R   R   R@   t   piR   R   R   R    R!   (    (    (    sB   lib/python2.7/site-packages/statsmodels/distributions/edgeworth.pyt   <module>   s$   .	"	#			