How to Compute the Volume of a Union of Balls

Suppose that a protein model consists of \(N\) atoms in three-dimensional space, each atom represented by a solid ball. The balls may overlap arbitrarily. How to compute the total volumn of this protein?

Abstract protein model as a union of overlapping atomic balls

Figure 1. Abstract three-dimensional protein model. Atomic balls may overlap; the target is the volume of their union. The arrangement is illustrative, not experimental protein data. -> Image source

1. Problem formulation

The desired molecular volume is the volume of their union: overlapping regions are counted only once, and all uncovered space, including enclosed voids, is excluded. This is a purely geometric model, not a solvent-accessible or solvent-excluded volume calculation.

For \(i\in\{1,\ldots,N\}\), let \(\boldsymbol c_i=(x_i,y_i,z_i)^\top\in\mathbb R^3\) and \(r_i>0\) denote the fixed center and radius, respectively. The corresponding closed ball is \(B_i=\{\boldsymbol x\in\mathbb R^3:\lVert\boldsymbol x-\boldsymbol c_i\rVert_2\leq r_i\}\), with individual volume \(V_i=\frac{4}{3}\pi r_i^3\). Assume \(1\leq N<\infty\). Writing \(\Omega=\bigcup_{i=1}^N B_i\), our objective is to compute or estimate

\[ V:=\operatorname{Vol}(\Omega). \]

Here \(\operatorname{Vol}(A)=\lambda_3(A)\), where \(\lambda_3\) is three-dimensional Lebesgue measure.

2. Decomposition of the union volume

Let \(\mathbf 1_A(\boldsymbol x)\) be the indicator of membership in a set \(A\): it equals one if \(\boldsymbol x\in A\) and zero otherwise. Define the multiplicity of a point as

\[ k(\boldsymbol x):=\sum_{i=1}^N\mathbf 1_{B_i}(\boldsymbol x)\in\{0,1,\ldots,N\}. \]

The union indicator satisfies \(\mathbf 1_\Omega(\boldsymbol x)=\mathbf 1_{\{k(\boldsymbol x)>0\}}\), so \(V=\int_{\mathbb R^3}\mathbf 1_\Omega(\boldsymbol x)\,d\boldsymbol x=\int_\Omega 1\,d\boldsymbol x\). For every \(\boldsymbol x\in\Omega\), we have \(k(\boldsymbol x)\geq1\) and therefore \(\sum_{i=1}^N\mathbf 1_{B_i}(\boldsymbol x)/k(\boldsymbol x)=1\). Intuitively, if a point is covered by \(k(\boldsymbol x)\) balls, each covering ball contributes a fraction \(1/k(\boldsymbol x)\) of its volume element, making the total contribution exactly one.

Multiplicity and reciprocal weighting

Figure 2. Planar cross-section of overlapping balls. Reciprocal multiplicity weighting ensures that each point in the union is counted exactly once.

Substituting this identity into the volume integral gives

\[ \begin{equation} \label{eq:volume-decomposition} \begin{aligned} V&=\int_\Omega\sum_{i=1}^N\frac{\mathbf 1_{B_i}(\boldsymbol x)}{k(\boldsymbol x)}\,d\boldsymbol x\\ &=\sum_{i=1}^N\int_\Omega\frac{\mathbf 1_{B_i}(\boldsymbol x)}{k(\boldsymbol x)}\,d\boldsymbol x\\ &=\sum_{i=1}^N\int_{B_i}\frac{1}{k(\boldsymbol x)}\,d\boldsymbol x. \end{aligned} \end{equation} \]

The second equality follows from linearity of integration for a finite sum; the last follows from \(B_i\subseteq\Omega\) and the indicator vanishing outside \(B_i\). The denominator is positive throughout the integration domain, so no undefined \(0/0\) is introduced.

3. Reformulation as expectations

