
    >qj9<                        d dl Zd dlZd dlmZ  G d d      Z G d de      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z	 G d de      Z
 G d de      Z G d de      Z G d de      Z G d de      Zy)    N)mpc                   B   e Zd ZdZd Zd Zd Zd Zd Zd$dZ	d$d	Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zej,                  fdZej,                  fdZej,                  fdZdej,                  fdZdej,                  fdZej,                  fdZej,                  fdZej,                  fdZej,                  fdZej,                  fdZ ej,                  fdZ!ej,                  fdZ"ej,                  fd Z#ej,                  fd!Z$d"ej,                  fd#Z%y")%ReferenceDistributiona*  Minimalist distribution infrastructure for generating reference data.

    The purpose is to generate reference values for unit tests of SciPy
    distribution accuracy and robustness.

    Handles array input with standard broadcasting rules, and method
    implementations are easily compared against their mathematical definitions.
    No attempt is made to handle edge cases or be fast, and arbitrary precision
    arithmetic is trusted for accuracy rather than making the method
    implementations "smart".

    Notes
    -----

    In this infrastructure, distributions families are classes, and
    fully-specified distributions (i.e. with definite values of all family
    parameters) are instances of these classes. Typically, the public methods
    accept as input only the argument at which the at which the function is to
    be evaluated. Unlike SciPy distributions, they never accept values of
    distribution family shape, location, or scale parameters. A few
    other parameters are noteworthy:

    - All methods accept `dtype` to control the output data type. The default
      is `np.float64`, but `object` or `mp.mpf` may be
      specified to output the full `mpf`.
    - `ppf`/`isf` accept a `guess` because they use a scalar rootfinder
      to invert the `cdf`/`sf`. This is passed directly into the `x0` method
      of `mpmath.findroot`; see its documentation for details.
    - moment accepts `order`, an integer that specifies the order of the (raw)
      moment, and `center`, which is the value about which the moment is
      taken. The default is to calculate the mean and use it to calculate
      central moments; passing ``0`` results in a noncentral moment. For
      efficiency, the mean can be passed explicitly if it is already known.

    Follow the example of SkewNormal to generate new reference distributions,
    overriding only `__init__` and `_pdf`*. Use the reference distributions to
    generate reference values for unit tests of SciPy distribution method
    precision and robustness (e.g. for extreme arguments). If the a SciPy
    methods implementation is independent and yet the output matches reference
    values generated with this infrastructure, it is unlikely that the SciPy
    and reference values are both inaccurate.

    * If the SciPy output *doesn't* match and the cause appears to be
    inaccuracy of the reference values (e.g. due to numerical issues that
    mpmath's arbitrary precision arithmetic doesn't handle), then it may be
    appropriate to override a method of the reference distribution rather than
    relying on the generic implementation. Otherwise, hesitate to override
    methods: the generic implementations are mathematically correct and easy
    to verify, whereas an override introduces many possibilities of mistakes,
    requires more time to write, and requires more time to review.

    In general, do not create custom unit tests to ensure that
    SciPy distribution methods are *correct* (in the sense of being consistent
    with the rest of the distribution methods); generic tests take care of
    that.
    c                 2   	 t         j                  d}t        |      	 t        j                  dk  rd}t        |      |j                         D ci c]  \  }}|| j                  |       c}}| _        y # t        $ r d t         _        Y ow xY wc c}}w )Nz`mpmath.dps` has been assigned. This is not intended usage; instead, assign the desired precision to `mpmath.mp.dps` (e.g. `from mpmath as mp; mp.dps = 50.   zR`mpmath.mp.dps <= 15`. Set a higher precision (e.g.`50`) to use this distribution.)mpmathdpsRuntimeErrorAttributeErrorr   items_make_mpf_array_params)selfkwargsmessagekeyvals        x/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/scipy/stats/tests/test_generation/reference_distributions.py__init__zReferenceDistribution.__init__@   s    	zz%1 #7++ & 66R<9Gw'' )/8$C D0055 8  	FJ	8s   A9 B9BBc                 ,   t        j                  |      }t        j                  |t         j                        j	                         }t        j                  |D cg c]  }t        j                  |       c}      j                  |      d   S c c}w Ndtype )npshapeasarrayfloat64ravelr   mpfreshape)r   xr   xis       r   r   z%ReferenceDistribution._make_mpf_arrayS   sa    JJq

