Skip to content

Commit 9cf588d

Browse files
author
Lê Nam Khánh
authored
Add algorithm Sigmoid (#570)
1 parent 86374cd commit 9cf588d

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using Algorithms.Numeric;
2+
using NUnit.Framework;
3+
using System;
4+
5+
namespace Algorithms.Tests.Numeric;
6+
7+
/// <summary>
8+
/// Tests for the Sigmoid class, which implements the sigmoid activation function.
9+
/// </summary>
10+
public static class SigmoidTests
11+
{
12+
// Standard tolerance for floating-point comparisons.
13+
private const double Tolerance = 1e-15;
14+
15+
/// <summary>
16+
/// Tests that the sigmoid function correctly calculates the center point (x=0).
17+
/// Sigmoid(0) should equal 0.5.
18+
/// </summary>
19+
[Test]
20+
public static void GetsCenterValue()
21+
{
22+
// Arrange
23+
double x = 0.0;
24+
double expected = 0.5;
25+
26+
// Act
27+
var result = Sigmoid.Calculate(x);
28+
29+
// Assert
30+
Assert.That(result, Is.EqualTo(expected).Within(Tolerance));
31+
}
32+
33+
/// <summary>
34+
/// Tests that the sigmoid function approaches 1 for large positive inputs (asymptotic behavior).
35+
/// </summary>
36+
[Test]
37+
public static void GetsAsymptoticValueForLargePositiveX()
38+
{
39+
// Arrange
40+
double x = 100.0;
41+
double expected = 1.0;
42+
43+
// Act
44+
var result = Sigmoid.Calculate(x);
45+
46+
// Assert
47+
// The result should be extremely close to 1.0.
48+
Assert.That(result, Is.EqualTo(expected).Within(1e-10));
49+
}
50+
51+
/// <summary>
52+
/// Tests that the sigmoid function approaches 0 for large negative inputs (asymptotic behavior).
53+
/// </summary>
54+
[Test]
55+
public static void GetsAsymptoticValueForLargeNegativeX()
56+
{
57+
// Arrange
58+
double x = -100.0;
59+
double expected = 0.0;
60+
61+
// Act
62+
var result = Sigmoid.Calculate(x);
63+
64+
// Assert
65+
// The result should be extremely close to 0.0.
66+
Assert.That(result, Is.EqualTo(expected).Within(1e-10));
67+
}
68+
69+
/// <summary>
70+
/// Tests the sigmoid calculation for various general positive and negative values.
71+
/// Values are confirmed against a reference calculation (or manually verified).
72+
/// </summary>
73+
/// <param name="input">The input value.</param>
74+
/// <param name="expected">The expected sigmoid output.</param>
75+
[TestCase(1.0, 0.7310585786300049)]
76+
[TestCase(5.0, 0.9933071490757153)]
77+
[TestCase(-1.0, 0.2689414213699951)]
78+
[TestCase(-5.0, 0.006692850924284855)]
79+
[TestCase(0.5, 0.6224593312018546)]
80+
[TestCase(-0.5, 0.3775406687981454)]
81+
public static void GetsStandardSigmoidValues(double input, double expected)
82+
{
83+
// Act
84+
var result = Sigmoid.Calculate(input);
85+
86+
// Assert
87+
Assert.That(result, Is.EqualTo(expected).Within(Tolerance));
88+
}
89+
90+
/// <summary>
91+
/// Tests that the calculation correctly handles floating-point values and large numbers.
92+
/// </summary>
93+
[Test]
94+
public static void HandlesFractionalAndLargeInput()
95+
{
96+
// Arrange
97+
double x1 = 3.14159; // PI approximation
98+
// Corrected expected value: 1 / (1 + e^-3.14159)
99+
double expected1 = 0.9585760624650355;
100+
101+
double x2 = -20.0;
102+
// Expected = 1 / (1 + e^20) - Should be very close to 0
103+
double expected2 = 2.0611536224385583E-9;
104+
105+
// Act & Assert 1
106+
var result1 = Sigmoid.Calculate(x1);
107+
Assert.That(result1, Is.EqualTo(expected1).Within(Tolerance));
108+
109+
// Act & Assert 2
110+
var result2 = Sigmoid.Calculate(x2);
111+
Assert.That(result2, Is.EqualTo(expected2).Within(Tolerance));
112+
}
113+
}

Algorithms/Numeric/Sigmoid.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric;
4+
5+
/// <summary>
6+
/// Provides the Sigmoid (Logistic) function, commonly used as an activation
7+
/// function in neural networks to squash values into the range (0, 1).
8+
/// Formula: sigma(x) = 1 / (1 + e^(-x)).
9+
/// </summary>
10+
public static class Sigmoid
11+
{
12+
/// <summary>
13+
/// Calculates the value of the Sigmoid function for a given input x.
14+
/// </summary>
15+
/// <param name="x">The input number.</param>
16+
/// <returns>The Sigmoid value, a double between 0 and 1.</returns>
17+
public static double Calculate(double x)
18+
{
19+
// The Sigmoid function is 1 / (1 + e^(-x))
20+
// We use Math.Exp(-x) to calculate e^(-x)
21+
double exponent = Math.Exp(-x);
22+
23+
// The result is 1.0 divided by (1.0 + exponent)
24+
return 1.0 / (1.0 + exponent);
25+
}
26+
}

0 commit comments

Comments
 (0)