Skip to content
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

HoldingPeriodReturnAnnualized #17 #23

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions gofin.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ func HoldingPeriodReturnPercentage(initialValue, finalValue float64) float64 {
return ((finalValue - initialValue) / initialValue)*100
}

// HoldingPeriodReturnAnnualized calculates the annualized holding period return (HPR)
func HoldingPeriodReturnAnnualized(initialValue, finalValue float64, holdingPeriodInYears float64) float64 {
hpr := HoldingPeriodReturn(initialValue, finalValue)
return math.Pow(1+hpr, 1/holdingPeriodInYears) - 1
}

// HoldingPeriodReturnAnnualized calculates the annualized holding period return (HPR)
// as a percentage
func HoldingPeriodReturnAnnualizedPercentage(initialValue, finalValue float64, holdingPeriodInYears float64) float64 {
hpr := HoldingPeriodReturn(initialValue, finalValue)
return (math.Pow(1+hpr, 1/holdingPeriodInYears) - 1)*100
}

func PresentValueAnnuityDue(interestRate float64, periods int, cashFlows []float64) float64 {
presentValueAnnuityDue := 0.0
for i := 0; i < len(cashFlows); i++ {
Expand Down
26 changes: 25 additions & 1 deletion gofin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func TestHoldingPeriodReturn(t *testing.T) {
}
}


func TestHoldingPeriodReturnPercentage(t *testing.T) {
var initialValue float64 = 1000.0
var finalValue float64 = 1200.0
Expand All @@ -89,6 +88,31 @@ func TestHoldingPeriodReturnPercentage(t *testing.T) {
}
}

func TestHoldingPeriodReturnAnnualized(t *testing.T) {
var initialValue float64 = 1000.0
var finalValue float64 = 1200.0
var holdingPeriodInYears = 2.0
var expected float64 = 0.10

actual := HoldingPeriodReturnAnnualized(initialValue, finalValue, holdingPeriodInYears)

if compareFloat64(actual, expected) {
t.Errorf("Test failed, expected: '%f', got: '%f'", expected, actual)
}
}

func TestHoldingPeriodReturnAnnualizePercentage(t *testing.T) {
var initialValue float64 = 1000.0
var finalValue float64 = 1200.0
var holdingPeriodInYears = 2.0
var expected float64 = 9.54

actual := HoldingPeriodReturnAnnualizedPercentage(initialValue, finalValue, holdingPeriodInYears)

if compareFloat64(actual, expected) {
t.Errorf("Test failed, expected: '%f', got: '%f'", expected, actual)
}
}

func TestFutureValueAnnuity(t *testing.T) {
var payment float64 = 100
Expand Down
Loading