-
Notifications
You must be signed in to change notification settings - Fork 1.6k
create present value module #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3883d68
create financial module (price plus tax)and tests
alirezasariri78 03b19b7
init present Value Module
alirezasariri78 5b536cc
Merge branch 'master' into master
alirezasariri78 25cc43d
use testcase in financial tests
alirezasariri78 08b70b9
fix null checking in financial
alirezasariri78 66eeaf6
cover empty cash flow exception
alirezasariri78 8410764
Merge branch 'master' into master
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Algorithms.Financial; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Financial; | ||
|
||
public static class PresentValueTests | ||
{ | ||
[Test] | ||
public static void Present_Value_General_Tests() | ||
{ | ||
PresentValue.Calculate(0.13, [10.0, 20.70, -293.0, 297.0]) | ||
.Should() | ||
.Be(4.69); | ||
|
||
PresentValue.Calculate(0.07, [-109129.39, 30923.23, 15098.93, 29734.0, 39.0]) | ||
.Should() | ||
.Be(-42739.63); | ||
|
||
PresentValue.Calculate(0.07, [109129.39, 30923.23, 15098.93, 29734.0, 39.0]) | ||
.Should() | ||
.Be(175519.15); | ||
|
||
PresentValue.Calculate(0.0, [109129.39, 30923.23, 15098.93, 29734.0, 39.0]) | ||
.Should() | ||
.Be(184924.55); | ||
} | ||
|
||
[Test] | ||
public static void Present_Value_Exception_Tests() | ||
{ | ||
Assert.Throws<ArgumentException>(() => PresentValue.Calculate(-1.0, [10.0, 20.70, -293.0, 297.0])); | ||
Assert.Throws<ArgumentException>(() => PresentValue.Calculate(1.0, [])); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Algorithms.Financial; | ||
|
||
/// <summary> | ||
/// PresentValue is the value of an expected income stream determined as of the date of valuation. | ||
/// </summary> | ||
public static class PresentValue | ||
{ | ||
public static double Calculate(double discountRate, List<double> cashFlows) | ||
{ | ||
if (discountRate < 0) | ||
{ | ||
throw new ArgumentException("Discount rate cannot be negative"); | ||
} | ||
|
||
if (cashFlows == null || cashFlows.Count == 0) | ||
{ | ||
throw new ArgumentException("Cash flows list cannot be empty"); | ||
} | ||
|
||
double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum(); | ||
|
||
return Math.Round(presentValue, 2); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.