# How do I tell if two sets of dots are in some way similar?

I have two sets of dots (x,y coordinates) and I'd like to figure out if there's a way to find out if the second set of dots is (more or less) "close" to the first one.

I may plot these two sets in two functions, but I don't know a way to tell if they are in some way similar.

Basically, what I have to do is:

1. collect some sets of data (sets of dots) as references for future classification;
2. collect one more set of data (this one is an "unknown" set);
3. compare the last set data to the ones collected before and see if it is somehow similar to the previous ones.

What I'm missing now is the third point: what is a way to "compare" these sets of data?

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 18 '15 at 16:46
• Have a look here Scientific Data Analysis – user9660 Nov 18 '15 at 17:08
• Does the ordering matter? Would you consider Table[ {i, i}, {i, 1, 5}] and Table[ {6-i, 6-i}, {i, 1, 5}] to be similar or different? – Emilio Pisanty Nov 18 '15 at 20:07
• And do the sets contain equal number of dots? – მამუკა ჯიბლაძე Nov 18 '15 at 20:39
• This question seems to be a statistical question better suited for Cross Validated rather than this forum. The selection process associated with the data needs to be explicitly considered. Samples might be made on the same experimental unit and the repeated measures aspect would influence how to interpret and construct any measure of similarity. – JimB Nov 19 '15 at 0:09

Assuming the two sets are related by some kind of geometric transformation you could use FindGeometricTransform for this.

As a demonstration let's generate some random points:

points = RandomReal[{0, 100}, {50, 2}];


Add a bit of noise and transform to get a second set:

at = AffineTransform[{{0.9, 0.1}, {0.1, 0.9}}];
distPoints = at /@ (RandomReal[1] + # & /@ points);


Show the sets:

Graphics[
{
Blue,
Point@points,
Red,
Point@distPoints
}
]


Now find the transformation connecting the two sets:

{er, trans} = FindGeometricTransform[distPoints, points]


Transform the first set with the transformation found and see how they compare:

Graphics[
{
Blue,
Point@(trans /@ points),
Red,
Point@distPoints
}
]


The small differences that are left will be caused by the random error we added.

You could use the error term (assigned to er above) as a measure of the similarity of the two data sets (assuming that you would regard two sets that differ only by a geometric transformation as being the same).

Note that you can constrain the type of transformation that FindGeometricTranslation looks for by using the TransformationClass option.

Assume the two data sets have five points each:

data1 = Table[RandomReal[], {5}, {2}]

data2 = Table[RandomReal[], {5}, {2}]

EuclideanDistance @@ {Flatten[(Nearest @@ {data1, data2}), 1], data1}


(* 0.819504 *)

Why this works:

Flatten[(Nearest @@ {data1, data2}), 1]


finds the nearest point in data2 for each successive point in data1.

Then EuclideanDistance @@ ... finds the overall distance.

Check: EuclideanDistance @@ {Flatten[(Nearest @@ {data1, data1}), 1], data1}

(* 0 *)

• I have not tested so I'll ask. Does this enforce that each point in one set has a unique corresponding point in the other? Maybe it doesn't matter too much once you account for the differences in both directions. – Daniel Lichtblau Nov 18 '15 at 21:42
• It does not enforce one point to one point. Is that what you seek? – David G. Stork Nov 18 '15 at 22:34
• Well, if you have say four points clustered in one case, they might all be close to one particular point in the other. Meanwhile that other might have four clustered points that are near to the remaining point in the first set. Do we want to consider those as similar? My initial thought was we do not, but now I'm not so sure. – Daniel Lichtblau Nov 18 '15 at 22:46

This finds nearest matches and eliminates each pair from further consideration.

data1 = Table[RandomReal[], {100}, {2}];
data2 = RandomVariate[NormalDistribution[0, .1], 2] + # & /@
RandomSample[data1, 80];
aligndata = NestWhile[
(pair =
Position[#, Min[#]][[1]] &@
Outer[ EuclideanDistance , #[[1]], #[[2]],
1]; {Drop[#[[1]], {pair[[1]]}], Drop[#[[2]], {pair[[2]]}],
Append[#[[3]],
{#[[1, pair[[1]]]], #[[2, pair[[2]]]]}]}) &
, {data1, data2, {}}, Length@#[[1]] > 0 && Length@#[[2]] > 0 &  ];

Graphics[{Red, PointSize[.015], Point[data1], Blue, Point[data2],
Black, Line /@ aligndata[[3]]}]


I'm not sure how to quantify the correlation. Total length of lines EuclideanDistance @@ # & /@ aligndata[[3]] // Total plus some accounting for missing data?