
    M/Ph`;                        d dl Zd dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZ  G d de          Z edd	
          Z G d de          Z edd
          Z G d de          Z edd
          Z G d de          Z edd
          Z G d de          Z edd
          Z G d de          Z edd
          Z G d d e          Z G d! d"e          ZdS )#    N)rv_discretepoissonnbinom)gammaln)
_lazywhere)GenericLikelihoodModelc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )genpoisson_p_genz%Generalized Poisson distribution
    c                 *    |dk    ||k    z  |dk    z  S )Nr    selfmualphaps       b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/distributions/discrete.py	_argcheckzgenpoisson_p_gen._argcheck   s    aE5L)QU33    c                    ||dz
  z  }t          j        t          j        dd          d||z  z             }t          j        t          j        dd          ||dz
  |z  z             }t          j        |          |dz
  t          j        |          z  z   }||t          j        |          z  t	          |dz             z   ||z  z   z  }|S )N      ?r      )npmaximum	nextafterlogr   )	r   xr   r   r   mu_pa1a2logpmf_s	            r   _logpmfzgenpoisson_p_gen._logpmf   s    a"f~ZQ**A,<==ZQ**B"r'Q,>??&**B"&**44Ar

NWQV__4rBw>>r   c                 V    t          j        |                     ||||                    S Nr   expr!   )r   r   r   r   r   s        r   _pmfzgenpoisson_p_gen._pmf   s$    vdll1b%33444r   c                     |S r#   r   r   s       r   meanzgenpoisson_p_gen.mean   s    	r   c                 2    d|||dz
  z  z  z   dz  }||z  }|S Nr      r   )r   r   r   r   dispersion_factorvars         r   r-   zgenpoisson_p_gen.var   s.    a!e!44q8"$
r   N	__name__
__module____qualname____doc__r   r!   r&   r(   r-   r   r   r   r
   r
   
   si         4 4 4  5 5 5      r   r
   genpoisson_pzGeneralized Poisson)namelongnamec                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )zipoisson_genz'Zero Inflated Poisson distribution
    c                 *    |dk    |dk    z  |dk    z  S Nr   r   r   r   r   ws      r   r   zzipoisson_gen._argcheck+       Q16"ad++r   c                     t          |dk    |||fd t          j        |d|z
  t          j        |           z  z                       S )Nr   c                     t          j        d|z
            | t          j        |          z  z   t          | dz             z
  |z
  S Nr   )r   r   r   )r   r   r;   s      r   <lambda>z'zipoisson_gen._logpmf.<locals>.<lambda>0   s=    BF26NNQ^,K!!b&//-*,.-/ r   r   )r   r   r   r%   r   r   r   r;   s       r   r!   zzipoisson_gen._logpmf.   sQ    !q&1b!*/ /&b1fs%;!;<<> > 	>r   c                 T    t          j        |                     |||                    S r#   r$   rA   s       r   r&   zzipoisson_gen._pmf4   s"    vdll1b!,,---r   c                 Z    |t          |                              |          d|z
  z  z   S )Nr   r   )r   cdfrA   s       r   _cdfzzipoisson_gen._cdf7   s,    7b>>>%%a((AE222r   c                     ||z
  d|z
  z  }t          |                              |          }t          |t          j                  r
d|||k     <   nt          j        |          r||k     rdS |S )Nr   rD   r           )r   ppf
isinstancer   ndarrayisscalar)r   qr   r;   q_modr   s         r   _ppfzzipoisson_gen._ppf;   st    Q1q5!rNNNu%%a$$ 	Aa!eHH[^^ 	A3r   c                     d|z
  |z  S Nr   r   r:   s      r   r(   zzipoisson_gen.meanF       A|r   c                 H    d||z  z   }||                      ||          z  }|S rQ   r(   )r   r   r;   r,   r-   s        r   r-   zzipoisson_gen.varI   s-    BJ 499R#3#33
r   c                 8    d|z
  t          j        ||          z  S rQ   )r   moment)r   nr   r;   s       r   _momentzzipoisson_gen._momentN   s    A2....r   N)r/   r0   r1   r2   r   r!   r&   rF   rO   r(   r-   rX   r   r   r   r7   r7   (   s         , , ,> > >. . .3 3 3	 	 	    
/ / / / /r   r7   	zipoissonzZero Inflated Poissonc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )zigeneralizedpoisson_genz3Zero Inflated Generalized Poisson distribution
    c                 *    |dk    |dk    z  |dk    z  S r9   r   r   r   r   r   r;   s        r   r   z"zigeneralizedpoisson_gen._argcheckX   r<   r   c                     t          |dk    |||||fd t          j        |d|z
  t                              ||||          z  z                       S )Nr   c                 l    t          j        d|z
            t                              | |||          z   S r?   )r   r   r3   logpmf)r   r   r   r   r;   s        r   r@   z2zigeneralizedpoisson_gen._logpmf.<locals>.<lambda>]   s.    bfR!Vnn&--aUA>>7? r   r   )r   r   r   r3   pmfr   r   r   r   r   r;   s         r   r!   z zigeneralizedpoisson_gen._logpmf[   si    !q&1b%A"6? ?&b1f&**1b%;;&< "< = => > 	>r   c           	      X    t          j        |                     |||||                    S r#   r$   rb   s         r   r&   zzigeneralizedpoisson_gen._pmfb   &    vdll1b%A66777r   c                     d|z
  |z  S rQ   r   r]   s        r   r(   zzigeneralizedpoisson_gen.meane   rR   r   c                 n    |dz
  }d|||z  z  z   dz  ||z  z   }||                      ||||          z  }|S r*   rT   r   r   r   r   r;   r,   r-   s          r   r-   zzigeneralizedpoisson_gen.varh   sM    Eq0Q6R? 499R1#=#==
r   Nr.   r   r   r   r[   r[   U   si         , , ,> > >8 8 8      r   r[   zigenpoissonz!Zero Inflated Generalized Poissonc                   H    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )zinegativebinomial_genz=Zero Inflated Generalized Negative Binomial distribution
    c                 *    |dk    |dk    z  |dk    z  S r9   r   r]   s        r   r   z zinegativebinomial_gen._argcheckw   r<   r   c                     |                      |||          \  }}t          |dk    ||||fd t          j        |d|z
  t	          j        |||          z  z                       S )Nr   c                 ^    t          j        d|z
            t          j        | ||          z   S r?   )r   r   r   r`   )r   sr   r;   s       r   r@   z0zinegativebinomial_gen._logpmf.<locals>.<lambda>}   s(    bfR!Vnn -1a00/1 r   r   )convert_paramsr   r   r   r   ra   r   r   r   r   r   r;   rn   s          r   r!   zzinegativebinomial_gen._logpmfz   s{    ""2ua001!q&1aA,1 1&b1f *Q1--&. ". / /0 0 	0r   c           	      X    t          j        |                     |||||                    S r#   r$   rb   s         r   r&   zzinegativebinomial_gen._pmf   rd   r   c                 t    |                      |||          \  }}|t          j        |||          d|z
  z  z   S rQ   )ro   r   rE   rp   s          r   rF   zzinegativebinomial_gen._cdf   sA    ""2ua001 6:aA&&!a%000r   c                     |                      |||          \  }}||z
  d|z
  z  }t          j        |||          }t          |t          j                  r
