({{, (RefLink[SpatialMedian, paclet:ref/SpatialMedian])[{(Subscript[x, 1]),(Subscript[x, 2]),…}]
gives the spatial median of the elements (Subscript[x, i]).}, {, (RefLink[SpatialMedian, paclet:ref/SpatialMedian])[data] 
gives the spatial median for several different forms of data. }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is also known as geometric median, median center and 1-median.

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is a robust location measure. It gives a point with minimum total distance to every point in the data.

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is equivalent to (SuperStar[x])=ArgMin[(∑_(i=1)^n)d(x,(Subscript[x, i])),x] for the unweighted case and (SuperStar[x])=ArgMin[(∑_(i=1)^n)(Subscript[w, i]) d(x,(Subscript[x, i])),x] for the weighted case.

The data data has the following forms and interpretations:

Under Euclidean distance, (RefLink[SpatialMedian, paclet:ref/SpatialMedian]) coincides with median in the one-dimensional case. In the multidimensional case, spatial median is unique whenever the points are not collinear.

The following options can be given:

By default, the following distance functions are used for different types of elements:

The setting (RefLink[Method, paclet:ref/Method])->{"InitialPoint"->{(Subscript[x, 0]),(Subscript[y, 0]),…}} allows for a custom initial point for computing (RefLink[SpatialMedian, paclet:ref/SpatialMedian]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (25)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Find the spatial median of a list of vectors:

SpatialMedian[{{1.,3.,5.},{7.,1.,2.},{9.,3.,1.},{4.,5.,6.}}]

Find the spatial median of a list of vectors with given weights:

SpatialMedian[WeightedData[{{1.,3.,5.},{-4.,1.,2.},{3.,3.,1.},{4.,5.,6.}},{1.,3.,4.,5.}]]

Compute the spatial median of geo locations:

cities={(Entity["Cambridge", Entity["City",{"Cambridge","Cambridgeshire","UnitedKingdom"}], "Entity[\"City\", {\"Cambridge\", \"Cambridgeshire\", \"UnitedKingdom\"}]", "city"]),(Entity["Oxford", Entity["City",{"Oxford","Oxfordshire","UnitedKingdom"}], "Entity[\"City\", {\"Oxford\", \"Oxfordshire\", \"UnitedKingdom\"}]", "city"]),(Entity["London", Entity["City",{"London","GreaterLondon","UnitedKingdom"}], "Entity[\"City\", {\"London\", \"GreaterLondon\", \"UnitedKingdom\"}]", "city"])};

sm=SpatialMedian[GeoPosition[cities]]

GeoGraphics[{GeoMarker[cities,Blue],GeoMarker[sm,Red]}]

OpenersExampleSection1(Spacer1)Scope  (5)

Same inputs with different precisions:

data={{1,2,3,4},{8,7,6,5},{9,4,1,3}};

SpatialMedian[N[data]]

SpatialMedian[N[data,30]]

Spatial median works with (RefLink[WeightedData, paclet:ref/WeightedData]):

wd=WeightedData[{{1.,3.},{-4.,2.},{3.,1.},{5.,6.}},{1.,3.,4.,5.}];

SpatialMedian[wd]

Spatial median of a large array:

data=RandomReal[1,{10^6,3}];

SpatialMedian[data]

Weighted spatial median:

weights=RandomReal[1,10^6];

SpatialMedian[WeightedData[data,weights]]

Spatial median of data involving quantities:

data=Quantity[RandomReal[1,{6,2}],"Meters"]

SpatialMedian[data]

Compute the spatial median of geodetic positions:

cities={(Entity["Paris", Entity["City",{"Paris","IleDeFrance","France"}], "Entity[\"City\", {\"Paris\", \"IleDeFrance\", \"France\"}]", "city"]),(Entity["Sydney", Entity["City",{"Sydney","NewSouthWales","Australia"}], "Entity[\"City\", {\"Sydney\", \"NewSouthWales\", \"Australia\"}]", "city"]),(Entity["Boston", Entity["City",{"Boston","Massachusetts","UnitedStates"}], "Entity[\"City\", {\"Boston\", \"Massachusetts\", \"UnitedStates\"}]", "city"]),(Entity["San Francisco", Entity["City",{"SanFrancisco","California","UnitedStates"}], "Entity[\"City\", {\"SanFrancisco\", \"California\", \"UnitedStates\"}]", "city"]),(Entity["Tokyo", Entity["City",{"Tokyo","Tokyo","Japan"}], "Entity[\"City\", {\"Tokyo\", \"Tokyo\", \"Japan\"}]", "city"])};

median=SpatialMedian[GeoPosition[cities]]

GeoGraphics[{GeoMarker[cities,Blue],GeoMarker[median,Red]},GeoProjection->{"AzimuthalEquidistant","Centering"->median}]

OpenersExampleSection2(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)DistanceFunction  (2)

By default, (RefLink[EuclideanDistance, paclet:ref/EuclideanDistance]) is used for numerical data:

data={{1.5,3.},{2.6,4.},{9.2,5.},{7.3,-5.}};

SpatialMedian[data]

The (RefLink[ChessboardDistance, paclet:ref/ChessboardDistance]) only takes into account the dimension with the largest separation:

SpatialMedian[data,DistanceFunction->ChessboardDistance]

(RefLink[DistanceFunction, paclet:ref/DistanceFunction]) can be given as a symbol:

dist[{u_,v_}, {x_,y_}] := Sqrt[3(u-x)^2+2(v-y)^2]

data={{1.5,.6},{2,0},{1.25,1.25},{3.,0.3}};

SpatialMedian[data,DistanceFunction->dist]

Or as a pure function:

dist1=Sqrt[{3,2}.(#1-#2)^2]&;

SpatialMedian[data,DistanceFunction->dist1]

OpenersExampleSubsection1(Spacer1)Method  (2)

Specify the initial point for the iterative procedure of spatial median:

data={{-0.5835,-0.6525},{-0.487,0.4963},{-0.4353,-0.319},{-0.01874,0.07936},{0.466,-0.7498}};

SpatialMedian[data,Method->{"InitialPoint"->{0.,0.}}]

"NMinimize" and the method options of (RefLink[FindMinimum, paclet:ref/FindMinimum]) can be used:

data={{1.5,3.},{2.6,4.},{9.2,5.},{7.3,-5.}};

SpatialMedian[data,Method->"NMinimize"]

Use with specified (RefLink[DistanceFunction, paclet:ref/DistanceFunction]):

SpatialMedian[data,DistanceFunction->BrayCurtisDistance,Method->"NMinimize"]

SpatialMedian[data,DistanceFunction->BrayCurtisDistance,Method->{"NMinimize",Method->"RandomSearch"}]

SpatialMedian[data,DistanceFunction->BrayCurtisDistance,Method->"PrincipalAxis"]

OpenersExampleSection3(Spacer1)Applications  (8)

Obtain a robust estimate of a multivariate location when outliers are present:

data={{3.,-5.},{2.,-5.},{0.,2.},{-4.,-3.},{10^8,-1.},{8.,-20000.}};

SpatialMedian[data]

Extreme values have a large influence on the (RefLink[Mean, paclet:ref/Mean]):

Mean[data]

Consider data from a Gaussian mixture distribution:

dist=MixtureDistribution[{0.98,0.02},{BinormalDistribution[0],BinormalDistribution[{0,0},{1000,1000},0.5]}];

BlockRandom[SeedRandom[1];
data=RandomVariate[dist,10^3];]

Histogram3D[data,PlotTheme->"Detailed"]

Estimate the center with (RefLink[Mean, paclet:ref/Mean]):

Mean[data]

The sample mean estimator has a large spread for non-Gaussian data. The standard deviation of the estimator is:

StandardDeviation[dist]/Sqrt[Length[data]]

Estimate the center with (RefLink[SpatialMedian, paclet:ref/SpatialMedian]):

SpatialMedian[data]

Assess the spread via bootstrapping. The spatial median has a smaller spread compared to the mean:

StandardDeviation[Table[SpatialMedian[RandomChoice[data,10^3]],{10^3}]]

Consider the stock prices of five companies: GOOG, MSFT, FB, AAPL and INTC in 2015 as five-dimensional data:

prices=TimeSeries[];

prices["ValueDimensions"]

DateListPlot[prices,PlotLegends->{"GOOG","MSFT","FB","AAPL","INTC"}]

Compute the log returns and estimate the center using (RefLink[Mean, paclet:ref/Mean]) and (RefLink[SpatialMedian, paclet:ref/SpatialMedian]):

logreturns=Differences[Log[prices["Values"]]];

mean=Mean[logreturns]

smed=SpatialMedian[logreturns]

Fit the data with (RefLink[MultivariateTDistribution, paclet:ref/MultivariateTDistribution]) and extract the location parameters:

edist=EstimatedDistribution[logreturns,MultivariateTDistribution[Array[x,5],Array[s,{5,5}],nu]];

locs=First[edist]

Spatial median estimator gives a closer estimate to the location parameters of multivariate t distribution than the empirical mean with the given stock data:

EuclideanDistance[locs,mean]>EuclideanDistance[locs,smed]

With the number of points equal to 3, spatial median is also the Fermat point:

data={{5,-1},{1,3},{-3,-2}};
fermatpoint=SpatialMedian[data]

Create equilateral triangles on each side:

pts=data+(Differences[Append[data,First[data]]].RotationMatrix[Pi/3]);
triangles=Triangle/@MapThread[Append,{Delete[data,#]&/@{3,1,2},pts}];

Construct the Fermat point geometrically and compare it with the result of (RefLink[SpatialMedian, paclet:ref/SpatialMedian]) (red):

Graphics[{Yellow,Triangle[data],GrayLevel[.75],triangles,Black,Line/@Transpose[{pts,data[[{3,1,2}]]}],Black,PointSize[Large],Point[Join[data,pts]],PointSize[0.03],Red,Point[fermatpoint]}]

Sample points from a convex polygon:

poly=Polygon[{{2.,-5.},{0.,2.},{-4.,-3.},{-5,-7}}];
pts=RandomPoint[poly,10^3];

Graphics[{Lighter[Blue,.9],poly,Black,PointSize[Small],Point[pts]}]

Estimate the center of the polygon by computing the spatial median of random points:

smed=SpatialMedian[pts]

Graphics[{Lighter[Blue,.9],poly,Black,PointSize[Small],Point[pts],PointSize[0.06],Red,Point[smed]}]

Find the spatial median of California based on the locations of cities:

cities=CityData[{All,"California"}];
locations=GeoPosition[CityData[#,"Coordinates"]&/@cities];

unweighted=SpatialMedian[locations]

Find the spatial median of California based on the locations of cities, weighted by population:

populations=CityData[#,"Population"]&/@cities;

weighted=SpatialMedian[WeightedData[locations,populations]]

Draw the city locations (gray), unweighted spatial median (red) and weighted spatial median (black):

legend=PointLegend[{Red,Black},{"unweighted spatial median","spatial median weighted by population"},LegendMarkerSize->Large];

Legended[GeoGraphics[{EdgeForm[Black],FaceForm[Orange],Polygon[(Entity["California, United States", Entity["AdministrativeDivision",{"California","UnitedStates"}], "Entity[\"AdministrativeDivision\", {\"California\", \"UnitedStates\"}]", "administrative division"])],GrayLevel[.4],PointSize[0.0075],Point[locations],Red,PointSize[0.04],Point[unweighted],Black,Point[weighted]},GeoRange->{{32,43},{-125,-114}},ImageSize->350],Placed[legend,"Bottom"]]

For geo locations that are far enough apart on the surface of the Earth, the spatial median depends significantly on the choice of the distance function:

cities = {(Entity["Champaign", Entity["City",{"Champaign","Illinois","UnitedStates"}], "Entity[\"City\", {\"Champaign\", \"Illinois\", \"UnitedStates\"}]", "city"]),(Entity["Boston", Entity["City",{"Boston","Massachusetts","UnitedStates"}], "Entity[\"City\", {\"Boston\", \"Massachusetts\", \"UnitedStates\"}]", "city"]),(Entity["Oxford", Entity["City",{"Oxford","Oxfordshire","UnitedKingdom"}], "Entity[\"City\", {\"Oxford\", \"Oxfordshire\", \"UnitedKingdom\"}]", "city"]),(Entity["Tokyo", Entity["City",{"Tokyo","Tokyo","Japan"}], "Entity[\"City\", {\"Tokyo\", \"Tokyo\", \"Japan\"}]", "city"]),(Entity["Linköping", Entity["City",{"Linkoping","Ostergotland","Sweden"}], "Entity[\"City\", {\"Linkoping\", \"Ostergotland\", \"Sweden\"}]", "city"]),(Entity["Lima", Entity["City",{"Lima","Lima","Peru"}], "Entity[\"City\", {\"Lima\", \"Lima\", \"Peru\"}]", "city"]),(Entity["Bengaluru", Entity["City",{"Bengaluru","Karnataka","India"}], "Entity[\"City\", {\"Bengaluru\", \"Karnataka\", \"India\"}]", "city"])};

geopos=GeoPosition[cities]

The spatial median under (RefLink[GeoPosition, paclet:ref/GeoPosition]):

geoMedian=SpatialMedian[geopos]

The spatial median of the projected coordinates under (RefLink[EuclideanDistance, paclet:ref/EuclideanDistance]):

euclidMedian=SpatialMedian[QuantityMagnitude@LatitudeLongitude[geopos]]

Show the locations of the spatial medians and the cities:

legend=SwatchLegend[{Gray,Red,Blue},{"Cities","SpatialMedian","EuclideanMedian"}];

Legended[GeoGraphics[MapThread[GeoMarker[#1,#2]&,{{geopos,geoMedian,euclidMedian},{Gray,Red,Blue}}]],legend]

Centroids of geographic entities can be approximated by the spatial median of the uniformly sampled geo locations. Obtain the country polygon of Spain:

region=DiscretizeGraphics[CountryData["Spain","Polygon"]]

Sample points from the region and compute the corresponding spatial median:

points=Reverse[RandomPoint[region,10000],{2}];

med=SpatialMedian[GeoPosition[points]]

Find the closest city from the spatial median:

centralcity=GeoNearest["City",med]//First

Visualize the results:

GeoGraphics[{Polygon[(Entity["Spain", Entity["Country","Spain"], "Entity[\"Country\", \"Spain\"]", "country"])],GeoMarker[med,Green],GeoMarker[centralcity,Red]},GeoProjection->"Mercator"]

OpenersExampleSection4(Spacer1)Properties & Relations  (5)

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is a multivariate location measure:

pts={{3,-5},{2,-5},{0,2},{-4,-3},{-3,2},{-5,-5},{-1,-5},{-1,-5},{5,-1},{-3,3},{10,-1},{8,-2}};

Graphics[{PointSize[0.03],Black,Point[pts]}]

Compute the spatial median:

spmed=SpatialMedian[pts]

(RefLink[Mean, paclet:ref/Mean]) is also a location measure:

mean=Mean[N@pts]

Visualize the data points with spatial median and mean:

Graphics[{PointSize[0.03],Black,Point[pts],Red,PointSize[0.04],Point[spmed],Blue,Point[mean],Black,Text["Spatial Median",spmed-{0,.8}],Text["Mean",mean+{0,.8}]},ImageSize->250]

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is the (Subscript[L, 1]) location estimator of spatial points:

data={{1.,3.},{4.,6.},{9.,2.},{5.,5.},{7.,-2.}};

spmed=SpatialMedian[data]

Compute (RefLink[SpatialMedian, paclet:ref/SpatialMedian]) from the definition with (RefLink[FindMinimum, paclet:ref/FindMinimum]):

sumofdists[x_,y_]:=Total[Sqrt[Total[(Transpose[data]-{x,y})^2]]]

Quiet@FindMinimum[sumofdists[x,y],{{x,5},{y,5}}]

Visualize the sum of distances function:

ContourPlot[sumofdists[x,y],{x,0,10},{y,-3,7},PlotLegends->Automatic,Contours->10,Epilog->{PointSize[Large],Black,Point[data],Red,PointSize[Large],Point[spmed],White,Text["Spatial Median",spmed+{0,-.7}]}]

(RefLink[Mean, paclet:ref/Mean]) (or spatial mean) is the (Subscript[L, 2]) location estimator of spatial points:

mean=Mean[data]

Compute (RefLink[Mean, paclet:ref/Mean]) from the definition with (RefLink[FindMinimum, paclet:ref/FindMinimum]):

sumofsquare[x_,y_]:=Total[Total[(Transpose[data]-{x,y})^2]]

Quiet@FindMinimum[sumofsquare[x,y],{{x,5},{y,5}}]

Visualize the sum of distances function:

ContourPlot[sumofsquare[x,y],{x,0,10},{y,-3,7},PlotLegends->Automatic,Contours->10,Epilog->{PointSize[Large],Black,Point[data],Red,PointSize[Large],Point[mean],White,Text["Mean",mean+{0,-.7}]}]

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) is the same as (RefLink[Median, paclet:ref/Median]) for univariate data:

data={1,10,3,5,6,-4};
SpatialMedian[data]==Median[data]

data=WeightedData[{1,10,3,5,6,-4},{1,2,3,4,5,6}];
SpatialMedian[data]==Median[data]

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) under (RefLink[ManhattanDistance, paclet:ref/ManhattanDistance]) for multivariate data is the same as (RefLink[Median, paclet:ref/Median]):

data={{1.5,3.},{2.6,4.},{9.2,5.},{7.3,-5.}};

SpatialMedian[data,DistanceFunction->ManhattanDistance]

Median[data]

(RefLink[SpatialMedian, paclet:ref/SpatialMedian]) finds a point in the domain that minimizes the sum of distances:

data=RandomVariate[NormalDistribution[],{10,3}];

MemberQ[data,SpatialMedian[data]]

(RefLink[CentralFeature, paclet:ref/CentralFeature]) finds a point that belongs to the data that minimizes the sum of distances:

MemberQ[data,CentralFeature[data]]

The sum of distances with respect to (RefLink[CentralFeature, paclet:ref/CentralFeature]) is greater than or equal to the one with respect to (RefLink[SpatialMedian, paclet:ref/SpatialMedian]):

sumofdists=Total[Sqrt[Total[(Transpose[data]-#)^2]]]&;

sumofdists[CentralFeature[data]]≥sumofdists[SpatialMedian[data]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Mean, paclet:ref/Mean])  ▪  (RefLink[Median, paclet:ref/Median])  ▪  (RefLink[TrimmedMean, paclet:ref/TrimmedMean])  ▪  (RefLink[WinsorizedMean, paclet:ref/WinsorizedMean])  ▪  (RefLink[BiweightLocation, paclet:ref/BiweightLocation])  ▪  (RefLink[CentralFeature, paclet:ref/CentralFeature])  ▪  (RefLink[DistanceMatrix, paclet:ref/DistanceMatrix])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Descriptive Statistics, paclet:guide/DescriptiveStatistics])}, {▪(RefLinkPlain[Robust Descriptive Statistics, paclet:guide/RobustDescriptiveStatistics])}, {▪(RefLinkPlain[Locations, Paths, and Routing, paclet:guide/LocationsPathsAndRouting])}})}})