
    >h(=                     H   S SK Jr  S SKrS SKJrJrJr  S SKJ	r	  S SK
Jr   " S S\5      r\" SS	S
9r " S S\5      r\" SSS
9r " S S\5      r\" SSS
9r " S S\5      r\" SSS
9r " S S\5      r\" SSS
9r " S S\5      r\" SSS
9r " S S \5      r " S! S"\5      rg)#    )apply_whereN)rv_discretepoissonnbinom)gammaln)GenericLikelihoodModelc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
genpoisson_p_gen   z%Generalized Poisson distribution
    c                 "    US:  X":H  -  US:  -  $ )Nr    selfmualphaps       uC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/distributions/discrete.py	_argcheckgenpoisson_p_gen._argcheck   s    aEL)QU33    c                    X$S-
  -  n[         R                  " [         R                  " SS5      SX5-  -   5      n[         R                  " [         R                  " SS5      X&S-
  U-  -   5      n[         R                  " U5      US-
  [         R                  " U5      -  -   nX[         R                  " U5      -  [	        US-   5      -   Xv-  -   -  nU$ )N      ?r      )npmaximum	nextafterlogr   )	r   xr   r   r   mu_pa1a2logpmf_s	            r   _logpmfgenpoisson_p_gen._logpmf   s    "f~ZZQ*A,<=ZZQ*Br'Q,>?&&*B"&&*44r
NWQV_4rw>>r   c                 N    [         R                  " U R                  XX45      5      $ Nr   expr#   )r   r   r   r   r   s        r   _pmfgenpoisson_p_gen._pmf   s    vvdll1%344r   c                     U$ r&   r   r   s       r   meangenpoisson_p_gen.mean   s    	r   c                 .    SX!US-
  -  -  -   S-  nXA-  nU$ Nr      r   )r   r   r   r   dispersion_factorvars         r   r2   genpoisson_p_gen.var   s*    a!e!44q8$
r   r   N__name__
__module____qualname____firstlineno____doc__r   r#   r)   r,   r2   __static_attributes__r   r   r   r
   r
      s     45r   r
   genpoisson_pzGeneralized Poisson)namelongnamec                   H    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSrg)zipoisson_gen)   z'Zero Inflated Poisson distribution
    c                 $    US:  US:  -  US:*  -  $ Nr   r   r   r   r   ws      r   r   zipoisson_gen._argcheck,       Q16"ad++r   c                     [        US:g  XU4S [        R                  " USU-
  [        R                  " U* 5      -  -   5      S9$ )Nr   c                     [         R                  " SU-
  5      U [         R                  " U5      -  -   [        U S-   5      -
  U-
  $ Nr   )r   r   r   )r   r   rD   s      r   <lambda>'zipoisson_gen._logpmf.<locals>.<lambda>3   s5    bffS1WoBFF2J>SAQQTVVr   r   
