o
    §ƒðj  ã                   @   sÚ   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 d dlmZmZ d dlmZmZ d d	lmZmZ d d
lmZ d dlmZ d dlm Z  G dd„ deƒZ!G dd„ deeƒZ"dS )é    )ÚSum)ÚBasic)ÚLambda)ÚDummy)ÚIntegral)ÚNamedArgsMixinÚrandom_symbolsÚ_symbol_converterÚPSpaceÚRandomSymbolÚ	is_randomÚDistribution)ÚContinuousDistributionÚSingleContinuousPSpace)ÚDiscreteDistributionÚSingleDiscretePSpace)ÚSingleFiniteDistributionÚSingleFinitePSpace)ÚContinuousDistributionHandmade)ÚDiscreteDistributionHandmade)ÚFiniteDistributionHandmadec                   @   sØ   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd(dd„Zdd„ Zddœdd„Zddœdd „Zd)d"d#„Zddœd$d%„Zddœd&d'„Zd!S )*ÚCompoundPSpacez²
    A temporary Probability Space for the Compound Distribution. After
    Marginalization, this returns the corresponding Probability Space of the
    parent distribution.
    c                 C   sh   t |ƒ}t|tƒrt||ƒS t|tƒrt||ƒS t|tƒr"t||ƒS t|tƒs-t	d| ƒ‚t
 | ||¡S )Nz2%s should be an isinstance of CompoundDistribution)r	   Ú
isinstancer   r   r   r   r   r   ÚCompoundDistributionÚ
ValueErrorr   Ú__new__)ÚclsÚsÚdistribution© r   ú™/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/stats/compound_rv.pyr      s   






ÿzCompoundPSpace.__new__c                 C   s   t | j| ƒS ©N)r   Úsymbol©Úselfr   r   r    Úvalue$   ó   zCompoundPSpace.valuec                 C   ó
   | j d S ©Nr   ©Úargsr#   r   r   r    r"   (   ó   
zCompoundPSpace.symbolc                 C   ó   | j jS r!   )r   Úis_Continuousr#   r   r   r    r-   ,   ó   zCompoundPSpace.is_Continuousc                 C   r,   r!   )r   Ú	is_Finiter#   r   r   r    r/   0   r.   zCompoundPSpace.is_Finitec                 C   r,   r!   )r   Úis_Discreter#   r   r   r    r0   4   r.   zCompoundPSpace.is_Discretec                 C   r'   )Né   r)   r#   r   r   r    r   8   r+   zCompoundPSpace.distributionc                 C   s   | j  | j¡S r!   )r   Úpdfr"   r#   r   r   r    r2   <   s   zCompoundPSpace.pdfc                 C   r,   r!   )r   Úsetr#   r   r   r    r3   @   r.   zCompoundPSpace.setc                 C   s
   |   ¡ jS r!   )Ú_get_newpspaceÚdomainr#   r   r   r    r5   D   r+   zCompoundPSpace.domainFc                 C   sX   t dƒ}| jjd }t|| j ||¡ƒ}|  | j||¡}|d ur"|S dt|ƒ }t|ƒ‚)NÚxr   ú3Compound Distribution for %s is not implemented yet)	r   r   r*   r   r2   Ú_transform_pspacer"   ÚstrÚNotImplementedError)r$   Úevaluater6   Úparent_distÚfuncÚ
new_pspaceÚmessager   r   r    r4   H   s   zCompoundPSpace._get_newpspacec                    sv   t |ˆ |ƒƒ‰ |j}t|tƒrt|tˆ |ƒƒS t|tƒr$t|tˆ |ƒƒS t|t	ƒr9‡ fdd„|D ƒ}t
|t|ƒƒS dS )z�
        This function returns the new pspace of the distribution using handmade
        Distributions and their corresponding pspace.
        c                    s   i | ]}|ˆ |ƒ“qS r   r   )Ú.0Úk©r2   r   r    Ú
<dictcomp>^   s    z4CompoundPSpace._transform_pspace.<locals>.<dictcomp>N)r   r3   r   r   r   r   r   r   r   r   r   r   )r$   ÚsymÚdistr2   Ú_setÚdensr   rB   r    r8   R   s   


þz CompoundPSpace._transform_pspaceT)Úcompound_evaluatec                K   ó.   |   |¡}| | j|ji¡}|j|fi |¤ŽS r!   )r4   Úsubsr%   Úcompute_density©r$   ÚexprrH   Úkwargsr>   r   r   r    rK   a   ó   
zCompoundPSpace.compute_densityc                K   rI   r!   )r4   rJ   r%   Úcompute_cdfrL   r   r   r    rP   f   rO   zCompoundPSpace.compute_cdfNc                 K   sf   |   |¡}| | j|ji¡}|r| | j|ji¡}t|tƒr(|j||fi |¤ŽS |j|||fi |¤ŽS r!   )r4   rJ   r%   r   r   Úcompute_expectation)r$   rM   Úrvsr;   rN   r>   r   r   r    rQ   k   s   

