<commit_before>var expect, spearson;

expect = require("expect.js");
spearson = require("../lib/spearson.js");

describe("Spearson", function () {

    describe("round", function () {
        it("the rounded value of 0.4281 is 0", function () {
            var res;
            res = spearson.round(0.4281);
            expect(res).to.equal(0);
        });
        it("the rounded value of 0.4281 with two decimal points is 0.43", function () {
            var res;
            res = spearson.round(0.4281, 2);
            expect(res).to.equal(0.43);
        });
    });

    describe("min", function () {
        it("the min of [7,8,1,2,5] is 1", function () {
            var res;
            res = spearson.min([7, 8, 1, 2, 5]);
            expect(res).to.equal(1);
        });
    });

    describe("max", function () {
        it("the max of [7,8,1,2,5] is 8", function () {
            var res;
            res = spearson.max([7, 8, 1, 2, 5]);
            expect(res).to.equal(8);
        });
    });

    describe("sum", function () {
        it("the sum of [1,2,3,4,5] is 15", function () {
         // Compare to MATLAB: sum([1 2 3 4 5])
         // Compare to R: sum(c(1,2,3,4,5))
            var res;
            res = spearson.sum([1, 2, 3, 4, 5]);
            expect(res).to.equal(15);
        });
    });

    describe("mean", function () {
        it("the mean of [1,2,3,4,5] is 3", function () {
         // Compare to MATLAB: mean([1 2 3 4 5])
         // Compare to R: mean(c(1,2,3,4,5))
            var res;
            res = spearson.mean([1, 2, 3, 4, 5]);
            expect(res).to.equal(3);
        });
    });

    describe("deviation", function () {
        it("the deviation of [1,2,3,4,5] is [-2,-1,0,1,2]", function () {
         // Compare to MATLAB: [1 2 3 4 5] - mean([1 2 3 4 5])
         // Compare to R: c(1,2,3,4,5) - mean(c(1,2,3,4,5))
            var res;
            res = spearson.deviation([1, 2, 3, 4, 5]);
            expect(res).to.eql([-2, -1, 0, 1, 2]);
        });
    });

    describe("variance", function () {
        it("the unbiased sample variance of [1,2,3,4,5] is 2.5", function () {
         // Compare to MATLAB: sum(([1 2 3 4 5] - mean([1 2 3 4 5])) .^ 2) / (5 - 1)
         // Compare to R: var(c(1,2,3,4,5))
            var res;
            res = spearson.variance([1, 2, 3, 4, 5]);
            expect(res).to.equal(2.5);
        });
        it("the biased sample / population variance of [1,2,3,4,5] is 2", function () {
         // Compare to MATLAB: sum(([1 2 3 4 5] - mean([1 2 3 4 5])) .^ 2) / 5
            var res;
            res = spearson.variance([1, 2, 3, 4, 5], true);
            expect(res).to.equal(2);
        });
    });

    describe("standardDeviation", function () {
        it("the unbiased sample standard deviation of [1,2,3,4,5] is 1.5811", function () {
         // Compare to MATLAB: std([1 2 3 4 5])
         // Compare to R: sd(c(1,2,3,4,5))
            var res;
            res = spearson.standardDeviation([1, 2, 3, 4, 5]);
            res = spearson.round(res, 4);
            expect(res).to.equal(1.5811);
        });
        it("the biased sample / population standard deviation of [1,2,3,4,5] is 1.4142", function () {
         // Compare to MATLAB: std([1 2 3 4 5], 1)
            var res;
            res = spearson.standardDeviation([1, 2, 3, 4, 5], true);
            res = spearson.round(res, 4);
            expect(res).to.equal(1.4142);
        });
    });

    describe("standardize", function () {
        it("the z score of [1,3,2] is [-1,1,0]", function () {
         // Compare to MATLAB: zscore([1,3,2])
            var res;
            res = spearson.standardize([1, 3, 2]);
            expect(res).to.eql([-1,1,0]);
        });
    });

    describe("rank", function () {
        it("the rank of [54,17,8,109,78] is [3,2,1,5,4]", function () {
         // Compare to MATLAB: tiedrank([54 17 8 109 78])
            var res;
            res = spearson.rank([54, 17, 8, 109, 78]);
            expect(res).to.eql([3, 2, 1, 5, 4]);
        });
    });

    describe("correlation", function () {
        describe("pearson", function () {
            it("the Pearson correlation coefficient of [1,7,2,3,4] and [9,3,5,2,1] is -0.6181", function () {
             // Compare to MATLAB: corr([1;7;2;3;4], [9;3;5;2;1])
             // Compare to R: cor(c(1,7,2,3,4), c(9,3,5,2,1))
                var res;
                res = spearson.correlation.pearson([1, 7, 2, 3, 4], [9, 3, 5, 2, 1]);
                res = spearson.round(res, 4);
                expect(res).to.equal(-0.6181);
            });
        });
        describe("spearman", function () {
            it("the Spearman correlation coefficient of [1,7,2,3,4] and [9,3,5,2,1] is -0.7", function () {
             // Compare to MATLAB: corr([1;7;2;3;4], [9;3;5;2;1], 'type', 'Spearman')
             // Compare to R: cor(x=c(1,7,2,3,4), y=c(9,3,5,2,1), method="spearman")
                var res;
                res = spearson.correlation.spearman([1, 7, 2, 3, 4], [9, 3, 5, 2, 1]);
                expect(res).to.equal(-0.7);
            });
            it("the Spearman correlation coefficient of [1,7,2,3,1] and [9,3,2,2,1] is 0.1579", function () {
             // Compare to MATLAB: corr([1;7;2;3;1], [9;3;2;2;1], 'type', 'Spearman')
             // Compare to R: cor(x=c(1,7,2,3,1), y=c(9,3,2,2,1), method="spearman")
                var res;
                res = spearson.correlation.spearman([1, 7, 2, 3, 1], [9, 3, 2, 2, 1]);
                res = spearson.round(res, 4);
                expect(res).to.equal(0.1579);
            });
        });
    });

    describe("hierarchicalClustering", function () {
        describe("linkage.single", function () {
            it("the distance at which all observations are merged into a single cluster is 126.7517", function () {
             // Compare to MATLAB: linkage(pdist([81 91 28 96 96; 91 63 55 16 49; 13 10 96 97 80]))
                var clust, res;
                clust = spearson.hierarchicalClustering([<commit_msg>Add test for pairwiseDistance.<commit_after>var expect, spearson;

expect = require("expect.js");
spearson = require("../lib/spearson.js");

describe("Spearson", function () {

    describe("round", function () {
        it("the rounded value of 0.4281 is 0", function () {
            var res;
            res = spearson.round(0.4281);
            expect(res).to.equal(0);
        });
        it("the rounded value of 0.4281 with two decimal points is 0.43", function () {
            var res;
            res = spearson.round(0.4281, 2);
            expect(res).to.equal(0.43);
        });
    });

    describe("min", function () {
        it("the min of [7,8,1,2,5] is 1", function () {
            var res;
            res = spearson.min([7, 8, 1, 2, 5]);
            expect(res).to.equal(1);
        });
    });

    describe("max", function () {
        it("the max of [7,8,1,2,5] is 8", function () {
            var res;
            res = spearson.max([7, 8, 1, 2, 5]);
            expect(res).to.equal(8);
        });
    });

    describe("sum", function () {
        it("the sum of [1,2,3,4,5] is 15", function () {
         // Compare to MATLAB: sum([1 2 3 4 5])
         // Compare to R: sum(c(1,2,3,4,5))
            var res;
            res = spearson.sum([1, 2, 3, 4, 5]);
            expect(res).to.equal(15);
        });
    });

    describe("mean", function () {
        it("the mean of [1,2,3,4,5] is 3", function () {
         // Compare to MATLAB: mean([1 2 3 4 5])
         // Compare to R: mean(c(1,2,3,4,5))
            var res;
            res = spearson.mean([1, 2, 3, 4, 5]);
            expect(res).to.equal(3);
        });
    });

    describe("deviation", function () {
        it("the deviation of [1,2,3,4,5] is [-2,-1,0,1,2]", function () {
         // Compare to MATLAB: [1 2 3 4 5] - mean([1 2 3 4 5])
         // Compare to R: c(1,2,3,4,5) - mean(c(1,2,3,4,5))
            var res;
            res = spearson.deviation([1, 2, 3, 4, 5]);
            expect(res).to.eql([-2, -1, 0, 1, 2]);
        });
    });

    describe("variance", function () {
        it("the unbiased sample variance of [1,2,3,4,5] is 2.5", function () {
         // Compare to MATLAB: sum(([1 2 3 4 5] - mean([1 2 3 4 5])) .^ 2) / (5 - 1)
         // Compare to R: var(c(1,2,3,4,5))
            var res;
            res = spearson.variance([1, 2, 3, 4, 5]);
            expect(res).to.equal(2.5);
        });
        it("the biased sample / population variance of [1,2,3,4,5] is 2", function () {
         // Compare to MATLAB: sum(([1 2 3 4 5] - mean([1 2 3 4 5])) .^ 2) / 5
            var res;
            res = spearson.variance([1, 2, 3, 4, 5], true);
            expect(res).to.equal(2);
        });
    });

    describe("standardDeviation", function () {
        it("the unbiased sample standard deviation of [1,2,3,4,5] is 1.5811", function () {
         // Compare to MATLAB: std([1 2 3 4 5])
         // Compare to R: sd(c(1,2,3,4,5))
            var res;
            res = spearson.standardDeviation([1, 2, 3, 4, 5]);
            res = spearson.round(res, 4);
            expect(res).to.equal(1.5811);
        });
        it("the biased sample / population standard deviation of [1,2,3,4,5] is 1.4142", function () {
         // Compare to MATLAB: std([1 2 3 4 5], 1)
            var res;
            res = spearson.standardDeviation([1, 2, 3, 4, 5], true);
            res = spearson.round(res, 4);
            expect(res).to.equal(1.4142);
        });
    });

    describe("standardize", function () {
        it("the z score of [1,3,2] is [-1,1,0]", function () {
         // Compare to MATLAB: zscore([1,3,2])
            var res;
            res = spearson.standardize([1, 3, 2]);
            expect(res).to.eql([-1,1,0]);
        });
    });

    describe("rank", function () {
        it("the rank of [54,17,8,109,78] is [3,2,1,5,4]", function () {
         // Compare to MATLAB: tiedrank([54 17 8 109 78])
            var res;
            res = spearson.rank([54, 17, 8, 109, 78]);
            expect(res).to.eql([3, 2, 1, 5, 4]);
        });
    });

    describe("correlation", function () {
        describe("pearson", function () {
            it("the Pearson correlation coefficient of [1,7,2,3,4] and [9,3,5,2,1] is -0.6181", function () {
             // Compare to MATLAB: corr([1;7;2;3;4], [9;3;5;2;1])
             // Compare to R: cor(c(1,7,2,3,4), c(9,3,5,2,1))
                var res;
                res = spearson.correlation.pearson([1, 7, 2, 3, 4], [9, 3, 5, 2, 1]);
                res = spearson.round(res, 4);
                expect(res).to.equal(-0.6181);
            });
        });
        describe("spearman", function () {
            it("the Spearman correlation coefficient of [1,7,2,3,4] and [9,3,5,2,1] is -0.7", function () {
             // Compare to MATLAB: corr([1;7;2;3;4], [9;3;5;2;1], 'type', 'Spearman')
             // Compare to R: cor(x=c(1,7,2,3,4), y=c(9,3,5,2,1), method="spearman")
                var res;
                res = spearson.correlation.spearman([1, 7, 2, 3, 4], [9, 3, 5, 2, 1]);
                expect(res).to.equal(-0.7);
            });
            it("the Spearman correlation coefficient of [1,7,2,3,1] and [9,3,2,2,1] is 0.1579", function () {
             // Compare to MATLAB: corr([1;7;2;3;1], [9;3;2;2;1], 'type', 'Spearman')
             // Compare to R: cor(x=c(1,7,2,3,1), y=c(9,3,2,2,1), method="spearman")
                var res;
                res = spearson.correlation.spearman([1, 7, 2, 3, 1], [9, 3, 2, 2, 1]);
                res = spearson.round(res, 4);
                expect(res).to.equal(0.1579);
            });
        });
    });

    describe("pairwiseDistance", function () {
        it("the pairwise distance of a bunch of numbers is [101.1039 126.7517 134.5214]", function () {
         // Compare to MATLAB: squareform(pdist([81 91 28 96 96; 91 63 55 16 49; 13 10 96 97 80]))
            var pdist;
            pdist = spearson.pairwiseDistance([
                [81, 91, 28, 96, 96],
                [91, 63, 55, 16, 49],
                [13, 10, 96, 97, 80]
            ], spearson.distance.euclidean);
            expect(pdist[0][0]).to.equal(0);
            expect(pdist[1][1]).to.equal(0);
            expect(pdist[2][2]).to.equal(0);
            expect(spearson.round(pdist[0][1], 4)).to.equal(101.1039);
            expect(spearson.round(pdist[0][2], 4)).to.equal(126.7517);
            expect(spearson.round(pdist[1][2], 4)).to.equal(134.5214);
        });
    });

    describe("hierarchicalClustering", function () {
        describe("linkage.single", function () {
            it("the distance at which all observations are merged into a single cluster is 126.7517", function () {
             // Compare to MATLAB: linkage(pdist([81 91 28 96 96; 91 63 55 16 49; 13 10 96 97 80]))
                var clust, res;
                clust = spearson.hierarchicalClustering([
{'fraction_non_alphanumeric': 0.18885544493324072, 'fraction_numerical': 0.08505055182526819, 'mean_word_length': 1.7771110158594086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474850', 'n_tokens_mistral': 5222, 'n_tokens_neox': 4455, 'n_words': 1140}