ó
áp7]c           @  s‰  d  Z  d d l m Z d d l Z d d l m Z m Z m Z d d l Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z m Z d d l m Z m Z d	 e j f d
 „  ƒ  YZ e ƒ  Z d e j f d „  ƒ  YZ e d d d d d d ƒ Z d e j f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d e j f d „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ d „  Z  d^ \ Z! Z" d_ \ Z! Z" d# „  Z# d$ „  Z$ d% „  Z% e e	 j& e# e$ d& e' d' d( d d) d* d+ d d, ƒZ( e e	 j& e j e j) d' d- d. d( d d/ d* d0 d d1 ƒZ* e e	 j+ e j) e j d' d2 ƒZ, d3 e j f d4 „  ƒ  YZ- d5 e j f d6 „  ƒ  YZ. d7 e j f d8 „  ƒ  YZ/ d9 e0 f d: „  ƒ  YZ1 e1 ƒ  Z2 e/ e	 j& e2 j3 e2 j4 e2 j5 e2 j6 e2 j7 d; d< d. d= d> e j8 d' d( d d? d* d@ d dA dB ƒZ9 e/ e	 j: e2 j3 e2 j4 e2 j5 e2 j6 e2 j7 d; d< d. d= d> e j8 d' d2 d d? d* dC d dD dE ƒZ; dF „  Z4 dG „  Z5 dH „  Z6 dI „  Z7 dJ „  Z< e/ e	 j& e< e4 e5 e6 e7 d; dK d. e j8 d> d= d' d( d dL d* dM d dN dO ƒZ= dP „  Z4 dQ „  Z5 dR „  Z6 dS „  Z7 dT „  Z> e/ e	 j& e j? e4 e5 e6 e7 d; d< d. d= d> e j8 d' d( d dU d* dV d dW dX ƒZ@ i dY d( 6dZ d2 6d[ d- 6ZA d\ „  ZB d d] „ ZD d S(`   sà  Various extensions to distributions

* skew normal and skew t distribution by Azzalini, A. & Capitanio, A.
* Gram-Charlier expansion distribution (using 4 moments),
* distributions based on non-linear transformation
  - Transf_gen
  - ExpTransf_gen, LogTransf_gen
  - TransfTwo_gen
    (defines as examples: square, negative square and abs transformations)
  - this versions are without __new__
* mnvormcdf, mvstdnormcdf : cdf, rectangular integral for multivariate normal
  distribution

TODO:
* Where is Transf_gen for general monotonic transformation ? found and added it
* write some docstrings, some parts I don't remember
* add Box-Cox transformation, parameterized ?


this is only partially cleaned, still includes test examples as functions

main changes
* add transf_gen (2010-05-09)
* added separate example and tests (2010-05-09)
* collect transformation function into classes

Example
-------

>>> logtg = Transf_gen(stats.t, np.exp, np.log,
                numargs = 1, a=0, name = 'lnnorm',
                longname = 'Exp transformed normal',
                extradoc = '
distribution of y = exp(x), with x standard normal'
                'precision for moment andstats is not very high, 2-3 decimals')
>>> logtg.cdf(5, 6)
0.92067704211191848
>>> stats.t.cdf(np.log(5), 6)
0.92067704211191848

>>> logtg.pdf(5, 6)
0.021798547904239293
>>> stats.t.pdf(np.log(5), 6)
0.10899273954837908
>>> stats.t.pdf(np.log(5), 6)/5.  #derivative
0.021798547909675815


Author: josef-pktd
License: BSD

