
    M/Ph_'                     x   d Z ddlZddlmZ  G d d          Z G d d          Zedk    rd	Z ej	        d
          Z
ej        Zd Zd Z ee
eee          Z ee                    d	                     e                     ej        ddd                    \  ZZe                     ej        ddd                    \  ZZ ee                                           e                    d          \  ZZer(ddlmZ  ej        ed            ej        d           ej        ZdZ eee          Z  ej        ddd          Z! ee                     e!                      ee                     e!                     er+ ej"                     e #                     ej        ddd          d ej$        d                      ej        d            ej"                    Z% e&g d          D ]h\  Z'Z(e%)                    dde'd	z              e #                     ej        ddd          e( ej$        d                      ej        de(z             id Z(e                     e(d!"          Z ej"                      ej        ed#            ej        d$e(z             dS dS dS )%a)  Parametric Mixture Distributions

Created on Sat Jun 04 2011

Author: Josef Perktold


Notes:

Compound Poisson has mass point at zero
https://en.wikipedia.org/wiki/Compound_Poisson_distribution
and would need special treatment

need a distribution that has discrete mass points and contiuous range, e.g.
compound Poisson, Tweedie (for some parameter range),
pdf of Tobit model (?) - truncation with clipping

Question: Metaclasses and class factories for generating new distributions from
existing distributions by transformation, mixing, compounding

    N)statsc                   0    e Zd ZdZ	 d	dZd
dZd Zd ZdS )ParametricMixtureDa  mixtures with a discrete distribution

    The mixing distribution is a discrete distribution like scipy.stats.poisson.
    All distribution in the mixture of the same type and parametrized
    by the outcome of the mixing distribution and have to be a continuous
    distribution (or have a pdf method).
    As an example, a mixture of normal distributed random variables with
    Poisson as the mixing distribution.


    assumes vectorized shape, loc and scale as in scipy.stats.distributions

    assume mixing_dist is frozen

    initialization looks fragile for all possible cases of lower and upper
    bounds of the distributions.

    MbP?c                    || _         || _        t          j        |j        j                  s|j        j        }n|                    d          }t          j        |j        j                  s|j        j        }n|	                    d          }|| _
        || _        t          j        ||dz             }|                    |          | _         ||          | _         ||          | _        dS )a  create a mixture distribution

        Parameters
        ----------
        mixing_dist : discrete frozen distribution
            mixing distribution
        base_dist : continuous distribution
            parametrized distributions in the mixture
        bd_args_func : callable
            function that builds the tuple of args for the base_dist.
            The function obtains as argument the values in the support of
            the mixing distribution and should return an empty tuple or
            a tuple of arrays.
        bd_kwds_func : callable
            function that builds the dictionary of kwds for the base_dist.
            The function obtains as argument the values in the support of
            the mixing distribution and should return an empty dictionary or
            a dictionary with arrays as values.
        cutoff : float
            If the mixing distribution has infinite support, then the
            distribution is truncated with approximately (subject to integer
            conversion) the cutoff probability in the missing tail. Random
            draws that are outside the truncated range are clipped, that is
            assigned to the highest or lowest value in the truncated support.

        g-C6?   N)mixing_dist	base_distnpisneginfdistappfisposinfbisfmambarangepmfmixing_probsbd_argsbd_kwds)	selfr	   r
   bd_args_funcbd_kwds_funccutoffloweruppermixing_supports	            k/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/sandbox/distributions/otherdist.py__init__zParametricMixtureD.__init__.   s    8 '"{;+-.. 	*$&EEOOD))E{;+-.. 	*$&EEOOD))E5%'22'OON;;#|N33#|N33    r   c                      j                             |          }t          j        | j         j                   j        z
                      t                    t          fd j	        D                       } fd j
        D             }d|i}|                    |             j        j         j	        i |}|fS )Nc              3   (   K   | ]}|         V  d S N ).0mdmrvs_idxs     r!   	<genexpr>z)ParametricMixtureD.rvs.<locals>.<genexpr>b   s'      <<8<<<<<<r#   c                 8    i | ]}|j         |                  S r'   )r   )r(   kr*   r   s     r!   
<dictcomp>z*ParametricMixtureD.rvs.<locals>.<dictcomp>c   s&    FFFA1dl1oh/FFFr#   size)r	   rvsr   clipr   r   astypeinttupler   r   updater
   )r   r/   mrvsr   r   kwdsr0   r*   s   `      @r!   r0   zParametricMixtureD.rvs]   s    ##D))GD$'4733dg=EEcJJ<<<<t|<<<<<FFFFFFFFt}G dn $,7$77H}r#   c                     t          j        |          }t          j        |          dk    r|d         } | j        j        |g| j        R i | j        }|| j        z                      d          }||fS Nr   ).N)	r   asarrayr/   r
   pdfr   r   r   sumr   xbd_probsprobs       r!   r<   zParametricMixtureD.pdfm   v    JqMM71::>>(A%4>%aG$,GGG$,GG4,,11"55X~r#   c                     t          j        |          }t          j        |          dk    r|d         } | j        j        |g| j        R i | j        }|| j        z                      d          }||fS r9   )	r   r;   r/   r
   cdfr   r   r   r=   r>   s       r!   rD   zParametricMixtureD.cdfu   rB   r#   N)r   )r   )__name__
__module____qualname____doc__r"   r0   r<   rD   r'   r#   r!   r   r      sj         & -4 -4 -4 -4^
 
 
 
       r#   r   c                   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 )ClippedContinuousa  clipped continuous distribution with a masspoint at clip_lower


    Notes
    -----
    first version, to try out possible designs
    insufficient checks for valid arguments and not clear
    whether it works for distributions that have compact support

    clip_lower is fixed and independent of the distribution parameters.
    The clip_lower point in the pdf has to be interpreted as a mass point,
    i.e. different treatment in integration and expect function, which means
    none of the generic methods for this can be used.

    maybe this will be better designed as a mixture between a degenerate or
    discrete and a continuous distribution

    Warning: uses equality to check for clip_lower values in function
    arguments, since these are floating points, the comparison might fail
    if clip_lower values are not exactly equal.
    We could add a check whether the values are in a small neighborhood, but
    it would be expensive (need to search and check all values).

    c                 "    || _         || _        d S r&   )r
   
clip_lower)r   r
   rL   s      r!   r"   zClippedContinuous.__init__   s    "$r#   c                 L    d|vr| j         }n|                    d          }||fS )z@helper method to get clip_lower from kwds or attribute

        rL   )rL   pop)r   r7   rL   s      r!   _get_clip_lowerz!ClippedContinuous._get_clip_lower   s4     t##JJ,//J4r#   c                 l    |                      |          \  }} | j        j        |i |}||||k     <   |S r&   )rO   r
   r0   )r   argsr7   rL   rvs_s        r!   r0   zClippedContinuous.rvs   sG    //55
D!t~!40400",TJr#   c                 F   t          j        |          }d|vr| j        }n|                    d          }t          j         | j        j        |g|R i |          }|| j        k    }t          j        |          r | j        j        |g|R i |}|||<   d|||k     <   |S NrL   r   )r   
atleast_1drL   rN   r
   r<   anyrD   )r   r?   rQ   r7   rL   pdf_raw	clip_mask	clip_probs           r!   r<   zClippedContinuous.pdf   s    M!t##JJ ,//J- 2 21 Dt D D Dt D DEE$/)	6) 	+**:EEEEEEI!*GI #$Jr#   c                     d|vr| j         }n|                    d          } | j        j        |g|R i |}d|||k     <   |S rT   )rL   rN   r
   rD   )r   r?   rQ   r7   rL   cdf_raws         r!   rD   zClippedContinuous.cdf   sb    t##JJ ,//J$$.$Q666666 #$Jr#   c                     d|vr| j         }n|                    d          } | j        j        |g|R i |}d|||k    <   |S )NrL   r   )rL   rN   r
   sf)r   r?   rQ   r7   rL   sf_raws         r!   r]   zClippedContinuous.sf   s`    t##JJ ,//J""14t444t44"#qJr#   c                     t           r&   )NotImplementedError)r   r?   rQ   r7   s       r!   r   zClippedContinuous.ppf   s    !!r#   c                    |                      |          \  }} | j        |g|R i |}t          j        |dz   g|||k             f          }dd lm}  |j        |dz
  |                                            | j        |g|R i |} |j        dt          ||                                          dz              |j	        | | j        |g|R i |            |j
        |g|gddd           d S )	Ngư>r   皙?g?zb-bozr-)linefmt	markerfmtbasefmt)rO   r<   r   concatenatematplotlib.pyplotpyplotxlimmaxylimplotstem)	r   r?   rQ   r7   rL   massxrpltxpdfs	            r!   rm   zClippedContinuous.plot   s=   //55
