Beispiel #1
0
        public void TestBadAlphabet()
        {
            var fc = new TallyAlphabet(new[] { "a", "b" });

            fc.Add("a");
            fc.Add("b");
            Assert.Throws <ArgumentException>(() => fc.Add("c"));
        }
Beispiel #2
0
        public void TestAlphabet()
        {
            var fc = new TallyAlphabet(new[] { "a", "b" });

            fc.Add("a");
            fc.Add("b");
            Assert.Equal(0.5f, fc.Probability("a"));
            Assert.Equal(0.5f, fc.Probability("b"));
            Assert.Equal(1f, ProbabilityDistribution.Entropy(fc.probability, fc.binCount), 2);
        }
Beispiel #3
0
        public void TestPairedAlphabet()
        {
            int binCount = 2;
            var fc       = new TallyAlphabet <int>(new[] { "a", "b" }, binCount, y => y);

            fc.Add("a", 0);
            fc.Add("a", 0);
            fc.Add("a", 1);
            fc.Add("a", 1);
            fc.Add("b", 0);
            fc.Add("b", 0);
            fc.Add("b", 0);
            fc.Add("b", 0);
            fc.Add("b", 1);
            Assert.Equal(4f / 9, fc.ProbabilityX("a"));
            Assert.Equal(5f / 9, fc.ProbabilityX("b"));
            Assert.Equal(6 / 9f, fc.ProbabilityY(0));
            Assert.Equal(3 / 9f, fc.ProbabilityY(1));
            Assert.Equal(2 / 9f, fc.ProbabilityXY("a", 0));
            Assert.Equal(2 / 4f, fc.ProbabilityYGivenX(0, "a"));
            Assert.Equal(2 / 4f, fc.ProbabilityYGivenX(1, "a"));
            Assert.Equal(4 / 5f, fc.ProbabilityYGivenX(0, "b"), 2);
            Assert.Equal(1 / 5f, fc.ProbabilityYGivenX(1, "b"), 2);
            Assert.Equal(0.8f, fc.EntropyYGivenX(binCount), 1);
            Assert.Equal(0.9f, fc.EntropyXGivenY(binCount), 1);
            Assert.Equal(1.8f, fc.EntropyXY(binCount), 1);
            Assert.Equal(0.99f, fc.EntropyX(binCount), 2);
            Assert.Equal(0.9f, fc.EntropyY(binCount), 1);
            Assert.Equal(0f, fc.EntropyXGivenY() - fc.EntropyXY() + fc.EntropyY(), 1);
            Assert.Equal(0f, fc.EntropyYGivenX() - fc.EntropyXY() + fc.EntropyX(), 1);
            Assert.Equal(0.07f, fc.MutualInformationXY(binCount), 2);
        }