iÿÿÿÿ(   t   print_functionN(   t   poly1dt   sqrtt   exp(   t   statst   special(   t   distributions(   t   ranget	   iteritems(   t   mvsk2mct   mc2mvskt   SkewNorm_genc           B  sG   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   s   univariate Skew-Normal distribution of Azzalini

    class follows scipy.stats.distributions pattern
    but with __init__


    c         C  s&   t  j j |  d d d d d d ƒd  S(   Nt   names   Skew Normal distributiont   shapest   alphat   extradoct    (   R   t   rv_continuoust   __init__(   t   self(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   L   s    c         C  s   d S(   Ni   (    (   R   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt	   _argcheckR   s    c         C  s‚   | t  j d | d ƒ } t j j d |  j ƒ } | | t  j d | d ƒ t j j d |  j ƒ } t  j | d k | | ƒ S(   Ni   i   t   sizei    (   t   npR   R   t   normt   rvst   _sizet   where(   R   R   t   deltat   u0t   u1(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   _rvsU   s    5c         C  s   |  j  | | ƒ S(   N(   t   _mom0_sc(   R   t   nR   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   _munp\   s    c         C  s?   d t  j d t  j ƒ t  j | d d ƒ t j | | ƒ S(   Ng       @i   (   R   R   t   piR   R   t   ndtr(   R   t   xR   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   _pdfa   s    t   mvskc         C  s   d  S(   N(    (   R   R$   R   t   moments(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   _stats_skipe   s    (	   t   __name__t
   __module__t   __doc__R   R   R   R!   R%   R(   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   D   s   					t   SkewNorm2_genc           B  s    e  Z d  Z d „  Z d „  Z RS(   sj   univariate Skew-Normal distribution of Azzalini

    class follows scipy.stats.distributions pattern

    c         C  s   d S(   Ni   (    (   R   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   s   s    c         C  s?   d t  j d t  j ƒ t  j | d d ƒ t j | | ƒ S(   Ng       @i   (   R   R   R"   R   R   R#   (   R   R$   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR%   v   s    (   R)   R*   R+   R   R%   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR,   m   s   	R   s   Skew Normal distributionR   R   R   s     -inf < alpha < inft   ACSkewT_genc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sz   univariate Skew-T distribution of Azzalini

    class follows scipy.stats.distributions pattern
    but with __init__
    c         C  s&   t  j j |  d d d d d d ƒd  S(   NR   s   Skew T distributionR   s	   df, alphaR   s¼  
Skewed T distribution by Azzalini, A. & Capitanio, A. (2003)_

the pdf is given by:
 pdf(x) = 2.0 * t.pdf(x, df) * t.cdf(df+1, alpha*x*np.sqrt((1+df)/(x**2+df)))

with alpha >=0

Note: different from skewed t distribution by Hansen 1999
.._
Azzalini, A. & Capitanio, A. (2003), Distributions generated by perturbation of
symmetry with emphasis on a multivariate skew-t distribution,
appears in J.Roy.Statist.Soc, series B, vol.65, pp.367-389

(   R   R   R   (   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   …   s    c         C  s   | | k | d k S(   Ni    (    (   R   t   dfR   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   ™   s    c         C  sH   t  j j | d |  j ƒ} t j | d |  j ƒ} | t j | | ƒ S(   NR   (   R   t   chi2R   R   t   skewnormR   R   (   R   R.   R   t   Vt   z(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   ¡   s    c         C  s   |  j  | | | ƒ S(   N(   R   (   R   R    R.   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR!   ¨   s    c         C  sL   d t  j j | | ƒ t j | d | | t j d | | d | ƒ ƒ S(   Ng       @i   i   (   R   t   tR%   R   t   stdtrR   R   (   R   R$   R.   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR%   ­   s    (   R)   R*   R+   R   R   R   R!   R%   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR-      s   				c         C  so   d  g |  } t d ƒ | d <xK t d |  ƒ D]: } | | d j ƒ  t d d g ƒ | | d | | <q- W| S(   Ni   i    (   t   NoneR   R   t   deriv(   t   Nt   plistR    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt	   _hermnormÙ   s
    8c   	        sh  t  |  ƒ } | d k  r' t d ƒ ‚ n  t d ƒ ‰ t |  d ƒ ‰ |  d ‰  | d k rl t | d ƒ } n  xÚ t d | d ƒ D]Å } d } xŠ t | d d ƒ D]t } | d | } | d rÐ |  | d } n' |  | d ˆ ˆ t j | d ƒ } | | | | ˆ | | 7} q¡ Wt ‚ t	 | ƒ t	 | ƒ ˆ | | | ‰ q€ W‡  ‡ ‡ f d †  } | ˆ f S(   sÛ   Return the Gaussian expanded pdf function given the list of central
    moments (first one is mean).

    version of scipy.stats, any changes ?
    the scipy.stats version has a bug and returns normal distribution

    i   s:   At least two moments must be given to approximate the pdf.i   i    i   g        c           s@   |  ˆ  ˆ } ˆ | ƒ t  | | d ƒ t d t j ƒ ˆ S(   Ng       @i   (   R   R   R   R"   (   R$   t   xn(   t   mut   sigt   totp(    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   thisfunc  s    (
   t   lent
   ValueErrorR   R   R9   R   t   scipyt
   factorial2t   SystemErrort   print(	   t   cntR7   t   Dvalst   kt   CkR    t   mt   momdiffR>   (    (   R;   R<   R=   sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   pdf_moments_stå   s,    	

'"

c   	        s»   t  |  ƒ } | d k  r' t d ƒ ‚ n  |  \ ‰  } } } t d ƒ ‰ t | ƒ ‰ | d k r¢ t | d ƒ } | d } | d } ˆ | | d | | d ‰ n  ‡  ‡ ‡ f d †  } | S(	   s:  Return the Gaussian expanded pdf function given the list of 1st, 2nd
    moment and skew and Fisher (excess) kurtosis.



    Parameters
    ----------
    mvsk : list of mu, mc2, skew, kurt
        distribution is matched to these four moments

    Returns
    -------
    pdffunc : function
        function that evaluates the pdf(x), where x is the non-standardized
        random variable.


    Notes
    -----

    Changed so it works only if four arguments are given. Uses explicit
    formula, not loop.

    This implements a Gram-Charlier expansion of the normal distribution
    where the first 2 moments coincide with those of the normal distribution
    but skew and kurtosis can deviate from it.

    In the Gram-Charlier distribution it is possible that the density
    becomes negative. This is the case when the deviation from the
    normal distribution is too large.



    References
    ----------
    http://en.wikipedia.org/wiki/Edgeworth_series
    Johnson N.L., S. Kotz, N. Balakrishnan: Continuous Univariate
    Distributions, Volume 1, 2nd ed., p.30
    i   s2   Four moments must be given to approximate the pdf.i   i   g      @g      8@i   c           sF   |  ˆ  ˆ } ˆ | ƒ t  j | | d ƒ t  j d t  j ƒ ˆ S(   Ng       @i   (   R   R   R   R"   (   R$   R:   (   R;   R<   R=   (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   pdffuncF  s    (   R?   R@   R   R   R9   (	   R&   R7   t   mc2t   skewt   kurtRF   t   C3t   C4RL   (    (   R;   R<   R=   sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   pdf_mvsk  s    (

!c           sé   t  |  ƒ } | d k  r' t d ƒ ‚ n  |  \ } } } } | | d } | | d d } t d ƒ ‰ t |  d ƒ ‰ |  d ‰  | d k rÐ t | d ƒ } | d }	 | d	 }
 ˆ |	 | d
 |
 | d ‰ n  ‡  ‡ ‡ f d †  } | S(   s  Return the Gaussian expanded pdf function given the list of central
    moments (first one is mean).

    Changed so it works only if four arguments are given. Uses explicit
    formula, not loop.

    Notes
    -----

    This implements a Gram-Charlier expansion of the normal distribution
    where the first 2 moments coincide with those of the normal distribution
    but skew and kurtosis can deviate from it.

    In the Gram-Charlier distribution it is possible that the density
    becomes negative. This is the case when the deviation from the
    normal distribution is too large.



    References
    ----------
    http://en.wikipedia.org/wiki/Edgeworth_series
    Johnson N.L., S. Kotz, N. Balakrishnan: Continuous Univariate
    Distributions, Volume 1, 2nd ed., p.30
    i   s:   At least two moments must be given to approximate the pdf.g      ø?g       @g      @i   i    g      @g      8@i   i   c           sF   |  ˆ  ˆ } ˆ | ƒ t  j | | d ƒ t  j d t  j ƒ ˆ S(   Ng       @i   (   R   R   R   R"   (   R$   R:   (   R;   R<   R=   (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR>   ˆ  s    (   R?   R@   R   R   R9   (   RE   R7   t   mcRM   t   mc3t   mc4RN   RO   RF   RP   RQ   R>   (    (   R;   R<   R=   sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   pdf_momentsK  s     


!t   NormExpan_genc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s   Gram-Charlier Expansion of Normal distribution

    class follows scipy.stats.distributions pattern
    but with __init__

    c   	      K  s  t  j j |  d d d d d d ƒ| j d d ƒ } | d k r t j | ƒ d	 \ } } } } | | | | f |  _ t | | | | f ƒ } nT | d
 k r³ t | ƒ } | |  _ n0 | d k r× | } t | ƒ |  _ n t	 d ƒ ‚ | |  _
 t |  j ƒ |  _ d  S(   NR   s   Normal Expansion distributionR   R   R   s  
        The distribution is defined as the Gram-Charlier expansion of
        the normal distribution using the first four moments. The pdf
        is given by

        pdf(x) = (1+ skew/6.0 * H(xc,3) + kurt/24.0 * H(xc,4))*normpdf(xc)

        where xc = (x-mu)/sig is the standardized value of the random variable
        and H(xc,3) and H(xc,4) are Hermite polynomials

        Note: This distribution has to be parameterized during
        initialization and instantiation, and does not have a shape
        parameter after instantiation (similar to frozen distribution
        except for location and scale.) Location and scale can be used
        as with other distributions, however note, that they are relative
        to the initialized distribution.
        t   modet   samplei   R&   t   centmoms   mode must be 'mvsk' or centmom(   R   R   R   t   getR   t   describeR&   R	   R
   R@   RE   RR   R%   (	   R   t   argst   kwdsRX   R;   R<   t   skt   kurRE   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   ”  s"    	c         C  s   |  j  | ƒ S(   N(   R   (   R   R    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR!   ¾  s    c         C  s   |  j  S(   N(   R&   (   R   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR(   Ã  s    (   R)   R*   R+   R   R!   R(   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRW     s   	*	c          K  s8   t  d „  t |  ƒ Dƒ ƒ } | j d d  ƒ } | | f S(   Nc         s  s?   |  ]5 \ } } | j  d  ƒ r | j d  d d ƒ | f Vq d S(   t   u_t    i   N(   t
   startswitht   replace(   t   .0RG   t   v(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pys	   <genexpr>ò  s    	t   u_args(   t   dictR   t   popR5   (   t   kwargst   u_kwargsRg   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   get_u_argskwargsð  s    t
   Transf_genc           B  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sU   a class for non-linear monotonic transformation of a continuous random variable

    c         O  só   | |  _  | |  _ | j d d ƒ |  _ | j d d ƒ } | j d d ƒ } | j d d  ƒ } | j d t j ƒ }	 | j d	 t j ƒ }
 | j d
 t ƒ |  _ t	 |   \ |  _
 |  _ | |  _ t t |  ƒ j d |	 d	 |
 d | d | d | ƒ d  S(   Nt   numargsi    R   t
   transfdistt   longnames#   Non-linear transformed distributionR   t   at   bt   decr(   t   funct   funcinvRi   Rn   R5   R   t   inft   FalseRs   Rl   Rg   Rk   t   klst   superRm   R   (   R   Rx   Rt   Ru   R]   Rj   R   Rp   R   Rq   Rr   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   û  s    			$c         O  s(   |  j  |  j _  |  j |  j j | Œ  ƒ S(   N(   R   Rx   Ru   R   (   R   R]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR     s    c         O  sO   |  j  s( |  j j |  j | ƒ | | Ž Sd |  j j |  j | ƒ | | Ž Sd  S(   Ng      ð?(   Rs   Rx   t   _cdfRu   (   R   R$   R]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRz     s    	c         O  sO   |  j  s( |  j |  j j | | | Ž ƒ S|  j |  j j d | | | Ž ƒ Sd  S(   Ni   (   Rs   Rt   Rx   t   _ppf(   R   t   qR]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR{   "  s    	(   R)   R*   R+   R   R   Rz   R{   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRm   ÷  s
   			c         C  s   t  j d |  ƒ S(   Ng      ð?(   R   t   divide(   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   inverse)  s    gš™™™™™©?gš™™™™™¹?g      "@g      ð?c         C  s   d d t  |  t S(   Ng      ð?i   (   t   muxt   stdx(   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   inversew.  s    c         C  s   d |  d t  t S(   Ng      ð?(   R   R€   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   inversew_inv0  s    c         C  s   |  S(   N(    (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   identit3  s    Rs   Rn   i    t   discfRp   s   normal-based discount factorsA   
distribution of discount factor y=1/(1+x)) with x N(0.05,0.1**2)i   Rq   t   lnnorms   Exp transformed normalso   
distribution of y = exp(x), with x standard normalprecision for moment andstats is not very high, 2-3 decimalsi   t   ExpTransf_genc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sµ   Distribution based on log/exp transformation

    the constructor can be called with a distribution class
    and generates the distribution of the transformed random variable

    c         O  s   d | k r | d |  _  n	 d |  _  d | k r> | d } n d } d | k r] | d } n d } t t |  ƒ j d d d | ƒ | |  _ d  S(   NRn   i   R   s   Log transformed distributionRq   i    (   Rn   Ry   R†   R   Rx   (   R   Rx   R]   Rj   R   Rq   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   Q  s    	c         G  s   |  j  j t j | ƒ | Œ S(   N(   Rx   t   cdfR   t   log(   R   R$   R]   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRz   c  s    c         G  s   t  j |  j j | | Œ ƒ S(   N(   R   R   Rx   t   ppf(   R   R|   R]   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR{   g  s    (   R)   R*   R+   R   Rz   R{   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR†   J  s   		t   LogTransf_genc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sµ   Distribution based on log/exp transformation

    the constructor can be called with a distribution class
    and generates the distribution of the transformed random variable

    c         O  s   d | k r | d |  _  n	 d |  _  d | k r> | d } n d } d | k r] | d } n d } t t |  ƒ j d | d | ƒ | |  _ d  S(   NRn   i   R   s   Log transformed distributionRq   i    (   Rn   Ry   RŠ   R   Rx   (   R   Rx   R]   Rj   R   Rq   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   q  s    	c         G  s   |  j  j t j | ƒ | Œ S(   N(   Rx   Rz   R   R   (   R   R$   R]   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRz   ƒ  s    c         G  s   t  j |  j j | | Œ ƒ S(   N(   R   Rˆ   Rx   R{   (   R   R|   R]   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR{   †  s    (   R)   R*   R+   R   Rz   R{   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRŠ   j  s   		t   TransfTwo_genc           B  sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sO  Distribution based on a non-monotonic (u- or hump-shaped transformation)

    the constructor can be called with a distribution class, and functions
    that define the non-linear transformation.
    and generates the distribution of the transformed random variable

    Note: the transformation, it's inverse and derivatives need to be fully
    specified: func, funcinvplus, funcinvminus, derivplus,  derivminus.
    Currently no numerical derivatives or inverse are calculated

    This can be used to generate distribution instances similar to the
    distributions in scipy.stats.

    c         O  so  | |  _  | |  _ | |  _ | |  _ | |  _ | j d d ƒ |  _ | j d d ƒ }	 | j d d ƒ }
 | j d d  ƒ } | j d t j	 ƒ } | j d	 t j	 ƒ } | j d
 t
 ƒ |  _ t |   \ |  _ |  _ | |  _ t t |  ƒ j d | d	 | d |	 d | j d |
 d | ƒ yD |  j j t d | d | d | d | d | d | d
 |  j ƒ ƒ Wn t k
 rjn Xd  S(   NRn   i    R   Ro   Rp   s#   Non-linear transformed distributionR   Rq   Rr   t   shapeR   Rx   Rt   t   funcinvplust   funcinvminust	   derivplust
   derivminus(   Rt   R   RŽ   R   R   Ri   Rn   R5   R   Rv   Rw   RŒ   Rl   Rg   Rk   Rx   Ry   R‹   R   R   t   _ctor_paramt   updateRh   t   AttributeError(   R   Rx   Rt   R   RŽ   R   R   R]   Rj   R   Rp   R   Rq   Rr   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   Á  s0    						$	c         G  s(   |  j  |  j _  |  j |  j j | Œ  ƒ S(   N(   R   Rx   Rt   R   (   R   R]   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   ë  s    c         O  s˜   |  j  d k r d } n$ |  j  d k r0 d } n t d ƒ ‚ | |  j | ƒ |  j j |  j | ƒ | | Ž |  j | ƒ |  j j |  j | ƒ | | Ž S(   Nt   ui   t   humpiÿÿÿÿs   shape can only be `u` or `hump`(   RŒ   R@   R   Rx   R%   R   R   RŽ   (   R   R$   R]   Rj   t   signpdf(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR%   ï  s    		.c         O  sh   |  j  d k rM |  j j |  j | ƒ | | Ž |  j j |  j | ƒ | | Ž Sd |  j | | | Ž Sd  S(   NR”   g      ð?(   RŒ   Rx   Rz   R   RŽ   t   _sf(   R   R$   R]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRz   ü  s     c         O  sh   |  j  d k rM |  j j |  j | ƒ | | Ž |  j j |  j | ƒ | | Ž Sd |  j | | | Ž Sd  S(   NR•   g      ð?(   RŒ   Rx   Rz   R   RŽ   (   R   R$   R]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR—     s     c         O  s   |  j  | | Œ S(   N(   R   (   R   R    R]   Rj   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR!     s    (	   R)   R*   R+   R   R   R%   Rz   R—   R!   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR‹   ±  s   	*						t
   SquareFuncc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s³   class to hold quadratic function with inverse function and derivative

    using instance methods instead of class methods, if we want extension
    to parameterized function
    c         C  s   t  j | ƒ S(   N(   R   R   (   R   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   inverseplus   s    c         C  s   d t  j | ƒ S(   Ng        (   R   R   (   R   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   inverseminus#  s    c         C  s   d t  j | ƒ S(   Ng      à?(   R   R   (   R   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   &  s    c         C  s   d d t  j | ƒ S(   Ng        g      à?(   R   R   (   R   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   )  s    c         C  s   t  j | d ƒ S(   Ni   (   R   t   power(   R   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt
   squarefunc,  s    (   R)   R*   R+   R™   Rš   R   R   Rœ   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR˜     s   				RŒ   R”   g        Rr   t
   squarenorms   squared normal distributions7   
distribution of the square of a normal random variables    y=x**2 with x N(0.0,1)s   squared t distributions2   
distribution of the square of a t random variables    y=x**2 with x t(dof,0.0,1)c         C  s   t  j |  ƒ S(   N(   R   R   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR™   ?  s    c         C  s   d t  j |  ƒ S(   Ng        (   R   R   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRš   B  s    c         C  s   d d t  j |  ƒ S(   Ng        g      à?(   R   R   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   E  s    c         C  s   d t  j |  ƒ S(   Ng      à?(   R   R   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   H  s    c         C  s   t  j |  d ƒ S(   Ni   (   R   R›   (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   negsquarefuncK  s    R•   t   negsquarenorms$   negative squared normal distributions@   
distribution of the negative square of a normal random variables    y=-x**2 with x N(0.0,1)c         C  s   |  S(   N(    (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR™   W  s    c         C  s   d |  S(   Ng        (    (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyRš   Z  s    c         C  s   d S(   Ng      ð?(    (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   ]  s    c         C  s   d S(   Ng        g      ð?g      ð¿(    (   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyR   `  s    c         C  s   t  j |  ƒ S(   N(   R   t   abs(   R$   (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   absfuncc  s    t   absnorms   absolute of normal distributions?   
distribution of the absolute value of a normal random variables    y=abs(x) with x N(0,1)s"   normal completion with ERROR < EPSss   completion with ERROR > EPS and MAXPTS function values used;
                    increase MAXPTS to decrease ERROR;s   N > 500 or N < 1c         K  s$  t  |  ƒ } t j |  ƒ }  t j | ƒ } t j | ƒ } t j t | | d d ƒ ƒ } |  j d k sx | j d k r‡ t d ƒ ‚ n  t  | ƒ | k r¨ t d ƒ ‚ n  | d k rÌ | j d k rÌ | } np | j d k rt  | ƒ | | d d k r| } n: | j | | f k r0| t j	 | d ƒ } n t d ƒ ‚ d | k rh| d k rhd	 | | d <qhn  t j
 |  ƒ } t j | ƒ } d t j | ƒ } t j | | d
 ƒ t j | | d ƒ t j | | | d ƒ t j j j j |  | | | |  \ }	 }
 } | r t d t | |	 ƒ n  |
 S(   sš	  standardized multivariate normal cumulative distribution function

    This is a wrapper for scipy.stats.kde.mvn.mvndst which calculates
    a rectangular integral over a standardized multivariate normal
    distribution.

    This function assumes standardized scale, that is the variance in each dimension
    is one, but correlation can be arbitrary, covariance = correlation matrix

    Parameters
    ----------
    lower, upper : array_like, 1d
       lower and upper integration limits with length equal to the number
       of dimensions of the multivariate normal distribution. It can contain
       -np.inf or np.inf for open integration intervals
    corrcoef : float or array_like
       specifies correlation matrix in one of three ways, see notes
    optional keyword parameters to influence integration
        * maxpts : int, maximum number of function values allowed. This
             parameter can be used to limit the time. A sensible
             strategy is to start with `maxpts` = 1000*N, and then
             increase `maxpts` if ERROR is too large.
        * abseps : float absolute error tolerance.
        * releps : float relative error tolerance.

    Returns
    -------
    cdfvalue : float
        value of the integral


    Notes
    -----
    The correlation matrix corrcoef can be given in 3 different ways
    If the multivariate normal is two-dimensional than only the
    correlation coefficient needs to be provided.
    For general dimension the correlation matrix can be provided either
    as a one-dimensional array of the upper triangular correlation
    coefficients stacked by rows, or as full square correlation matrix

    See Also
    --------
    mvnormcdf : cdf of multivariate normal distribution without
        standardization

    Examples
    --------

    >>> print(mvstdnormcdf([-np.inf,-np.inf], [0.0,np.inf], 0.5))
    0.5
    >>> corr = [[1.0, 0, 0.5],[0,1,0],[0.5,0,1]]
    >>> print(mvstdnormcdf([-np.inf,-np.inf,-100.0], [0.0,0.0,0.0], corr, abseps=1e-6))
    0.166666399198
    >>> print(mvstdnormcdf([-np.inf,-np.inf,-100.0],[0.0,0.0,0.0],corr, abseps=1e-8))
    something wrong completion with ERROR > EPS and MAXPTS function values used;
                        increase MAXPTS to decrease ERROR; 1.048330348e-006
    0.166666546218
    >>> print(mvstdnormcdf([-np.inf,-np.inf,-100.0],[0.0,0.0,0.0], corr,                             maxpts=100000, abseps=1e-8))
    0.166666588293

    i   g       @s   can handle only 1D boundss   bounds have different lengthsi   iÿÿÿÿs    corrcoef has incorrect dimensiont   maxptsi'  i    s   something wrong(   R?   R   t   arrayt   zerost   intt   ndimR@   R   RŒ   t   tril_indicest   isneginft   isposinft   onest   putmaskRA   R   t   kdet   mvnt   mvndstRD   t
   informcode(   t   lowert   uppert   corrcoefR^   R    t   correlt   lowinft   uppinft   infint   errort   cdfvaluet   inform(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   mvstdnormcdfÅ  s:    ?!	-		-c         K  s½   t  j |  ƒ }  | d k r8 t  j |  j ƒ t  j } n t  j | ƒ } t  j | ƒ } t  j t  j | ƒ ƒ } | | | } |  | | }  t  j | ƒ } | | | j	 } t
 | |  | |  S(   sD  multivariate normal cumulative distribution function

    This is a wrapper for scipy.stats.kde.mvn.mvndst which calculates
    a rectangular integral over a multivariate normal distribution.

    Parameters
    ----------
    lower, upper : array_like, 1d
       lower and upper integration limits with length equal to the number
       of dimensions of the multivariate normal distribution. It can contain
       -np.inf or np.inf for open integration intervals
    mu : array_lik, 1d
       list or array of means
    cov : array_like, 2d
       specifies covariance matrix
    optional keyword parameters to influence integration
        * maxpts : int, maximum number of function values allowed. This
             parameter can be used to limit the time. A sensible
             strategy is to start with `maxpts` = 1000*N, and then
             increase `maxpts` if ERROR is too large.
        * abseps : float absolute error tolerance.
        * releps : float relative error tolerance.

    Returns
    -------
    cdfvalue : float
        value of the integral


    Notes
    -----
    This function normalizes the location and scale of the multivariate
    normal distribution and then uses `mvstdnormcdf` to call the integration.

    See Also
    --------
    mvstdnormcdf : location and scale standardized multivariate normal cdf
    N(   R   R¤   R5   R«   RŒ   Rv   R   t   diagt
   atleast_2dt   TR»   (   R²   R;   t   covR±   R^   t   stdevt   divrowt   corr(    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt	   mvnormcdf:  s    ((   gš™™™™™©?gš™™™™™¹?(   g      "@g      ð?(E   R+   t
   __future__R    t   numpyR   R   R   R   RA   R   R   t   scipy.statsR   t   statsmodels.compat.pythonR   R   t    statsmodels.stats.moment_helpersR	   R
   R   R   R0   R,   t	   skewnorm2R-   R9   RK   RR   RV   RW   Rl   Rm   R~   R   R€   R   R‚   Rƒ   R   t   Truet   invdnormalgRˆ   t
   lognormalgt   gammat   loggammaexpgR†   RŠ   R‹   t   objectR˜   t   sqfuncRœ   R™   Rš   R   R   Rv   t   squarenormalgR3   t   squaretgRž   t   negsquarenormalgR¡   R    t
   absnormalgR°   R»   R5   RÃ   (    (    (    sG   lib/python2.7/site-packages/statsmodels/sandbox/distributions/extras.pyt   <module>3   sš   %		Z		(	>	Bc	2						!	 Gi	

					
					
U

	u