o
    §ƒðjÚ.  ã                   @   sê  d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dlm<Z< G dd„ de0ƒZ=G dd„ de=e(ƒZ>G dd„ de.ƒZ?G d d!„ d!e?e*ƒZ@G d"d#„ d#e?e-ƒZAG d$d%„ d%e,ƒZBG d&d'„ d'e/e?ƒZCG d(d)„ d)eBe)ƒZDd*S )+é    )ÚSumÚ	summation)ÚBasic)Úcacheit)ÚLambda)ÚI)ÚEqÚNe)ÚS)ÚDummyÚsymbols)Úsympify)Ú	factorial)Úexp)Úfloor)Ú	Piecewise)ÚAnd)Úpoly)Úseries)ÚPolynomialError)Ú!reduce_rational_inequalities_wrap)	ÚNamedArgsMixinÚSinglePSpaceÚSingleDomainÚrandom_symbolsÚPSpaceÚConditionalDomainÚRandomDomainÚProductDomainÚDistribution)ÚProbability)ÚRangeÚ	FiniteSet)ÚUnion)ÚContains)Ú
filldedent)Ú_sympifyc                   @   ó   e Zd Zdd„ ZdS )ÚDiscreteDistributionc                 G   ó
   | j |Ž S ©N©Úpdf©ÚselfÚargs© r0   ú‘/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/stats/drv.pyÚ__call__    ó   
zDiscreteDistribution.__call__N)Ú__name__Ú
__module__Ú__qualname__r2   r0   r0   r0   r1   r(      ó    r(   c                   @   s¬   e Zd ZdZejZdd„ Zedd„ ƒZ	e
dd„ ƒZdd	„ Zd
d„ Ze
dd„ ƒZdd„ Zdd„ Ze
dd„ ƒZdd„ Zdd„ Ze
dd„ ƒZdd„ Zdd„ Zd$dd „Zd!d"„ Zd#S )%ÚSingleDiscreteDistributionzË Discrete distribution of a single variable.

    Serves as superclass for PoissonDistribution etc....

    Provides methods for pdf, cdf, and sampling

    See Also:
        sympy.stats.crv_types.*
    c                 G   s    t tt|ƒƒ}tj| g|¢R Ž S r*   )ÚlistÚmapr   r   Ú__new__)Úclsr/   r0   r0   r1   r;   1   s   z"SingleDiscreteDistribution.__new__c                  G   ó   d S r*   r0   )r/   r0   r0   r1   Úcheck5   s   z SingleDiscreteDistribution.checkc                 K   sf   t ddtd�}t ddtd�}| jj}|  |¡}t|||t|ƒffi |¤Ž}t|||kfdƒ}t||ƒS )zB Compute the CDF from the PDF.

        Returns a Lambda.
        ÚxT)Úintegerr<   Úz©Úrealr<   )r   T)	r   r   ÚsetÚinfr,   r   r   r   r   )r.   Úkwargsr?   rA   Ú
left_boundr,   Úcdfr0   r0   r1   Úcompute_cdf9   s   

z&SingleDiscreteDistribution.compute_cdfc                 C   r=   r*   r0   ©r.   r?   r0   r0   r1   Ú_cdfJ   ó   zSingleDiscreteDistribution._cdfc                 K   ó.   |s|   |¡}|dur|S | jdi |¤Ž|ƒS ©z Cumulative density function Nr0   )rK   rI   )r.   r?   rF   rH   r0   r0   r1   rH   M   ó
   
zSingleDiscreteDistribution.cdfc                 K   sN   t ddtd�\}}|  |¡}ttt| | ƒ| || jj| jjfƒ}t	||ƒS )zV Compute the characteristic function from the PDF.

        Returns a Lambda.
        zx, tTrB   )
