Skip to content

Commit 9dff52d

Browse files
authored
Merge branch 'master' into distance/minkowski
2 parents ea99692 + 87776ee commit 9dff52d

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Algorithms.Financial;
5+
using FluentAssertions;
6+
using NUnit.Framework;
7+
8+
namespace Algorithms.Tests.Financial;
9+
10+
public static class PresentValueTests
11+
{
12+
[TestCase(0.13,new[] { 10.0, 20.70, -293.0, 297.0 },4.69)]
13+
[TestCase(0.07,new[] { -109129.39, 30923.23, 15098.93, 29734.0, 39.0 }, -42739.63)]
14+
[TestCase(0.07, new[] { 109129.39, 30923.23, 15098.93, 29734.0, 39.0 }, 175519.15)]
15+
[TestCase(0.0, new[] { 109129.39, 30923.23, 15098.93, 29734.0, 39.0 }, 184924.55)]
16+
17+
public static void Present_Value_General_Tests(double discountRate,double[] cashFlow ,double expected)
18+
=>
19+
PresentValue.Calculate(discountRate, cashFlow.ToList())
20+
.Should()
21+
.Be(expected);
22+
23+
24+
[TestCase(-1.0, new[] { 10.0, 20.70, -293.0, 297.0 })]
25+
[TestCase(1.0,new double[] {})]
26+
27+
public static void Present_Value_Exception_Tests(double discountRate, double[] cashFlow)
28+
=> Assert.Throws<ArgumentException>(() => PresentValue.Calculate(discountRate, cashFlow.ToList()));
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using Algorithms.LinearAlgebra.Distances;
3+
using FluentAssertions;
4+
using System;
5+
6+
namespace Algorithms.Tests.LinearAlgebra.Distances;
7+
8+
public class ChebyshevTests
9+
{
10+
[TestCase(new[] { 1.0, 1.0 }, new[] { 2.0, 2.0 }, 1.0)]
11+
[TestCase(new[] { 1.0, 1.0, 9.0 }, new[] { 2.0, 2.0, -5.2 }, 14.2)]
12+
[TestCase(new[] { 1.0, 2.0, 3.0 }, new[] { 1.0, 2.0, 3.0 }, 0.0)]
13+
[TestCase(new[] { 1.0, 2.0, 3.0, 4.0 }, new[] { 1.75, 2.25, -3.0, 0.5 }, 6.0)]
14+
public void DistanceTest(double[] point1, double[] point2, double expectedDistance)
15+
{
16+
Chebyshev.Distance(point1, point2).Should().BeApproximately(expectedDistance, 0.01);
17+
}
18+
19+
[TestCase(new[] { 2.0, 3.0 }, new[] { -1.0 })]
20+
[TestCase(new[] { 1.0 }, new[] { 1.0, 2.0, 3.0 })]
21+
public void DistanceThrowsArgumentExceptionOnDifferentPointDimensions(double[] point1, double[] point2)
22+
{
23+
Action action = () => Chebyshev.Distance(point1, point2);
24+
action.Should().Throw<ArgumentException>();
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Algorithms.Financial;
6+
7+
/// <summary>
8+
/// PresentValue is the value of an expected income stream determined as of the date of valuation.
9+
/// </summary>
10+
public static class PresentValue
11+
{
12+
public static double Calculate(double discountRate, List<double> cashFlows)
13+
{
14+
if (discountRate < 0)
15+
{
16+
throw new ArgumentException("Discount rate cannot be negative");
17+
}
18+
19+
if (cashFlows.Count == 0)
20+
{
21+
throw new ArgumentException("Cash flows list cannot be empty");
22+
}
23+
24+
double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum();
25+
26+
return Math.Round(presentValue, 2);
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Algorithms.LinearAlgebra.Distances;
5+
6+
/// <summary>
7+
/// Implementation of Chebyshev distance.
8+
/// It is the maximum absolute difference between the measures in all dimensions of two points.
9+
/// In other words, it is the maximum distance one has to travel along any coordinate axis to get from one point to another.
10+
///
11+
/// It is commonly used in various fields such as chess, warehouse logistics, and more.
12+
/// </summary>
13+
public static class Chebyshev
14+
{
15+
/// <summary>
16+
/// Calculate Chebyshev distance for two N-Dimensional points.
17+
/// </summary>
18+
/// <param name="point1">First N-Dimensional point.</param>
19+
/// <param name="point2">Second N-Dimensional point.</param>
20+
/// <returns>Calculated Chebyshev distance.</returns>
21+
public static double Distance(double[] point1, double[] point2)
22+
{
23+
if (point1.Length != point2.Length)
24+
{
25+
throw new ArgumentException("Both points should have the same dimensionality");
26+
}
27+
28+
// distance = max(|x1-y1|, |x2-y2|, ..., |xn-yn|)
29+
return point1.Zip(point2, (x1, x2) => Math.Abs(x1 - x2)).Max();
30+
}
31+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ find more than one implementation for the same objective but using different alg
5858
* [IHeuristicKnapsackSolver](./Algorithms/Knapsack/IHeuristicKnapsackSolver.cs)
5959
* [Linear Algebra](./Algorithms/LinearAlgebra)
6060
* [Distances](./Algorithms/LinearAlgebra/Distances)
61+
* [Chebyshev](./Algorithms/LinearAlgebra/Distances/Chebyshev.cs)
6162
* [Euclidean](./Algorithms/LinearAlgebra/Distances/Euclidean.cs)
6263
* [Manhattan](./Algorithms/LinearAlgebra/Distances/Manhattan.cs)
6364
* [Minkowski](./Algorithms/LinearAlgebra/Distances/Minkowski.cs)

0 commit comments

Comments
 (0)