z"CompoundPSpace.compute_expectationc                K   ó&   |   |¡}| | j|ji¡}| |¡S r!   )r4   rJ   r%   Úprobability©r$   Ú	conditionrH   rN   r>   r   r   r    rT   t   ó   

zCompoundPSpace.probabilityc                K   rS   r!   )r4   rJ   r%   Úconditional_spacerU   r   r   r    rX   y   rW   z CompoundPSpace.conditional_space©F)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr%   r"   r-   r/   r0   r   r2   r3   r5   r4   r8   rK   rP   rQ   rT   rX   r   r   r   r    r      s8    











	r   c                   @   sf   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	ddd„Z
dd„ Zedd„ ƒZdS )r   aÈ  
    Class for Compound Distributions.

    Parameters
    ==========

    dist : Distribution
        Distribution must contain a random parameter

    Examples
    ========

    >>> from sympy.stats.compound_rv import CompoundDistribution
    >>> from sympy.stats.crv_types import NormalDistribution
    >>> from sympy.stats import Normal
    >>> from sympy.abc import x
    >>> X = Normal('X', 2, 4)
    >>> N = NormalDistribution(X, 4)
    >>> C = CompoundDistribution(N)
    >>> C.set
    Interval(-oo, oo)
    >>> C.pdf(x, evaluate=True).simplify()
    exp(-x**2/64 + x/16 - 1/16)/(8*sqrt(pi))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Compound_probability_distribution

    c                 C   s>   t |tttfƒsdt|ƒ }t|ƒ‚|  |¡s|S t | |¡S )Nr7   )	r   r   r   r   r9   r:   Ú_compound_checkr   r   )r   rE   r?   r   r   r    r   Ÿ   s   ÿ
zCompoundDistribution.__new__c                 C   s   | j d jS r(   )r*   r3   r#   r   r   r    r3   ¨   r&   zCompoundDistribution.setc                 C   ó   t | jd tƒS r(   )r   r*   r   r#   r   r   r    r-   ¬   ó   z"CompoundDistribution.is_Continuousc                 C   r`   r(   )r   r*   r   r#   r   r   r    r/   °   ra   zCompoundDistribution.is_Finitec                 C   r`   r(   )r   r*   r   r#   r   r   r    r0   ´   ra   z CompoundDistribution.is_DiscreteFc                 C   sv   | j d }dd„ |j D ƒ}t|tƒrtdddd�}| |¡}n	tdƒ}| |¡}|D ]	}|  |||¡}q*t||ƒ|ƒS )Nr   c                 S   s   g | ]}t |ƒr|‘qS r   )r   )r@   Úrvr   r   r    Ú
<listcomp>º   s    z,CompoundDistribution.pdf.<locals>.<listcomp>ÚyTF)ÚintegerÚnegative)r*   r   r   r   Úpmfr2   Ú_marginaliser   )r$   r6   r;   rE   Úrandomsrd   rM   rb   r   r   r    r2   ¸   s   


zCompoundDistribution.pdfc                 C   sˆ   t |jjtƒr|jj |¡}n|jj |¡}|jjj}|jjs#|jj	r0t
|| ||j|jfƒ}nt|| ||j|jfƒ}|rB| ¡ S |S r!   )r   Úpspacer   r   rg   r2   r5   r3   r0   r/   r   Ú_infÚ_supr   Údoit)r$   rM   rb   r;   Úrv_densÚrv_domr   r   r    rh   Å   s   
ÿÿz!CompoundDistribution._marginalisec                 C   s2   g }|j D ]	}| t|ƒ¡ qt|ƒdkrdS dS )zN
        Checks if the given distribution contains random parameters.
        r   FT)r*   Úextendr   Úlen)r$   rE   ri   Úargr   r   r    r_   Õ   s   
z$CompoundDistribution._compound_checkNrY   )rZ   r[   r\   r]   r   r^   r3   r-   r/   r0   r2   rh   Úclassmethodr_   r   r   r   r    r      s    	




r   N)#Úsympy.concrete.summationsr   Úsympy.core.basicr   Úsympy.core.functionr   Úsympy.core.symbolr   Úsympy.integrals.integralsr   Úsympy.stats.rvr   r   r	   r
   r   r   r   Úsympy.stats.crvr   r   Úsympy.stats.drvr   r   Úsympy.stats.frvr   r   Úsympy.stats.crv_typesr   Úsympy.stats.drv_typesr   Úsympy.stats.frv_typesr   r   r   r   r   r   r    Ú<module>   s    $o