diff --git a/src/Numerics/Properties/Resources.Designer.cs b/src/Numerics/Properties/Resources.Designer.cs index c1690ed7..91cd19e5 100644 --- a/src/Numerics/Properties/Resources.Designer.cs +++ b/src/Numerics/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.4016 +// Runtime Version:2.0.50727.4200 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -60,6 +60,15 @@ namespace MathNet.Numerics.Properties { } } + /// + /// Looks up a localized string similar to The argument must be between 0 and 1.. + /// + internal static string ArgumentBetween0And1 { + get { + return ResourceManager.GetString("ArgumentBetween0And1", resourceCulture); + } + } + /// /// Looks up a localized string similar to Value cannot be in the range -1 < x < 1.. /// diff --git a/src/Numerics/Properties/Resources.resx b/src/Numerics/Properties/Resources.resx index 6ce30845..1c096480 100644 --- a/src/Numerics/Properties/Resources.resx +++ b/src/Numerics/Properties/Resources.resx @@ -282,4 +282,7 @@ The number of rows or columns of a matrix must be positive. + + The argument must be between 0 and 1. + \ No newline at end of file diff --git a/src/Numerics/SpecialFunctions.cs b/src/Numerics/SpecialFunctions.cs index e4f0b4d8..991174bc 100644 --- a/src/Numerics/SpecialFunctions.cs +++ b/src/Numerics/SpecialFunctions.cs @@ -309,5 +309,31 @@ namespace MathNet.Numerics throw new ArgumentException(Properties.Resources.ArgumentTooLargeForIterationLimit, "a,b"); } + + /// + /// Computes the logit function. + /// + /// The parameter for which to compute the logit function. This number should be + /// between 0 and 1. + /// The logarithm of divided by 1.0 - . + public static double Logit(double p) + { + if (p < 0.0 || p > 1.0) + { + throw new ArgumentOutOfRangeException(Resources.ArgumentBetween0And1); + } + + return Math.Log(p / (1.0 - p)); + } + + /// + /// Computes the logistic function. + /// + /// The parameter for which to compute the logistic function. + /// The logistic function of . + public static double Logistic(double p) + { + return 1.0 / (Math.Exp(-p) + 1.0); + } } } \ No newline at end of file diff --git a/src/UnitTests/SpecialFunctionsTests/SpecialFunctionsTests.cs b/src/UnitTests/SpecialFunctionsTests/SpecialFunctionsTests.cs index 727f0a0a..3ba3fd55 100644 --- a/src/UnitTests/SpecialFunctionsTests/SpecialFunctionsTests.cs +++ b/src/UnitTests/SpecialFunctionsTests/SpecialFunctionsTests.cs @@ -230,5 +230,35 @@ namespace MathNet.Numerics.UnitTests.SpecialFunctionsTests { AssertHelpers.AlmostEqual(f, SpecialFunctions.BetaRegularized(a,b,x), 12); } + + [Test] + [Row(0.000000, Double.NegativeInfinity)] + [Row(0.000010, -11.512915464920228103874353849992239636376994324587)] + [Row(0.001000, -6.9067547786485535272274487616830597875179908939086)] + [Row(0.100000, -2.1972245773362193134015514347727700402304323440139)] + [Row(0.500000, 0.0)] + [Row(0.900000, 2.1972245773362195801634726294284168954491240598975)] + [Row(0.999000, 6.9067547786485526081487245019905638981131702804661)] + [Row(0.999990, 11.512915464924779098232747799811946290419057060965)] + [Row(1.000000, Double.PositiveInfinity)] + public void Logit(double p, double x) + { + AssertHelpers.AlmostEqual(x, SpecialFunctions.Logit(p), 15); + } + + [Test] + [Row(0.000000, Double.NegativeInfinity)] + [Row(0.000010, -11.512915464920228103874353849992239636376994324587)] + [Row(0.001000, -6.9067547786485535272274487616830597875179908939086)] + [Row(0.100000, -2.1972245773362193134015514347727700402304323440139)] + [Row(0.500000, 0.0)] + [Row(0.900000, 2.1972245773362195801634726294284168954491240598975)] + [Row(0.999000, 6.9067547786485526081487245019905638981131702804661)] + [Row(0.999990, 11.512915464924779098232747799811946290419057060965)] + [Row(1.000000, Double.PositiveInfinity)] + public void Logistic(double x, double p) + { + AssertHelpers.AlmostEqual(x, SpecialFunctions.Logistic(p), 15); + } } } \ No newline at end of file