weiszfeld: Weiszfeld Algorithm for Computing L1-median

View source: R/weiszfeld.R

weiszfeldR Documentation

Weiszfeld Algorithm for Computing L1-median

Description

Geometric median, also known as L1-median, is a solution to the following problem

\textrm{argmin} \sum_{i=1}^n \| x_i - y \|_2

for a given data x_1,x_2,\ldots,x_n \in R^p.

Usage

weiszfeld(X, weights = NULL, maxiter = 496, abstol = 1e-06)

Arguments

X

an (n\times p) matrix for p-dimensional signal. If vector is given, it is assumed that p=1.

weights

NULL for equal weight rep(1/n,n); otherwise, it has to be a vector of length n.

maxiter

maximum number of iterations.

abstol

stopping criterion

Examples

## generate sin(x) data with noise for 100 replicates
set.seed(496)
t = seq(from=0,to=10,length.out=20)
X = array(0,c(100,20))
for (i in 1:100){
   X[i,] = sin(t) + stats::rnorm(20, sd=0.5)
}

## compute L1-median and L2-mean
vecL2 = base::colMeans(X)
vecL1 = weiszfeld(X)

## visualize
opar <- par(no.readonly=TRUE)
par(mfrow=c(1,3), pty="s")
matplot(t(X[1:5,]), type="l", main="5 generated data", ylim=c(-2,2))
plot(t, vecL2, type="l", col="blue", main="L2-mean",   ylim=c(-2,2))
plot(t, vecL1, type="l", col="red",  main="L1-median", ylim=c(-2,2))
par(opar)


maotai documentation built on March 31, 2023, 6:48 p.m.