Dtx
2T222T22^jo.!J,@AA'''''' 	C)))tx)D)))D))Cdhhjj))#-...XTXb04000400111*vt	= 	= 	= 	=r#   N)rE   rF   rG   rH   r"   rO   r0   r<   rD   r]   r   rm   r'   r#   r!   rJ   rJ      s         2% % %         $  (
 
 
" " "    r#   rJ   __main__r          @c                     dS )Nr'   r'   r?   s    r!   <lambda>rw     s    2 r#   c                 6    | dt          j        |           z  dS )Nrb   )locscale)r   	ones_likerv   s    r!   rw   rw     s    1s2<??/BCC r#         i  )r/   d   )binsz'poisson mixture of normal distributions        g:0yE>      r:   3         ?   )r?   ry   rz   zclipped normal distribution)r   r   g      ?rt   zclipped normal, loc = %3.2fg      ?i  )ry   r/   2   zclipped normal rvs, loc = %3.2f)*rH   numpyr   scipyr   r   rJ   rE   doplotspoissonmdistnormbdist
bd_args_fn
bd_kwds_fnpdprintr<   linspacepbprD   pcbpcr0   mrh   ri   rq   histtitleclip_lower_cnormr?   figurerm   sqrtfig	enumerateiry   add_subplotr'   r#   r!   <module>r      s   .          ` ` ` ` ` ` ` `Jw w w w w w w wx zG EM"EJEJCCJ 
	E5*j	A	AB	E"&&))FF;2;qB''((EArff[R[2b))**GB	E"&&((OOOVVVFC  =''''''S!!!!	;<<< JEKe[11ED!R  A	E%))A,,	E%))A,, ; 	


{r{2q"--3gbgajj
III	/000cjlli 0 0 011 	; 	;FAsOOAa!$$$JJ;2;r1b11s'"'!**JMMMCI3c9:::: iiCdi++
2	3c9:::::i D; ;r#   