Index: Ringtoets/Common/src/Ringtoets.Common.Service/ProbabilityAssessmentService.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.Service/ProbabilityAssessmentService.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.Service/ProbabilityAssessmentService.cs (revision 678dc8c8dbb01fd889a082ae88c9f9d42f597a10) @@ -0,0 +1,78 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using MathNet.Numerics.Distributions; +using Ringtoets.Common.Data.Probability; + +namespace Ringtoets.Common.Service +{ + /// + /// Service for calculating . + /// + public static class ProbabilityAssessmentService + { + /// + /// Calculates the given and . + /// + /// The probability assesment input to use for the calculation. + /// The reliability to use for the calculation. + /// Thrown when is null. + public static ProbabilityAssessmentOutput Calculate(ProbabilityAssessmentInput probabilityAssessmentInput, double reliability) + { + if (probabilityAssessmentInput == null) + { + throw new ArgumentNullException("probabilityAssessmentInput"); + } + + var requiredProbability = RequiredProbability(probabilityAssessmentInput.Contribution / 100.0, probabilityAssessmentInput.Norm, probabilityAssessmentInput.N); + var probability = ReliabilityToProbability(reliability); + var requiredReliability = ProbabilityToReliability(requiredProbability); + var factorOfSafety = FactorOfSafety(reliability, requiredReliability); + + return new ProbabilityAssessmentOutput(1/requiredProbability, + requiredReliability, + 1/probability, + reliability, + factorOfSafety); + } + + private static double RequiredProbability(double contribution, double norm, double lengthEffectN) + { + return contribution*(1/norm)/lengthEffectN; + } + + private static double ReliabilityToProbability(double reliability) + { + return Normal.CDF(0, 1, -reliability); + } + + private static double ProbabilityToReliability(double probability) + { + return Normal.InvCDF(0, 1, 1 - probability); + } + + private static double FactorOfSafety(double reliability, double requiredReliability) + { + return reliability/requiredReliability; + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Service/Ringtoets.Common.Service.csproj =================================================================== diff -u -r507e30ebf13ba63fcedbe9a5b8457f302da26429 -r678dc8c8dbb01fd889a082ae88c9f9d42f597a10 --- Ringtoets/Common/src/Ringtoets.Common.Service/Ringtoets.Common.Service.csproj (.../Ringtoets.Common.Service.csproj) (revision 507e30ebf13ba63fcedbe9a5b8457f302da26429) +++ Ringtoets/Common/src/Ringtoets.Common.Service/Ringtoets.Common.Service.csproj (.../Ringtoets.Common.Service.csproj) (revision 678dc8c8dbb01fd889a082ae88c9f9d42f597a10) @@ -32,25 +32,38 @@ AllRules.ruleset + + ..\..\..\..\packages\MathNet.Numerics.3.8.0\lib\net40\MathNet.Numerics.dll + Properties\GlobalAssembly.cs + + + {3bbfd65b-b277-4e50-ae6d-bd24c3434609} + Core.Common.Base + {c90b77da-e421-43cc-b82e-529651bc21ac} Core.Common.Version + + {D4200F43-3F72-4F42-AF0A-8CED416A38EC} + Ringtoets.Common.Data + Copying.licenseheader + + + + \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Service.Test/ProbabilityAssessmentServiceTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.Service.Test/ProbabilityAssessmentServiceTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.Service.Test/ProbabilityAssessmentServiceTest.cs (revision 678dc8c8dbb01fd889a082ae88c9f9d42f597a10) @@ -0,0 +1,151 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using NUnit.Framework; +using Ringtoets.Common.Data.Probability; +using Ringtoets.Common.Data.TestUtil; + +namespace Ringtoets.Common.Service.Test +{ + [TestFixture] + public class ProbabilityAssessmentServiceTest + { + [Test] + public void Calculate_NullProbabilityAssessmentInput_ThrowsArgumentNullException() + { + //Call + TestDelegate test = () => ProbabilityAssessmentService.Calculate(null, double.NaN); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("probabilityAssessmentInput", exception.ParamName); + } + + [Test] + [TestCase(30000, 100, 2, 60000)] + [TestCase(30000, 100, 1, 30000)] + [TestCase(30000, 24, 2, 250000)] + [TestCase(30000, 24, 1, 125000)] + [TestCase(20000, 100, 2, 40000)] + [TestCase(20000, 100, 1, 20000)] + [TestCase(20000, 24, 2, 166666.6667)] + [TestCase(20000, 24, 1, 83333.33)] + public void RequiredProbability_DifferentInputs_ReturnsExpectedValue(int norm, double contribution, int lengthEffectN, double expectedResult) + { + // Setup + var input = new ProbabilityAssessmentInput + { + Norm = norm, + Contribution = contribution, + N = lengthEffectN + }; + + // Call + var probabilityAssessmentOutput = ProbabilityAssessmentService.Calculate(input, double.NaN); + + // Assert + Assert.AreEqual(expectedResult, probabilityAssessmentOutput.RequiredProbability, probabilityAssessmentOutput.RequiredProbability.GetAccuracy()); + } + + [Test] + [TestCase(30000, 100, 2, 4.149409984)] + [TestCase(30000, 100, 1, 3.987878937)] + [TestCase(30000, 24, 2, 4.465183916)] + [TestCase(30000, 24, 1, 4.314451022)] + [TestCase(20000, 100, 2, 4.055626981)] + [TestCase(20000, 100, 1, 3.890591886)] + [TestCase(20000, 24, 2, 4.377587847)] + [TestCase(20000, 24, 1, 4.2240038)] + public void RequiredReliability_DifferentInputs_ReturnsExpectedValue(int norm, double contribution, int lengthEffectN, double expectedResult) + { + // Setup + var input = new ProbabilityAssessmentInput + { + Norm = norm, + Contribution = contribution, + N = lengthEffectN + }; + + // Call + var probabilityAssessmentOutput = ProbabilityAssessmentService.Calculate(input, double.NaN); + + // Assert + Assert.AreEqual(expectedResult, probabilityAssessmentOutput.RequiredReliability, probabilityAssessmentOutput.RequiredReliability.GetAccuracy()); + } + + [Test] + [TestCase(1.23456, 1.23456)] + [TestCase(789.123, 789.123)] + public void Reliability_DifferentInputs_ReturnsExpectedValue(double reliability, double expectedResult) + { + // Setup + var input = new ProbabilityAssessmentInput(); + + // Call + var probabilityAssessmentOutput = ProbabilityAssessmentService.Calculate(input, reliability); + + // Assert + Assert.AreEqual(expectedResult, probabilityAssessmentOutput.Reliability, probabilityAssessmentOutput.Reliability.GetAccuracy()); + } + + [Test] + [TestCase(4, 31574.3855346)] + [TestCase(5, 3488555.78723)] + public void Probability_DifferentInputs_ReturnsExpectedValue(double reliability, double expectedResult) + { + // Setup + var input = new ProbabilityAssessmentInput(); + + // Call + var probabilityAssessmentOutput = ProbabilityAssessmentService.Calculate(input, reliability); + + // Assert + Assert.AreEqual(expectedResult, probabilityAssessmentOutput.Probability, probabilityAssessmentOutput.Probability.GetAccuracy()); + } + + [Test] + [TestCase(30000, 100, 2, 4.107479655, 0.989894869)] + [TestCase(30000, 100, 2, 4.149409984, 1)] + [TestCase(30000, 24, 2, 4.107479655, 0.919890363)] + [TestCase(30000, 24, 2, 4.149409984, 0.929280868)] + [TestCase(20000, 100, 2, 4.107479655, 1.012785366)] + [TestCase(20000, 100, 2, 4.149409984, 1.023124169)] + [TestCase(20000, 24, 2, 4.107479655, 0.938297482)] + [TestCase(20000, 24, 2, 4.149409984, 0.947875892)] + public void FactorOfSafety_DifferentInputs_ReturnsExpectedValue(int norm, double contribution, int lengthEffectN, double reliability, double expectedResult) + { + // Setup + var input = new ProbabilityAssessmentInput + { + Norm = norm, + Contribution = contribution, + N = lengthEffectN + }; + + // Call + var probabilityAssessmentOutput = ProbabilityAssessmentService.Calculate(input, reliability); + + // Assert + Assert.AreEqual(expectedResult, probabilityAssessmentOutput.FactorOfSafety, probabilityAssessmentOutput.FactorOfSafety.GetAccuracy()); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Service.Test/Ringtoets.Common.Service.Test.csproj =================================================================== diff -u -r507e30ebf13ba63fcedbe9a5b8457f302da26429 -r678dc8c8dbb01fd889a082ae88c9f9d42f597a10 --- Ringtoets/Common/test/Ringtoets.Common.Service.Test/Ringtoets.Common.Service.Test.csproj (.../Ringtoets.Common.Service.Test.csproj) (revision 507e30ebf13ba63fcedbe9a5b8457f302da26429) +++ Ringtoets/Common/test/Ringtoets.Common.Service.Test/Ringtoets.Common.Service.Test.csproj (.../Ringtoets.Common.Service.Test.csproj) (revision 678dc8c8dbb01fd889a082ae88c9f9d42f597a10) @@ -39,17 +39,40 @@ MinimumRecommendedRules.ruleset + + ..\..\..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + Copying.licenseheader + + + + {3bbfd65b-b277-4e50-ae6d-bd24c3434609} + Core.Common.Base + + + {d4200f43-3f72-4f42-af0a-8ced416a38ec} + Ringtoets.Common.Data + + + {D951D6DA-FE83-4920-9FDB-63BF96480B54} + Ringtoets.Common.Service + + + {4843D6E5-066F-4795-94F5-1D53932DD03C} + Ringtoets.Common.Data.TestUtil + + + + + \ No newline at end of file