r   r   r,   r   r   r   rD   rE   Úsupr   )r.   rF   r?   Útr,   Úcfr0   r0   r1   Úcompute_characteristic_functionU   s   
(
z:SingleDiscreteDistribution.compute_characteristic_functionc                 C   r=   r*   r0   ©r.   rQ   r0   r0   r1   Ú_characteristic_function`   rL   z3SingleDiscreteDistribution._characteristic_functionc                 K   rM   )z Characteristic function Nr0   )rU   rS   )r.   rQ   rF   rR   r0   r0   r1   Úcharacteristic_functionc   rO   z2SingleDiscreteDistribution.characteristic_functionc                 K   sP   t ddd�}t ddd�}|  |¡}tt|| ƒ| || jj| jjfƒ}t||ƒS )NrQ   T©rC   r?   ©r@   )r   r,   r   r   rD   rE   rP   r   )r.   rF   rQ   r?   r,   Úmgfr0   r0   r1   Ú"compute_moment_generating_functionk   s
   
$
z=SingleDiscreteDistribution.compute_moment_generating_functionc                 C   r=   r*   r0   rT   r0   r0   r1   Ú_moment_generating_functions   rL   z6SingleDiscreteDistribution._moment_generating_functionc                 K   s.   |s|   |¡}|d ur|S | jdi |¤Ž|ƒS )Nr0   )r[   rZ   )r.   rQ   rF   rY   r0   r0   r1   Úmoment_generating_functionv   s
   
z5SingleDiscreteDistribution.moment_generating_functionc                 K   s^   t ddd�}t ddd�}| jj}|  |¡}t||||ffi |¤Ž}|||kff}t|t|Ž ƒS )zG Compute the Quantile from the PDF.

        Returns a Lambda.
        r?   TrX   ÚprW   )r   rD   rE   r,   r   r   r   )r.   rF   r?   r]   rG   r,   rH   rD   r0   r0   r1   Úcompute_quantile}   s   
z+SingleDiscreteDistribution.compute_quantilec                 C   r=   r*   r0   rJ   r0   r0   r1   Ú	_quantile‹   rL   z$SingleDiscreteDistribution._quantilec                 K   rM   rN   )r_   r^   )r.   r?   rF   Úquantiler0   r0   r1   r`   Ž   rO   z#SingleDiscreteDistribution.quantileTc                 K   sô   |rezDt ||ƒ}tddd�}|  |¡}| ¡ }t t||d|d ƒ ¡ |ƒ}	d}
t|d ƒD ]}|
| || ¡|	 || ¡ t|ƒ 7 }
q-|
W S  t	yd   t
||  |¡ || jj| jjffi |¤Ž Y S w t||  |¡ || jj| jjffi |¤ŽS )z- Expectation of expression over distribution rQ   TrW   r   é   )r   r   r\   Údegreer   ÚremoveOÚrangeÚcoeff_monomialr   r   r   r,   rD   rE   rP   r   )r.   ÚexprÚvarÚevaluaterF   r]   rQ   rY   ÚdegÚtaylorÚresultÚkr0   r0   r1   Úexpectation–   s.   

*ÿ
ÿÿÿÿz&SingleDiscreteDistribution.expectationc                 G   r)   r*   r+   r-   r0   r0   r1   r2   ±   r3   z#SingleDiscreteDistribution.__call__N)T)r4   r5   r6   Ú__doc__r
   ÚIntegersrD   r;   Ústaticmethodr>   r   rI   rK   rH   rS   rU   rV   rZ   r[   r\   r^   r_   r`   rm   r2   r0   r0   r0   r1   r8   $   s0    







r8   c                   @   s   e Zd ZdZdZdS )ÚDiscreteDomainze
    A domain with discrete support with step size one.
    Represented using symbols and Range.
    TN)r4   r5   r6   rn   Úis_Discreter0   r0   r0   r1   rq   µ   s    rq   c                   @   r'   )ÚSingleDiscreteDomainc                 C   ó   t | j| jƒS r*   )r$   ÚsymbolrD   ©r.   r0   r0   r1   Ú
as_boolean½   s   zSingleDiscreteDomain.as_booleanN©r4   r5   r6   rw   r0   r0   r0   r1   rs   ¼   r7   rs   c                   @   s   e Zd ZdZedd„ ƒZdS )ÚConditionalDiscreteDomainzb
    Domain with discrete support of step size one, that is restricted by
    some condition.
    c                 C   sB   | j }t| j ƒdkrttdƒƒ‚t|ƒd }t| j|ƒ | jj	¡S )Nra   zJ
                Multivariate conditional domains are not yet implemented.r   )
r   ÚlenÚNotImplementedErrorr%   r9   r   Ú	conditionÚ	intersectÚ
fulldomainrD   )r.   Úrvr0   r0   r1   rD   Æ   s   ÿ
ÿzConditionalDiscreteDomain.setN)r4   r5   r6   rn   ÚpropertyrD   r0   r0   r0   r1   ry   Á   s    ry   c                   @   s@   e Zd ZdZdZedd„ ƒZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dS )ÚDiscretePSpaceTc                 C   s   | j | jŽ S r*   )Údensityr   rv   r0   r0   r1   r,   Õ   s   zDiscretePSpace.pdfc                    sf   t |ƒ}t‡ fdd„|D ƒƒsJ ‚t|ƒdkrttdƒƒ‚t||d ƒ}| ˆ jj¡}t	|d j
|ƒS )Nc                 3   s   � | ]	}|j ˆ jv V  qd S r*   )ru   r   ©Ú.0Úrrv   r0   r1   Ú	<genexpr>Û   s   € z'DiscretePSpace.where.<locals>.<genexpr>ra   zIMultivariate discrete
            random variables are not yet supported.r   )r   Úallrz   r{   r%   r   r}   ÚdomainrD   rs   ru   )r.   r|   ÚrvsÚconditional_domainr0   rv   r1   ÚwhereÙ   s   ÿzDiscretePSpace.wherec              	   C   s  t |tƒ}|rt|jd |jd ƒ}z(|  |¡j}|dks"|tju r&tjW S |dks0|| j	jkr4tj
W S |  |¡}W n= tyw   ddlm} |j|j }||ƒ}t |tƒs`ddlm} ||ƒ}tddd�}	t|	|ƒ}
|
 | |
