weiszfeld: Weiszfeld Algorithm for Computing L1-median

Description Usage Arguments Examples

View source: R/weiszfeld.R

Description

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

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

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

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## 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 Oct. 25, 2021, 9:06 a.m.