The support and the event

◆ The Blueprint
Probability from a joint density

For continuous random variables \(Y_1\) and \(Y_2\) with joint density \(f\), the probability of an event is the double integral of \(f\) over the event's region \(A\). A joint density is never negative and integrates to 1 over its support, the region where \(f > 0\).

$$P\big((Y_1, Y_2) \in A\big) = \iint_A f(y_1, y_2)\,dy_2\,dy_1$$
$$\iint f(y_1, y_2)\,dy_2\,dy_1 = 1$$
The antiderivatives, y₂ first
$$\int_0^{y_1 - c} 3y_1\,dy_2 = 3y_1(y_1 - c)$$
$$\begin{aligned} &\int_c^1 \big(3y_1^2 - 3c\,y_1\big)\,dy_1 \\ &\quad = \Big[y_1^3 - \tfrac{3c}{2}y_1^2\Big]_c^1 \\ &\quad = \Big(1 - \tfrac{3c}{2}\Big) - \Big(c^3 - \tfrac{3c^3}{2}\Big) \\ &\quad = 1 - \tfrac{3c}{2} + \tfrac{c^3}{2} \end{aligned}$$
The antiderivatives, y₁ first
$$\int_{y_2 + c}^1 3y_1\,dy_1 = \tfrac{3}{2}\big[1 - (y_2 + c)^2\big]$$
$$\begin{aligned} &\int_0^{1 - c} \tfrac{3}{2}\big[1 - (y_2 + c)^2\big]\,dy_2 \\ &\quad = \tfrac{3}{2}(1 - c) - \tfrac{1}{2}\Big[(y_2 + c)^3\Big]_0^{1-c} \\ &\quad = \tfrac{3}{2}(1 - c) - \tfrac{1}{2}(1 - c^3) \\ &\quad = 1 - \tfrac{3c}{2} + \tfrac{c^3}{2} \end{aligned}$$
Reading limits from the picture

The outer variable's limits are constants: the smallest and largest values it takes on the region. The inner variable's limits are functions of the outer variable: the two ends of a slice at that value inside the region. Here the region's boundary is the line \(y_2 = y_1 - c\), the axis \(y_2 = 0\), and the edge \(y_1 = 1\).

Order of integration