jd¡¡}Y nw |d u r€t|ƒ}|s„|S tj
| S )	Nr   ra   FT)r‚   )ÚDiscreteDistributionHandmaderA   rW   )Ú
isinstancer	   r   r/   r‹   rD   r
   ÚEmptySetÚZerorˆ   ÚOneÚ	eval_probr{   Úsympy.stats.rvr‚   ÚlhsÚrhsr(   Úsympy.stats.drv_typesrŒ   r   ÚSingleDiscretePSpaceÚprobabilityÚ	__class__Úvaluer    )r.   r|   Ú
complementÚ_domainÚprobr‚   rf   ÚdensrŒ   rA   Úspacer0   r0   r1   r—   ä   s0   


÷
zDiscretePSpace.probabilityc           	         sÊ   t ˆjƒd }t|tƒr8tddd�}dd„ |jD ƒ\}}}ˆj ||| ¡}t|||| || d fƒ ¡ }|S t|t	ƒrPt
|ˆjƒ‰ t‡ fdd„|D ƒƒ}|S t|tƒrct‡fd	d„|jD ƒƒ}|S d S )
Nr   ÚnTrX   c                 s   s   � | ]}|V  qd S r*   r0   rƒ   r0   r0   r1   r†     s   € z+DiscretePSpace.eval_prob.<locals>.<genexpr>ra   c                 3   s   � | ]}ˆ |ƒV  qd S r*   r0   ©r„   r?   r+   r0   r1   r†   	  s   € c                 3   s   � | ]}ˆ   |¡V  qd S r*   )r‘   r    rv   r0   r1   r†     s   € )r9   r   r�   r!   r/   r,   Úreplacer   Údoitr"   r   Úsumr#   )	r.   r›   ÚsymrŸ   rE   rP   ÚstepÚsummandr   r0   )r,   r.   r1   r‘   ý   s*   
ÿÿÿ

