Browse Source

Added Logit and Logistic functions.

Signed-off-by: jvangael <jurgen.vangael@gmail.com>
la-knuth
Jurgen Van Gael 17 years ago
parent
commit
5b651f1285
  1. 11
      src/Numerics/Properties/Resources.Designer.cs
  2. 3
      src/Numerics/Properties/Resources.resx
  3. 26
      src/Numerics/SpecialFunctions.cs
  4. 30
      src/UnitTests/SpecialFunctionsTests/SpecialFunctionsTests.cs

11
src/Numerics/Properties/Resources.Designer.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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 {
}
}
/// <summary>
/// Looks up a localized string similar to The argument must be between 0 and 1..
/// </summary>
internal static string ArgumentBetween0And1 {
get {
return ResourceManager.GetString("ArgumentBetween0And1", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Value cannot be in the range -1 &lt; x &lt; 1..
/// </summary>

3
src/Numerics/Properties/Resources.resx

@ -282,4 +282,7 @@
<data name="MatrixRowsOrColumnsMustBePositive" xml:space="preserve">
<value>The number of rows or columns of a matrix must be positive.</value>
</data>
<data name="ArgumentBetween0And1" xml:space="preserve">
<value>The argument must be between 0 and 1.</value>
</data>
</root>

26
src/Numerics/SpecialFunctions.cs

@ -309,5 +309,31 @@ namespace MathNet.Numerics
throw new ArgumentException(Properties.Resources.ArgumentTooLargeForIterationLimit, "a,b");
}
/// <summary>
/// Computes the logit function. <seealso cref="http://en.wikipedia.org/wiki/Logit"/>
/// </summary>
/// <param name="p">The parameter for which to compute the logit function. This number should be
/// between 0 and 1.</param>
/// <returns>The logarithm of <paramref name="p"/> divided by 1.0 - <paramref name="p"/>.</returns>
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));
}
/// <summary>
/// Computes the logistic function. <seealso cref="http://en.wikipedia.org/wiki/Logistic"/>
/// </summary>
/// <param name="p">The parameter for which to compute the logistic function.</param>
/// <returns>The logistic function of <paramref name="p"/>.</returns>
public static double Logistic(double p)
{
return 1.0 / (Math.Exp(-p) + 1.0);
}
}
}

30
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);
}
}
}
Loading…
Cancel
Save