
    M/Ph6@                        d Z ddlmZ ddlmZ ddlZd Z G d dej                  Z	d Z
d	\  ZZd
\  ZZd Zd Zd Z e	ej        eedddd          Z e	ej        ej        ej        dddd          Z e	ej        ej        ej        d          Z	  G d dej                  Z G d dej                  Zd Z	 	  G d dej                  Z G d d           Z e            Z eej        ej        ej        ej        ej         ej!        d!d"ej"        dd#d$%          Z# eej$        ej        ej        ej        ej         ej!        d!d"ej"        dd#d&%          Z%d' Zd( Zd) Z d* Z!d+ Z& eej        e&eee e!d,ej"         d"dd-d.%          Z'd/ Zd0 Zd1 Z d2 Z!d3 Z( eej        ej)        eee e!d!d"ej"        dd4d5%          Z*dS )6a{   A class for the distribution of a non-linear monotonic transformation of a continuous random variable

simplest usage:
example: create log-gamma distribution, i.e. y = log(x),
            where x is gamma distributed (also available in scipy.stats)
    loggammaexpg = Transf_gen(stats.gamma, np.log, np.exp)

example: what is the distribution of the discount factor y=1/(1+x)
            where interest rate x is normally distributed with N(mux,stdx**2)')?
            (just to come up with a story that implies a nice transformation)
    invnormalg = Transf_gen(stats.norm, inversew, inversew_inv, decr=True, a=-np.inf)

This class does not work well for distributions with difficult shapes,
    e.g. 1/x where x is standard normal, because of the singularity and jump at zero.

Note: I'm working from my version of scipy.stats.distribution.
      But this script runs under scipy 0.6.0 (checked with numpy: 1.2.0rc2 and python 2.4)

This is not yet thoroughly tested, polished or optimized

TODO:
  * numargs handling is not yet working properly, numargs needs to be specified (default = 0 or 1)
  * feeding args and kwargs to underlying distribution is untested and incomplete
  * distinguish args and kwargs for the transformed and the underlying distribution
    - currently all args and no kwargs are transmitted to underlying distribution
    - loc and scale only work for transformed, but not for underlying distribution
    - possible to separate args for transformation and underlying distribution parameters

  * add _rvs as method, will be faster in many cases


Created on Tuesday, October 28, 2008, 12:40:37 PM
Author: josef-pktd
License: BSD

    )stats)distributionsNc                  r    d |                                  D             }|                    dd           }||fS )Nc                 n    i | ]2\  }}|                     d           |                    d dd          |3S )u_    )
startswithreplace).0kvs      m/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/distributions/transformed.py
<dictcomp>z$get_u_argskwargs.<locals>.<dictcomp>.   sN     + + +da||D))+		$A&& + + +    u_args)itemspop)kwargsu_kwargsr   s      r   get_u_argskwargsr   ,   sE    + +fllnn + + +H\\(D))F8r   c                   .     e Zd ZdZ fdZd Zd Z xZS )
Transf_genzUa class for non-linear monotonic transformation of a continuous random variable

    c                 ,   || _         || _        |                    dd          | _        |                    dd          }|                    dd          }|                    dd           }|                    dt          j                   }	|                    d	t          j                  }
|                    d
d          | _        t          di |\  | _        | _	        || _
        t                                          |	|
||j        |           d S )Nnumargsr   name
transfdistlongname#Non-linear transformed distributionextradocabdecrFr!   r"   r   shapesr    )funcfuncinvr   r   npinfr#   r   r   r   klssuper__init__r%   )selfr+   r'   r(   argsr   r   r   r    r!   r"   	__class__s              r   r-   zTransf_gen.__init__9   s    	 zz)Q//zz&,//::j*OPP::j$//JJsRVG$$JJsBF##JJvu--	 &6%?%?%?%?"T] 	14
X 	 	
 	
 	
 	
 	
r   c                     | j         s) | j        j        |                     |          g|R i |S d | j        j        |                     |          g|R i |z
  S N      ?)r#   r+   _cdfr(   r.   xr/   r   s       r   r4   zTransf_gen._cdfT   sp    y 	I 48=aB4BBB6BBB t||AHHHHHHHHr   c                     | j         s)|                      | j        j        |g|R i |          S |                      | j        j        d|z
  g|R i |          S )Nr	   )r#   r'   r+   _ppf)r.   qr/   r   s       r   r8   zTransf_gen._ppf\   sq    y 	D99]TX]1>t>>>v>>???99]TX]1q5B4BBB6BBCCCr   __name__
