From 4b5bc84b711da10e894d40eeca9fc45c68fe52c5 Mon Sep 17 00:00:00 2001 From: Nikolay Pozdnichenko Date: Sat, 30 Nov 2024 17:55:19 +0600 Subject: [PATCH] Add tests for characteristics of dependence --- Libiada.Core.Tests/Core/ChainsStorage.cs | 9 ++++----- .../AccordanceCalculatorsTests.cs | 7 +++++-- .../BinaryCalculatorsTests.cs | 7 +++++-- .../BinaryCalculators/GeometricMeanTests.cs | 18 ++++++++++++++++++ ...nvolvedPartialDependenceCoefficientTests.cs | 2 ++ .../MutualDependenceCoefficientTests.cs | 1 + ...malizedPartialDependenceCoefficientTests.cs | 2 ++ .../PartialDependenceCoefficientTests.cs | 2 ++ .../BinaryCalculators/RedundancyTests.cs | 2 ++ .../BinaryCalculators/GeometricMean.cs | 2 +- .../BinaryCalculators/Redundancy.cs | 2 +- 11 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Libiada.Core.Tests/Core/ChainsStorage.cs b/Libiada.Core.Tests/Core/ChainsStorage.cs index e9b61c47..416c5208 100644 --- a/Libiada.Core.Tests/Core/ChainsStorage.cs +++ b/Libiada.Core.Tests/Core/ChainsStorage.cs @@ -333,7 +333,7 @@ public static class ChainsStorage // 0 A A B A _ A _ _ B A new Chain( - [1, 1, 2, 1, 0, 1, 0, 0, 2], + [1, 1, 2, 1, 0, 1, 0, 0, 2, 1], [NullValue.Instance(), Elements["A"], Elements["B"]]), // ----------- sequences from Morozenko's paper ----------- @@ -367,13 +367,12 @@ public static class ChainsStorage [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [NullValue.Instance(), Elements["A"], Elements["B"]]), - // 7 A _ _ _ _ _ _ _ _ _ B _ B B A _ _ A A A _ _ A _ _ _ _ _ B + // 7 A _ _ _ _ _ _ _ B _ B B A _ _ A A A _ _ A _ _ _ _ _ B new Chain( [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 2, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 2], [NullValue.Instance(), Elements["A"], Elements["B"]]), - // 8 - // 7 A _ _ A _ _ B _ _ _ _ _ A A _ A _ A _ _ _ B _ A B + // 8 A _ _ A _ _ B _ _ _ _ _ A A _ A _ A _ _ _ B _ A B new Chain( [1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 2, 0, 1, 2], [NullValue.Instance(), Elements["A"], Elements["B"]]), @@ -388,7 +387,7 @@ public static class ChainsStorage [1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0], [NullValue.Instance(), Elements["A"], Elements["B"]]), - // 11 A _ B _ _ _ _ _ _ A _ B _ _ _ _ A _ _ B _ _ _ _ A B _ 0 + // 11 A _ B _ _ _ _ _ _ A _ B _ _ _ _ A _ _ B _ _ _ _ A B _ _ new Chain( [1, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 2, 0, 0], [NullValue.Instance(), Elements["A"], Elements["B"]]), diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/AccordanceCalculators/AccordanceCalculatorsTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/AccordanceCalculators/AccordanceCalculatorsTests.cs index ccb6bdad..6ae5e9a8 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/AccordanceCalculators/AccordanceCalculatorsTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/AccordanceCalculators/AccordanceCalculatorsTests.cs @@ -49,8 +49,11 @@ protected void CalculationTest(int index, double firstValue, double secondValue) CongenericChain secondChain = binaryChains[index].CongenericChain(elements["B"]); double result1 = calculator.Calculate(firstChain, secondChain, Link.End); double result2 = calculator.Calculate(secondChain, firstChain, Link.End); - Assert.That(result1, Is.EqualTo(firstValue).Within(0.0001d)); - Assert.That(result2, Is.EqualTo(secondValue).Within(0.0001d)); + Assert.Multiple(() => + { + Assert.That(result1, Is.EqualTo(firstValue).Within(0.0001d)); + Assert.That(result2, Is.EqualTo(secondValue).Within(0.0001d)); + }); } /// diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/BinaryCalculatorsTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/BinaryCalculatorsTests.cs index 4b849de1..387c6b44 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/BinaryCalculatorsTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/BinaryCalculatorsTests.cs @@ -42,7 +42,10 @@ protected void CalculationTest(int index, double firstValue, double secondValue) { double result1 = Calculator.Calculate(Chains[index].GetRelationIntervalsManager(elements["A"], elements["B"]), Link.End); double result2 = Calculator.Calculate(Chains[index].GetRelationIntervalsManager(elements["B"], elements["A"]), Link.End); - Assert.That(result1, Is.EqualTo(firstValue).Within(0.0001)); - Assert.That(result2, Is.EqualTo(secondValue).Within(0.0001)); + Assert.Multiple(() => + { + Assert.That(result1, Is.EqualTo(firstValue).Within(0.0001)); + Assert.That(result2, Is.EqualTo(secondValue).Within(0.0001)); + }); } } diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/GeometricMeanTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/GeometricMeanTests.cs index 95b540b6..9680bdfd 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/GeometricMeanTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/GeometricMeanTests.cs @@ -21,6 +21,24 @@ public class GeometricMeanTests : BinaryCalculatorsTests /// The second value. /// [TestCase(0, 1.7321, 1)] + [TestCase(1, 1, 0)] + [TestCase(2, 3, 0)] + [TestCase(3, 4.527019056, 2.080083823)] + [TestCase(4, 1, 0)] + [TestCase(5, 1, 0)] + [TestCase(6, 12, 0)] + [TestCase(7, 6.92820323, 1)] + [TestCase(8, 2.289428485, 3.464101615)] + [TestCase(9, 5.360108411, 1)] + [TestCase(10, 2, 2.5198421)] + [TestCase(11, 1.861209718, 5.59344471)] + [TestCase(12, 4, 4)] + [TestCase(13, 3, 2.466212074)] + [TestCase(14, 1, 3.556893304)] + [TestCase(15, 1, 1)] + [TestCase(16, 4.30886938, 3)] + [TestCase(17, 3.036588972, 4)] + [TestCase(19, 1, 5.646216173)] public void SpatialDependenceTest(int index, double firstValue, double secondValue) { CalculationTest(index, firstValue, secondValue); diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/InvolvedPartialDependenceCoefficientTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/InvolvedPartialDependenceCoefficientTests.cs index 0a44e9bb..08b5f81f 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/InvolvedPartialDependenceCoefficientTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/InvolvedPartialDependenceCoefficientTests.cs @@ -21,6 +21,7 @@ public class InvolvedPartialDependenceCoefficientTests : BinaryCalculatorsTests< /// /// The second value. /// + [TestCase(0, 0.285714286, 0.338143834)] [TestCase(1, 0, 0)] [TestCase(2, 0, 0)] [TestCase(3, 0, 0.5461)] @@ -38,6 +39,7 @@ public class InvolvedPartialDependenceCoefficientTests : BinaryCalculatorsTests< [TestCase(15, 0.3429, 0.35)] [TestCase(16, 0.3745, 0.3965)] [TestCase(17, 0.6072, 0.3757)] + [TestCase(19, 0.759718859, -0.162330299)] public void K2Test(int index, double firstValue, double secondValue) { CalculationTest(index, firstValue, secondValue); diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/MutualDependenceCoefficientTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/MutualDependenceCoefficientTests.cs index c49dacdd..efff9b98 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/MutualDependenceCoefficientTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/MutualDependenceCoefficientTests.cs @@ -18,6 +18,7 @@ public class MutualDependenceCoefficientTests : BinaryCalculatorsTests /// The value. /// + [TestCase(0, 0.310825552)] [TestCase(1, 0)] [TestCase(2, 0)] [TestCase(3, 0)] diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/NormalizedPartialDependenceCoefficientTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/NormalizedPartialDependenceCoefficientTests.cs index d53f5616..05a8210b 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/NormalizedPartialDependenceCoefficientTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/NormalizedPartialDependenceCoefficientTests.cs @@ -21,6 +21,7 @@ public class NormalizedPartialDependenceCoefficientTests : BinaryCalculatorsTest /// /// The second value. /// + [TestCase(0, 0.2, 0.094680274)] [TestCase(1, 0, 0)] [TestCase(2, 0, 0)] [TestCase(3, 0, 0.1214)] @@ -38,6 +39,7 @@ public class NormalizedPartialDependenceCoefficientTests : BinaryCalculatorsTest [TestCase(15, 0.0357, 0.1458)] [TestCase(16, 0.0904, 0.0478)] [TestCase(17, 0.1401, 0.0578)] + [TestCase(19, 0.264250038, -0.042347035)] public void NormalizedK1Test(int index, double firstValue, double secondValue) { CalculationTest(index, firstValue, secondValue); diff --git a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/PartialDependenceCoefficientTests.cs b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/PartialDependenceCoefficientTests.cs index 0eee0de3..ff6e9001 100644 --- a/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/PartialDependenceCoefficientTests.cs +++ b/Libiada.Core.Tests/Core/Characteristics/Calculators/BinaryCalculators/PartialDependenceCoefficientTests.cs @@ -21,6 +21,7 @@ public class PartialDependenceCoefficientTests : BinaryCalculatorsTests /// The second value. /// + [TestCase(0, 0.5, 0.236700684)] [TestCase(1, 0, 0)] [TestCase(2, 0, 0)] [TestCase(3, 0, 0.5461)] @@ -38,6 +39,7 @@ public class PartialDependenceCoefficientTests : BinaryCalculatorsTests /// /// The second value. /// + [TestCase(0, 0.5, 0.59175171)] [TestCase(1, 0, 0)] [TestCase(2, 0, 0)] [TestCase(3, 0, 0.7282)] @@ -37,6 +38,7 @@ public class RedundancyTests : BinaryCalculatorsTests [TestCase(15, 0.8571, 0.875)] [TestCase(16, 0.4369, 0.6938)] [TestCase(17, 0.6072, 0.5636)] + [TestCase(19, 0.759718859, -0.216440399)] public void ChainCalculationTest(int index, double firstValue, double secondValue) { CalculationTest(index, firstValue, secondValue); diff --git a/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/GeometricMean.cs b/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/GeometricMean.cs index b1395f1a..8f575b72 100644 --- a/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/GeometricMean.cs +++ b/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/GeometricMean.cs @@ -31,6 +31,6 @@ public override double Calculate(BinaryIntervalsManager manager, Link link) double result = intervals.Where(t => t > 0).Sum(t => Math.Log(t, 2)); - return Math.Pow(2, intervals.Length == 0 ? 0 : result / intervals.Length); + return intervals.Length == 0 ? 0 : Math.Pow(2, result / intervals.Length); } } diff --git a/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/Redundancy.cs b/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/Redundancy.cs index 538b8fd0..a8cb22de 100644 --- a/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/Redundancy.cs +++ b/Libiada.Core/Core/Characteristics/Calculators/BinaryCalculators/Redundancy.cs @@ -63,6 +63,6 @@ public override double Calculate(BinaryIntervalsManager manager, Link link) GeometricMean geometricMeanCalculator = new(); double binaryGeometricMean = geometricMeanCalculator.Calculate(manager, link); - return 1 - (binaryGeometricMean / Math.Pow(2, avG)); + return manager.PairsCount == 0 ? 0 : 1 - (binaryGeometricMean / Math.Pow(2, avG)); } }