15
$\begingroup$

Bug introduced in 10 and fixed in 10.3

There still persists the failure to return 30.72 (another bug according to Daniel Lichtblau but from the developer's point of view it may be a feature).


Nearest[data,x,{n,r}] is supposed to give the n or fewer nearest neighbors to x that are within radius r of x. Example:

Nearest[{1, 2, 3, 4, 5, 6}, 3.6, {10, 1.5}] (* gives {4, 3, 5} *)
Nearest[{1, 2, 3, 4, 5, 6}, 3, {10, 2}] (* gives {3, 2, 4, 1, 5}, so it's inclusive *)

But this is not working for my data:

v = {10.38,17.77,21.25,20.38,14.34,15.7,19.98,20.83,21.82,24.04,23.24,17.89,24.8,23.95,22.61,27.54,20.13,20.68,22.15,14.36,15.71,12.44,14.26,23.04,21.38,16.4,21.53,20.25,25.27,15.05,25.11,18.7,23.98,26.47,17.88,21.59,21.72,18.42,25.2,20.82,21.58,21.35,24.81,20.28,21.81,17.6,16.84,18.66,14.63,22.3,21.6,16.34,18.24,18.7,22.02,18.75,18.57,21.59,19.31,11.79,14.88,20.98,22.15,13.86,23.84,23.94,21.01,19.04,17.33,16.49,21.31,14.64,24.52,15.79,16.52,19.65,10.94,16.15,23.97,18,20.97,15.86,24.91,26.29,15.65,18.52,21.46,24.59,21.8,15.24,24.02,22.76,14.76,18.3,19.83,23.03,17.84,19.94,12.84,19.77,24.98,13.43,20.52,19.4,19.29,15.56,18.33,18.54,19.67,21.26,16.99,20.76,19.65,20.19,15.83,21.53,15.76,16.67,22.91,20.01,10.82,17.12,20.2,10.89,16.39,17.21,24.69,18.91,16.39,25.12,13.29,19.48,21.54,13.93,21.91,22.47,16.67,15.39,17.57,13.39,11.97,18.05,17.31,15.92,14.97,14.65,16.58,18.77,15.18,17.91,20.78,20.7,15.34,13.08,15.34,17.94,20.74,19.46,12.74,12.96,20.18,15.94,18.15,22.22,22.04,19.76,9.71,18.8,24.68,16.95,12.39,19.63,11.89,14.71,15.15,14.45,18.06,20.11,22.22,13.06,21.87,26.57,20.31,14.92,22.41,15.11,18.58,17.19,17.39,15.9,23.12,21.41,18.22,26.86,23.21,16.33,22.29,21.84,22.49,20.22,19.56,19.32,26.67,23.75,18.6,16.68,17.27,20.26,22.54,12.91,22.14,18.94,18.47,25.56,23.81,16.93,18.35,17.48,21.56,32.47,13.16,13.9,17.53,20.25,17.02,13.47,15.46,15.51,23.97,22.33,19.08,27.08,33.81,27.81,15.91,21.25,26.97,21.46,27.85,39.28,15.6,15.04,18.19,23.77,23.5,19.86,17.43,14.11,25.22,14.93,23.56,18.45,19.82,17.08,19.33,17.05,28.77,17.27,23.2,33.56,27.06,23.06,22.13,19.38,22.07,31.12,18.95,21.84,16.21,20.39,16.82,13.04,20.99,15.67,24.48,17.36,14.16,19.98,17.84,15.18,26.6,14.02,18.18,18.77,15.7,18.4,20.76,13.12,19.96,18.89,19.73,19.1,16.02,17.46,13.78,13.27,12.35,18.14,18.17,23.09,18.9,19.89,23.86,18.61,18.16,24.49,15.82,14.4,12.71,13.84,19.11,15.69,13.37,10.72,18.6,16.85,14.08,18.87,18.9,17,16.18,19.66,13.32,21.51,15.21,17.3,12.88,17.93,20.71,21.88,15.51,19.35,19.86,14.78,19.02,21,14.23,21.43,17.53,24.27,16.54,16.84,14.96,21.68,15.45,14.71,18.9,14.74,16.03,14.96,17.07,19.22,17.46,25.74,14.07,19.07,18.59,16.21,15.49,18.32,18.07,21.57,18.84,18.29,16.95,21.78,26.83,18.02,17.25,21.9,23.29,13.21,15.1,17.35,16.07,16.07,20.22,28.21,15.15,18.83,12.96,14.93,22.72,17.48,13.72,23.29,14.09,16.16,15.5,23.21,12.22,16.84,19.97,22.28,17.72,17.18,18.89,17.46,14.83,17.26,21.02,10.91,18.29,16.17,14.95,18.59,14.86,21.37,20.66,17.92,17.57,16.83,21.68,22.11,29.81,21.17,21.7,21.08,12.17,21.41,19.04,13.98,16.02,19.13,19.12,21.28,14.98,21.98,16.62,17.67,22.53,17.68,19.54,21.97,16.94,19.62,19.54,15.98,19.6,15.66,17.2,25.42,15.79,18.32,16.85,24.89,28.03,17.66,19.34,20.52,21.54,25,28.23,13.98,17.15,30.72,29.29,25.25,25.13,28.2,27.15,26.27,26.99,18.36,18.22,20.13,20.74,18.1,23.33,18.18,18.49,28.14,14.93,29.97,15.62,15.73,20.53,16.62,14.59,19.51,18.03,19.24,14.06,17.64,11.28,16.41,16.85,18.82,16.17,20.2,22.44,13.23,20.56,12.83,20.54,20.21,18.17,17.31,17.52,21.24,16.74,24.49,16.32,19.83,12.87,13.14,20.04,17.12,15.7,23.95,14.69,14.7,20.52,13.66,19.07,18.61,20.58,20.26,18.22,16.7,13.9,21.6,19.83,18.68,15.68,13.1,18.75,12.27,13.17,13.44,17.56,20.02,16.33,20.67,17.62,20.86,22.55,24.44,25.49,25.44,14.44,24.99,25.42,28.06,20.7,23.23,16.35,16.58,19.34,24.21,21.48,22.44,29.43,21.94,28.92,27.61,19.59,27.88,22.68,23.93,27.15,29.37,30.62,25.09,22.39,28.25,28.08,29.33,24.54}

Nearest[v, 39.28, 6]
(* gives {39.28, 33.81, 33.56, 32.47, 31.12, 30.72}, which is fine *)

Nearest[v, 39.28, {6, 8.56}]
(* should have given the same answer because 39.28 - 30.72 = 8.56,
   but gives {39.28} instead *)

Nearest[v, 39.28, {6, 9}]
(* same as above, which is even stranger *)

I cannot explain this behavior. Any ideas?

Mathematica version: 10.2.0.0

$\endgroup$
  • 1
    $\begingroup$ Your last code kills the kernel when I try to run it. $\endgroup$ – march Sep 22 '15 at 17:01
  • 1
    $\begingroup$ @march does not crash for me on v10.2 $\endgroup$ – IPoiler Sep 22 '15 at 17:12
  • 2
    $\begingroup$ This bug has been fixed in the development version. As others have pointed out, the 8.56 radius will only return 5 nearest points because 39.28 - 30.72 // InputForm is 8.560000000000002. $\endgroup$ – ilian Sep 22 '15 at 18:38
  • 5
    $\begingroup$ The failure to return 30.72 is, in my view, a bug. Reported as such. The developer can always decide it's a feature. In exact arithmetic the error in the last place would not be present, and Nearest probably should not exclude without some allowance for a small number of ULPs. That at least is my opinion. $\endgroup$ – Daniel Lichtblau Sep 22 '15 at 18:48
  • 1
    $\begingroup$ Try this data: v2 = RandomSample[v, 10]~Join~Nearest[v, 39.28, 6] // RandomSample on your three Nearest. $\endgroup$ – Silvia Sep 23 '15 at 1:37
8
$\begingroup$

Specifying the DistanceFunction seems to fix it.

Nearest[v, 39.28, {All, 8.57}, DistanceFunction -> (Norm[#1 - #2] &)]

{39.28, 33.81, 33.56, 32.47, 31.12, 30.72}

Note that the radius had to be changed also because Nearest will return points whose distance are strictly less than the radius.

$\endgroup$
  • $\begingroup$ Thanks. But the comment on the radius is not true: Nearest[{1, 2, 3, 4, 5, 6}, 3, {10, 2}] will gives {3, 2, 4, 1, 5}. $\endgroup$ – ziyuang Sep 22 '15 at 18:08
  • $\begingroup$ @ziyuang Hmm that's odd. Odder still is the fact thatNearest[{1.1, 2.1, 3.1, 4.1, 5.1, 6}, 3.1, {10, 2}] returns {3.1, 4.1, 2.1} but Nearest[{1.1, 2.1, 3.1, 4.1, 5.1, 6}, 3.1, {10, 2}, DistanceFunction -> (Norm[#1 - #2] &)] returns {3.1, 4.1, 2.1, 5.1, 1.1}. Adjusting WorkingPrecision also seems to have no effect on what the former returns. $\endgroup$ – IPoiler Sep 22 '15 at 18:18
  • $\begingroup$ But for integer inputs it seems correct: Nearest[Range[6], 3, {10, 2}] == Nearest[Range[6], 3, {10, 2}, DistanceFunction -> (Norm[#1 - #2] &)] (* True *) $\endgroup$ – ziyuang Sep 22 '15 at 18:22
5
$\begingroup$

The problem seems to be because of the data v contains Integer numbers and real numbers.

check this

v2=DeleteCases[v, _Integer];
Nearest[v2, 39.28, {6, 8.56}]
(*{39.28, 33.81, 33.56, 32.47, 31.12}*)

Nearest[N[v], 39.28, {6, 8.56}]   (*@ilian*)
(*{39.28, 33.81, 33.56, 32.47, 31.12}*)
$\endgroup$
  • 1
    $\begingroup$ Could also use N[v] instead of v2. $\endgroup$ – ilian Sep 22 '15 at 18:36
  • $\begingroup$ yes that can be done. $\endgroup$ – Algohi Sep 22 '15 at 18:38
  • $\begingroup$ Both seem to be missing 30.72. $\endgroup$ – Daniel Lichtblau Sep 22 '15 at 20:27
  • $\begingroup$ Than is because of precision. check this Nearest[N[v], 39.28, {6, 8.561}] $\endgroup$ – Algohi Sep 22 '15 at 20:29
  • $\begingroup$ Good observation! I guess Mathematica get confused when seeing Integer, which cause a distance-function not able to deal with Real being chosen. $\endgroup$ – Silvia Sep 23 '15 at 2:22
4
$\begingroup$

It's a rounding problem. Try (Working on V9):

Nearest[Rationalize@v, Rationalize@39.28, {6, Rationalize@8.56}] // N
(*
  {39.28, 33.81, 33.56, 32.47, 31.12, 30.72}
*)
$\endgroup$
  • $\begingroup$ This only returns {39.28} for me in v10.2. $\endgroup$ – IPoiler Sep 22 '15 at 17:24
  • 1
    $\begingroup$ @It'sPronouncedOiler Surely a version bug! $\endgroup$ – Dr. belisarius Sep 22 '15 at 17:32
  • 1
    $\begingroup$ Actually Nearest[v, 39.28, {6, 9}] gives the same result ({39.28}) on 10.2, so it may not a rounding problem. $\endgroup$ – ziyuang Sep 22 '15 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.