mean

mean, function

def pure mean(r: ranvar): number
def pure mean(r: ranvar, z: zedfunc): number

With one argument, returns the mean of the ranvar r. With two arguments, returns the expected value of z under the distribution r: each value z(k) is weighted by the probability of outcome k.

Examples

r = poisson(2)

show table "Mean" with
  mean(r) as "Value"

This produces the following table:

Value
1.999924

The two-argument overload can compute expected inventory costs. Here, demand is either zero or ten units, with equal probability. Stock is five units; each leftover unit costs 2 and each unmet unit of demand costs 10.

demand = mixture(dirac(0), 0.5, dirac(10))
stock = 5

// linear(1) represents the possible demand quantity.
cost = 2 * max(stock - linear(1), 0)
    + 10 * max(linear(1) - stock, 0)

show summary "Cost comparison" with
  mean(demand, cost) as "Expected cost"
  valueAt(cost, mean(demand)) as "Cost at mean demand"

The expected cost is 30: a cost of 10 when demand is zero and 50 when demand is ten, each with probability 0.5. By contrast, evaluating the cost at the mean demand of five units gives 0. The two calculations are not interchangeable when the cost function is nonlinear.

Remarks

The expectation uses the distribution represented by the ranvar. Ranvars group outcomes into buckets, with coarser resolution at larger magnitudes. Probability is spread uniformly over the integers within each bucket. A cost or reward that changes sharply within a bucket is therefore averaged over those integers, not evaluated at an exact original outcome.

See also