I have a set of (floating point) numbers between x = 1 and y = 9:
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
How can I compute a metric of the proximity, in the given interval, between number A and number B?
What I’ve Tried
If the amplitude (max - min) of the above set is 9 - 1 = 8 I am able to compute the relative value of any number using the formula (n - min) / (max - min), computing this for all values yields:
(1 - 1) / (9 - 1) = 0(2 - 1) / (9 - 1) = 0.125(3 - 1) / (9 - 1) = 0.25(4 - 1) / (9 - 1) = 0.375(5 - 1) / (9 - 1) = 0.5(6 - 1) / (9 - 1) = 0.625(7 - 1) / (9 - 1) = 0.75(8 - 1) / (9 - 1) = 0.875(9 - 1) / (9 - 1) = 1
Dividing the minimum relative value (between A and B) with the maximum relative value (also between A and B), seems to produce the kind of metric I’m looking for. Here are a few examples:
var_dump(min(0.875, 0.25) / max(0.875, 0.25)); // 0.286 between 8 and 3
var_dump(min(0.875, 0.375) / max(0.875, 0.375)); // 0.429 between 8 and 4
var_dump(min(0.875, 0.75) / max(0.875, 0.75)); // 0.857 between 8 and 7
var_dump(min(0.875, 0.875) / max(0.875, 0.875)); // 1 between 8 and 8
var_dump(min(0.25, 0.25) / max(0.25, 0.25)); // 1 between 3 and 3
The Problem
Whenever the minimum value of the set comes into play, the result will always be 0:
var_dump(min(0.875, 0) / max(0.875, 0)); // 0 between 8 and 1
var_dump(min(0.125, 0) / max(0.125, 0)); // 0 between 2 and 1
var_dump(min(0, 0) / max(0, 0)); // 0 between 1 and 1 (ERR!)
Any ideas on how to solve this?
I was suggesting something like this:
The proximity between 1 and 2 is the same as 2 and 3. This seems to make sense.
The largest proximity you’ll get is when the numbers you specify are the bounds of your predefined range.
The smallest proximity you’ll get is when the numbers you specify are equal.
If you want the opposite to be true (which I suppose better reflects proximity, you could do:
Which would output:
Now, the same number produces 1 as you specified and the bounds produce 0 as they’re the least-proximate pair of values. Combinations like (1,2), (2,3), (3,4), etc. all produce the same value, as do combinations like (2,4), (3,5), (4,6), etc, etc.