Tutorials › VEE Mathematical Statistics › Expectations of Piecewise Functions

VEE Mathematical Statistics · Expectation and Variability · Tutorial 72 of 880

Expectations of Piecewise Functions

Learn to split the support correctly and calculate the expected value of a function whose formula changes across intervals.

Beginner 9 min read

What You'll Learn

  • Partition a discrete support according to the intervals in a piecewise function.
  • Set up continuous expectation integrals using the density and the relevant boundaries.
  • Check that each part of the support is included exactly once.
  • Work with a hypothetical deductible-and-limit payment function.
  • Recognize why applying a function to the mean generally does not give the expected function value.

Why Piecewise Expectations Need Care

A function of a random variable may follow different rules in different regions. A benefit might be zero below a deductible, increase with the loss over a middle range, and stop increasing once a limit is reached. To find its expected value, use the rule that applies at each possible value of the random variable; one formula over the whole support will not generally work.

The central task is to split the support at every boundary where the formula changes, then calculate the contribution from each part. For a discrete random variable, those contributions are probability-weighted values. For a continuous random variable, they are integrals of the function times the density.

Definition: Suppose \(X\) has support \(S\), and \(g\) is defined by a different formula on each of a collection of nonoverlapping regions. The expectation \(E[g(X)]\) is calculated by applying the appropriate formula for \(g\) at each value in the support. For a discrete \(X\), sum \(g(x)P(X=x)\) over the support. For a continuous \(X\) with density \(f_X\), integrate \(g(x)f_X(x)\) over the support. The formulas below assume the expectation is finite.

The Calculation Rule

Write the regions for the separate formulas so that they do not overlap and together cover the support of \(X\). If a boundary value belongs to one region, make that assignment explicit. This matters especially for discrete variables, which can place positive probability at a boundary.

Theorem: If \(X\) is discrete with probability mass function \(p_X(x)=P(X=x)\), and \(g(x)=g_j(x)\) on region \(I_j\), then
$$ E[g(X)] = \sum_j \ \sum_{x\in S\cap I_j} g_j(x)p_X(x). $$

If \(X\) is continuous with density \(f_X\), the corresponding rule is

$$ E[g(X)] = \sum_j \int_{S\cap I_j} g_j(x)f_X(x)\,dx. $$

Here, an integral over a region means an integral over the part of that region where \(X\) can actually occur. Thus, intervals outside the support contribute nothing.

Why Splitting the Sum or Integral Works

For a discrete random variable, expectation is the sum of the value of the function at each possible outcome, weighted by that outcome’s probability. The support is divided into disjoint regions, so each outcome appears in exactly one inner sum. On that region, the appropriate formula is \(g_j(x)\). Grouping the original expectation by regions therefore gives

$$ E[g(X)] = \sum_{x\in S}g(x)p_X(x) = \sum_j\ \sum_{x\in S\cap I_j}g_j(x)p_X(x). $$

For a continuous random variable, the density-based expectation is the integral of \(g(x)f_X(x)\) over the support. Dividing the support into nonoverlapping regions divides that integral into the sum of the integrals over those regions. These are the same expectation calculation, organized so the correct formula for \(g\) is used throughout each integral.

1
Identify the support.
Record the values \(X\) can take. Do not automatically integrate or sum over every interval mentioned in the definition of \(g\).
2
Mark the boundaries.
Locate each point where the formula for \(g\) changes, and determine which piece includes each boundary.
3
Use the appropriate expectation formula.
Multiply by the probability mass for a discrete variable, or by the density inside each continuous integral.
4
Add the contributions and check.
Confirm that the pieces cover the support exactly once and that the result is consistent with the values of \(g(X)\).

Worked Examples

Worked Example: A Piecewise Function of a Discrete Variable

Let \(X\) have the following distribution:

$$ \begin{array}{c|ccccc} x & 0 & 1 & 2 & 3 & 4\\ \hline P(X=x) & 0.10 & 0.20 & 0.30 & 0.25 & 0.15 \end{array} $$