+113zz2"266":23;;EB2FF2s   Bc                     t        d      )Nz_pdf must be overridden.)NotImplementedErrorr   r"   s     r   _pdfzReferenceDistribution._pdfX   s    !"<==    c                      j                   j                  t        j                   u rJ j                  j                  t        j                  ur$t        j
                    j                  |fi z
  S   j                  di \  }}t	        j                   fd||f      }|dk  r|}|S t        j
                    j                  |fi z
  }|S )Nc                 *     j                   | fi S Nr'   r"   r   r   s    r   <lambda>z,ReferenceDistribution._cdf.<locals>.<lambda>a       			! 6v 6 r(         ?r   )_cdf__func__r   _sfr   one_supportquadr   r"   r   abress   ` `   r   r1   zReferenceDistribution._cdf[   s    YY#8#=#==XX&&.C.G.GG66HDHHQ1&111t}}&v&1gg6A?3Yc
 %'FFXTXXa-B6-B$B
r(   c                      j                   j                  t        j                   u rJ j                  j                  t        j                  ur$t        j
                    j                  |fi z
  S   j                  di \  }}t	        j                   fd||f      }|dk  r|}|S t        j
                    j                  |fi z
  }|S )Nc                 *     j                   | fi S r+   r,   r-   s    r   r.   z+ReferenceDistribution._sf.<locals>.<lambda>k   r/   r(   r0   r   )r3   r2   r   r1   r   r4   r5   r6   r7   s   ` `   r   r3   zReferenceDistribution._sfe   s    XX"7";";;YY''/D/I/II66IDIIa26222t}}&v&1gg6A?3Yc
 %'FFYTYYq-CF-C$C