Beispiel #4
0
        public void TestCompareWithProbability()
        {
            var fc = new TallyAlphabet <int>(new[] { "a", "b", "c" }, 3, y => y);

            fc.Add("a", 2);
            fc.Add("a", 1);
            fc.Add("a", 0);
            fc.Add("b", 2);
            fc.Add("b", 1);
            fc.Add("b", 0);
            fc.Add("c", 2);
            fc.Add("c", 1);
            fc.Add("c", 0);
            Assert.Equal(1 / 3f, fc.ProbabilityX("a"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityX("b"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityX("c"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityY(0));
            Assert.Equal(1 / 3f, fc.ProbabilityY(1));
            Assert.Equal(1 / 9f, fc.ProbabilityXY("a", 0));
            Assert.Equal(1 / 3f, fc.ProbabilityYGivenX(0, "a"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityYGivenX(1, "a"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityYGivenX(0, "b"), 2);
            Assert.Equal(1 / 3f, fc.ProbabilityYGivenX(1, "b"), 2);
            Assert.Equal(1f, fc.EntropyYGivenX(3), 1);

            Assert.Equal(9, fc.probabilityXY.Length);
            Assert.Equal(new [] { 1 / 3f, 1 / 3f, 1 / 3f }, fc.probabilityX);
            Assert.Equal(new [] { 1 / 3f, 1 / 3f, 1 / 3f }, fc.probabilityY);
            Assert.Equal(new [, ] {
                { 1 / 9f, 1 / 9f, 1 / 9f },
                { 1 / 9f, 1 / 9f, 1 / 9f },
                { 1 / 9f, 1 / 9f, 1 / 9f },
            }, fc.probabilityXY);

            Assert.Equal(1 / 2f, ProbabilityDistribution.ConditionalEntropyYX(fc.probabilityXY, fc.probabilityX, fc.probabilityXY.Length));
            Assert.Equal(1f, ProbabilityDistribution.ConditionalEntropyYX(fc.probabilityXY, fc.probabilityX, fc.probabilityX.Length));
            Assert.Equal(1f, fc.EntropyXGivenY(3), 1);
            Assert.Equal(1 / 2f, ProbabilityDistribution.ConditionalEntropyXY(fc.probabilityXY, fc.probabilityY, fc.probabilityXY.Length));
            Assert.Equal(1f, ProbabilityDistribution.ConditionalEntropyXY(fc.probabilityXY, fc.probabilityY, fc.probabilityY.Length));
            Assert.Equal(2f, fc.EntropyXY(3), 1);
            Assert.Equal(2f, ProbabilityDistribution.JointEntropy(fc.probabilityXY, fc.probabilityX.Length), 1);
            Assert.Equal(1f, ProbabilityDistribution.JointEntropy(fc.probabilityXY, fc.probabilityXY.Length), 1);
            Assert.Equal(3.2f, ProbabilityDistribution.JointEntropy(fc.probabilityXY, 2), 1);
            Assert.Equal(1f, fc.EntropyX(3), 2);
            Assert.Equal(1f, ProbabilityDistribution.Entropy(fc.probabilityX, fc.probabilityX.Length), 2);
            Assert.Equal(0.5f, ProbabilityDistribution.Entropy(fc.probabilityX, fc.probabilityXY.Length), 2);
            Assert.Equal(1f, fc.EntropyY(3), 1);
            Assert.Equal(1f, ProbabilityDistribution.Entropy(fc.probabilityY, fc.probabilityY.Length), 2);
            Assert.Equal(0.5f, ProbabilityDistribution.Entropy(fc.probabilityY, fc.probabilityXY.Length), 2);
            // H(X|Y) = H(X,Y) - H(Y)
            // This should always be true.
            Assert.Equal(0f, fc.EntropyXGivenY() - fc.EntropyXY() + fc.EntropyY(), 1);
            Assert.Equal(0f, ProbabilityDistribution.ConditionalEntropyXY(fc.probabilityXY, fc.probabilityY)
                         - ProbabilityDistribution.JointEntropy(fc.probabilityXY)
                         + ProbabilityDistribution.ConditionalEntropyXY(fc.probabilityXY, fc.probabilityY), 1);
            Assert.Equal(0f, fc.MutualInformationXY(3), 2);
        }
Beispiel #5
0
        public void TestPairedAlphabet3()
        {
            var fc = new TallyAlphabet <int>(new[] { "a", "b" }, 2, y => y);

            fc.Add("a", 1);
            fc.Add("a", 1);
            fc.Add("a", 0);
            fc.Add("a", 0);
            fc.Add("b", 1);
            fc.Add("b", 1);
            fc.Add("b", 0);
            fc.Add("b", 0);
            Assert.Equal(1 / 2f, fc.ProbabilityX("a"));
            Assert.Equal(1 / 2f, fc.ProbabilityX("b"));
            Assert.Equal(1 / 2f, fc.ProbabilityY(0));
            Assert.Equal(1 / 2f, fc.ProbabilityY(1));
            Assert.Equal(2 / 8f, fc.ProbabilityXY("a", 0));
            Assert.Equal(1 / 2f, fc.ProbabilityYGivenX(0, "a"));
            Assert.Equal(1 / 2f, fc.ProbabilityYGivenX(1, "a"));
            Assert.Equal(1 / 2f, fc.ProbabilityYGivenX(0, "b"), 2);
            Assert.Equal(1 / 2f, fc.ProbabilityYGivenX(1, "b"), 2);
            Assert.Equal(1f, fc.EntropyYGivenX(2), 1);
            Assert.Equal(1f, fc.EntropyXGivenY(2), 1);
            Assert.Equal(2f, fc.EntropyXY(2), 1);
            Assert.Equal(1f, fc.EntropyX(2), 2);
            Assert.Equal(1f, fc.EntropyY(2), 1);
            // H(X|Y) = H(X,Y) - H(Y)
            // This should always be true.
            Assert.Equal(0f, fc.EntropyXGivenY() - fc.EntropyXY() + fc.EntropyY(), 1);
            Assert.Equal(0f, fc.EntropyYGivenX() - fc.EntropyXY() + fc.EntropyX(), 1);
            Assert.Equal(0f, fc.MutualInformationXY(2), 2);
        }
Beispiel #6
0
        public void TestPairedAlphabet2()
        {
            var fc = new TallyAlphabet <int>(new[] { "a", "b" }, 2, y => y);

            fc.Add("a", 1);
            fc.Add("a", 1);
            fc.Add("a", 1);
            fc.Add("a", 1);
            fc.Add("b", 0);
            fc.Add("b", 0);
            fc.Add("b", 0);
            fc.Add("b", 0);
            Assert.Equal(1 / 2f, fc.ProbabilityX("a"));
            Assert.Equal(1 / 2f, fc.ProbabilityX("b"));
            Assert.Equal(1 / 2f, fc.ProbabilityY(0));
            Assert.Equal(1 / 2f, fc.ProbabilityY(1));
            Assert.Equal(0f, fc.ProbabilityXY("a", 0));
            Assert.Equal(0f, fc.ProbabilityYGivenX(0, "a"));
            Assert.Equal(1f, fc.ProbabilityYGivenX(1, "a"));
            Assert.Equal(1f, fc.ProbabilityYGivenX(0, "b"), 2);
            Assert.Equal(0f, fc.ProbabilityYGivenX(1, "b"), 2);
            Assert.Equal(0f, fc.EntropyYGivenX(2), 1);
            Assert.Equal(0f, fc.EntropyXGivenY(2), 1);
            Assert.Equal(1f, fc.EntropyXY(2), 1);
            Assert.Equal(1f, fc.EntropyX(2), 2);
            Assert.Equal(1f, fc.EntropyY(2), 1);
            Assert.Equal(0f, fc.EntropyXGivenY() - fc.EntropyXY() + fc.EntropyY(), 1);
            Assert.Equal(0f, fc.EntropyYGivenX() - fc.EntropyXY() + fc.EntropyX(), 1);
            Assert.Equal(1f, fc.MutualInformationXY(2), 2);
        }