def expectation(expr, condition=None, numsamples=None, evaluate=True, **kwargs):
 if (not random_symbols(expr)):
  return expr
 if numsamples:
  return sampling_E(expr, condition, numsamples=numsamples)
 if (condition is not None):
  return expectation(given(expr, condition), evaluate=evaluate)
 if expr.is_Add:
  return Add(*[expectation(arg, evaluate=evaluate) for arg in expr.args])
 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 ) ) : return expr if numsamples : return sampling_E ( expr , condition , numsamples = numsamples ) if ( condition is not None ) : return expectation ( given ( expr , condition ) , evaluate = evaluate ) if expr . is_Add : return Add ( * [ expectation ( arg , evaluate = evaluate ) for arg in expr . args ] ) result = pspace ( expr ) . integrate ( expr ) if ( evaluate and hasattr ( result , 'doit' ) ) : return result . doit ( ** kwargs ) else : return result
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 .