fill_value)r   r   r   r(   r   r   r   rD   s       r   r#   zipoisson_gen._logpmf/   sD    FAJVvva37bffbSk"99:	
 	
r   c                 N    [         R                  " U R                  XU5      5      $ r&   r'   rN   s       r   r)   zipoisson_gen._pmf7   s    vvdll1!,--r   c                 D    U[        US9R                  U5      SU-
  -  -   $ )Nr   r   )r   cdfrN   s       r   _cdfzipoisson_gen._cdf:   s%    7b>%%a(AE222r   c                     X-
  SU-
  -  n[        US9R                  U5      n[        U[        R                  5      (       a	  SXQU:  '   U$ [        R
                  " U5      (       a  X:  a  gU$ )Nr   rS   r           )r   ppf
isinstancer   ndarrayisscalar)r   qr   rD   q_modr   s         r   _ppfzipoisson_gen._ppf>   sd    1q5!rNu%a$$A!eH  [[^^r   c                     SU-
  U-  $ Nr   r   rC   s      r   r,   zipoisson_gen.meanI       A|r   c                 :    SX!-  -   nX0R                  X5      -  nU$ rb   r,   )r   r   rD   r1   r2   s        r   r2   zipoisson_gen.varL   s$    J 99R#33
r   c                 :    SU-
  [         R                  " X5      -  $ rb   )r   moment)r   nr   rD   s       r   _momentzipoisson_gen._momentQ   s    A...r   r   N)r5   r6   r7   r8   r9   r   r#   r)   rU   r_   r,   r2   rk   r:   r   r   r   r?   r?   )   s/    ,
.3	
/r   r?   	zipoissonzZero Inflated Poissonc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
zigeneralizedpoisson_genX   z3Zero Inflated Generalized Poisson distribution
    c                 $    US:  US:  -  US:*  -  $ rB   r   r   r   r   r   rD   s        r   r   "zigeneralizedpoisson_gen._argcheck[   rF   r   c                     [        US:g  XX4U4S [        R                  " USU-
  [        R	                  XX45      -  -   5      S9$ )Nr   c                 b    [         R                  " SU-
  5      [        R                  XX#5      -   $ rI   )r   r   r;   logpmf)r   r   r   r   rD   s        r   rJ   2zigeneralizedpoisson_gen._logpmf.<locals>.<lambda>c   s%    266#'?%%aU6,7r   r   rL   )r   r   r   r;   pmfr   r   r   r   r   rD   s         r   r#    zigeneralizedpoisson_gen._logpmf^   sN    FEa 7 vva37l.>.>qe.O"OOP
 	
r   c           	      P    [         R                  " U R                  XX4U5      5      $ r&   r'   ry   s         r   r)   zigeneralizedpoisson_gen._pmfi       vvdll1%A677r   c                     SU-
  U-  $ rb   r   rr   s        r   r,   zigeneralizedpoisson_gen.meanl   rd   r   c                 \    US-
  nSX!U-  -  -   S-  XA-  -   nXPR                  XX45      -  nU$ r/   rf   r   r   r   r   rD   r1   r2   s          r   r2   zigeneralizedpoisson_gen.varo   s@    Eq0Q6? 99R#==
r   r   Nr4   r   r   r   ro   ro   X   s     ,	
8r   ro   zigenpoissonz!Zero Inflated Generalized Poissonc                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)zinegativebinomial_gen{   z=Zero Inflated Generalized Negative Binomial distribution
    c                 $    US:  US:  -  US:*  -  $ rB   r   rr   s        r   r    zinegativebinomial_gen._argcheck~   rF   r   c                     U R                  X#U5      u  pd[        US:g  XXE4S [        R                  " USU-
  [        R
                  " XU5      -  -   5      S9$ )Nr   c                 d    [         R                  " SU-
  5      [        R                  " XU5      -   $ rI   )r   r   r   rv   )r   sr   rD   s       r   rJ   0zinegativebinomial_gen._logpmf.<locals>.<lambda>   s     sQw&--a2H Hr   r   rL   )convert_paramsr   r   r   r   rx   r   r   r   r   r   rD   r   s          r   r#   zinegativebinomial_gen._logpmf   sY    ""2a0F1LHvva37fjjq.A"AAB	
 	
r   c           	      P    [         R                  " U R                  XX4U5      5      $ r&   r'   ry   s         r   r)   zinegativebinomial_gen._pmf   r}   r   c                 j    U R                  X#U5      u  pdU[        R                  " XU5      SU-
  -  -   $ rb   )r   r   rT   r   s          r   rU   zinegativebinomial_gen._cdf   s7    ""2a0 6::aA&!a%000r   c                    U R                  X#U5      u  pdX-
  SU-
  -  n[        R                  " XvU5      n[        U[        R
                  5      (       a	  SXU:  '   U$ [        R                  " U5      (       a  X:  a  gU$ )Nr   r   rX   )r   r   rY   rZ   r   r[   r\   )	r   r]   r   r   r   rD   r   r^   r   s	            r   r_   zinegativebinomial_gen._ppf   st    ""2a01q5!JJu#a$$A!eH  [[^^r   c                     SU-
  U-  $ rb   r   rr   s        r   r,   zinegativebinomial_gen.mean   rd   r   c                 R    SX!US-
  -  -  -   XA-  -   nXPR                  XX45      -  nU$ rb   rf   r   s          r   r2   zinegativebinomial_gen.var   s7    q1u 55> 99R#==
r   c                 d    U R                  X#U5      u  pdSU-
  [        R                  " XU5      -  $ rb   )r   r   ri   )r   rj   r   r   r   rD   r   s          r   rk   zinegativebinomial_gen._moment   s0    ""2a0AqQ///r   c                 2    SU-  USU-
  -  -  nXDU-   -  nXE4$ Nr   r0   r   r   r   r   r   sizeprobs         r   r   %zinegativebinomial_gen.convert_params   ,    EzB1I%by!|r   r   N)r5   r6   r7   r8   r9   r   r#   r)   rU   r_   r,   r2   rk   r   r:   r   r   r   r   r   {   s4    ,
81

0r   r   zinegbinz+Zero Inflated Generalized Negative Binomialc                   0    \ rS rSrSrS rS rS rS rSr	g)	truncatedpoisson_gen   z/Truncated Poisson discrete random variable
    c                     US:  US:  -  $ Nr   r   r   r   
truncations      r   r   truncatedpoisson_gen._argcheck   s     aJ",--r   c                 $    US-   U R                   4$ rb   br   s      r   _get_support!truncatedpoisson_gen._get_support       A~tvv%%r   c                    Sn[        [        [        R                  " U5      5      S-   5       H  nU[        R
                  " XR5      -  nM     [        R                  " U[        R                  * 5      nUS:  n[        R                  Xg'   US:  n[        R                  " SXG   -
  5      Xg'   [        R                  " X5      U-
  nU$ rB   )rangeintr   maxr   rx   	full_likeinfnanr   rv   )	r   r   r   r   rx   ilog_1_m_pmflocr"   s	            r   r#   truncatedpoisson_gen._logpmf   s    s266*-.23A7;;q%%C 4 ll30Ag66Ag66!ch,/..'+5r   c                 N    [         R                  " U R                  XU5      5      $ r&   r'   )r   r   r   r   s       r   r)   truncatedpoisson_gen._pmf   s    vvdll1*566r   r   N)
r5   r6   r7   r8   r9   r   r   r#   r)   r:   r   r   r   r   r      s    .&7r   r   truncatedpoissonzTruncated Poissonc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
truncatednegbin_gen   zLTruncated Generalized Negative Binomial (NB-P) discrete random variable
    c                     US:  US:  -  $ r   r   r   r   r   r   r   s        r   r   truncatednegbin_gen._argcheck   s    aJ",--r   c                 $    US-   U R                   4$ rb   r   r   s        r   r    truncatednegbin_gen._get_support   r   r   c                    U R                  X#U5      u  pgSn[        [        [        R                  " U5      5      S-   5       H  n	U[
        R                  " XU5      -  nM     [        R                  " U[        R                  * 5      n
US:  n[        R                  X'   US:  n[        R                  " SX   -
  5      X'   [
        R                  " XU5      U
-
  nU$ rB   )r   r   r   r   r   r   rx   r   r   r   r   rv   )r   r   r   r   r   r   r   r   rx   r   r   r   r"   s                r   r#   truncatednegbin_gen._logpmf   s    ((A6
s266*-.23A6::at,,C 4 ll30Ag66Ag66!ch,/--.<r   c           	      P    [         R                  " U R                  XX4U5      5      $ r&   r'   )r   r   r   r   r   r   s         r   r)   truncatednegbin_gen._pmf   s    vvdll1%J?@@r   c                 2    SU-  USU-
  -  -  nXDU-   -  nXE4$ r   r   r   s         r   r   "truncatednegbin_gen.convert_params   r   r   r   N)r5   r6   r7   r8   r9   r   r   r#   r)   r   r:   r   r   r   r   r      s!    .& Ar   r   truncatednegbinz'Truncated Generalized Negative Binomialc                   |   ^  \ rS rSrSrU 4S jrSU 4S jjrU 4S jrS rSSS.S	 jr	S
 r
S rS rS rS rSrU =r$ )DiscretizedCount   u  Count distribution based on discretized distribution

Parameters
----------
distr : distribution instance
d_offset : float
    Offset for integer interval, default is zero.
    The discrete random variable is ``y = floor(x + offset)`` where x is
    the continuous random variable.
    Warning: not verified for all methods.
add_scale : bool
    If True (default), then the scale of the base distribution is added
    as parameter for the discrete distribution. The scale parameter is in
    the last position.
kwds : keyword arguments
    The extra keyword arguments are used delegated to the ``__init__`` of
    the super class.
    Their usage has not been checked, e.g. currently the support of the
    distribution is assumed to be all non-negative integers.

Notes
-----
`loc` argument is currently not supported, scale is not available for
discrete distributions in scipy. The scale parameter of the underlying
continuous distribution is the last shape parameter in this
DiscretizedCount distribution if ``add_scale`` is True.

The implementation was based mainly on [1]_ and [2]_. However, many new
discrete distributions have been developed based on the approach that we
use here. Note, that in many cases authors reparameterize the distribution,
while this class inherits the parameterization from the underlying
continuous distribution.

References
----------
.. [1] Chakraborty, Subrata, and Dhrubajyoti Chakravarty. "Discrete gamma
   distributions: Properties and parameter estimations." Communications in
   Statistics-Theory and Methods 41, no. 18 (2012): 3301-3324.

.. [2] Alzaatreh, Ayman, Carl Lee, and Felix Famoye. 2012. “On the Discrete
   Analogues of Continuous Distributions.” Statistical Methodology 9 (6):
   589–603.


c                 *   > [         [        U ]  U 5      $ r&   )superr   __new__)clsargskwds	__class__s      r   r   DiscretizedCount.__new__*  s     [#.s33r   c                   > Xl         X l        UR                  U l        X0l        UR                  bf  [        UR                  R                  S5      5      U l        U(       a5  UR                  SUR                  S-   05        U =R                  S-  sl        O)U(       a  UR                  SS05        SU l        OSU l        [        TU ](  " S0 UD6  g )N,shapesz, sr   r   r   r   )distrd_offset_ctor_param	add_scaler   lensplitk_shapesupdater   __init__)r   r   r   r   r   r   s        r   r   DiscretizedCount.__init__/  s    
  ,,"<<# 2 23 78DMXu||e';<=" XsO, ! ! 4 r   c                 B   > [         TU ]  5       nU R                  US'   U$ )Nr   )r   _updated_ctor_paramr   )r   dicr   s     r   r   $DiscretizedCount._updated_ctor_paramD  s"    g)+zzG
r   c                 H    U R                   (       a  US   nUS S nX4$ SnX4$ )Nr   r   )r   )r   r   scales      r   _unpack_argsDiscretizedCount._unpack_argsI  s6    >>HE9D { E{r   N)r   random_statec          	          U R                  U5      u  p4Uc  [        U SS5      n[        R                  " U R                  R
                  " X4UUS.6U R                  -   5      nU$ )N_sizer   )r   r   r   )r   getattrr   truncr   rvsr   )r   r   r   r   r   rvs         r   _rvsDiscretizedCount._rvsQ  s_    ''-<4!,DXXdjjnndd2>@mm$ % 	r   c                     U R                   nU R                  S:w  a  XR                  -   nU R                  U5      u  p$UR                  " U/UQ7SU06UR                  " US-   /UQ7SU06-
  nU$ )Nr   r   r   )r   r   r   sfr   r   r   r   r   r   s         r   r)   DiscretizedCount._pmfZ  sq    

==AMM!A''-XXa,$,e,XXa!e0d0%01r   c                     U R                   nU R                  U5      u  p$U R                  S:w  a  XR                  -   nUR                  " US-   /UQ7SU06nU$ Nr   r   r   )r   r   r   rT   r   s         r   rU   DiscretizedCount._cdfe  sV    

''-==AMM!AIIa!e0d0%0r   c                     U R                   nU R                  U5      u  p$U R                  S:w  a  XR                  -   nUR                  " US-   /UQ7SU06nU$ r   )r   r   r   r   r   s         r   _sfDiscretizedCount._sfm  sV    

''-==AMM!AHHQU/T//r   c                     U R                   nU R                  U5      u  p$UR                  " U/UQ7SU06nU R                  S:w  a  XPR                  -   n[        R
                  " US-  5      nU$ Nr   r   g?)r   r   rY   r   r   floorr   r   r   r   r   qcr]   s          r   r_   DiscretizedCount._ppfu  e    

''-YYq-4-u-==Amm#BHHR9%&r   c                     U R                   nU R                  U5      u  p$UR                  " U/UQ7SU06nU R                  S:w  a  XPR                  -   n[        R
                  " US-  5      nU$ r  )r   r   isfr   r   r  r  s          r   _isfDiscretizedCount._isf  r	  r   )r   r   r   r   r   )r   T)r5   r6   r7   r8   r9   r   r   r   r   r   r)   rU   r  r_   r  r:   __classcell__r   s   @r   r   r      sG    ,\4
!*
  $$ 	 r   r   c                   F   ^  \ rS rSrSrSU 4S jjrS rS	S jrS rSr	U =r
$ )
DiscretizedModeli  aj  experimental model to fit discretized distribution

Count models based on discretized distributions can be used to model
data that is under- or over-dispersed relative to Poisson or that has
heavier tails.

Parameters
----------
endog : array_like, 1-D
    Univariate data for fitting the distribution.
exog : None
    Explanatory variables are not supported. The ``exog`` argument is
    only included for consistency in the signature across models.
distr : DiscretizedCount instance
    (required) Instance of a DiscretizedCount distribution.

See Also
--------
DiscretizedCount

Examples
--------
>>> from scipy import stats
>>> from statsmodels.distributions.discrete import (
        DiscretizedCount, DiscretizedModel)

>>> dd = DiscretizedCount(stats.gamma)
>>> mod = DiscretizedModel(y, distr=dd)
>>> res = mod.fit()
>>> probs = res.predict(which="probs", k_max=5)

c                 ^  > Ub  [        S5      e[        TU ]	  XUS9  U R                  R	                  S5        [        U5      UR                  -
  U l        SU l        UR                  U l	        SU l
        UR                  U l        S[        R                  " U R                  5      -  U l        g )Nexog is not supportedr   r   r   g      ?)
ValueErrorr   r   
_init_keysappendr   r   df_residdf_modelk_extra
k_constantnparamsr   onesstart_params)r   endogexogr   r   s       r   r   DiscretizedModel.__init__  s    455E2w'E
U^^3~~~~"''$,,"77r   c                     Un[         R                  " U R                  R                  " U R                  /UQ76 5      nUR                  5       $ r&   )r   r   r   r)   r  sum)r   paramsr   lls       r   loglikeDiscretizedModel.loglike  s8    
 VVDJJOODJJ667vvxr   c                     Ub  [        S5      eUnUS:X  a3  U R                  R                  " [        R                  " U5      /UQ76 nU$ [        S5      e)Nr  probsz+only which="probs" is currently implemented)r  r   rx   r   arange)r   r$  r   whichk_maxr   prs          r   predictDiscretizedModel.predict  sR    455G		% 0848BIJKKr   c                 (    UnU R                   " U6 nU$ )zCfrozen distribution instance of the discrete distribution.
        r  )r   r$  r   r   s       r   	get_distrDiscretizedModel.get_distr  s     

D!r   )r  r  r  r  r  r  )NN)NN   )r5   r6   r7   r8   r9   r   r&  r.  r1  r:   r  r  s   @r   r  r    s#    @8
L r   r  )statsmodels.compat.scipyr   numpyr   scipy.statsr   r   r   scipy.specialr   statsmodels.base.modelr   r
   r;   r?   rm   ro   r   r   r   r   r   r   r   r   r  r   r   r   <module>r9     s    0  4 4 ! 9{ 4  ^)>@)/K )/X {#:<	{ < (	02
3[ 3j "z:<7; 7@ (-?1DF + B &+<68L{ L^H- Hr   