The probabilities sum to \(0.10+0.20+0.30+0.25+0.15=1.00\). Define \(g(x)=2x+1\) when \(x<2\), and \(g(x)=x^2-1\) when \(x\ge 2\). Find \(E[g(X)]\).

Method. Use the discrete expectation formula, assigning \(x=2\) to the second piece because its condition is \(x\ge 2\).

For \(x=0\), \(g(0)=2(0)+1=1\), so the weighted contribution is \(1(0.10)=0.10\).

For \(x=1\), \(g(1)=2(1)+1=3\), so the contribution is \(3(0.20)=0.60\).

For \(x=2\), \(g(2)=2^2-1=3\), so the contribution is \(3(0.30)=0.90\).

For \(x=3\), \(g(3)=3^2-1=8\), giving \(8(0.25)=2.00\). For \(x=4\), \(g(4)=4^2-1=15\), giving \(15(0.15)=2.25\).

$$ E[g(X)] =0.10+0.60+0.90+2.00+2.25 =5.85. $$

Check. The weighted contributions include each support value once and use probabilities summing to 1. The result is \(5.85\). A common mistake is to apply the first rule at \(x=2\); the stated condition \(x<2\) excludes that boundary.

Worked Example: Integrating Across Two Boundaries

Suppose \(X\) is uniform on \([0,4]\), so its density is \(f_X(x)=1/4\) for \(0\le x\le 4\). Define

$$ g(x)= \begin{cases} x^2, & 0\le x<1,\\ 2x, & 1\le x<3,\\ 8-x, & 3\le x\le 4. \end{cases} $$

Find \(E[g(X)]\). The support is \([0,4]\), and the boundaries \(1\) and \(3\) split it into three intervals.

Method. Integrate \(g(x)f_X(x)\) over each part of the support. The density is \(1/4\) on all three parts.

On \([0,1)\), the integral before multiplying by the density is \(\int_0^1 x^2\,dx=[x^3/3]_0^1=1/3\). On \([1,3)\), it is \(\int_1^3 2x\,dx=[x^2]_1^3=9-1=8\). On \([3,4]\), it is \(\int_3^4(8-x)\,dx=[8x-x^2/2]_3^4=24-19.5=4.5\).

$$ E[g(X)] = \frac14\left(\frac13+8+4.5\right) = \frac14\left(\frac{77}{6}\right) = \frac{77}{24} \approx 3.2083. $$

Check. The three intervals cover the full support, and their unweighted integrals sum to \(77/6\). The density factor \(1/4\) is applied once to that sum, giving \(77/24\), or \(3.2083\) to four decimal places. The answer is between the minimum and maximum values of \(g\) on the support.

Worked Example: A Hypothetical Deductible-and-Limit Payment

For a hypothetical insurance benefit, let the loss \(X\), measured in thousands of dollars, be uniform on \([0,5]\). Thus \(f_X(x)=1/5\) on that interval. The payment function is zero for losses up to \(1\) thousand dollars, is \(x-1\) for \(1<x<3\), and is capped at \(2\) thousand dollars for \(x\ge 3\). Find the expected payment \(E[g(X)]\), in thousands of dollars.

Method. Represent the payment by its three formulas and integrate each formula over the part of the support where it applies. The deductible boundary is \(1\), and the cap begins at \(3\).

$$ g(x)= \begin{cases} 0, & 0\le x\le 1,\\ x-1, & 1<x<3,\\ 2, & 3\le x\le 5. \end{cases} $$

The first region contributes \(\int_0^1 0\,dx=0\). For the middle region, \[ \int_1^3(x-1)\,dx = \left[\frac{x^2}{2}-x\right]_1^3 = (4.5-3)-(0.5-1) = 2. \] For the capped region, \(\int_3^5 2\,dx=2(5-3)=4\).

$$ E[g(X)] = \frac15(0+2+4) = 1.2\text{ thousand dollars} = \$1{,}200. $$

Check. The payment never exceeds \(2\) thousand dollars, and the expected payment is \(1.2\) thousand dollars, which is within the payment range. A common mistake is to integrate the uncapped formula \(x-1\) beyond \(x=3\); losses above that point receive the capped payment of \(2\), not an increasing payment.

