-
Notifications
You must be signed in to change notification settings - Fork 0
/
xirr.go
127 lines (102 loc) · 2.06 KB
/
xirr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package xirr
import (
"fmt"
"math"
"sort"
"time"
)
const MaxError float64 = 1e-10
const MaxComputeWithGuessIterations uint32 = 50
type (
// Payment is a single payment.
Payment struct {
Date time.Time
Amount float64
}
Payments []Payment
)
func (p Payments) Len() int {
return len(p)
}
func (p Payments) Less(i, j int) bool {
return p[i].Date.Before(p[j].Date)
}
func (p Payments) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
return
}
// Compute computes xirr for given payments.
// The payments should contain atleast 1 positive & 1 negative cash flow.
func Compute(payments Payments) (float64, error) {
if err := validate(payments); err != nil {
return 0, err
}
// Sort by date.
sort.Sort(payments)
var (
rate = computeWithGuess(payments, 0.1)
guess = -0.99
)
for guess < 1 && (math.IsNaN(rate) || math.IsInf(rate, 0)) {
rate = computeWithGuess(payments, guess)
guess += 0.01
}
return rate, nil
}
func computeWithGuess(payments []Payment, guess float64) float64 {
var (
r = guess
e = 1.0
)
for i := 0; i < int(MaxComputeWithGuessIterations); i++ {
if e <= MaxError {
return r
}
r1 := r - xirr(payments, r)/dxirr(payments, r)
e = math.Abs(r1 - r)
r = r1
}
return math.NaN()
}
func xirr(payments []Payment, rate float64) float64 {
var result float64
for _, p := range payments {
exp := getExp(p, payments[0])
result += p.Amount / math.Pow(1+rate, exp)
}
return result
}
func dxirr(payments []Payment, rate float64) float64 {
var result float64
for _, p := range payments {
exp := getExp(p, payments[0])
result -= p.Amount * exp / math.Pow(1+rate, exp+1)
}
return result
}
func validate(payments []Payment) error {
var (
positive, negative bool
)
for _, p := range payments {
if p.Amount > 0 {
positive = true
break
}
}
for _, p := range payments {
if p.Amount < 0 {
negative = true
break
}
}
if positive && negative {
return nil
} else {
return fmt.Errorf("invalid payments")
}
}
func getExp(p, p0 Payment) float64 {
d := p.Date.Sub(p0.Date).Hours() / 24
return d / 365.0
}