Observe that all the functions given here have analogous ones implemented in the R package Gmedian, but we provide a new version to harmonize arguments and returns.
In all that follows, we consider a random variable \(X\) taking values in \(\mathbb{R}^p\), for some integer \(p \geq 2\). The geometric median \(m\) of \(X\) is defined as the minimizer of the convex functional \(G : \mathbb{R}^p \to \mathbb{R}\), given for all \(h \in \mathbb{R}^p\) by Haldane (1948):
\[ G(h) := \mathbb{E} \left[ \| X - h \| - \| X \| \right], \]
where \(\| \cdot \|\) denotes the Euclidean norm. The subtraction of the constant term \(\|X\|\) ensures that no assumption is required on the existence of the first-order moment of \(X\). Moreover, if the distribution of \(X\) is not concentrated near a straight line, the geometric median is uniquely defined Kemperman (1987). It is well known that when the distribution of \(X\) is symmetric, the geometric median coincides with the mean.
A key advantage of the geometric median is its robustness: it has a breakdown point of 50%, meaning that up to half the data can be arbitrarily corrupted without causing the estimator to diverge. In contrast, the mean has a breakdown point of 0% and is highly sensitive to outliers.
One can also consider a weighted version of the geometric median. Let \(w \in [0,1]\) be a random weight, and define the functional:
\[ G(h) := \mathbb{E} \left[ w \| X - h \| - w \| X \| \right]. \]
This extends the concept of the geometric median to weighted data, allowing for more flexible modeling.
A common approach to estimating the geometric median is to use an M-estimator based on a sample. More precisely, let \(X_1, \ldots, X_N\) be i.i.d. copies of the random variable \(X\), and let \(w_1, \ldots, w_N \in [0,1]\) be associated weights. One then considers the empirical functional \(G_N : \mathbb{R}^p \to \mathbb{R}\), defined for all \(h \in \mathbb{R}^p\) by
\[ G_N(h) := \frac{1}{N} \sum_{i=1}^{N} w_i \left( \|X_i - h\| - \|X_i\| \right). \]
Contrary to the empirical mean, this functional does not admit a closed-form minimizer; hence, the empirical geometric median must be computed approximately. A standard method for this task is the Weiszfeld algorithm (Weiszfeld 1937), which iteratively refines an estimate of the median. See also (Vardi and Zhang 2000, beck2013weiszfeld) for more recent studies and convergence results. The procedure is described below:
Input:
Repeat until convergence or maximum iterations:
num = 0 and den = 0Stop when \(\|m^{(t+1)} - m^{(t)}\|_2 < \varepsilon\) or \(t > T_{\max}\)
Return final estimate \(m^{(t+1)}\)
The Weiszfeld algorithm can be interpreted either as a fixed-point iteration or as a gradient descent method applied to a convex but non-smooth objective function. This algorithm has been extensively studied and refined over the years (see, e.g., (Beck and Sabach 2015) and references therein). In particular, the asymptotic efficiency of the resulting estimators has been established by (Vardi and Zhang 2000).
We provide a reference implementation in the function WeiszfeldMedian,
which takes the following arguments:
X: A matrix of size \(N \times p\) containing the data, where each
row is an observation.init: Initial guess for the median (default is the zero
vector).weights: A vector of weights of length \(N\) (default is a vector
of ones).epsilon: Tolerance for the stopping criterion (default is
\(10^{-8}\)).nitermax: Maximum number of iterations (default is 100).The function returns the estimated geometric median.
In the case of moderate sample sizes in moderate dimensions, the Weiszfeld algorithm is very efficient. However, it can become computationally expensive when applied to large-scale data in high-dimensional or functional spaces (see (Cardot et al. 2013) for a detailed discussion). In such scenarios, one may prefer a stochastic gradient algorithm (Robbins and Monro 1951), or its averaged version (ASGD) (Ruppert 1988; Polyak and Juditsky 1992; Cardot et al. 2013), which is described below.
This algorithm has been extensively studied in recent years. For example, (Cardot et al. 2013) established the asymptotic efficiency of the estimates, while (Godichon-Baggioni 2016) provided \(L^p\) and almost sure convergence rates. Additionally, non-asymptotic confidence balls are available from (Cardot et al. 2017).
Another alternative for online estimation of the geometric median is the stochastic Newton algorithm (Godichon-Baggioni and Lu 2023). This method is particularly useful when the distribution of \(X\) is highly concentrated along a straight line. However, due to its higher computational cost, we do not provide an implementation here.
The ASGD algorithm is implemented in the function ASGMedian, which
takes the following arguments:
X: A matrix of size \(N \times p\) corresponding to the data (rows
are observations).init: Initial value for the median (default is the zero
vector).weights: A vector of size \(N\) containing weights (default:
ones).gamma: ASG step-size parameter (default: 2).alpha: ASG decay rate parameter (default: 0.75).nstart: Number of restarts for the ASG algorithm (default:
1).epsilon: Stopping criterion (algorithm stops when consecutive
iterations differ by less than this threshold, default: 1e-8).nitermax: Maximum number of iterations (default: 100).It returns the estimated geometric median.
We consider a Gaussian random vector \(X \sim \mathcal{N}(0, I_p)\) with \(p = 100\). In Figure @ref{fig:fig-median}, we generate \(100\) replications for sample sizes \(100\), \(1000\), \(2000\), \(5000\), and \(10000\). For each replication, we compute both the squared estimation error and the computation time of the two methods: the Weiszfeld algorithm and the ASGD algorithm.
As shown in the figure beside, while the Weiszfeld algorithm provides slightly better estimation accuracy than ASGD, it is significantly more computationally demanding, especially as the sample size increases (see also (Cardot et al. 2013)).
Figure 1.1: Comparison of the squared errors and computation time between Weiszfeld and ASGD algorithms with respect to the sample size.
We continue to consider a random variable \(X\) taking values in \(\mathbb{R}^p\). The Median Covariation Matrix (MCM for short) of \(X\), denoted by \(V\), is a robust alternative to the classical covariance matrix. It is defined as the minimizer of the functional \(G_m : \mathcal{M}_p(\mathbb{R}) \to \mathbb{R}\), given for any square matrix \(M \in \mathcal{M}_p(\mathbb{R})\) by (Kraus and Panaretos 2012; Cardot and Godichon-Baggioni 2015):
\[ G_m(M) := \mathbb{E} \left[ \left\| (X - m)(X - m)^\top - M \right\|_{F} - \left\| (X - m)(X - m)^\top \right\|_{F} \right], \]
where \(\| \cdot \|_{F}\) denotes the Frobenius norm, and \(m\) is the geometric median of \(X\). As in the case of the median, the subtraction of the term \(\left\| (X - m)(X - m)^\top \right\|_{F}\) ensures that the functional is well-defined without requiring the existence of second-order moments. Moreover, \(V\) can be interpreted as the geometric median of the random matrix \((X - m)(X - m)^\top\), and thus its uniqueness is guaranteed as long as the distribution of \(X\) is not concentrated along a straight line.
As with the median, a weighted version can also be considered. Given a random weight \(w \in [0,1]\), the MCM \(V\) is then defined as the minimizer of the functional:
\[ G_m(M) := \mathbb{E} \left[ w \left\| (X - m)(X - m)^\top - M \right\|_{F} - w \left\| (X - m)(X - m)^\top \right\|_{F} \right]. \]
In practice, the MCM can be approximated using either a Weiszfeld-type algorithm or an averaged stochastic gradient algorithm, as described in (Cardot and Godichon-Baggioni 2015).
As in the case of the geometric median, one can consider a (weighted) empirical version of the functional for the Median Covariation Matrix (MCM). For any \(M \in \mathcal{M}_{p}(\mathbb{R})\), define
\[ G_{m,n}(M) = \frac{1}{N} \sum_{i=1}^{N} w_i \left\| (X_i - m)(X_i - m)^\top - M \right\|_{F} - w_i \left\| (X_i - m)(X_i - m)^\top \right\|_{F}, \]
where the weights \(w_i\) lie in \([0,1]\). This functional can be minimized using a Weiszfeld-type algorithm. In practice, the geometric median \(m\) is typically unknown and must be replaced by an estimate \(\hat{m}\) computed via either the Weiszfeld algorithm or the ASGD algorithm. Consequently, one aims to minimize the modified functional
\[ G_{\hat{m},n}(M) = \frac{1}{N} \sum_{i=1}^{N} w_i \left\| (X_i - \hat{m})(X_i - \hat{m})^\top - M \right\|_{F} - w_i \left\| (X_i - \hat{m})(X_i - \hat{m})^\top \right\|_{F}. \]
This leads to the Weiszfeld-type algorithm for computing the MCM, which can be described step by step as follows:
Input:
Repeat until convergence or maximum iterations:
Stop when \(\|V^{(k)} - V^{(k-1)}\|_F < \varepsilon\) or \(k > T_{\max}\)
Return the final estimate \(V^{(k)}\)
Contrary to the case of the geometric median, there is currently no theoretical guarantee ensuring the convergence of the Weiszfeld algorithm for estimating the Median Covariation Matrix (MCM) when the true median \(m\) is unknown and replaced by an estimate \(\hat{m}\).
The method is implemented in the function WeiszfeldMedianCovariance,
which accepts the following arguments:
X: A matrix of size \(N \times p\) representing the data, where
each row is an observation.median_est: An estimate of the median, defaulting to
WeiszfeldMedian(X)$median.init_cov: Initial value for the MCM, defaulting to the identity
matrix.weights: A vector of size \(N\) containing the weights, defaulting
to ones.epsilon: Convergence threshold for the stopping criterion,
default is 1e-08.nitermax: Maximum number of iterations, default is 100.It returns the estimated MCM.
N <- 1000
p <- 4
X <- matrix(rnorm(N * p), ncol = p)
# Compute Weiszfeld geometric median
med_est <- STARRS::WeiszfeldMedian(X)
# Compute the associated Median Covariation Matrix
res <- STARRS::WeiszfeldMedianCovariance(X, median_est = med_est)
res
#> [,1] [,2] [,3] [,4]
#> [1,] 0.655917843 0.015474195 -0.007667449 0.006937508
#> [2,] 0.015474195 0.600407263 0.023613777 -0.006419356
#> [3,] -0.007667449 0.023613777 0.642708489 -0.013824633
#> [4,] 0.006937508 -0.006419356 -0.013824633 0.646918819As in the case of the geometric median, one can estimate the Median Covariation Matrix (MCM) using a stochastic gradient algorithm and its averaged version Cardot and Godichon-Baggioni (2015). Since the true median is typically unknown in practice, it must be replaced by an estimate, obtained, for example, using WeiszfeldMedian() or ASGMedian(). This leads to the following procedure.
The algorithm is implemented in the function ASGMedianCovariance,
which accepts the following arguments:
X: A matrix of size \(N \times p\) representing the data, where
each row is an observation.median_est: An estimate of the median.init_cov: Initial value of the MCM, by default set to the
identity matrix.weights: A vector of size \(N\) of weights, defaulting to 1.gamma: Step size constant (default: 2).alpha: Step size decay rate (default: 0.75).nstart: Number of random starts (default: 1).epsilon: Convergence threshold (default: 1e-8).nitermax: Maximum number of iterations (default: 100).It returns the estimated median covariation matrix.
N <- 1000
p <- 4
X <- matrix(rnorm(N * p), ncol = p)
# Compute the median using ASGMedian
med_est <- STARRS::ASGMedian(X)
# Compute the associated Median Covariation Matrix
res <- STARRS::ASGMedianCovariance(X, median_est = med_est)
res
#> [,1] [,2] [,3] [,4]
#> [1,] 0.63325907 -0.01156667 -0.02505632 0.00840683
#> [2,] -0.01156667 0.66386213 0.05937059 -0.03094419
#> [3,] -0.02505632 0.05937059 0.67344639 0.01261885
#> [4,] 0.00840683 -0.03094419 0.01261885 0.67988928We consider a Gaussian random vector \(X \sim \mathcal{N}(0, I_p)\) with \(p = 100\). We generate 100 samples of sizes 100, 1000, 2000, 5000, and 10000, and compute both the squared error of estimation and the computation time for each method (Weiszfeld given by WeiszfeldMedianCovariance() and ASGD given by ASGMedianCovariance()).
As shown in the figure beside, while the Weiszfeld algorithm performs slightly better than ASGD in terms of estimation error, it is considerably more computationally expensive (see also Cardot and Godichon-Baggioni (2015)).