Worked Example: Restricting the Integrals to the Support

Let \(X\) have density \(f_X(x)=x/8\) for \(0\le x\le 4\), and density zero elsewhere. This is a valid density because \(\int_0^4 (x/8)\,dx=[x^2/16]_0^4=1\). Define \(g(x)=x\) for \(0\le x<2\) and \(g(x)=4\) for \(x\ge 2\). Find \(E[g(X)]\).

Method. Split at \(2\), multiply each function formula by the density, and integrate only over the support \([0,4]\).

On \([0,2)\), the integrand is \(x(x/8)=x^2/8\), so the contribution is \[ \int_0^2\frac{x^2}{8}\,dx = \left[\frac{x^3}{24}\right]_0^2 = \frac{8}{24} = \frac13. \] On \([2,4]\), the integrand is \(4(x/8)=x/2\), giving \[ \int_2^4\frac{x}{2}\,dx = \left[\frac{x^2}{4}\right]_2^4 = \frac{16-4}{4} = 3. \]

$$ E[g(X)]=\frac13+3=\frac{10}{3}. $$

Check. The two contributions cover all of \([0,4]\), and the expectation is \(10/3\). Extending the second integral beyond \(4\) would be incorrect: the density is zero outside the stated support.

Common Traps and Practical Checks

A piecewise calculation is easiest to audit when each interval, function formula, and probability or density factor is visible. Before simplifying, check that no part of the support has been skipped or counted twice. For discrete variables, list the support values that fall in each piece. For continuous variables, mark the support endpoints and all formula boundaries on the integration limits.

  • Using a formula on the wrong side of a boundary. Conditions such as \(x<2\) and \(x\ge 2\) assign the boundary to exactly one piece. Follow those conditions literally.
  • Forgetting the probability mass or density. The expectation is not just a sum or integral of the function values. Weight by \(P(X=x)\) or multiply by \(f_X(x)\).
  • Integrating beyond the support. Only values \(X\) can take contribute. If a stated interval extends outside the support, intersect it with the support before calculating.
  • Substituting the mean into the function. In general, \(E[g(X)]\) is not \(g(E[X])\). In the first worked example, \(E[X]=2.15\), so \(g(E[X])=2.15^2-1=3.6225\), which differs from the correctly calculated \(E[g(X)]=5.85\).

Piecewise expectations are useful whenever an outcome is transformed by thresholds, caps, or other rules that change by region. The same procedure applies whether the underlying distribution is discrete or continuous: first locate the support, then divide it at the function’s boundaries, and finally add the correctly weighted contributions.

Check Your Understanding

For each problem, split the support wherever the formula changes before evaluating the expectation.

  1. \(X\) takes values \(-1,0,2,5\) with probabilities \(0.20,0.25,0.35,0.20\), respectively. Let \(g(x)=x+3\) for \(x<1\) and \(g(x)=2x\) for \(x\ge 1\). Find \(E[g(X)]\). (Answer: \(4.55\))
  2. \(X\) is uniform on \([0,6]\). Let \(g(x)=x\) for \(0\le x<2\), \(g(x)=3\) for \(2\le x<4\), and \(g(x)=6-x\) for \(4\le x\le 6\). Find \(E[g(X)]\). (Answer: \(5/3\))
  3. \(X\) has density \(f_X(x)=x/8\) on \([0,4]\) and zero elsewhere. Let \(g(x)=0\) for \(0\le x<1\), \(g(x)=x^2\) for \(1\le x<3\), and \(g(x)=3\) for \(3\le x\le4\). Find \(E[g(X)]\). (Answer: \(61/16=3.8125\))
  4. A hypothetical loss \(X\), in thousands of dollars, is uniform on \([0,8]\). A payment is zero when \(x\le2\), equals \(x-2\) when \(2<x<5\), and is capped at \(3\) when \(x\ge5\). Find the expected payment in dollars. (Answer: \(\$1{,}687.50\))
Clinical Biostats