Nearest neighbor using chi-square distance.
This is a chi-square implementation for nearest neighbor. More information can be found here.
"The chi-square distance is special because it is at the heart of correspondence analysis, extensively used in ecological research. The first premise of this distance function is that it is calculated on relative counts, and not on the original ones, and the second is that it standardizes by the mean and not by the variance." 
var ChiSquare = ;
Create a new chi-square nearest neighbor instance.
vector is an an array (neighbors) of arrays (neighbor features).
k most nearest neighbors to
subject (the target neighbor).
k default is
neighbors is a sorted, (most similar first) array where each object has the following properties:
vector- the neighbor vector
d- the chi-square distance, with zero being the most similar.
var ChiSquare = ;var vectors =26 4 13 11 00 10 9 8 00 0 15 3 013 5 3 10 731 21 13 16 59 6 0 11 232 26 0 23 032 21 0 10 224 17 0 25 616 3 12 20 211 0 7 8 024 37 5 18 1;var nn = vectors;var subject = 32 26 0 23 0;var mostSimilar = 24 17 0 25 6;var neighbors = nn;var neighbor = neighbors1; // the subject itself should always be returned as the first element because the item will be most similar to itself, so we get the second elementneighbors0dshould;neighborvectorshould;
(The MIT License)
Copyright (c) 2014 Nick Poorman email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.