
    !g6@                     j   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jB                  d!d"ejD                  dd#d$%      Z# eejH                  ej:                  ej<                  ej>                  ej@                  ejB                  d!d"ejD                  dd#d&%      Z%d' Zd( Zd) Z d* Z!d+ Z& eej                   e&eee e!d,ejD                   d"dd-d.%      Z'd/ Zd0 Zd1 Z d2 Z!d3 Z( eej                   ejR                  eee e!d!d"ejD                  dd4d5%      Z*y)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            	          | j                         D ci c]*  \  }}|j                  d      r|j                  ddd      |, }}}|j                  dd       }||fS c c}}w )Nu_    u_args)items
startswithreplacepop)kwargskvu_kwargsr	   s        j/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/sandbox/distributions/transformed.pyget_u_argskwargsr   ,   sf    6<lln +da||D) 		$A&) +H +\\(D)F8+s   /A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                    || _         || _        |j                  dd      | _        |j                  dd      }|j                  dd      }|j                  dd       }|j                  dt        j
                         }	|j                  d	t        j
                        }
|j                  d
d      | _        t        di |\  | _        | _	        || _
        t        | 1  |	|
||j                  |       y )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::j$/JJsRVVG$JJsBFF#JJvu-	 &6%?%?"T] 	14

X 	 	
    c                     | j                   s/ | j                  j                  | j                  |      g|i |S d | j                  j                  | j                  |      g|i |z
  S N      ?)r   r'   _cdfr$   r*   xr+   r   s       r   r1   zTransf_gen._cdfT   s^    yy 488==aB4B6BB t||AHHHHHr-   c                     | j                   s/| j                   | j                  j                  |g|i |      S | j                   | j                  j                  d|z
  g|i |      S )Nr   )r   r#   r'   _ppf)r*   qr+   r   s       r   r5   zTransf_gen._ppf\   s]    yy99]TXX]]1>t>v>??99]TXX]]1q5B4B6BCCr-   __name__
__module____qualname____doc__r)   r1   r5   __classcell__r,   s   @r   r   r   4   s    
6IDr-   r   c                 .    t        j                  d|       S r/   )r%   divider3   s    r   inverserA   c   s    99S!r-   )g?g?)g      "@r0   c                 .    ddt         z   | t        z  z   z  S )Nr0   r   muxstdxr@   s    r   inversewrF   k   s    !c'AH$%%r-   c                 .    d| z  dz
  t         z
  t        z  S r/   rC   r@   s    r   inversew_invrH   o   s    !GcMC4''r-   c                     | S Nr"   r@   s    r   identitrK   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        | 	  ||       || _        y Nr   r   r   zLog transformed distributionr   r   r   r   r   r(   r)   r'   r*   r'   r+   r   r   r   r,   s         r   r)   zExpTransf_gen.__init__   sd     !),DLDLV&>D1D&=sAA14(r-   c                 b     | j                   j                  t        j                  |      g| S rJ   )r'   r1   r%   logr*   r3   r+   s      r   r1   zExpTransf_gen._cdf   $    txx}}RVVAY...r-   c                 b    t        j                   | j                  j                  |g|       S rJ   )r%   expr'   r5   r*   r6   r+   s      r   r5   zExpTransf_gen._ppf   %    vvmdhhmmA--..r-   r7   r=   s   @r   rQ   rQ      s    &//r-   rQ   c                   .     e Zd ZdZ fdZd Zd Z xZS )LogTransf_genrR   c                     d|v r|d   | _         nd| _         d|v r|d   }nd}d|v r|d   }nd}t        | 	  ||       || _        y rT   rV   rW   s         r   r)   zLogTransf_gen.__init__   sb    !),DLDLV&>D1D&=sAA14(r-   c                 b     | j                   j                  t        j                  |      g| S rJ   )r'   r1   r%   r]   rZ   s      r   r1   zLogTransf_gen._cdf   r[   r-   c                 b    t        j                   | j                  j                  |g|       S rJ   )r%   rY   r'   r5   r^   s      r   r5   zLogTransf_gen._ppf   r_   r-   r7   r=   s   @r   ra   ra      s    $//r-   ra   c                      t        d       t        t        j                  dd      } t        | j	                  d             t        t        j
                  j	                  dd             t        | j                                t        t        j
                  j                  d             t        | j                  d             t        d       t        t        j                        }t        |j                  dd	             t        t        j                  j	                  dd	             t        |j                  d
d             t        t        j                  j	                  d
d             t        d       t        t        j                        }t        |j                  d
d	             t        t        j                  j	                  d
d	             t        t        j                        }t        |j                  d
d	             y )NzResults for lognormalr   zLog transformed normal generalrU   r      )sizezResults for expgamma
   rN      zResults for loglaplace)printrQ   r   normcdflognormrvsra   gammar1   loggammalaplace
loglaplace)
lognormalgloggammaexpgloglaplacegloglaplaceexpgs       r   examples_transfrw      sf    

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

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

"#.K	+

1b
!"	%



