(expr, condition=None, numsamples=None, evaluate=True, **kwargs)
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
Returns the expected value of a random expression
Returns the expected value of a random expression
def expectation(expr, condition=None, numsamples=None, evaluate=True, **kwargs):
    """
    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
    """

    if not random_symbols(expr):  # expr isn't random?
        return expr
    if numsamples:  # Computing by monte carlo sampling?
        return sampling_E(expr, condition, numsamples=numsamples)

    # Create new expr and recompute E
    if condition is not None:  # If there is a condition
        return expectation(given(expr, condition), evaluate=evaluate)

    # A few known statements for efficiency

    if expr.is_Add:  # We know that E is Linear
        return Add(*[expectation(arg, evaluate=evaluate)
                     for arg in expr.args])

    # Otherwise case is simple, pass work off to the ProbabilitySpace
    result = pspace(expr).integrate(expr)
    if evaluate and hasattr(result, 'doit'):
        return result.doit(**kwargs)
    else:
        return result
def expectation ( expr , condition = None , numsamples = None , evaluate = True , * * kwargs ) : if not random_symbols ( expr ) : # expr isn't random? return expr if numsamples : # Computing by monte carlo sampling? return sampling_E ( expr , condition , numsamples = numsamples ) # Create new expr and recompute E if condition is not None : # If there is a condition return expectation ( given ( expr , condition ) , evaluate = evaluate ) # A few known statements for efficiency if expr . is_Add : # We know that E is Linear return Add ( * [ expectation ( arg , evaluate = evaluate ) for arg in expr . args ] ) # Otherwise case is simple, pass work off to the ProbabilitySpace result = pspace ( expr ) . integrate ( expr ) if evaluate and hasattr ( result , 'doit' ) : return result . doit ( * * kwargs ) else : return result