For a density, which is never negative, the order of integration does not change the value of a double integral (Fubini's theorem). Pick the order with the simpler limits: for some regions one order needs the region split into pieces and the other does not.

In R

Nest integrate() calls, wrapping the inner one in Vectorize() so that it accepts a vector of outer values. Part 1 of The Script computes both orders this way.

System diagram
Reliability R(t)

System density f(t)

◆ The Blueprint
The exponential lifetime

A component with failure rate \(\lambda\) has lifetime density \(f(t) = \lambda e^{-\lambda t}\) for \(t \ge 0\), reliability \(R(t) = P(T > t) = e^{-\lambda t}\), and mean life \(1/\lambda\).

Series: the minimum

A series system fails at its first component failure, so its lifetime is \(\min(T_1, \ldots, T_n)\). By independence,

$$\begin{aligned} R(t) &= P(T_1 > t, \ldots, T_n > t) \\ &= \prod_{i=1}^{n} e^{-\lambda_i t} = e^{-(\lambda_1 + \cdots + \lambda_n)t} \end{aligned}$$

For two components this is the joint density integrated over the corner where both lifetimes exceed \(t\):

$$\begin{aligned} &\int_t^\infty\!\!\int_t^\infty \lambda_1 e^{-\lambda_1 t_1}\,\lambda_2 e^{-\lambda_2 t_2}\,dt_2\,dt_1 \\ &\quad = \int_t^\infty \lambda_1 e^{-\lambda_1 t_1}\,e^{-\lambda_2 t}\,dt_1 = e^{-\lambda_1 t}\,e^{-\lambda_2 t} \end{aligned}$$
Parallel: the maximum

A parallel system fails at its last component failure, so its lifetime is \(\max(T_1, \ldots, T_n)\), and

$$\begin{aligned} R(t) &= 1 - P(T_1 \le t, \ldots, T_n \le t) \\ &= 1 - \prod_{i=1}^{n} \big(1 - e^{-\lambda_i t}\big) \end{aligned}$$
Mixed wiring

Blocks wired in series multiply their reliabilities, and a parallel block of components has reliability one minus the product of their failure probabilities. A parallel pair followed by a third component has \(R(t) = \big[1 - (1 - e^{-\lambda_1 t})(1 - e^{-\lambda_2 t})\big]e^{-\lambda_3 t}\).

Mean time to failure

For a lifetime \(T \ge 0\), \(E[T] = \int_0^\infty P(T > t)\,dt\), so the MTTF is the area under \(R(t)\). A series system has MTTF \(1/(\lambda_1 + \cdots + \lambda_n)\). A parallel system of \(n\) components with the same rate \(\lambda\) has MTTF \(\frac{1}{\lambda}\big(1 + \frac12 + \cdots + \frac1n\big)\). The MTTF shown here is this exact area, from 0 to infinity.

Median life

The median life \(m\) solves \(R(m) = 0.5\). For a series system of exponentials, \(m = \ln 2/(\lambda_1 + \cdots + \lambda_n)\). The median shown here solves \(R(m) = 0.5\) numerically, with the same search as uniroot().

The system density

The system lifetime's density is \(f(t) = -dR(t)/dt\), since \(R(t) = 1 - F(t)\) and \(F\) is the distribution function.

In R

prod(exp(-lam * t)) and 1 - prod(1 - exp(-lam * t)) give the two reliabilities, integrate() the MTTF, and uniroot() the median life. Part 2 of The Script computes all three.

Joint density

The slice

Marginal density

◆ The Blueprint
Marginal densities

The marginal density of one variable integrates the joint density over every value of the other, over the support.

$$\begin{aligned} f_1(y_1) &= \int f(y_1, y_2)\,dy_2 \\ f_2(y_2) &= \int f(y_1, y_2)\,dy_1 \end{aligned}$$

On a support that is not a rectangle, the limits depend on the other variable. On the triangle \(0 \le y_2 \le y_1 \le 1\) with \(f = 3y_1\), \(f_1(y_1) = \int_0^{y_1} 3y_1\,dy_2 = 3y_1^2\), the same marginal as \(6y_1^2y_2\) on the square.

The three densities worked out

\(f = 6y_1^2y_2\):

$$\begin{aligned} f_1 &= 6y_1^2\Big[\tfrac{y_2^2}{2}\Big]_0^1 = 3y_1^2 \\ f_2 &= 6y_2\Big[\tfrac{y_1^3}{3}\Big]_0^1 = 2y_2 \end{aligned}$$

\(f = y_1 + y_2\):

$$f_1 = y_1 + \tfrac12, \qquad f_2 = y_2 + \tfrac12$$

\(f = 12y_1y_2(1 - y_2)\):

$$\begin{aligned} f_1 &= 12y_1\Big(\tfrac12 - \tfrac13\Big) = 2y_1 \\ f_2 &= 12y_2(1 - y_2)\Big[\tfrac{y_1^2}{2}\Big]_0^1 \\ &= 6y_2(1 - y_2) \end{aligned}$$
Marginals and independence

\(Y_1\) and \(Y_2\) are independent exactly when \(f(y_1, y_2) = f_1(y_1)\,f_2(y_2)\) at every point. \(6y_1^2y_2 = (3y_1^2)(2y_2)\) and \(12y_1y_2(1 - y_2) = (2y_1)\big(6y_2(1 - y_2)\big)\), so those pairs are independent. \((y_1 + \frac12)(y_2 + \frac12)\) differs from \(y_1 + y_2\), so that pair is dependent. A joint density on a rectangle that factors into a function of \(y_1\) times a function of \(y_2\) always gives independent variables.

In R

integrate(function(y2) f(0.5, y2), 0, 1) gives the marginal at one point; wrapping it in Vectorize() gives the whole marginal. Part 3 of The Script computes both marginals.

Joint density

Conditional density

Conditional densities at five values of y₂

◆ The Blueprint
Conditional density

Where \(f_2(y_2) > 0\), the conditional density of \(Y_1\) given \(Y_2 = y_2\) is the slice of the joint density at \(y_2\), divided by the marginal so that it integrates to 1 over \(y_1\).

$$f(y_1 \mid y_2) = \frac{f(y_1, y_2)}{f_2(y_2)}$$
$$P(Y_1 \in A \mid Y_2 = y_2) = \int_A f(y_1 \mid y_2)\,dy_1$$
This density

\(\int_0^1 (y_1 - \frac12)\,dy_1 = 0\), so both marginals are 1 (uniform on \([0, 1]\)) for every \(c\), and \(f(y_1 \mid y_2) = 1 + c(y_2 - \frac12)(y_1 - \frac12)\). The density is never negative only when \(|c| \le 4\), since its smallest value is \(1 - |c|/4\).

$$\begin{aligned} &P\big(Y_1 > \tfrac12 \mid Y_2 = y_2\big) \\ &\quad = \int_{1/2}^{1} \Big[1 + c\big(y_2 - \tfrac12\big)\big(y_1 - \tfrac12\big)\Big]dy_1 \\ &\quad = \tfrac12 + \tfrac{c}{8}\big(y_2 - \tfrac12\big) \end{aligned}$$
Independence

\(Y_1\) and \(Y_2\) are independent exactly when \(f(y_1, y_2) = f_1(y_1)\,f_2(y_2)\) at every point, or equivalently when \(f(y_1 \mid y_2) = f_1(y_1)\) for every \(y_2\). Here \(f_1 f_2 = 1\), so independence holds only at \(c = 0\). One point where the joint density and the product differ is enough to show dependence.

Covariance

\(\text{Cov}(Y_1, Y_2) = c\left(\int_0^1 (y - \tfrac12)^2\,dy\right)^2 = c/144\), and each variance is \(1/12\), so the correlation is \(c/12\), at most \(1/3\) in size. This family of densities with uniform marginals is the Farlie-Gumbel-Morgenstern family.

In R

Divide the joint density by integrate()'s value of the marginal, then integrate the result over the event. Part 4 of The Script computes the conditional probability this way.

Sketch

Your scores
◆ The Blueprint
The score

Your points are joined left to right by straight lines and held flat beyond the first and last points. The score compares that curve \(\hat s\) with the true density \(f\) at 200 evenly spaced points and reports R² as a percentage, rounded, and never below 0.

$$\text{score} = 100 \times \max\!\left(0,\; 1 - \frac{\sum (\hat s - f)^2}{\sum (f - \bar f)^2}\right)$$

A flat line at the true curve's average height \(\bar f\) scores 0, and any sketch with R² of at least 0.995 scores 100.

Marginal densities

Integrate the joint density over the other variable's range, which can depend on the other variable's value when the support is not a rectangle.

Challenges 1 and 4 have different joint densities with the same marginal, \(3y_1^2\). On the square, \(f_1(y_1) = \int_0^1 6y_1^2 y_2\,dy_2 = 3y_1^2\). On the triangle, \(y_2\) runs from 0 to \(y_1\), so \(f_1(y_1) = \int_0^{y_1} 3y_1\,dy_2 = 3y_1^2\).

$$f_1(y_1) = \int f(y_1, y_2)\,dy_2$$
Conditional densities

Find the marginal of the variable you condition on, then divide the joint density by it at the given value.

$$f(y_1 \mid y_2) = \frac{f(y_1, y_2)}{f_2(y_2)}$$
In R

approx(x, y, xout = grid, rule = 2) joins the points the same way. Part 5 of The Script scores two example sketches.

Base R, function by function
CallWhat it returns
integrate(f, lower, upper)The integral of a function of one variable, with an error estimate. Limits may be -Inf or Inf; take $value for the number.
Vectorize(g)A version of g that takes a vector and returns a vector, so an inner integral can serve as an outer integrand.
uniroot(f, c(a, b))A root of f between a and b; $root is the number. The ends must give values of opposite sign.
dexp(t, rate), pexp(t, rate)The exponential density and distribution function, so the reliability is 1 - pexp(t, rate).
approx(x, y, xout, rule = 2)Straight lines through the points, held flat past the ends: the Anvil's sketch.
jointpdfs_in_R.R