þzDiscretePSpace.eval_probc                 C   sH   t t| jƒ| j|  |¡ ƒ}| dd„ | jD ƒ¡}t| j|ƒ}t	||ƒS )Nc                 S   ó   i | ]}||j “qS r0   ©ru   ©r„   r   r0   r0   r1   Ú
<dictcomp>  ó    z4DiscretePSpace.conditional_space.<locals>.<dictcomp>)
r   Útupler   r,   r—   ÚxreplaceÚvaluesry   rˆ   r�   )r.   r|   r‚   rˆ   r0   r0   r1   Úconditional_space  s   
z DiscretePSpace.conditional_spaceN)r4   r5   r6   Úis_realrr   r€   r,   r‹   r—   r‘   r¯   r0   r0   r0   r1   r�   Ñ   s    
r�   c                   @   r'   )ÚProductDiscreteDomainc                 C   s   t dd„ | jD ƒŽ S )Nc                 S   s   g | ]}|j ‘qS r0   )rw   )r„   rˆ   r0   r0   r1   Ú
<listcomp>  s    z4ProductDiscreteDomain.as_boolean.<locals>.<listcomp>)r   Údomainsrv   r0   r0   r1   rw     s   z ProductDiscreteDomain.as_booleanNrx   r0   r0   r0   r1   r±     r7   r±   c                   @   sh   e Zd ZdZdZedd„ ƒZedd„ ƒZdd
d„Zddd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zd	S )r–   z> Discrete probability space over a single univariate variable Tc                 C   s   | j jS r*   )ÚdistributionrD   rv   r0   r0   r1   rD     s   zSingleDiscretePSpace.setc                 C   rt   r*   )rs   ru   rD   rv   r0   r0   r1   rˆ   #  s   zSingleDiscretePSpace.domainr0   ÚscipyNc                 C   s   | j | jj|||d�iS )zp
        Internal sample method.

        Returns dictionary mapping RandomSymbol to realization value.
        )ÚlibraryÚseed)r™   r´   Úsample)r.   Úsizer¶   r·   r0   r0   r1   r¸   '  s   zSingleDiscretePSpace.samplec                 K   s”   |p| j f}| j |vr|S t|ƒ}| dd„ |D ƒ¡}| j j}z| jj||fd|i|¤ŽW S  tyI   t|| j || j	j
| j	jffi |¤Ž Y S w )Nc                 S   r§   r0   r¨   r©   r0   r0   r1   rª   5  r«   z<SingleDiscretePSpace.compute_expectation.<locals>.<dictcomp>rh   )r™   r&   r­   ru   r´   rm   r{   r   r,   rD   rE   rP   )r.   rf   r‰   rh   rF   r?   r0   r0   r1   Úcompute_expectation/  s   
ÿ
ÿÿz(SingleDiscretePSpace.compute_expectationc                 K   ó6   || j krtddd�}t|| jj|fi |¤ŽƒS tƒ ‚)Nr?   TrW   )r™   r   r   r´   rH   r{   )r.   rf   rF   r?   r0   r0   r1   rI   ?  ó   
z SingleDiscretePSpace.compute_cdfc                 K   s   || j kr| jS tƒ ‚r*   )r™   r´   r{   )r.   rf   rF   r0   r0   r1   Úcompute_densityF  s   
z$SingleDiscretePSpace.compute_densityc                 K   r»   ©NrQ   TrW   )r™   r   r   r´   rV   r{   ©r.   rf   rF   rQ   r0   r0   r1   rS   K  r¼   z4SingleDiscretePSpace.compute_characteristic_functionc                 K   r»   r¾   )r™   r   r   r´   r\   r{   r¿   r0   r0   r1   rZ   R  r¼   z7SingleDiscretePSpace.compute_moment_generating_functionc                 K   r»   )Nr]   TrW   )r™   r   r   r´   r`   r{   )r.   rf   rF   r]   r0   r0   r1   r^   Y  r¼   z%SingleDiscretePSpace.compute_quantile)r0   rµ   N)NT)r4   r5   r6   rn   r°   r€   rD   rˆ   r¸   rº   rI   r½   rS   rZ   r^   r0   r0   r0   r1   r–     s    



r–   N)EÚsympy.concrete.summationsr   r   Úsympy.core.basicr   Úsympy.core.cacher   Úsympy.core.functionr   Úsympy.core.numbersr   Úsympy.core.relationalr   r	   Úsympy.core.singletonr
   Úsympy.core.symbolr   r   Úsympy.core.sympifyr   Ú(sympy.functions.combinatorial.factorialsr   Ú&sympy.functions.elementary.exponentialr   Ú#sympy.functions.elementary.integersr   Ú$sympy.functions.elementary.piecewiser   Úsympy.logic.boolalgr   Úsympy.polys.polytoolsr   Úsympy.series.seriesr   Úsympy.polys.polyerrorsr   Úsympy.stats.crvr   r’   r   r   r   r   r   r   r   r   r   Ú sympy.stats.symbolic_probabilityr    Úsympy.sets.fancysetsr!   r"   Úsympy.sets.setsr#   Úsympy.sets.containsr$   Úsympy.utilitiesr%   r&   r(   r8   rq   rs   ry   r�   r±   r–   r0   r0   r0   r1   Ú<module>   sD    , F