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

feature: Irr function #22

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ which are as follows:
| nper | ✅ | Computes the number of periodic payments|
| pv | ✅ | Computes the present value of a payment|
| rate | ✅ | Computes the rate of interest per period|
| irr | | Computes the internal rate of return|
| irr | | Computes the internal rate of return|
| npv | ✅ | Computes the net present value of a series of cash flow|
| mirr | | Computes the modified internal rate of return|

Expand All @@ -49,6 +49,8 @@ While the numpy-financial package contains a set of elementary financial functio
+ [Example(Nper-Loan)](#examplenper-loan)
* [Rate(Interest Rate)](#rate)
+ [Example(Rate-Investment)](#examplerate-investment)
* [Irr(Internal Rate of Return)](#irr)
+ [Example(Irr)](#exampleirr)

Detailed documentation is available at [godoc](https://godoc.org/github.com/razorpay/go-financial).
## Amortisation(Generate Table)
Expand Down Expand Up @@ -638,3 +640,58 @@ func main() {
}
```
[Run on go-playground](https://play.golang.org/p/H2uybe1dbRj)

## Irr

```go
func Irr(values []decimal.Decimal, maxIter int64, tolerance, prev_point, next_point decimal.Decimal) (decimal.Decimal, error)
```

Params:
```text
values : the value of the cash flow for that time period. Values provided here must be an array of float64
maxIter : total number of iterations for which the function should run
tolerance : accept result only if the difference in iteration values is less than the tolerance provided
prev_point : an initial point to start approximating from
next_point : next point to use for secant
```

Returns:
```text
rate : a rate for the corresponding values
error : returns nil if NPV is close to zero (returns an error conversely)
```

Irr computes the rate to ensure a net zero cashflow

### Example(Irr)

If an initial inflow of $123400 is done followed by successive outflows of $36200, $54800 and $48100; for what value of rate does the npv evalute to zero ? (assuming 100 iterations, 1e-6 threshold and 0.1 and 0.2 as initial guessing points)

```go
package main

import (
"fmt"
gofinancial "github.com/razorpay/go-financial"
"github.com/shopspring/decimal"
)

func main() {
cashflow := []decimal.Decimal{decimal.NewFromFloat(-123400), decimal.NewFromFloat(36200), decimal.NewFromFloat(54800), decimal.NewFromFloat(48100)}
maxIter := 100
tolerance := decimal.NewFromFloat(1e-6)
prev_point := decimal.NewFromFloat(0.1)
next_point := decimal.NewFromFloat(0.2)

irr, err := gofinancial.Irr(cashflow, maxIter, tolerance, prev_point, next_point)
if err != nil {
fmt.Printf(err)
} else {
fmt.Printf("irr: %v ", irr)
}
// Output:
// irr: 0.05961637856732953787613704103503
}
```
[Run on go-playground](https://play.golang.org/p/izqvtBWEOqJ)
34 changes: 34 additions & 0 deletions reducing_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,37 @@ func Rate(pv, fv, pmt decimal.Decimal, nper int64, when paymentperiod.Type, maxI
}
return nextIterRate, nil
}

/*
IRR computes the rate of return for a balanced cashflow (if feasible!) using the secant method of approximation.

Params:
values : the value of the cash flow for that time period. Values provided here must be an array of float64
maxIter : total number of iterations for which the function should run
tolerance : accept result only if the difference in iteration values is less than the tolerance provided
prev_point : an initial point to start approximating from
next_point : next point to use for secant

References:
[G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
Addison-Wesley, 2003, pg. 348.
*/
func Irr(values []decimal.Decimal, maxIter int64, tolerance, prev_point, next_point decimal.Decimal) (decimal.Decimal, error) {
x_p := prev_point
x_n := next_point

for i := int64(0); i < maxIter; i++ {
y_p := Npv(x_p, values)
y_n := Npv(x_n, values)
_v1 := x_n.Sub(x_p).Div(y_n.Sub(y_p).Add(decimal.NewFromFloat(0.0000001)))
_v2 := y_n.Neg().Mul(_v1)
x_n, x_p = x_n.Add(_v2), x_n

}

if Npv(x_n, values).LessThan(tolerance) {
return x_n, nil
}

return decimal.Zero, ErrOutOfBounds
}
61 changes: 61 additions & 0 deletions reducing_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,64 @@ func Test_Rate(t *testing.T) {
})
}
}

func Test_Irr(t *testing.T) {
type args struct {
cashFlow []decimal.Decimal
maxIter int64
tolerance decimal.Decimal
prev_point decimal.Decimal
next_point decimal.Decimal
}
tests := []struct {
name string
args args
want decimal.Decimal
anyErr error
}{
{
name: "success",
args: args{
cashFlow: []decimal.Decimal{decimal.NewFromFloat(1000), decimal.NewFromFloat(-900), decimal.NewFromFloat(200), decimal.NewFromFloat(-400)},
maxIter: 100,
tolerance: decimal.NewFromFloat(1e-6),
prev_point: decimal.NewFromFloat(0.1),
next_point: decimal.NewFromFloat(0.2),
},
want: decimal.NewFromFloat(0.06491271005017),
anyErr: nil,
},
{
name: "success",
args: args{
cashFlow: []decimal.Decimal{decimal.NewFromFloat(-123400), decimal.NewFromFloat(36200), decimal.NewFromFloat(54800), decimal.NewFromFloat(48100)},
maxIter: 100,
tolerance: decimal.NewFromFloat(1e-6),
prev_point: decimal.NewFromFloat(0.1),
next_point: decimal.NewFromFloat(0.2),
},
want: decimal.NewFromFloat(0.0596163785673),
anyErr: nil,
},
{
name: "failure",
args: args{
cashFlow: []decimal.Decimal{decimal.NewFromFloat(1000), decimal.NewFromFloat(-900), decimal.NewFromFloat(200)},
maxIter: 100,
tolerance: decimal.NewFromFloat(1e-6),
prev_point: decimal.NewFromFloat(0.1),
next_point: decimal.NewFromFloat(0.2),
},
want: decimal.Zero,
anyErr: ErrOutOfBounds,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := Irr(tt.args.cashFlow, tt.args.maxIter, tt.args.tolerance, tt.args.prev_point, tt.args.next_point); err != tt.anyErr || isAlmostEqual(got, tt.want, decimal.NewFromFloat(precision)) != nil {
t.Errorf("IRR returned (%v,%v), wanted (%v,%v)", got, err, tt.want, tt.anyErr)
}
})
}
}