Skip to content

Commit

Permalink
Merge pull request #29 from lazarospsa/ModifiedInternalRateOfReturn
Browse files Browse the repository at this point in the history
Modified internal rate of return
  • Loading branch information
lazarospsa authored Dec 18, 2023
2 parents 13b08be + 96387c2 commit 87271ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions gofin.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ func PresentValuePerpetuity(interestRate, cashFlow float64) float64 {
return cashFlow / interestRate
}

// ModifiedInternalRateOfReturn calculates the Modified Internal Rate of Return (MIRR)
func ModifiedInternalRateOfReturn(initialInvestment float64, cashOutflows []float64, cashInflows []float64, financeRate float64) float64 {
npvOutflows := -initialInvestment
for i, cashOutflow := range cashOutflows {
npvOutflows += cashOutflow / math.Pow(1+financeRate, float64(i+1))
}

npvInflows := 0.0
for i, cashInflow := range cashInflows {
npvInflows += cashInflow / math.Pow(1+financeRate, float64(i+1))
}

mirr := math.Pow((npvInflows / -npvOutflows), 1/float64(len(cashOutflows))) - 1
return mirr
}

// PresentValuePerpetuityDue returns the present value of a perpetuity due.
// The present value of a perpetuity due is the cash flow divided by the discount rate multiplied by 1 plus the discount rate.
// The interest rate must be greater than zero to avoid division by zero.
Expand Down
14 changes: 14 additions & 0 deletions gofin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ func TestPresentValuePerpetuityDue(t *testing.T) {
}
}

func TestModifiedInternalRateOfReturn(t *testing.T) {
var initialInvestment float64 = 1000
var cashOutflows []float64 = []float64{100, 100, 100, 100, 100}
var cashInflows []float64 = []float64{100, 100, 100, 100, 100}
var financeRate float64 = 0.1
var expected float64 = 0.1

actual := ModifiedInternalRateOfReturn(initialInvestment, cashOutflows, cashInflows, financeRate)

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

func TestPresentValueGrowingPerpetuity(t *testing.T) {
var interestRate float64 = 0.1
var growthRate float64 = 0.1
Expand Down

0 comments on commit 87271ae

Please sign in to comment.