r(   r   c                 $     j                   j                  t        j                   u rK j                  j                  t        j                  ur%  j                  t        j
                  z
  |fi S  fd}t	        j                  ||      S )Nc                 0     j                   | fi z
  S r+   )r1   r"   r   pr   s    r   fz%ReferenceDistribution._ppf.<locals>.fs   s    499Q)&)A--r(   )_ppfr2   r   _isfr   r4   findrootr   r@   guessr   rA   s   `` ` r   rB   zReferenceDistribution._ppfo   sk    YY#8#=#==YY''/D/I/II499RVVaZ9&99	.{{1e$$r(   c                 $     j                   j                  t        j                   u rK j                  j                  t        j                  ur%  j                  t        j
                  z
  |fi S  fd}t	        j                  ||      S )Nc                 0     j                   | fi z
  S r+   )r3   r?   s    r   rA   z%ReferenceDistribution._isf.<locals>.f{   s    488A((1,,r(   )rC   r2   r   rB   r   r4   rD   rE   s   `` ` r   rC   zReferenceDistribution._isfw   sk    YY#8#=#==YY''/D/I/II499RVVaZ9&99	-{{1e$$r(   c                 N    t        j                   | j                  |fi |      S r+   )r   logr'   r   r"   r   s      r   _logpdfzReferenceDistribution._logpdf   !    vvidii,V,--r(   c                 N    t        j                   | j                  |fi |      S r+   )r   rJ   r1   rK   s      r   _logcdfzReferenceDistribution._logcdf   rM   r(   c                 N    t        j                   | j                  |fi |      S r+   )r   rJ   r3   rK   s      r   _logsfzReferenceDistribution._logsf   s!    vvhdhhq+F+,,r(   c                 D    t         j                   t         j                  fS r+   r   infr   r   s     r   r5   zReferenceDistribution._support   s    wr(   c                 l      fd}  j                   di \  }}t        j                  |||f      S )Nc                 `     j                   | fi }t        j                  |      }| |z  S r+   )rL   r   exp)r"   logpdfpdfr   r   s      r   	integrandz1ReferenceDistribution._entropy.<locals>.integrand   s1    !T\\!.v.F&&.CDKr(   r   )r5   r   r6   )r   r   r[   r8   r9   s   ``   r   _entropyzReferenceDistribution._entropy   s6    	 
 t}}&v&1wwy1a&))r(   c                 ,     | j                   dddd|S )N   r   ordercenterr   )_momentrU   s     r   _meanzReferenceDistribution._mean   s    t||8!A888r(   c                 P     | j                   di |} | j                  dd|d|S )N   r_   r   rc   rb   )r   r   mus      r   _varzReferenceDistribution._var   s0    TZZ!&!t||9!B9&99r(   c                      | j                   di |} | j                  dd|d|}t        j                  |      } | j                  dd|d|}||dz  z  S )Nre   r_      r   )rc   rb   r   sqrt)r   r   rg   u2sigmau3s         r   _skewzReferenceDistribution._skew   sd    TZZ!&!T\\7"77T\\7"77E1H}r(   c                      | j                   di |} | j                  dd|d|} | j                  dd|d|}||dz  z  dz
  S )Nre   r_      rj   r   rf   )r   r   rg   rl   u4s        r   	_kurtosiszReferenceDistribution._kurtosis   s[    TZZ!&!T\\7"77T\\7"77BEzA~r(   c                       fd}  j                   di   j                  di \  }}t        j                  |||f      S )Nc                 <     j                   | fi | z
  z  z  S r+   r,   )r"   ra   r   r`   r   s    r   r[   z0ReferenceDistribution._moment.<locals>.integrand   s'    499Q)&)1v:*===r(   r   )rc   r5   r   r6   )r   r`   ra   r   r[   r8   r9   s   ````   r   rb   zReferenceDistribution._moment   sN    	> >TZZ)&)Ft}}&v&1wwy1a&))r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   	vectorizer'   r   r   r   r   r"   r   funr:   s        r   rZ   zReferenceDistribution.pdf   M    ll499%  #!$t||$zz#U+B//r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   r1   r   r   r   rx   s        r   cdfzReferenceDistribution.cdf   rz   r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   r3   r   r   r   rx   s        r   sfzReferenceDistribution.sf   sM    ll488$  #!$t||$zz#U+B//r(   c                     t        j                  | j                  dh      }| j                  |      } |||fi | j                  }t        j
                  ||      d   S Nr^   )excludedr   r   )r   rw   rB   r   r   r   r   r"   rF   r   ry   r:   s         r   ppfzReferenceDistribution.ppf   S    ll499s3  #!U+dll+zz#U+B//r(   c                     t        j                  | j                  dh      }| j                  |      } |||fi | j                  }t        j
                  ||      d   S r   )r   rw   rC   r   r   r   r   s         r   isfzReferenceDistribution.isf   r   r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   rL   r   r   r   rx   s        r   rY   zReferenceDistribution.logpdf   M    ll4<<(  #!$t||$zz#U+B//r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   rO   r   r   r   rx   s        r   logcdfzReferenceDistribution.logcdf   r   r(   c                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   rQ   r   r   r   rx   s        r   logsfzReferenceDistribution.logsf   sM    ll4;;'  #!$t||$zz#U+B//r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   r5   r   r   r   r   ry   r:   s       r   supportzReferenceDistribution.support   ;    ll4==)!DLL!zz#U+B//r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   r\   r   r   r   s       r   entropyzReferenceDistribution.entropy   r   r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   rc   r   r   r   s       r   meanzReferenceDistribution.mean   ;    ll4::&!DLL!zz#U+B//r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   rh   r   r   r   s       r   varzReferenceDistribution.var   s;    ll499%!DLL!zz#U+B//r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   ro   r   r   r   s       r   skewzReferenceDistribution.skew   r   r(   c                     t        j                  | j                        } |di | j                  }t        j                  ||      d   S r   )r   rw   rs   r   r   r   s       r   kurtosiszReferenceDistribution.kurtosis   s;    ll4>>*!DLL!zz#U+B//r(   Nc                     t        j                  | j                        }| j                  |      } ||fi | j                  }t        j
                  ||      d   S r   )r   rw   rb   r   r   r   )r   r`   ra   r   ry   r:   s         r   momentzReferenceDistribution.moment   sM    ll4<<($$U+%(4<<(zz#U+B//r(   )r   )&__name__
__module____qualname____doc__r   r   r'   r1   r3   rB   rC   rL   rO   rQ   r5   r\   rc   rh   ro   rs   rb   r   r   rZ   r|   r~   r   r   rY   r   r   r   r   r   r   r   r   r   r   r(   r   r   r      s6   7r8&G
>%%..-*9:* :: 0 :: 0 ** 0 BJJ 0 BJJ 0 !jj 0 !jj 0  ZZ 0 JJ 0
 JJ 0
  0
 

 0
  0
  ZZ 0
 $(rzz 0r(   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )
SkewNormala  Reference implementation of the SkewNormal distribution.

    Follow the example here to generate new reference distributions.
    Use the reference distributions to generate reference values of
    distributions functions. For now, copy-paste the output into unit
    tests. Full code to generate reference values does not need to be
    included as a comment in the test; just refer to the reference
    distribution used and the settings (e.g. mp.dps=50).
    c                &    t         |   |       y )N)r8   superr   )r   r8   	__class__s     r   r   zSkewNormal.__init__  s     	1r(   c                 D    t         j                   t         j                  fS r+   rS   )r   r8   s     r   r5   zSkewNormal._support  s     wr(   c                 d    dt        j                  |      z  t        j                  ||z        z  S Nre   )r   npdfncdf)r   r"   r8   s      r   r'   zSkewNormal._pdf!  s'     2771:~A..r(   )r   r   r   r   r   r5   r'   __classcell__r   s   @r   r   r     s    
/r(   r   c                   6     e Zd Z fdZd Zd Zd Zd Z xZS )	BetaPrimec                (    t         |   ||       y )N)r8   r9   r   )r   r8   r9   r   s      r   r   zBetaPrime.__init__3      1"r(   c                 B    t         j                  t         j                  fS r+   )r   zerorT   rU   s     r   r5   zBetaPrime._support6  s    wwr(   c                     |t         j                  z
  t        j                  |      z  ||z   t        j                  |      z  z
  t        j                  t        j                  ||            z
  S r+   )r   r4   rJ   log1pbetar   r"   r8   r9   s       r   rL   zBetaPrime._logpdf9  sL    BFF
BFF1I%Q(;;bffRWWQPQ]>SSSr(   c                 P    t        j                  | j                  |||            S )N)r"   r8   r9   )r   rX   rL   r   s       r   r'   zBetaPrime._pdf<  s     vvdllQ!ql122r(   c           	      H    dt        j                  ||d|d|z   z  d      z
  S )Ng      ?r   r^   T)regularized)r   betaincr   s       r   r3   zBetaPrime._sf?  s&    RZZ1aAaCdCCCr(   )	r   r   r   r   r5   rL   r'   r3   r   r   s   @r   r   r   1  s    #T3Dr(   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )Burrc                (    t         |   ||       y )N)cdr   )r   r   r   r   s      r   r   zBurr.__init__E  r   r(   c                 &    dt         j                  fS Nr   rS   )r   r   r   s      r   r5   zBurr._supportH      "&&yr(   c                 B    ||z  || dz
  z  z  d|| z  z   | dz
  z  z  S )Nr^   r   )r   r"   r   r   s       r   r'   z	Burr._pdfK  s5    1uqaR!V}$A1"IA26'BBBr(   c                 $    |d|z  z  dz
  d|z  z  S )Ng      r^   r   )r   r@   rF   r   r   s        r   rB   z	Burr._ppfN  s    DFa46**r(   r   r   r   r   r5   r'   rB   r   r   s   @r   r   r   C  s    #C+r(   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )
LogLaplacec                &    t         |   |       y )N)r   r   )r   r   r   s     r   r   zLogLaplace.__init__T      1r(   c                 &    dt         j                  fS r   rS   )r   r   s     r   r5   zLogLaplace._supportW  r   r(   c                     |t         j                  k  r|dz  ||t         j                  z
  z  z  S |dz  || t         j                  z
  z  z  S r   r   r4   )r   r"   r   s      r   r'   zLogLaplace._pdfZ  sF    rvv:q51q266z?**q51rBFF{+++r(   c                     |dk  rd|z  t         j                  |z  z  S dt         j                  |z
  z  t         j                   |z  z  S )Nr0   g       @re   r   )r   qrF   r   s       r   rB   zLogLaplace._ppf`  sB    s7!Grvvz**!$!44r(   r   r   s   @r   r   r   R  s    ,5r(   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )	LogNormalc                &    t         |   |       y )N)sr   )r   r   r   s     r   r   zLogNormal.__init__i  r   r(   c                 &    dt         j                  fS r   rS   )r   r   s     r   r5   zLogNormal._supportl  r   r(   c                    t         j                  ||z  t        j                  dt         j                  z        z  z  t        j                  t         j                   dz  t        j
                  |      |z  dz  z        z  S r   )r   r4   rk   pirX   rJ   r   r"   r   s      r   r'   zLogNormal._pdfo  s^    FFa!ebgga"%%i001ffbffWq[BFF1IMA#5567	
r(   c                 X    t        j                  t        j                  |      |z        S r+   )r   r   rJ   r   s      r   r1   zLogNormal._cdfu  s    wwrvvay1}%%r(   )r   r   r   r   r5   r'   r1   r   r   s   @r   r   r   g  s    
&r(   r   c                       e Zd Zd Zy)Normalc                 ,    t        j                  |      S r+   )r   r   r&   s     r   r'   zNormal._pdf{  s    wwqzr(   N)r   r   r   r'   r   r(   r   r   r   y  s    r(   r   c                   $     e Zd Z fdZd Z xZS )NormInvGaussc                (    t         |   ||       y )N)alphar   r   )r   r   r   r   s      r   r   zNormInvGauss.__init__  s    u40r(   c                 >   t        j                  d|dz  z         }t         j                  dz  |z  t        j                  t        j                  |dz  |dz  z
              z  }||dz  z  t        j                  d||z        z  t        j                  ||z        z  S )Nr^   re   )r   rk   r   rX   besselk)r   r"   r   r   r   r8   s         r   r'   zNormInvGauss._pdf  s     GGA1HEE2Irwwuax$'/A'B CC1b5y2::aq11BFF46NBBr(   r   r   r   r   r'   r   r   s   @r   r   r     s    1Cr(   r   c                   $     e Zd Z fdZd Z xZS )Pearson3c                &    t         |   |       y )N)r   r   )r   r   r   s     r   r   zPearson3.__init__  s    d#r(   c                     d|z  }|dz  }| }t        |      t        j                  |      z  |||z
  z  |dz
  z  z  t        j                  | ||z
  z        z  }t        |j                        |k(  r|S dS )Nre   r^   r   )absr   gammarX   real)r   r"   r   r9   r8   r   r:   s          r   r'   zPearson3._pdf  sy    HqDB!fRXXa[ AqsGqs#33bffaR1X6FF#((ms*s11r(   r   r   s   @r   r   r     s    $2r(   r   c                   $     e Zd Z fdZd Z xZS )StudentTc                &    t         |   |       y )N)dfr   )r   r   r   s     r   __initzStudentT.__init  s    Br(   c                 8   t        j                  |t         j                  z   dz        t        j                  |t         j                  z        t        j                  |dz        z  z  t         j                  ||z  |z  z   |t         j                  z    dz  z  z  S r   )r   r   r4   rk   r   )r   r"   r   s      r   r'   zStudentT._pdf  sr    "rvv+q)2772:+>"Q$+OP66AaCF?RVVnQ&678 	9r(   )r   r   r   _StudentT__initr'   r   r   s   @r   r   r     s     9r(   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )
TruncExponc                &    t         |   |       y )N)r9   r   )r   r9   r   s     r   r   zTruncExpon.__init__  r   r(   c                 
    d|fS r   r   )r   r9   s     r   r5   zTruncExpon._support  s    !tr(   c                 ^    t        j                  |        t        j                  |       z  S r+   r   rX   expm1r   r"   r9   s      r   r'   zTruncExpon._pdf  s#    r
{288QB<''r(   c                     t        j                  |       t        j                  |       z
  t        j                  |       z  S r+   r   r   s      r   r3   zTruncExpon._sf  s0    r
RVVQBZ'1"55r(   )r   r   r   r   r5   r'   r3   r   r   s   @r   r   r     s    (6r(   r   )numpyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r   <module>r      s      ~0 ~0B"/& "/TD% D$+  +5& 5*&% &$" 
C( 
C	2$ 	29$ 96& 6r(   