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\).
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\).
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.
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.
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\).
A series system fails at its first component failure, so its lifetime is \(\min(T_1, \ldots, T_n)\). By independence,
For two components this is the joint density integrated over the corner where both lifetimes exceed \(t\):
A parallel system fails at its last component failure, so its lifetime is \(\max(T_1, \ldots, T_n)\), and
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}\).
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.
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 lifetime's density is \(f(t) = -dR(t)/dt\), since \(R(t) = 1 - F(t)\) and \(F\) is the distribution function.
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.
The marginal density of one variable integrates the joint density over every value of the other, over the support.
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.
\(f = 6y_1^2y_2\):
\(f = y_1 + y_2\):
\(f = 12y_1y_2(1 - y_2)\):
\(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.
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.
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\).
\(\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\).
\(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.
\(\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.
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.
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.
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.
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\).
Find the marginal of the variable you condition on, then divide the joint density by it at the given value.
approx(x, y, xout = grid, rule = 2) joins the points the same way. Part 5 of The Script scores two example sketches.
| Call | What 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. |