d|||k     <   nt	          j        |          r||k     rdS |S )Nr   r   rH   )ro   r   rI   rJ   r   rK   rL   )	r   rM   r   r   r   r;   rn   rN   r   s	            r   rO   zzinegativebinomial_gen._ppf   s    ""2ua001Q1q5!Jua##a$$ 	Aa!eHH[^^ 	A3r   c                     d|z
  |z  S rQ   r   r]   s        r   r(   zzinegativebinomial_gen.mean   rR   r   c                 d    d|||dz
  z  z  z   ||z  z   }||                      ||||          z  }|S rQ   rT   rg   s          r   r-   zzinegativebinomial_gen.var   sD    q1u 55B> 499R1#=#==
r   c                 n    |                      |||          \  }}d|z
  t          j        |||          z  S rQ   )ro   r   rV   )r   rW   r   r   r   r;   rn   s          r   rX   zzinegativebinomial_gen._moment   s:    ""2ua001Aq!Q////r   c                 6    d|z  |d|z
  z  z  }|||z   z  }||fS Nr   r+   r   r   r   r   r   sizeprobs         r   ro   z%zinegativebinomial_gen.convert_params   0    EzB1I%tby!d|r   N)r/   r0   r1   r2   r   r!   r&   rF   rO   r(   r-   rX   ro   r   r   r   rj   rj   t   s         , , ,0 0 08 8 81 1 1
 
 
    
0 0 0    r   rj   zinegbinz+Zero Inflated Generalized Negative Binomialc                   *    e Zd ZdZd Zd Zd Zd ZdS )truncatedpoisson_genz/Truncated Poisson discrete random variable
    c                     |dk    |dk    z  S Nr   r   r   r   
