-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyields.go
43 lines (35 loc) · 1.1 KB
/
yields.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
package fixedincome
import (
"github.com/khezen/rootfinding"
"github.com/konimarti/fixedincome/pkg/term"
)
var (
Precision = 6
)
// Irr calculates the internal rate of return of a security
func Irr(investment float64, s Security) (float64, error) {
f := func(irr float64) float64 {
return s.PresentValue(&term.Flat{irr, 0.0}) - investment
}
root, err := rootfinding.Brent(f, -20.0, 20.0, Precision)
return root, err
}
// Spread calculates the implied static (zero-volatility) spread
func Spread(investment float64, s Security, ts term.Structure) (float64, error) {
f := func(spread float64) float64 {
value := s.PresentValue(ts.SetSpread(spread))
return value - investment
}
root, err := rootfinding.Brent(f, -10000.0, 10000.0, Precision)
return root, err
}
// ImpliedVola calculates the implied volatility for a given option price
func ImpliedVola(price float64, o Option, ts term.Structure) (float64, error) {
f := func(vola float64) float64 {
o.SetVola(vola)
value := o.PresentValue(ts)
return value - price
}
root, err := rootfinding.Brent(f, 0.0, 1000.0, Precision)
return root, err
}