q"
%&"5==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                    || _         || _        || _        || _        || _        |j                  dd      | _        |j                  dd      }	|j                  dd      }
|j                  dd       }|j                  dt        j                         }|j                  d	t        j                        }|j                  d
d      | _	        t        di |\  | _        | _        || _        t        | =  |||	|j                   |
       y )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   r,   s                 r   r)   zTransfTwo_gen.__init__1  s    
 	&("$ zz)Q/zz&,/::j*OP::j$/JJsRVVG$JJsBFF#ZZ/
 &6%?%?"T] 	14

X 	 	
r-   c                     | j                   | j                  _         | j                   | j                  j                  |       S rJ   )_sizer'   r#   _rvs)r*   r+   s     r   r   zTransfTwo_gen._rvsP  s/    yy-..r-   c                 l   | j                   dk(  rd}n| j                   dk(  rd}nt        d      || j                  |       | j                  j                  | j                  |      g|i |z  | j                  |       | j                  j                  | j                  |      g|i |z  z
  z  S )Nur   humpzshape can only be `u` or `hump`)r{   
ValueErrorr~   r'   _pdfr|   r   r}   )r*   r3   r+   r   signpdfs        r   r   zTransfTwo_gen._pdfT  s    ::GZZ6!G>??$..+mdhhmmD<L<LQ<O.aRV.aZ`.aa//!,}txx}}T=N=Nq=Q 0GTX 0G?E0G GG H 	Hr-   c                    | j                   dk(  r_ | j                  j                  | j                  |      g|i | | j                  j                  | j	                  |      g|i |z
  S d | j
                  |g|i |z
  S )Nr   r0   )r{   r'   r1   r|   r}   _sfr2   s       r   r1   zTransfTwo_gen._cdfb  s    :: 488==!1!1!!4FtFvFd//2DTDVDE E !5d5f555r-   c                    | j                   dk(  r_ | j                  j                  | j                  |      g|i | | j                  j                  | j	                  |      g|i |z
  S d | j                  |g|i |z
  S )Nr   r0   )r{   r'   r1   r|   r}   r2   s       r   r   zTransfTwo_gen._sfk  s    :: 488==!1!1!!4FtFvFd//2DTDVDE E 16t6v666r-   c                 (     | j                   |g| S rJ   )_mom0_sc)r*   nr+   r   s       r   _munpzTransfTwo_gen._munpt  s    t}}Q&&&r-   )r8   r9   r:   r;   r)   r   r   r1   r   r   r<   r=   s   @r   ry   ry      s'     
>/H67'r-   ry   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)
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 rJ   r%   sqrtr*   r3   s     r   inversepluszSquareFunc.inverseplus  s    wwqzr-   c                 2    dt        j                  |      z
  S N        r   r   s     r   inverseminuszSquareFunc.inverseminus      RWWQZr-   c                 2    dt        j                  |      z  S N      ?r   r   s     r   r~   zSquareFunc.derivplus  r   r-   c                 8    ddt        j                  |      z  z
  S Nr   r   r   r   s     r   r   zSquareFunc.derivminus  s    S2771:%%%r-   c                 .    t        j                  |d      S NrN   r%   powerr   s     r   
squarefunczSquareFunc.squarefunc  s    xx1~r-   N)	r8   r9   r:   r;   r   r   r~   r   r   r"   r-   r   r   r     s       &r-   r   r   r   
squarenormzsquared normal distribution)r{   r   r   r   r   r   zsquared t distributionc                 .    t        j                  |        S rJ   r   r@   s    r   r   r     s    77A2;r-   c                 4    dt        j                  |        z
  S r   r   r@   s    r   r   r         !r-   c                 :    ddt        j                  |        z  z
  S r   r   r@   s    r   r~   r~     s    rwwr{"""r-   c                 4    dt        j                  |        z  S r   r   r@   s    r   r   r     r   r-   c                 0    t        j                  | d       S r   r   r@   s    r   negsquarefuncr     s    HHQN?r-   r   negsquarenormz$negative squared normal distributionc                     | S rJ   r"   r@   s    r   r   r     rL   r-   c                     d| z
  S r   r"   r@   s    r   r   r     s    7Nr-   c                      yr/   r"   r@   s    r   r~   r~     s    r-   c                      y)Ng      r"   r@   s    r   r   r     s    r-   c                 ,    t        j                  |       S rJ   )r%   absr@   s    r   absfuncr     s    66!9r-   absnormzabsolute of normal distribution)+r;   scipyr   scipy.statsr   numpyr%   r   rv_continuousr   rA   rD   rE   rF   rH   rK   rk   invdnormalgr]   rY   rs   ro   rt   rQ   ra   rw   ry   r   sqfuncr   r   r   r~   r   r&   squarenormalgtsquaretgr   negsquarenormalgr   r   
absnormalgr"   r-   r   <module>r      sS  #H  % ,D,, ,D^ 	T	T&( X|$!";Y
 

BFFBFF !QX!9
 %++rvvrvvqA /M//  /F/M// /D$&R>U'M// U'D . 
ejj&*;*;V=O=O$1163C3CVEVEV$'3"&&&'lEb  &"3"3V5G5G,,f.>.>@Q@Q"cRVV!"@X# !]K!*JfSV)**P#  5::rvv{L$jsbff#$9?`
r-   