__module____qualname____doc__r-   r4   r8   __classcell__r0   s   @r   r   r   4   sj         
 
 
 
 
6I I ID D D D D D Dr   r   c                 ,    t          j        d|           S r2   )r)   divider6   s    r   inverserD   c   s    9S!r   )g?g?)g      "@r3   c                 2    ddt           z   | t          z  z   z  S )Nr3   r	   muxstdxrC   s    r   inversewrI   k   s    !c'AH$%%r   c                 2    d| z  dz
  t           z
  t          z  S r2   rF   rC   s    r   inversew_invrK   o   s    !GcMC4''r   c                     | S Nr&   rC   s    r   identitrN   s       Hr   Tdiscfznormal-based discount factor)r#   r   r   r      lnnormzExp transformed normal)r   r!   r   r   r	   )r   c                   .     e Zd ZdZ fdZd Zd Z xZS )ExpTransf_genDistribution based on log/exp transformation

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

    c                     d|v r|d         | _         nd| _         d|v r	|d         }nd}d|v r	|d         }nd}t                                          ||           || _        d S Nr   r	   r   zLog transformed distributionr!   r   r!   r   r   r,   r-   r+   r.   r+   r/   r   r   r!   r0   s         r   r-   zExpTransf_gen.__init__   s     !),DLLDLV&>DD1D&==sAAA14(((r   c                 L     | j         j        t          j        |          g|R  S rM   )r+   r4   r)   logr.   r6   r/   s      r   r4   zExpTransf_gen._cdf   &    tx}RVAYY.....r   c                 L    t          j         | j        j        |g|R            S rM   )r)   expr+   r8   r.   r9   r/   s      r   r8   zExpTransf_gen._ppf   (    vmdhmA----...r   r:   r@   s   @r   rT   rT      s`             &/ / // / / / / / /r   rT   c                   .     e Zd ZdZ fdZd Zd Z xZS )LogTransf_genrU   c                     d|v r|d         | _         nd| _         d|v r	|d         }nd}d|v r	|d         }nd}t                                          ||           || _        d S rW   rY   rZ   s         r   r-   zLogTransf_gen.__init__   s    !),DLLDLV&>DD1D&==sAAA14(((r   c                 L     | j         j        t          j        |          g|R  S rM   )r+   r4   r)   r`   r]   s      r   r4   zLogTransf_gen._cdf   r^   r   c                 L    t          j         | j        j        |g|R            S rM   )r)   r\   r+   r8   ra   s      r   r8   zLogTransf_gen._ppf   rb   r   r:   r@   s   @r   rd   rd      s`             $/ / // / / / / / /r   rd   c                     t          d           t          t          j        dd          } t          |                     d                     t          t          j                            dd                     t          |                                            t          t          j                            d                     t          |                     d                     t          d           t          t          j                  }t          |	                    dd	                     t          t          j
                            dd	                     t          |	                    d
d                     t          t          j
                            d
d                     t          d           t          t          j                  }t          |	                    d
d	                     t          t          j                            d
d	                     t          t          j                  }t          |	                    d
d	                     d S )NzResults for lognormalr   zLog transformed normal generalrX   r	      )sizezResults for expgamma
   rQ      zResults for loglaplace)printrT   r   normcdflognormrvsrd   gammar4   loggammalaplace
loglaplace)
lognormalgloggammaexpgloglaplacegloglaplaceexpgs       r   examples_transfrz      s    

!"""uzQ5UVVVJ	*..

	%-

Aq
!
!"""	*



	%-

a
 
 !!!	*..a.
 
 !!! 

 !!! --L	,

Ar
"
"###	%.

Q
#
#$$$	,

Ar
"
"###	%.

Q
#
#$$$
 

"###..K	+

1b
!
!"""	%


q"
%
%&&&"5=11N	.

a
$
$%%%%%r   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )TransfTwo_genaO  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                 V   || _         || _        || _        || _        || _        |                    dd          | _        |                    dd          }	|                    dd          }
|                    dd           }|                    dt          j                   }|                    d	t          j                  }|                    d
d          | _	        t          di |\  | _        | _        || _        t                                          |||	|j        |
           d S )Nr   r   r   r   r   r   r    r!   r"   shapeFr$   r&   )r'   funcinvplusfuncinvminus	derivplus
derivminusr   r   r)   r*   r~   r   r   r   r+   r,   r-   r%   )r.   r+   r'   r   r   r   r   r/   r   r   r   r    r!   r"   r0   s                 r   r-   zTransfTwo_gen.__init__1  s   
 	&("$ zz)Q//zz&,//::j*OPP::j$//JJsRVG$$JJsBF##ZZ//
 &6%?%?%?%?"T] 	14
X 	 	
 	
 	
 	
 	
r   c                 h    | j         | j        _         |                      | j        j        |           S rM   )_sizer+   r'   _rvs)r.   r/   s     r   r   zTransfTwo_gen._rvsP  s*    yy-...r   c                 \   | j         dk    rd}n| j         dk    rd}nt          d          ||                     |           | j        j        |                     |          g|R i |z  |                     |           | j        j        |                     |          g|R i |z  z
  z  S )Nur	   humpzshape can only be `u` or `hump`)r~   
ValueErrorr   r+   _pdfr   r   r   )r.   r6   r/   r   signpdfs        r   r   zTransfTwo_gen._pdfT  s    :GGZ6!!GG>???$..++mdhmD<L<LQ<O<O.aRV.a.a.aZ`.a.aa//!,,}tx}T=N=Nq=Q=Q 0GTX 0G 0G 0G?E0G 0G GG H 	Hr   c                     | j         dk    rS | j        j        |                     |          g|R i | | j        j        |                     |          g|R i |z
  S d | j        |g|R i |z
  S )Nr   r3   )r~   r+   r4   r   r   _sfr5   s       r   r4   zTransfTwo_gen._cdfb  s    : 48=!1!1!!4!4FtFFFvFFd//22DTDDDVDDE E !5d555f5555r   c                     | j         dk    rS | j        j        |                     |          g|R i | | j        j        |                     |          g|R i |z
  S d | j        |g|R i |z
  S )Nr   r3   )r~   r+   r4   r   r   r5   s       r   r   zTransfTwo_gen._sfk  s    : 48=!1!1!!4!4FtFFFvFFd//22DTDDDVDDE E 16t666v6666r   c                      | j         |g|R  S rM   )_mom0_sc)r.   nr/   r   s       r   _munpzTransfTwo_gen._munpt  s    t}Q&&&&&r   )r;   r<   r=   r>   r-   r   r   r4   r   r   r?   r@   s   @r   r|   r|      s          
 
 
 
 