truncations      r   r   ztruncatedpoisson_gen._argcheck   s     aJ",--r   c                     |dz   | j         fS rQ   br   s      r   _get_supportz!truncatedpoisson_gen._get_support       A~tv%%r   c                    d}t          t          t          j        |                    dz             D ]}|t	          j        ||          z  }t          j        |t          j                   }|dk    }t          j        ||<   |dk     }t          j	        d||         z
            ||<   t	          j
        ||          |z
  }|S r9   )rangeintr   maxr   ra   	full_likeinfnanr   r`   )	r   r   r   r   ra   ilog_1_m_pmflocr    s	            r   r!   ztruncatedpoisson_gen._logpmf   s    s26*--..233 	& 	&A7;q"%%%CC l300Ag6CAg6!c#h,//C.B''+5r   c                 T    t          j        |                     |||                    S r#   r$   )r   r   r   r   s       r   r&   ztruncatedpoisson_gen._pmf   s"    vdll1b*55666r   N)r/   r0   r1   r2   r   r   r!   r&   r   r   r   r   r      sZ         . . .& & &  7 7 7 7 7r   r   truncatedpoissonzTruncated Poissonc                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )truncatednegbin_genzLTruncated Generalized Negative Binomial (NB-P) discrete random variable
    c                     |dk    |dk    z  S r   r   r   r   r   r   r   s        r   r   ztruncatednegbin_gen._argcheck   s    aJ",--r   c                     |dz   | j         fS rQ   r   r   s        r   r   z truncatednegbin_gen._get_support   r   r   c                    |                      |||          \  }}d}t          t          t          j        |                    dz             D ]}	|t          j        |	||          z  }t          j        |t          j                   }
|dk    }t          j	        |
|<   |dk     }t          j
        d||         z
            |
|<   t          j        |||          |
z
  }|S r9   )ro   r   r   r   r   r   ra   r   r   r   r   r`   )r   r   r   r   r   r   rz   r{   ra   r   r   r   r    s                r   r!   ztruncatednegbin_gen._logpmf   s    ((UA66
ds26*--..233 	- 	-A6:at,,,CC l300Ag6CAg6!c#h,//C-4..<r   c           	      X    t          j        |                     |||||                    S r#   r$   )r   r   r   r   r   r   s         r   r&   ztruncatednegbin_gen._pmf   s&    vdll1b%J??@@@r   c                 6    d|z  |d|z
  z  z  }|||z   z  }||fS rx   r   ry   s         r   ro   z"truncatednegbin_gen.convert_params   r|   r   N)	r/   r0   r1   r2   r   r   r!   r&   ro   r   r   r   r   r      sl         . . .& & &   A A A    r   r   truncatednegbinz'Truncated Generalized Negative Binomialc                   j     e Zd ZdZ fdZd fd	Z fdZd Zddd	d
Zd Z	d Z
d Zd Zd Z xZS )DiscretizedCountun  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                 T    t          t          |                               |           S r#   )superr   __new__)clsargskwds	__class__s      r   r   zDiscretizedCount.__new__"  s#     [#&&..s333r   r   Tc                    || _         || _        |j        | _        || _        |j        ^t          |j                            d                    | _        |r/|                    d|j        dz   i           | xj        dz  c_        n(|r|                    ddi           d| _        nd| _         t                      j
        di | d S )N,shapesz, sr   rn   r   r   )distrd_offset_ctor_param	add_scaler   lensplitk_shapesupdater   __init__)r   r   r   r   r   r   s        r   r   zDiscretizedCount.__init__'  s    
  ,"<# 2 23 7 788DM #Xu|e';<==="  "XsO,,, ! !  4     r   c                 \    t                                                      }| j        |d<   |S )Nr   )r   _updated_ctor_paramr   )r   dicr   s     r   r   z$DiscretizedCount._updated_ctor_param<  s(    gg))++zG
