Skip to main content
`Ctrl`+`K`
scipy.stats.rv_continuous.

# expect

rv_continuous.expect(func=None, args=(), loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds)

  Calculate expected value of a function with respect to the distribution by numerical integration.

  The expected value of a function `f(x)` with respect to a distribution `dist` is defined as:

              ub
      E[f(x)] = Integral(f(x) * dist.pdf(x)),
              lb

  where `ub` and `lb` are arguments and `x` has the `dist.pdf(x)` distribution. If the bounds `lb` and `ub` correspond to the support of the distribution, e.g. `[-inf, inf]` in the default case, then the integral is the unrestricted expectation of `f(x)`. Also, the function `f(x)` may be defined such that `f(x)` is `0` outside a finite interval in which case the expectation is calculated within the finite range `[lb, ub]`.

  Parameters:
    func callable, optional

      Function for which integral is calculated. Takes only one argument. The default is the identity mapping f(x) = x.

    args tuple, optional

      Shape parameters of the distribution.

    loc float, optional

      Location parameter (default=0).

    scale float, optional

      Scale parameter (default=1).

    lb, ub scalar, optional

      Lower and upper bound for integration. Default is set to the support of the distribution.

    conditional bool, optional

      If True, the integral is corrected by the conditional probability of the integration interval. The return value is the expectation of the function, conditional on being in the given interval. Default is False.

    Additional keyword arguments are passed to the integration routine.
  Returns:
    expect float

      The calculated expected value.

  Notes

  The integration behavior of this function is inherited from `scipy.integrate.quad`. Neither this function nor `scipy.integrate.quad` can verify whether the integral exists or is finite. For example `cauchy(0).mean()` returns `np.nan` and `cauchy(0).expect()` returns `0.0`.

  Likewise, the accuracy of results is not verified by the function. `scipy.integrate.quad` is typically reliable for integrals that are numerically favorable, but it is not guaranteed to converge to a correct value for all possible intervals and integrands. This function is provided for convenience; for critical applications, check results against other integration methods.

  The function is not vectorized.

  Examples

  To understand the effect of the bounds of integration consider

      >>> from scipy.stats import expon
      >>> expon(1).expect(lambda x: 1, lb=0.0, ub=2.0)
      0.6321205588285578

  This is close to

      >>> expon(1).cdf(2.0) - expon(1).cdf(0.0)
      0.6321205588285577

  If `conditional=True`

      >>> expon(1).expect(lambda x: 1, lb=0.0, ub=2.0, conditional=True)
      1.0000000000000002

  The slight deviation from 1 is due to numerical integration.

  The integrand can be treated as a complex-valued function by passing `complex_func=True` to `scipy.integrate.quad` .

      >>> import numpy as np
      >>> from scipy.stats import vonmises
      >>> res = vonmises(loc=2, kappa=1).expect(lambda x: np.exp(1j*x),
      ...                                       complex_func=True)
      >>> res
      (-0.18576377217422957+0.40590124735052263j)

      >>> np.angle(res)  # location of the (circular) distribution
      2.0