ó
”¼\c           @  s2  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z 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  m! Z! d d	 l" m# Z# d
 e f d     YZ$ d e$ f d     YZ% d e$ f d     YZ& d e f d     YZ' d e' f d     YZ( d e f d     YZ) d e' f d     YZ* d e* f d     YZ+ d e$ f d     YZ, d   Z- d   Z. d   Z/ d   Z0 d9 d   Z2 d9 d9 e3 d!  Z4 d9 d9 e3 d"  Z5 d# e f d$     YZ6 d9 e3 d9 d%  Z7 d9 e3 d&  Z8 d9 e3 d'  Z9 d9 e3 d(  Z: d9 d)  Z; d9 d*  Z< d9 e j= d+  Z> d9 e j= d,  Z? d9 e j= d-  Z@ d9 d. e3 d/  ZA d9 d. e3 d0  ZB d9 d. d1  ZC d2   ZD d3   ZE d4   ZF d9 d5  ZG d6 eH f d7     YZI d8   ZJ d9 S(:   s	  
Main Random Variables Module

Defines abstract random variable type.
Contains interfaces for probability space object (PSpace) as well as standard
operators, P, E, sample, density, where

See Also
========

sympy.stats.crv
sympy.stats.frv
sympy.stats.rv_interface
i’’’’(   t   print_functiont   division(   t   Basict   St   Exprt   Symbolt   Tuplet   Andt   Addt   Eqt   lambdifyt   Equalityt   Lambdat   sympifyt   Dummyt   Net   KroneckerDeltat
   DiracDeltat   Mul(   t   x(   t   string_types(   t
   Relational(   t   Boolean(   t	   FiniteSett
   ProductSett   Intersection(   t   solvesett   RandomDomainc           B  s_   e  Z d  Z e Z e Z e Z e Z d   Z e	 d    Z
 e	 d    Z d   Z d   Z RS(   s¬   
    Represents a set of variables and the values which they can take

    See Also
    ========

    sympy.stats.crv.ContinuousDomain
    sympy.stats.frv.FiniteDomain
    c         G  s   t  |   } t j |  | |  S(   N(   R   R   t   __new__(   t   clst   symbolst   args(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   -   s    c         C  s   |  j  d S(   Ni    (   R   (   t   self(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   1   s    c         C  s   |  j  d S(   Ni   (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   set5   s    c         C  s   t     d  S(   N(   t   NotImplementedError(   R    t   other(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   __contains__9   s    c         C  s   t     d  S(   N(   R"   (   R    t   expr(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   compute_expectation<   s    (   t   __name__t
   __module__t   __doc__t   Falset   is_ProductDomaint	   is_Finitet   is_Continuoust   is_DiscreteR   t   propertyR   R!   R$   R&   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR      s   			t   SingleDomainc           B  s>   e  Z d  Z d   Z e d    Z e d    Z d   Z RS(   s   
    A single variable and its domain

    See Also
    ========

    sympy.stats.crv.SingleContinuousDomain
    sympy.stats.frv.SingleFiniteDomain
    c         C  s"   | j  s t  t j |  | |  S(   N(   t	   is_Symbolt   AssertionErrorR   R   (   R   t   symbolR!   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   J   s    c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR3   N   s    c         C  s   t  |  j  S(   N(   R   R3   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   R   s    c         C  sH   t  |  d k r t St |  d \ } } |  j | k oG | |  j k S(   Ni   i    (   t   lenR*   t   tupleR3   R!   (   R    R#   t   symt   val(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR$   V   s    (   R'   R(   R)   R   R/   R3   R   R$   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR0   @   s
   		t   ConditionalDomainc           B  s\   e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z d   Z	 RS(   s«   
    A RandomDomain with an attached condition

    See Also
    ========

    sympy.stats.crv.ConditionalContinuousDomain
    sympy.stats.frv.ConditionalFiniteDomain
    c         C  s8   | j  t d   t |  D   } t j |  | |  S(   Nc         s  s   |  ] } | | j  f Vq d  S(   N(   R3   (   t   .0t   rs(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>h   s   (   t   xreplacet   dictt   random_symbolsR   R   (   R   t
   fulldomaint	   condition(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   g   s    c         C  s
   |  j  j S(   N(   R>   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   l   s    c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR>   p   s    c         C  s   |  j  d S(   Ni   (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR?   t   s    c         C  s   t  d   d  S(   Ns)   Set of Conditional Domain not Implemented(   R"   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR!   x   s    c         C  s   t  |  j j   |  j  S(   N(   R   R>   t
   as_booleanR?   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR@   |   s    (
   R'   R(   R)   R   R/   R   R>   R?   R!   R@   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR8   ]   s   		t   PSpacec           B  s   e  Z d  Z d
 Z d
 Z d
 Z d
 Z e d    Z	 e d    Z
 e d    Z e d    Z d   Z d   Z d   Z d   Z d	   Z RS(   s[  
    A Probability Space

    Probability Spaces encode processes that equal different values
    probabilistically. These underly Random Symbols which occur in SymPy
    expressions and contain the mechanics to evaluate statistical statements.

    See Also
    ========

    sympy.stats.crv.ContinuousPSpace
    sympy.stats.frv.FinitePSpace
    c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   domain   s    c         C  s   |  j  d S(   Ni   (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   density   s    c           s   t    f d     j D  S(   Nc         3  s   |  ] } t  |    Vq d  S(   N(   t   RandomSymbol(   R9   R6   (   R    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>   s    (   t	   frozensetR   (   R    (    (   R    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   values   s    c         C  s
   |  j  j S(   N(   RB   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR       s    c         C  s   t     d  S(   N(   R"   (   R    R?   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   where¤   s    c         C  s   t     d  S(   N(   R"   (   R    R%   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   compute_density§   s    c         C  s   t     d  S(   N(   R"   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   sampleŖ   s    c         C  s   t     d  S(   N(   R"   (   R    R?   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   probability­   s    c         C  s   t     d  S(   N(   R"   (   R    R%   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR&   °   s    N(   R'   R(   R)   t   NoneR,   R-   R.   t   is_realR/   RB   RC   RF   R   RG   RH   RI   RJ   R&   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRA      s   				t   SinglePSpacec           B  sS   e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z RS(   sx   
    Represents the probabilities of a set of random events that can be
    attributed to a single variable/symbol.
    c         C  sO   t  | t  r t |  } n  t  | t  s< t d   n  t j |  | |  S(   Ns#   s should have been string or Symbol(   t
   isinstanceR   R   t	   TypeErrorR   R   (   R   t   st   distribution(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   ¹   s
    c         C  s   t  |  j |   S(   N(   RD   R3   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   valueĄ   s    c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR3   Ä   s    c         C  s   |  j  d S(   Ni   (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRQ   Č   s    c         C  s   |  j  j |  j  S(   N(   RQ   t   pdfR3   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRS   Ģ   s    (	   R'   R(   R)   R   R/   RR   R3   RQ   RS   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRM   “   s   	RD   c           B  s”   e  Z d  Z d d  Z e Z e Z e Z e Z	 e
 d    Z e
 d    Z e
 d    Z d   Z d   Z d   Z e
 d    Z d	   Z e
 d
    Z RS(   s]  
    Random Symbols represent ProbabilitySpaces in SymPy Expressions
    In principle they can take on any value that their symbol can take on
    within the associated PSpace with probability determined by the PSpace
    Density.

    Random Symbols contain pspace and symbol properties.
    The pspace property points to the represented Probability Space
    The symbol is a standard SymPy Symbol that is used in that probability space
    for example in defining a density.

    You can form normal SymPy expressions using RandomSymbols and operate on
    those expressions with the Functions

    E - Expectation of a random expression
    P - Probability of a condition
    density - Probability Density of an expression
    given - A new random expression (with new random symbols) given a condition

    An object of the RandomSymbol type should almost never be created by the
    user. They tend to be created instead by the PSpace class's value method.
    Traditionally a user doesn't even do this but instead calls one of the
    convenience functions Normal, Exponential, Coin, Die, FiniteRV, etc....
    c         C  s   d d l  m } | d  k r( t   } n  t | t  sF t d   n  t | t  sd t d   n  |  | k r t | t  r t }  n  t	 j
 |  | |  S(   Ni’’’’(   t   JointRandomSymbols   symbol should be of type Symbols(   pspace variable should be of type PSpace(   t   sympy.stats.joint_rvRT   RK   RA   RN   R   RO   RM   RD   R   R   (   R   R3   t   pspaceRT   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   ė   s    	c         C  s   |  j  d S(   Ni   (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   <lambda>ž   t    c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRW   ’   RX   c         C  s
   |  j  j S(   N(   R3   t   name(   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRW      RX   c         C  s
   |  j  j S(   N(   R3   t   is_positive(   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   _eval_is_positive  s    c         C  s
   |  j  j S(   N(   R3   t
   is_integer(   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   _eval_is_integer  s    c         C  s   |  j  j p |  j j S(   N(   R3   RL   RV   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   _eval_is_real  s    c         C  s
   |  j  j S(   N(   R3   t   is_commutative(   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR_     s    c         C  s   |  j  |  j f S(   N(   RV   R3   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   _hashable_content  s    c         C  s   |  h S(   N(    (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   free_symbols  s    N(   R'   R(   R)   RK   R   t   Truet	   is_finitet	   is_symbolt   is_Atomt	   _diff_wrtR/   RV   R3   RY   R[   R]   R^   R_   R`   Ra   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRD   Ń   s   				t   ProductPSpacec           B  s   e  Z d  Z RS(   sĶ   
    Abstract class for representing probability spaces with multiple random
    variables.

    See Also
    ========

    sympy.stats.rv.IndependentProductPSpace
    sympy.stats.joint_rv.JointPSpace
    (   R'   R(   R)   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRg     s   
t   IndependentProductPSpacec           B  sæ   e  Z d  Z d   Z e d    Z e d    Z e d    Z e d    Z e d    Z	 d e d  Z e d    Z e d	    Z d
   Z d   Z d   Z d   Z e d  Z RS(   s   
    A probability space resulting from the merger of two independent probability
    spaces.

    Often created using the function, pspace
    c   	        sų   i  } x, | D]$ } x | j  D] } | | | <q Wq Wt g  | j   D] } | j ^ qE   } d d l m  m   t |  t    f d   | D  k  r­ t	 d   n  t
 d   | D  rÜ d d l m } | }  n  t j |  t |    } | S(   Ni’’’’(   t   MarginalDistributiont   CompoundDistributionc         3  s6   |  ], } t  | j    f  s t | j  Vq d  S(   N(   RN   RQ   R4   R   (   R9   t   space(   Rj   Ri   (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>6  s    	s   Overlapping Random Variablesc         s  s   |  ] } | j  Vq d  S(   N(   R,   (   R9   Rk   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>;  s    (   t   ProductFinitePSpace(   RF   R   t   keysR3   RU   Ri   Rj   R4   t   sumt
   ValueErrort   allt   sympy.stats.frvRl   R   R   (	   R   t   spacest   rs_space_dictRk   RR   R7   R   Rl   t   obj(    (   Rj   Ri   s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   ,  s    (+	c         C  sE   t  g  |  j D] } | j ^ q   } | j t d   |  j D   S(   Nc         s  s   |  ] } | | j  f Vq d  S(   N(   R3   (   R9   t   rv(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>F  s    (   R   Rr   RS   t   subsR<   RF   (   R    Rk   t   p(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRS   C  s    %c         C  s<   i  } x/ |  j  D]$ } x | j D] } | | | <q  Wq W| S(   N(   Rr   RF   (   R    t   dRk   RR   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRs   H  s
    c         C  s)   t  g  |  j j   D] } | j ^ q   S(   N(   R   Rs   Rm   R3   (   R    R7   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   P  s    c         C  s   t  |  j   S(   N(   R   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRr   T  s    c         C  s   t  d   |  j D  S(   Nc         s  s   |  ] } | j  Vq d  S(   N(   RF   (   R9   Rk   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>Z  s    (   t   sumsetsRr   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRF   X  s    c         K  sw   | p |  j  } t |  } x3 |  j D]( } | j | | | j  @d t | } q% W| rs t | d  rs | j |   S| S(   Nt   evaluatet   doit(   RF   RE   Rr   R&   R*   t   hasattrR{   (   R    R%   t   rvsRz   t   kwargsRk   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR&   \  s    &c         C  s#   t  g  |  j D] } | j ^ q   S(   N(   t   ProductDomainRr   RB   (   R    Rk   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRB   e  s    c         C  s   t  d   d  S(   Ns'   Density not available for ProductSpaces(   R"   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRC   i  s    c         C  s   d   |  j  D S(   Nc         S  s8   i  |  ]. } | j    j   D] \ } } | |  q q S(    (   RI   t   items(   R9   Rk   t   kt   v(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys
   <dictcomp>n  s   	 (   Rr   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRI   m  s    c         K  sĪ  t  } t | t  r; t | j d | j d  } t } n  | j | j } t |  } t	 d d t d t } |  j
 |  } t g  | D] } t |  j ^ q  rjd d l m }	 m }
 | |  j k r.t t |  j  t | g   } t d   | D  } |  j j |  j | |  } t | j |  S|	 |  } |
 | |  } | j | j | j d   } nO d d	 l m } m } | |  } | | |  } | j | j | j d   } | sĆ| St  j! | S(
   Ni    i   t   zt   realt   Finitei’’’’(   t   ContinuousDistributionHandmadet   SingleContinuousPSpacec         s  s   |  ] } | j  Vq d  S(   N(   R3   (   R9   R:   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>  s    (   t   DiscreteDistributionHandmadet   SingleDiscretePSpace("   R*   RN   R   R	   R   Rb   t   lhst   rhsR=   R   RH   t   anyRV   R-   t   sympy.stats.crvR   R   RF   R5   R!   RE   RB   t	   integrateRS   R   R3   RJ   t	   __class__RR   t   sympy.stats.drvR   R   R   t   One(   R    R?   R~   t   cond_invR%   R}   R   t   densRu   R   R   t   randomsymbolsR   RS   Rk   t   resultR   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRJ   q  s.    	("!c         K  s   t  d d t d t } t |  } t d   | D  rY |  j t | |  |  } n |  j t | |  |  } t | |  S(   NR   R   t   finitec         s  s   |  ] } t  |  j Vq d  S(   N(   RV   R-   (   R9   Ru   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>  s    (   R   Rb   R=   R   R&   R   R   R   (   R    R%   R~   R   R}   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRH     s    	c         K  s   t  d   d  S(   Ns0   CDF not well defined on multivariate expressions(   Ro   (   R    R%   R~   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   compute_cdf  s    c         K  s  t  |  } | j t d   |  j D   } t g  | D] } t |  j ^ q8  r d d l m } m	 } | } | |  j
 |  }	 n” t g  | D] } t |  j ^ q  rą d d l m }
 m } | } |
 |  j
 |  }	 nH t g  | D] } t |  j ^ qź  r(d d l m } | j |  |  S| rd   |  j D } |	 j |  j |  } |  j | j |  } t |	 j |  } n  | |	 |  S(   Nc         s  s   |  ] } | | j  f Vq d  S(   N(   R3   (   R9   Ru   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>  s    i’’’’(   t   ConditionalContinuousDomaint   ContinuousPSpace(   t   ConditionalDiscreteDomaint   DiscretePSpace(   t   FinitePSpacec         S  s%   i  |  ] } t  t |   |  q S(    (   R   t   str(   R9   Ru   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys
   <dictcomp>­  s   	 (   R=   R;   R<   RF   R   RV   R-   R   R   R   RB   R.   R   R   R   Rp   R,   Rq   R   t   conditional_spaceR   R&   RS   R   (   R    R?   t	   normalizeR~   R}   Ru   R   R   Rk   RB   R   R   R   t   replacementt   normRS   RC   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR     s&    "(((N(   R'   R(   R)   R   R/   RS   Rs   R   Rr   RF   RK   R*   R&   RB   RC   RI   RJ   RH   R   Rb   R   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRh   $  s   						R   c           B  sk   e  Z d  Z e Z d   Z e d    Z e d    Z e d    Z	 e d    Z
 d   Z d   Z RS(   s¶   
    A domain resulting from the merger of two independent domains

    See Also
    ========
    sympy.stats.crv.ProductContinuousDomain
    sympy.stats.frv.ProductFiniteDomain
    c         G  sé   g  } x7 | D]/ } | j  s, | j |  q | j | j  q Wt |   } t d   | D  r{ d d l m } | }  n  t d   | D  rŖ d d l m	 } | }  n  t d   | D  rŁ d d l
 m } | }  n  t j |  |  S(   Nc         s  s   |  ] } | j  Vq d  S(   N(   R,   (   R9   RB   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>É  s    i’’’’(   t   ProductFiniteDomainc         s  s   |  ] } | j  Vq d  S(   N(   R-   (   R9   RB   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>Ģ  s    (   t   ProductContinuousDomainc         s  s   |  ] } | j  Vq d  S(   N(   R.   (   R9   RB   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>Ļ  s    (   t   ProductDiscreteDomain(   R+   t   appendt   extendt   domainsR   Rp   Rq   R¢   R   R£   R   R¤   R   R   (   R   R§   t   domains2RB   R¢   R£   R¤   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   æ  s     				c         C  s   t  d   |  j D  S(   Nc         s  s+   |  ]! } | j  D] } | | f Vq q d  S(   N(   R   (   R9   RB   R3   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>×  s    (   R<   R§   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   sym_domain_dictÕ  s    c         C  s0   t  g  |  j D] } | j D] } | ^ q q   S(   N(   R   R§   R   (   R    RB   R6   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   Ś  s    c         C  s   |  j  S(   N(   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR§   ß  s    c         C  s   t  d   |  j D  S(   Nc         s  s   |  ] } | j  Vq d  S(   N(   R!   (   R9   RB   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>å  s    (   R   R§   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR!   ć  s    c         C  sl   xe |  j  D]Z } t g  | D]1 } t | j j | d   t j k r | ^ q  } | | k r
 t Sq
 Wt S(   Ni    (	   R§   RE   R   R   t   containsR   t   trueR*   Rb   (   R    R#   RB   t   itemt   elem(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR$   ē  s    c         C  s&   t  g  |  j D] } | j   ^ q   S(   N(   R   R§   R@   (   R    RB   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR@   ō  s    (   R'   R(   R)   Rb   R+   R   R/   R©   R   R§   R!   R$   R@   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR   “  s   		c         C  s6   t  |  d d  } | d k	 r. t | t   Sg  Sd S(   s>   
    Returns all RandomSymbols within a SymPy Expression.
    t   atomsN(   t   getattrRK   t   listRD   (   R%   R®   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR=   ų  s    c           s   t  |   }  t |  t  r1 |  j d k	 r1 |  j St |       sV t d |    n  t   f d     D  r}   d j St g    D] } | j ^ q   S(   s3  
    Returns the underlying Probability Space of a random expression.

    For internal use.

    Examples
    ========

    >>> from sympy.stats import pspace, Normal
    >>> from sympy.stats.rv import IndependentProductPSpace
    >>> X = Normal('X', 0, 1)
    >>> pspace(2*X + 1) == X.pspace
    True
    s6   Expression containing Random Variable expected, not %sc         3  s%   |  ] } | j    d  j  k Vq d S(   i    N(   RV   (   R9   Ru   (   R}   (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys	   <genexpr>  s    i    N(	   R   RN   RD   RV   RK   R=   Ro   Rp   Rh   (   R%   Ru   (    (   R}   s-   lib/python2.7/site-packages/sympy/stats/rv.pyRV     s    c         C  s   t    j |    S(   s   
    Union of sets
    (   RE   t   union(   t   sets(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRy     s    c         C  sN   i  } xA |  D]9 } g  | D] } | j  | j  k r | ^ q d | | <q W| S(   s}  
    Build a dictionary to swap RandomSymbols based on their underlying symbol.

    i.e.
    if    ``X = ('x', pspace1)``
    and   ``Y = ('x', pspace2)``
    then ``X`` and ``Y`` match and the key, value pair
    ``{X:Y}`` will appear in the result

    Inputs: collections a and b of random variables which share common symbols
    Output: dict mapping RVs in a to RVs in b
    i    (   R3   (   t   at   bRx   t   rsat   rsb(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   rs_swap&  s    7c         K  s”  t  |  s t |  |  r  |  St | t  rD t | | j  } n  t  |  } t | t  rRt |  d k rRt t |   j	 t
  rRt |  d } t | |  } t | t  rą t j | j k rą t | j d  } n  d } xU | D]M } |  j | |  } | t k rt S| t k rķ | |  j | |  7} qķ qķ W| d k rNt S| St t |  |   }	 |	 j | |  }
 t |	 j |
 j  } |  j |  }  |  S(   s   Conditional Random Expression
    From a random expression and a condition on that expression creates a new
    probability space from the condition and returns the same expression on that
    conditional probability space.

    Examples
    ========

    >>> from sympy.stats import given, density, Die
    >>> X = Die('X', 6)
    >>> Y = given(X, X > 3)
    >>> density(Y).dict
    {4: 1/3, 5: 1/3, 6: 1/3}

    Following convention, if the condition is a random symbol then that symbol
    is considered fixed.

    >>> from sympy.stats import Normal
    >>> from sympy import pprint
    >>> from sympy.abc import z

    >>> X = Normal('X', 0, 1)
    >>> Y = Normal('Y', 0, 1)
    >>> pprint(density(X + Y, Y)(z), use_unicode=False)
                    2
           -(-Y + z)
           -----------
      ___       2
    \/ 2 *e
    ------------------
             ____
         2*\/ pi
    i   i    (   R=   t   pspace_independentRN   RD   R	   R3   R   R4   RV   RB   R8   R5   R   R   R   t   RealsR   R°   Rv   Rb   R*   R   R   R·   RF   R;   (   R%   R?   R~   t   condsymbolsRu   t   resultst   sumst   rest   tempt	   fullspaceRk   t   swapdict(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   given9  s4    #!!c         K  sĖ   t  |   s |  S| r) t |  | d | S| d k	 rN t t |  |  d | S|  j r t g  |  j D] } t | d | ^ qd   St |   j	 |  d | | } | rĆ t
 | d  rĆ | j |   S| Sd S(   sL  
    Returns the expected value of a random expression

    Parameters
    ==========

    expr : Expr containing RandomSymbols
        The expression of which you want to compute the expectation value
    given : Expr containing RandomSymbols
        A conditional expression. E(X, X>0) is expectation of X given X > 0
    numsamples : int
        Enables sampling and approximates the expectation with this many samples
    evalf : Bool (defaults to True)
        If sampling return a number rather than a complex expression
    evaluate : Bool (defaults to True)
        In case of continuous systems return unevaluated integral

    Examples
    ========

    >>> from sympy.stats import E, Die
    >>> X = Die('X', 6)
    >>> E(X)
    7/2
    >>> E(2*X + 1)
    8

    >>> E(X, X > 3) # Expectation of X given that it is above 3
    5
    t
   numsamplesRz   R{   N(   R=   t
   sampling_ERK   t   expectationRĮ   t   is_AddR   R   RV   R&   R|   R{   (   R%   R?   RĀ   Rz   R~   t   argR   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRÄ     s     	&c         K  s4  t  |   }  t  |  } | d k	 rM t | t t f  rM t d |   n  | t k r` t j St |  t t f  s t d |    n  |  t j	 k r t j
 S|  t j k r“ t j S| rŠ t |  | d | | S| d k	 rõ t t |  | |  |  St |   j |  |  } | r,t | d  r,| j   S| Sd S(   sz  
    Probability that a condition is true, optionally given a second condition

    Parameters
    ==========

    condition : Combination of Relationals containing RandomSymbols
        The condition of which you want to compute the probability
    given_condition : Combination of Relationals containing RandomSymbols
        A conditional expression. P(X > 1, X > 0) is expectation of X > 1
        given X > 0
    numsamples : int
        Enables sampling and approximates the probability with this many samples
    evaluate : Bool (defaults to True)
        In case of continuous systems return unevaluated integral

    Examples
    ========

    >>> from sympy.stats import P, Die
    >>> from sympy import Eq
    >>> X, Y = Die('X', 6), Die('Y', 6)
    >>> P(X > 3)
    1/2
    >>> P(Eq(X, 5), X > 2) # Probability that X == 5 given that X > 2
    1/4
    >>> P(X > Y)
    5/12
    s4   %s is not a relational or combination of relationalsRĀ   R{   N(   R   RK   RN   R   R   Ro   R*   R   t   ZeroR«   R   t   falset
   sampling_PRJ   RĮ   RV   R|   R{   (   R?   t   given_conditionRĀ   Rz   R~   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRJ   ¹  s0     
t   Densityc           B  s2   e  Z e d     Z e d    Z e d  Z RS(   c         C  s   |  j  d S(   Ni    (   R   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRW   ś  RX   c         C  s(   t  |  j  d k r  |  j d Sd  Sd  S(   Ni   (   R4   R   RK   (   R    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR?   ü  s    c         K  s  d d l  m } |  j |  j } } | d  k	 rD t | | |  } n  t | t  ro t | j |  ro | j j	 St
 |  s t t t t |   St | t  rŅ t | j d  rŅ t t |  t  rŅ | j j	 St |  j | |  } | r	t | d  r	| j   S| Sd  S(   Ni’’’’(   t   JointPSpaceRQ   R{   (   RU   RĢ   R%   R?   RK   RĮ   RN   RD   RV   RQ   R=   R   R   R   R|   RM   RH   R{   (   R    Rz   R~   RĢ   R%   R?   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyR{     s"    


(   R'   R(   R/   R%   R?   Rb   R{   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRĖ   ł  s   c         K  s8   | r t  |  | d | | St |  |  j d | |  S(   s'  
    Probability density of a random expression, optionally given a second
    condition.

    This density will take on different forms for different types of
    probability spaces. Discrete variables produce Dicts. Continuous
    variables produce Lambdas.

    Parameters
    ==========

    expr : Expr containing RandomSymbols
        The expression of which you want to compute the density value
    condition : Relational containing RandomSymbols
        A conditional expression. density(X > 1, X > 0) is density of X > 1
        given X > 0
    numsamples : int
        Enables sampling and approximates the density with this many samples

    Examples
    ========

    >>> from sympy.stats import density, Die, Normal
    >>> from sympy import Symbol

    >>> x = Symbol('x')
    >>> D = Die('D', 6)
    >>> X = Normal(x, 0, 1)

    >>> density(D).dict
    {1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
    >>> density(2*D).dict
    {2: 1/6, 4: 1/6, 6: 1/6, 8: 1/6, 10: 1/6, 12: 1/6}
    >>> density(X)(x)
    sqrt(2)*exp(-x**2/2)/(2*sqrt(pi))
    RĀ   Rz   (   t   sampling_densityRĖ   R{   (   R%   R?   Rz   RĀ   R~   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRC     s    &c         K  sd   | d k	 r% t t |  | |  |  St |   j |  |  } | r\ t | d  r\ | j   S| Sd S(   s®  
    Cumulative Distribution Function of a random expression.

    optionally given a second condition

    This density will take on different forms for different types of
    probability spaces.
    Discrete variables produce Dicts.
    Continuous variables produce Lambdas.

    Examples
    ========

    >>> from sympy.stats import density, Die, Normal, cdf

    >>> D = Die('D', 6)
    >>> X = Normal('X', 0, 1)

    >>> density(D).dict
    {1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
    >>> cdf(D)
    {1: 1/6, 2: 1/3, 3: 1/2, 4: 2/3, 5: 5/6, 6: 1}
    >>> cdf(3*D, D > 2)
    {9: 1/4, 12: 1/2, 15: 3/4, 18: 1}

    >>> cdf(X)
    Lambda(_z, erf(sqrt(2)*_z/2)/2 + 1/2)
    R{   N(   RK   t   cdfRĮ   RV   R   R|   R{   (   R%   R?   Rz   R~   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRĪ   G  s    
c         K  sd   | d k	 r% t t |  | |  |  St |   j |  |  } | r\ t | d  r\ | j   S| Sd S(   s:  
    Characteristic function of a random expression, optionally given a second condition

    Returns a Lambda

    Examples
    ========

    >>> from sympy.stats import Normal, DiscreteUniform, Poisson, characteristic_function

    >>> X = Normal('X', 0, 1)
    >>> characteristic_function(X)
    Lambda(_t, exp(-_t**2/2))

    >>> Y = DiscreteUniform('Y', [1, 2, 7])
    >>> characteristic_function(Y)
    Lambda(_t, exp(7*_t*I)/3 + exp(2*_t*I)/3 + exp(_t*I)/3)

    >>> Z = Poisson('Z', 2)
    >>> characteristic_function(Z)
    Lambda(_t, exp(2*exp(_t*I) - 2))
    R{   N(   RK   t   characteristic_functionRĮ   RV   t   compute_characteristic_functionR|   R{   (   R%   R?   Rz   R~   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRĻ   q  s    
c         K  sd   | d  k	 r% t t |  | |  |  St |   j |  |  } | r\ t | d  r\ | j   S| Sd  S(   NR{   (   RK   t   moment_generating_functionRĮ   RV   t"   compute_moment_generating_functionR|   R{   (   R%   R?   Rz   R~   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRŃ     s    
c         K  s;   | d k	 r% t t |  | |  |  St |   j |  |  S(   sł  
    Returns the domain where a condition is True.

    Examples
    ========

    >>> from sympy.stats import where, Die, Normal
    >>> from sympy import symbols, And

    >>> D1, D2 = Die('a', 6), Die('b', 6)
    >>> a, b = D1.symbol, D2.symbol
    >>> X = Normal('x', 0, 1)

    >>> where(X**2<1)
    Domain: (-1 < x) & (x < 1)

    >>> where(X**2<1).set
    Interval.open(-1, 1)

    >>> where(And(D1<=D2 , D2<3))
    Domain: (Eq(a, 1) & Eq(b, 1)) | (Eq(a, 1) & Eq(b, 2)) | (Eq(a, 2) & Eq(b, 2))
    N(   RK   RG   RĮ   RV   (   R?   RŹ   R~   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRG     s    c         K  s   t  t |  | d d  S(   sū   
    A realization of the random expression

    Examples
    ========

    >>> from sympy.stats import Die, sample
    >>> X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)

    >>> die_roll = sample(X + Y + Z) # A random realization of three dice
    RĀ   i   (   t   nextt   sample_iter(   R%   R?   R~   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRI   ¼  s    c         K  s?   y t  |  | | |  SWn! t k
 r: t |  | | |  SXd S(   s„  
    Returns an iterator of realizations from the expression given a condition

    Parameters
    ==========

    expr: Expr
        Random expression to be realized
    condition: Expr, optional
        A conditional expression
    numsamples: integer, optional
        Length of the iterator (defaults to infinity)

    Examples
    ========

    >>> from sympy.stats import Normal, sample_iter
    >>> X = Normal('X', 0, 1)
    >>> expr = X*X + 3
    >>> iterator = sample_iter(expr, numsamples=3)
    >>> list(iterator) # doctest: +SKIP
    [12, 4, 7]

    See Also
    ========

    Sample
    sampling_P
    sampling_E
    sample_iter_lambdify
    sample_iter_subs

    N(   t   sample_iter_lambdifyRO   t   sample_iter_subs(   R%   R?   RĀ   R~   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRŌ   Ė  s    #c           sõ     r t  t |       n t  |    t  j   t  |  |     rf t    |   n  yJ  j   } g   D] } | | ^ q| }  |     rÆ  |   n  Wn t k
 rĻ t d   n X       f d   } |   S(   sd   
    See sample_iter

    Uses lambdify for computation. This is fast but does not always work.
    s'   Expr/condition too complex for lambdifyc          3  s£   d }  x |   k  r  j    } g   D] } | | ^ q( }   r  |   } | t k rw | t k rw t d   n  | s q	 q n   |   V|  d 7}  q	 Wd  S(   Ni    s(   Conditions must not contain free symbolsi   (   RI   Rb   R*   Ro   (   t   countRx   Ru   R   t   gd(   R?   t   fnt   given_fnRĀ   t   psR}   (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   return_generator  s    	(   RV   R   R°   RF   R
   RI   t	   ExceptionRO   (   R%   R?   RĀ   R~   Rx   Ru   R   RÜ   (    (   R?   RŁ   RŚ   RĀ   RŪ   R}   s-   lib/python2.7/site-packages/sympy/stats/rv.pyRÕ   õ  s"    
c         k  sĀ   | d k	 r$ t t |  |   } n t |   } d } x | | k  r½ | j   } | d k	 r¢ | j |  } | t k r | t k r t d   n  | s¢ q9 q¢ n  |  j |  V| d 7} q9 Wd S(   s_   
    See sample_iter

    Uses subs for computation. This is slow but almost always works.
    i    s(   Conditions must not contain free symbolsi   N(   RK   RV   R   RI   R;   Rb   R*   Ro   (   R%   R?   RĀ   R~   RŪ   R×   Rx   RŲ   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRÖ   #  s    	i   c   
      K  s”   d } d } t  |  | d | | } xR | D]J } | t k rX | t k rX t d   n  | rk | d 7} q+ | d 7} q+ Wt |  | }	 | r |	 j   S|	 Sd S(   sf   
    Sampling version of P

    See Also
    ========

    P
    sampling_E
    sampling_density

    i    RĀ   s(   Conditions must not contain free symbolsi   N(   RŌ   Rb   R*   Ro   R   t   evalf(
   R?   RŹ   RĀ   RŽ   R~   t
   count_truet   count_falset   samplesRI   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRÉ   =  s    
c         K  sF   t  |  | d | | } t t |    | } | r> | j   S| Sd S(   se   
    Sampling version of E

    See Also
    ========

    P
    sampling_P
    sampling_density
    RĀ   N(   RŌ   R   R°   RŽ   (   R%   RŹ   RĀ   RŽ   R~   Rį   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRĆ   a  s    
c         K  sG   i  } x: t  |  | d | | D]  } | j | d  d | | <q W| S(   sj   
    Sampling version of density

    See Also
    ========
    density
    sampling_P
    sampling_E
    RĀ   i    i   (   RŌ   t   get(   R%   RŹ   RĀ   R~   R»   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRĶ   w  s
    c         C  sk   t  |  |  r t St d d t } t |  t | |   t |   k pj t | t |  |   t |  k S(   s  
    Dependence of two random expressions

    Two expressions are independent if knowledge of one does not change
    computations on the other.

    Examples
    ========

    >>> from sympy.stats import Normal, dependent, given
    >>> from sympy import Tuple, Eq

    >>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
    >>> dependent(X, Y)
    False
    >>> dependent(2*X + Y, -Y)
    True
    >>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
    >>> dependent(X, Y)
    True

    See Also
    ========

    independent
    R   R   (   Rø   R*   R   Rb   RC   R	   (   R³   R“   R   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt	   dependent  s
    $c         C  s   t  |  |  S(   s  
    Independence of two random expressions

    Two expressions are independent if knowledge of one does not change
    computations on the other.

    Examples
    ========

    >>> from sympy.stats import Normal, independent, given
    >>> from sympy import Tuple, Eq

    >>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
    >>> independent(X, Y)
    True
    >>> independent(2*X + Y, -Y)
    False
    >>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
    >>> independent(X, Y)
    False

    See Also
    ========

    dependent
    (   Rć   (   R³   R“   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   independent®  s    c         C  s~   t  t |  j  } t  t |   j  } t t  t |    j t |    d k r[ t St | j |   d k rz t Sd S(   sc  
    Tests for independence between a and b by checking if their PSpaces have
    overlapping symbols. This is a sufficient but not necessary condition for
    independence and is intended to be used internally.

    Notes
    =====

    pspace_independent(a, b) implies independent(a, b)
    independent(a, b) does not imply pspace_independent(a, b)
    i    N(	   R!   RV   R   R4   R=   t   intersectionR*   Rb   RK   (   R³   R“   t	   a_symbolst	   b_symbols(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRø   Ģ  s    -c         C  sB   | d k r t |   } n  | s% |  Sd   | D } |  j |  S(   s£   
    Given a random expression replace all random variables with their symbols.

    If symbols keyword is given restrict the swap to only the symbols listed.
    c         S  s   i  |  ] } | j  |  q S(    (   R3   (   R9   Ru   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pys
   <dictcomp>ķ  s   	 N(   RK   R=   R;   (   R%   R   RĄ   (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   rv_subsć  s    t   NamedArgsMixinc           B  s   e  Z d Z d    Z RS(   c         C  sR   y |  j  |  j j |  SWn0 t k
 rM t d t |   j | f   n Xd  S(   Ns!   '%s' object has no attribute '%s'(   R   t	   _argnamest   indexRo   t   AttributeErrort   typeR'   (   R    t   attr(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   __getattr__ó  s
    (    (   R'   R(   Rź   Rļ   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyRé   š  s   c         C  s   |  t  k r t |   n  d S(   sh   
    Check a condition on input value.

    Raises ValueError with message if condition is not True
    N(   R*   Ro   (   R?   t   message(    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   _value_checkś  s    N(K   R)   t
   __future__R    R   t   sympyR   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   t	   sympy.abcR   t   sympy.core.compatibilityR   t   sympy.core.relationalR   t   sympy.logic.boolalgR   t   sympy.sets.setsR   R   R   t   sympy.solvers.solvesetR   R   R0   R8   RA   RM   RD   Rg   Rh   R   R=   RV   Ry   R·   RK   RĮ   Rb   RÄ   RJ   RĖ   RC   RĪ   RĻ   RŃ   RG   RI   t   InfinityRŌ   RÕ   RÖ   RÉ   RĆ   RĶ   Rć   Rä   Rø   Rč   t   objectRé   Rń   (    (    (    s-   lib/python2.7/site-packages/sympy/stats/rv.pyt   <module>   s\   p##4FD				I7?!-*!*.#	%		