r   c                 B    | j         r|d         }|d d         }nd}||fS )Nr   r   )r   )r   r   scales      r   _unpack_argszDiscretizedCount._unpack_argsA  s3    > 	HE9DDEU{r   N)rz   random_statec          	          |                      |          \  }}|t          | dd          }t          j         | j        j        ||||d| j        z             }|S )N_sizer   )r   rz   r   )r   getattrr   truncr   rvsr   )r   rz   r   r   r   rvs         r   _rvszDiscretizedCount._rvsI  st    ''--e<4!,,DXndjnd%d2>@ @ @m$ % % 	r   c                     | j         }| j        dk    r
|| j        z   }|                     |          \  }} |j        |g|R d|i |j        |dz   g|R d|iz
  }|S )Nr   r   r   )r   r   r   sfr   r   r   r   r   r   s         r   r&   zDiscretizedCount._pmfR  s    
=ADM!A''--eUXa,$,,,e,,UXa!e0d000%001r   c                     | j         }|                     |          \  }}| j        dk    r
|| j        z   } |j        |dz   g|R d|i}|S Nr   r   r   )r   r   r   rE   r   s         r   rF   zDiscretizedCount._cdf]  sc    
''--e=ADM!AEIa!e0d000%00r   c                     | j         }|                     |          \  }}| j        dk    r
|| j        z   } |j        |dz   g|R d|i}|S r   )r   r   r   r   r   s         r   _sfzDiscretizedCount._sfe  sc    
''--e=ADM!AEHQU/T/////r   c                     | j         }|                     |          \  }} |j        |g|R d|i}| j        dk    r
|| j        z   }t	          j        |dz            }|S Nr   r   g?)r   r   rI   r   r   floorr   r   r   r   r   qcrM   s          r   rO   zDiscretizedCount._ppfm  s    
''--eUYq-4---u--=Adm#BHR9%&&r   c                     | j         }|                     |          \  }} |j        |g|R d|i}| j        dk    r
|| j        z   }t	          j        |dz            }|S r   )r   r   isfr   r   r   r   s          r   _isfzDiscretizedCount._isfw  r   r   )r   T)r/   r0   r1   r2   r   r   r   r   r   r&   rF   r   rO   r   __classcell__r   s   @r   r   r      s        , ,\4 4 4 4 4
! ! ! ! ! !*    
    $$     	 	 	            r   r   c                   8     e Zd ZdZd fd	Zd Zd	dZd Z xZS )
DiscretizedModela  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)

    Nc                 l   |t          d          t                                          |||           | j                            d           t          |          |j        z
  | _        d| _        |j        | _	        d| _
        |j        | _        dt          j        | j                  z  | _        d S )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   zDiscretizedModel.__init__  s    4555E222w'''E

U^3~~"'$,"7"77r   c                     |}t          j         | j        j        | j        g|R            }|                                S r#   )r   r   r   r&   r   sum)r   paramsr   lls       r   loglikezDiscretizedModel.loglike  s=    
 VODJODJ666677vvxxr      c                     |t          d          |}|dk    r' | j        j        t          j        |          g|R  }|S t          d          )Nr   probsz+only which="probs" is currently implemented)r   r   ra   r   arange)r   r   r   whichk_maxr   prs          r   predictzDiscretizedModel.predict  s`    4555G	% 0 084888BIJKKKr   c                     |} | j         | }|S )zCfrozen distribution instance of the discrete distribution.
        r   )r   r   r   r   s       r   	get_distrzDiscretizedModel.get_distr  s     
D!r   )NN)NNr   )	r/   r0   r1   r2   r   r   r   r   r   r   s   @r   r   r     s~         @8 8 8 8 8 8  
L 
L 
L 
L      r   r   )numpyr   scipy.statsr   r   r   scipy.specialr   scipy._lib._utilr   statsmodels.base.modelr   r
   r3   r7   rY   r[   rh   rj   r}   r   r   r   r   r   r   r   r   r   <module>r      s       4 4 4 4 4 4 4 4 4 4 ! ! ! ! ! ! ' ' ' ' ' ' 9 9 9 9 9 9    {   4  ^)>@ @ @'/ '/ '/ '/ '/K '/ '/ '/T M{#:< < <	    {   4 ('	02 2 2
2 2 2 2 2[ 2 2 2h "!z:< < <7 7 7 7 7; 7 7 7@ ('-?1DF F F     +   B &%+<68 8 8L L L L L{ L L L^H H H H H- H H H H Hr   