>/ / /H H H6 6 67 7 7' ' ' ' ' ' 'r   r|   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )
SquareFunczclass to hold quadratic function with inverse function and derivative

    using instance methods instead of class methods, if we want extension
    to parametrized function
    c                 *    t          j        |          S rM   r)   sqrtr.   r6   s     r   inversepluszSquareFunc.inverseplus  s    wqzzr   c                 0    dt          j        |          z
  S N        r   r   s     r   inverseminuszSquareFunc.inverseminus      RWQZZr   c                 0    dt          j        |          z  S N      ?r   r   s     r   r   zSquareFunc.derivplus  r   r   c                 6    ddt          j        |          z  z
  S Nr   r   r   r   s     r   r   zSquareFunc.derivminus  s    S271::%%%r   c                 ,    t          j        |d          S NrQ   r)   powerr   s     r   
squarefunczSquareFunc.squarefunc  s    x1~~r   N)	r;   r<   r=   r>   r   r   r   r   r   r&   r   r   r   r     si                     & & &    r   r   r   r   
squarenormzsquared normal distribution)r~   r!   r"   r   r   r   zsquared t distributionc                 ,    t          j        |            S rM   r   rC   s    r   r   r     s    7A2;;r   c                 2    dt          j        |            z
  S r   r   rC   s    r   r   r         !r   c                 8    ddt          j        |            z  z
  S r   r   rC   s    r   r   r     s    rwr{{"""r   c                 2    dt          j        |            z  S r   r   rC   s    r   r   r     r   r   c                 .    t          j        | d           S r   r   rC   s    r   negsquarefuncr     s    HQNN?r   r   negsquarenormz$negative squared normal distributionc                     | S rM   r&   rC   s    r   r   r     rO   r   c                     d| z
  S r   r&   rC   s    r   r   r     s    7Nr   c                     dS r2   r&   rC   s    r   r   r     s    3r   c                     dS )Ng      r&   rC   s    r   r   r     s    9r   c                 *    t          j        |           S rM   )r)   absrC   s    r   absfuncr     s    6!99r   absnormzabsolute of normal distribution)+r>   scipyr   scipy.statsr   numpyr)   r   rv_continuousr   rD   rG   rH   rI   rK   rN   rn   invdnormalgr`   r\   rv   rr   rw   rT   rd   rz   r|   r   sqfuncr   r   r   r   r   r*   squarenormalgtsquaretgr   negsquarenormalgr   r   
absnormalgr&   r   r   <module>r      s  # #H       % % % % % %      ,D ,D ,D ,D ,D, ,D ,D ,D^   	T	T& & &( ( (   jX|$!";Y  
 Z
BFBF !QX!9  
 z%+rvrvqAAA /  /  /  /  /M/  /  /  /F/ / / / /M/ / / /D$& $& $&R>U' U' U' U' U'M/ U' U' U'D       . 
ej&*;V=O$163CVEV$'3"&&'lEb      =&"3V5G,f.>@Q"cRV!"@X      # # #     !=]K!*JfSV)**P# # #            ]5:rv{L$jsbf#$9?`  


r   