To distinguish the geometric union \(\Omega\) from the underlying probability space, use \((\mathcal S,\mathcal F,\mathbb P)\) for the latter. For a measurable random element \(X:\mathcal S\to E\), its law is the pushforward measure \(P_X:=X_\#\mathbb P\) on the measurable state space \(E\). For every measurable subset \(A\) of \(E\),

\[ P_X(A)=\mathbb P(X^{-1}(A))=\mathbb P(X\in A). \]

Here the final expression abbreviates the probability of the preimage event. If \(E=\mathbb R^d\) and \(P_X\ll\lambda_d\), its PDF is a version of the Radon–Nikodym derivative \(p_X=dP_X/d\lambda_d\), defined almost everywhere. The expectation of an integrable function \(h(X)\) can be written as \(\mathbb E[h(X)]=\int_E h(x)\,P_X(dx)\). In the presence of a PDF, this equals \(\int_E h(x)p_X(x)\,dx\).

For each \(i\), consider \(\boldsymbol X_i\sim\operatorname{Uniform}(B_i)\). Uniformity here is with respect to volume: for every Borel set \(A\subseteq\mathbb R^3\), \(P_{\boldsymbol X_i}(A)=\lambda_3(A\cap B_i)/V_i\). Its PDF is \(p_{\boldsymbol X_i}(\boldsymbol x)=\mathbf 1_{B_i}(\boldsymbol x)/V_i\). Consequently,

\[ \mathbb E\left[\frac{1}{k(\boldsymbol X_i)}\right] =\int_{B_i}\frac{p_{\boldsymbol X_i}(\boldsymbol x)}{k(\boldsymbol x)}\,d\boldsymbol x =\frac{1}{V_i}\int_{B_i}\frac{1}{k(\boldsymbol x)}\,d\boldsymbol x. \]

The reciprocal is well-defined almost surely; if a globally defined integrand is needed, set it to zero outside \(\Omega\). Multiplying by \(V_i\) and substituting into \(\eqref{eq:volume-decomposition}\) yields the exact identity

\[ \begin{equation} \label{eq:volume-expectation} V=\sum_{i=1}^N V_i\,\mathbb E\left[\frac{1}{k(\boldsymbol X_i)}\right],\qquad \boldsymbol X_i\sim\operatorname{Uniform}(B_i). \end{equation} \]

Since \(1/N\leq1/k(\boldsymbol X_i)\leq1\) almost surely, all expectations exist. They can be estimated separately by Monte Carlo sampling once a uniform sampler for each ball is established.

4. Uniform sampling within a ball

Fix a ball \(B_i\). A point inside it can be written as \(\boldsymbol x=\boldsymbol c_i+\rho\boldsymbol d\), where \(0\leq\rho\leq r_i\) and \(\lVert\boldsymbol d\rVert_2=1\). We construct a random direction \(\boldsymbol D\) and a radial distance \(R\) independently, then combine them to obtain a uniform point. Throughout this section the index \(i\) on \(R\) and its law is suppressed for readability.

Direction and radial sampling inside a ball

Figure 3. Schematic cross-section of the three-dimensional sampling construction. A normalized Gaussian vector determines the direction, and an independent uniform scalar determines the radial distance.

4.1. Sampling the direction

Generate \(\boldsymbol Z\sim\mathcal N(\boldsymbol 0,\mathbf I_3)\), whose density is \(p_{\boldsymbol Z}(\boldsymbol z)=(2\pi)^{-3/2}\exp(-\lVert\boldsymbol z\rVert_2^2/2)\). As the density depends only on the norm, the distribution is rotationally invariant: \(\mathbf Q\boldsymbol Z\overset{d}{=}\boldsymbol Z\) for every orthogonal matrix \(\mathbf Q\). Set \(\boldsymbol D:=\boldsymbol Z/\lVert\boldsymbol Z\rVert_2\). On the null event \(\boldsymbol Z=\boldsymbol 0\), assign any fixed unit direction.

Norm preservation implies \(\mathbf Q\boldsymbol D\overset{d}{=}\boldsymbol D\). By uniqueness of the rotation-invariant probability measure on \(\mathbb S^2=\{\boldsymbol d\in\mathbb R^3:\lVert\boldsymbol d\rVert_2=1\}\), the law of \(\boldsymbol D\) is \(P_{\boldsymbol D}=\sigma/(4\pi)\), where \(\sigma\) is surface-area measure on the unit sphere. Thus \(\boldsymbol D\sim\operatorname{Uniform}(\mathbb S^2)\); this is uniformity with respect to surface area, not three-dimensional Lebesgue measure.

4.2. Deriving the target radial CDF

To derive the desired radial distribution, first consider a hypothetical \(\boldsymbol Y_i\sim\operatorname{Uniform}(B_i)\) and let \(R^*:=\lVert\boldsymbol Y_i-\boldsymbol c_i\rVert_2\). For \(0\leq\rho\leq r_i\), spatial uniformity gives

\[ \begin{aligned} \mathbb P(R^*\leq\rho)&=\mathbb P(\lVert\boldsymbol Y_i-\boldsymbol c_i\rVert_2\leq\rho)\\ &=\frac{\frac43\pi\rho^3}{\frac43\pi r_i^3}=\left(\frac{\rho}{r_i}\right)^3. \end{aligned} \]

Define \(F(\rho):=P_{R^*}(( -\infty,\rho])\) as the target CDF. Its full expression is

\[ \begin{equation} \label{eq:radial-cdf} F(\rho)=\begin{cases} 0,&\rho<0,\\ (\rho/r_i)^3,&0\leq\rho\leq r_i,\\ 1,&\rho>r_i. \end{cases} \end{equation} \]

This CDF is absolutely continuous (continuous and piecewise polynomial with bounded derivative). Thus the radial law has a PDF given almost everywhere by \(F'(\rho)\):

\[ p_{R^*}(\rho)=\begin{cases} 3\rho^2/r_i^3,&0<\rho<r_i,\\ 0,&\text{otherwise}. \end{cases} \]

The CDF is not differentiable at \(r_i\), but assigning any nonnegative finite density value there leaves the law unchanged. Notice that equal radial intervals do not generally correspond to equal volumes. We do not need the PDF to construct the radial sampler.

4.3. Constructing a radial random variable from its CDF

Given the target CDF \(F\), we seek a random variable \(R\) with \(F_R=F\). Start with \(U\sim\operatorname{Uniform}(0,1)\), so \(\mathbb P(U\leq u)=u\) for \(u\in[0,1]\). We seek a deterministic, strictly increasing bijection \(g:[0,1]\to[0,r_i]\) and define \(R:=g(U)\). The requirement is \(\mathbb P(g(U)\leq\rho)=F(\rho)\) for every \(\rho\in[0,r_i]\). By monotonicity and invertibility,

\[ \begin{aligned} F(\rho)&=\mathbb P(g(U)\leq\rho)\\ &=\mathbb P(U\leq g^{-1}(\rho))\\ &=g^{-1}(\rho). \end{aligned} \]

The last equality follows from uniformity and \(g^{-1}(\rho)\in[0,1]\). Thus \(g=(F|_{[0,r_i]})^{-1}\); the restriction matters because the complete CDF is not invertible on all of \(\mathbb R\). Solving \(u=(\rho/r_i)^3\) for \(\rho\) gives \(g(u)=r_i u^{1/3}\). This is indeed a strictly increasing bijection, and we obtain

\[ \begin{equation} \label{eq:radial-sampling} R:=r_iU^{1/3},\qquad U\sim\operatorname{Uniform}(0,1). \end{equation} \]

To verify the construction, for \(0\leq\rho\leq r_i\) we have \(\mathbb P(R\leq\rho)=\mathbb P(U\leq(\rho/r_i)^3)=(\rho/r_i)^3\). Outside this interval the probabilities are zero and one, respectively. Hence \(F_R=F\), so \(P_R=P_{R^*}\), and \(p_R=p_{R^*}\) almost everywhere.

This is inverse transform sampling. In measure-theoretic terms, we have constructed a map \(g\) satisfying \(g_\#P_U=P_{R^*}\). Indeed, for each Borel set \(A\subseteq\mathbb R\), \(P_R(A)=\mathbb P(U\in g^{-1}(A))=P_U(g^{-1}(A))=(g_\#P_U)(A)\), where \(g^{-1}(A)\) denotes a set preimage. Equivalently, \(P_R=(g\circ U)_\#\mathbb P\). Here \(P_U\) can be regarded as a measure on \([0,1]\), the domain of \(g\); \(g_\#P_U\) is regarded as a measure on \(\mathbb R\) supported on \([0,r_i]\). The target and constructed laws coincide, rather than merely sharing a density at selected points.

4.4. Combining direction and distance

Generate \(U\) and \(\boldsymbol Z\) independently. Their respective functions \(R\) and \(\boldsymbol D\) are therefore independent. Set \(\boldsymbol X_i:=\boldsymbol c_i+R\boldsymbol D\). The complete sampling construction is

\[ \begin{equation} \label{eq:ball-sampling} \begin{aligned} \boldsymbol Z&\sim\mathcal N(\boldsymbol 0,\mathbf I_3),\qquad U\sim\operatorname{Uniform}(0,1),\\ \boldsymbol X_i&:=\boldsymbol c_i+r_iU^{1/3}\frac{\boldsymbol Z}{\lVert\boldsymbol Z\rVert_2}. \end{aligned} \end{equation} \]

Only \(\boldsymbol Z\) and \(U\) need to be sampled; the center and radius remain fixed.

4.5. Proof of spatial uniformity

Correct direction and radial marginals alone do not generally guarantee uniformity. Independence gives the joint law \(P_{(R,\boldsymbol D)}=P_R\otimes P_{\boldsymbol D}\). For the map \(T_i(\rho,\boldsymbol d)=\boldsymbol c_i+\rho\boldsymbol d\), the constructed law is \(P_{\boldsymbol X_i}=(T_i)_\#(P_R\otimes P_{\boldsymbol D})\). For any Borel set \(A\subseteq\mathbb R^3\),

\[ \begin{aligned} P_{\boldsymbol X_i}(A) &=\int_{\mathbb S^2}\int_0^{r_i}\mathbf 1_A(\boldsymbol c_i+\rho\boldsymbol d)\frac{3\rho^2}{r_i^3}\,d\rho\,\frac{\sigma(d\boldsymbol d)}{4\pi}\\ &=\frac{1}{V_i}\int_{\mathbb S^2}\int_0^{r_i}\mathbf 1_A(\boldsymbol c_i+\rho\boldsymbol d)\rho^2\,d\rho\,\sigma(d\boldsymbol d)\\ &=\frac{1}{V_i}\int_{B_i}\mathbf 1_A(\boldsymbol x)\,d\boldsymbol x\\ &=\frac{\lambda_3(A\cap B_i)}{V_i}. \end{aligned} \]

The third equality uses the spherical-coordinate volume element \(d\boldsymbol x=\rho^2\,d\rho\,\sigma(d\boldsymbol d)\). This is exactly the desired law, proving \(\boldsymbol X_i\sim\operatorname{Uniform}(B_i)\).

5. Monte Carlo estimation

For each ball \(B_i\), generate \(M\geq1\) i.i.d. samples \(\boldsymbol X_{i,1},\ldots,\boldsymbol X_{i,M}\) using \(\eqref{eq:ball-sampling}\), independently across balls. Specifically, use independent copies \(\boldsymbol Z_{i,j},U_{i,j}\) of the base random variables. For each sampled point, compute its multiplicity over all input balls, \(k(\boldsymbol X_{i,j})=\sum_{\ell=1}^N\mathbf 1_{B_\ell}(\boldsymbol X_{i,j})\). Membership in \(B_\ell\) is tested by comparing \(\lVert\boldsymbol X_{i,j}-\boldsymbol c_\ell\rVert_2^2\) with \(r_\ell^2\).

Replacing each expectation in \(\eqref{eq:volume-expectation}\) with the empirical mean gives the estimator

\[ \begin{equation} \label{eq:volume-estimator} \widehat V_M:=\sum_{i=1}^N\frac{V_i}{M}\sum_{j=1}^M\frac{1}{k(\boldsymbol X_{i,j})}. \end{equation} \]

5.1. Unbiasedness

Since every \(\boldsymbol X_{i,j}\) has the same law as \(\boldsymbol X_i\), linearity of expectation gives

\[ \begin{aligned} \mathbb E[\widehat V_M]&=\sum_{i=1}^N\frac{V_i}{M}\sum_{j=1}^M\mathbb E\left[\frac{1}{k(\boldsymbol X_{i,j})}\right]\\ &=\sum_{i=1}^N V_i\mathbb E\left[\frac{1}{k(\boldsymbol X_i)}\right]=V. \end{aligned} \]

Thus the estimator is unbiased for every positive integer \(M\).

5.2. Strong consistency

For each ball consider an infinite i.i.d. sequence and use its first \(M\) samples. Since reciprocal multiplicity is bounded, the strong law of large numbers gives \(M^{-1}\sum_{j=1}^M 1/k(\boldsymbol X_{i,j})\xrightarrow{\mathrm{a.s.}}\mathbb E[1/k(\boldsymbol X_i)]\) as \(M\to\infty\). There are finitely many balls, so these convergences hold simultaneously almost surely. It follows that \(\widehat V_M\xrightarrow{\mathrm{a.s.}}V\).

5.3. Variance

Let \(\sigma_i^2:=\operatorname{Var}(1/k(\boldsymbol X_i))\). Under the independent sampling scheme, \(\operatorname{Var}(\widehat V_M)=M^{-1}\sum_{i=1}^N V_i^2\sigma_i^2\). For fixed geometry and \(N\), the root-mean-square error is \(O(M^{-1/2})\). If the balls are disjoint up to null sets, reciprocal multiplicity equals one almost surely and the estimator is exact for any \(M\).

6. Algorithm summary

Code implementation: Github gist

Given centers, positive radii, and a per-ball sampling budget \(M\), compute each \(V_i\), generate \(M\) independent samples inside each \(B_i\), count each sample's multiplicity across all balls, and combine the weighted empirical means according to \(\eqref{eq:volume-estimator}\). The algorithm uses \(NM\) samples and takes \(O(N^2M)\) time with direct membership testing. Spatial indexing can reduce the cost of multiplicity queries without changing the estimator.

The exact identity \(\eqref{eq:volume-expectation}\) and sampling construction \(\eqref{eq:ball-sampling}\) together yield an unbiased, strongly consistent estimator without requiring a closed-form expression for the union geometry.

Notation

Here I list the notations used in the derivation. We follow common conventions in probability theory and mathematical modeling:

  • Uppercase symbols denote random variables or random vectors, and lowercase symbols denote their realizations or integration variables. Conventional deterministic symbols such as \(N\), \(M\), \(V\), and \(B_i\) are exceptions.
  • Bold italic letters denote vectors; bold upright letters denote matrices. Ordinary italic letters denote scalars.
  • \(\mathbb P\) denotes the probability measure on the underlying sample space; \(P_X\) denotes the law of a random element \(X\), not its density. Lowercase \(p_X\) denotes a PDF when it exists.
  • Pushforwards are written \(f_\#\mu\), with \((f_\#\mu)(A)=\mu(f^{-1}(A))\) for measurable \(A\). A set preimage does not require \(f\) to be invertible.
Symbol Meaning
\(N\) Number of atomic balls.
\(i,\ell\) Ball indices, ranging from \(1\) to \(N\).
\(j\) Sample index within a ball.
\(\boldsymbol c_i\) Fixed center of ball \(i\), with Cartesian components \(x_i,y_i,z_i\).
\(r_i\) Fixed positive radius of ball \(i\).
\(B_i\) Closed solid ball with center \(\boldsymbol c_i\) and radius \(r_i\).
\(V_i\) Individual volume of \(B_i\), equal to \(4\pi r_i^3/3\).
\(\Omega\) Geometric union of all balls; not the probability sample space.
\(V\) Exact volume of \(\Omega\).
\(A\) Generic measurable set in the relevant state space (Borel in Euclidean applications here).
\(\lambda_d\) Lebesgue measure on \(\mathbb R^d\); \(\lambda_1\) is length and \(\lambda_3\) is volume.
\(\operatorname{Vol}\) Three-dimensional Lebesgue measure \(\lambda_3\).
\(\boldsymbol x\) Deterministic spatial coordinate or integration variable.
\(\lVert\cdot\rVert_2\) Euclidean norm.
\(\mathbf 1_A\) Indicator of membership in \(A\).
\(k(\boldsymbol x)\) Multiplicity: number of balls covering \(\boldsymbol x\).
\((\mathcal S,\mathcal F,\mathbb P)\) Underlying probability space: sample space, sigma-algebra, and probability measure.
\(X,E\) Generic random element and its measurable state space.
\(P_X\) Law of \(X\), defined by \(P_X=X_\#\mathbb P\).
\(f_\#\mu\) Pushforward of a measure \(\mu\) by a measurable map \(f\).
\(f^{-1}(A)\) Preimage of a set \(A\) under \(f\).
\(\ll\) Absolute continuity of measures.
\(p_X\) PDF of a Euclidean random element \(X\) when its law is absolutely continuous with respect to Lebesgue measure.
\(F_X\) CDF of a real random variable \(X\), defined by \(F_X(x)=P_X(( -\infty,x])\).
\(\mathbb E\) Expectation under \(\mathbb P\).
\(h\) Generic integrable function of a random element.
\(\boldsymbol X_i\) Random vector uniformly distributed inside \(B_i\).
\(\boldsymbol Y_i\) Hypothetical uniform point used to derive the target radial law.
\(\boldsymbol Z,\boldsymbol z\) Standard three-dimensional Gaussian random vector and its realization.
\(\mathcal N(\boldsymbol 0,\mathbf I_3)\) Standard three-dimensional Gaussian distribution.
\(\mathbf I_3\) Three-dimensional identity matrix.
\(\mathbf Q\) Arbitrary orthogonal matrix.
\(\overset{d}{=}\) Equality in distribution.
\(\mathbb S^2\) Unit sphere in \(\mathbb R^3\).
\(\sigma\) Surface-area measure on \(\mathbb S^2\), with total mass \(4\pi\).
\(\boldsymbol D,\boldsymbol d\) Uniform random unit direction and its realization.
\(R^*\) Target radial random variable induced by the hypothetical uniform point \(\boldsymbol Y_i\).
\(F\) Target radial CDF, equal to \(F_{R^*}\).
\(R,\rho\) Constructed radial random variable and its realization; the index \(i\) is implicit.
\(U,u\) Uniform random variable on \([0,1]\) and its realization.
\(g\) Deterministic inverse-CDF transformation \(g(u)=r_i u^{1/3}\).
\(g^{-1}\) Inverse function of the strictly increasing bijection \(g\) when applied to a point.
\(T_i\) Map from radial distance and direction to spatial coordinates: \(T_i(\rho,\boldsymbol d)=\boldsymbol c_i+\rho\boldsymbol d\).
\(\otimes\) Product of measures, used for the joint law of independent random variables.
\(M\) Number of Monte Carlo samples per ball.
\(\boldsymbol Z_{i,j},U_{i,j}\) Independent base random variables for sample \(j\) of ball \(i\).
\(\boldsymbol X_{i,j}\) The \(j\)-th random sample from \(\operatorname{Uniform}(B_i)\).
\(\widehat V_M\) Random Monte Carlo estimator of the union volume.
\(\operatorname{Var}\) Variance operator.
\(\sigma_i^2\) Variance of reciprocal multiplicity under uniform sampling in \(B_i\).
\(\mathrm{a.s.}\) Almost surely (with probability one).
\(O(\cdot)\) Asymptotic order notation.