-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyields_test.go
133 lines (118 loc) · 2.86 KB
/
yields_test.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
128
129
130
131
132
133
package fixedincome_test
import (
"fmt"
"math"
"testing"
"time"
"github.com/konimarti/fixedincome"
"github.com/konimarti/fixedincome/pkg/instrument/bond"
"github.com/konimarti/fixedincome/pkg/instrument/option"
"github.com/konimarti/fixedincome/pkg/maturity"
"github.com/konimarti/fixedincome/pkg/term"
)
func TestYields(t *testing.T) {
testData := []struct {
B *bond.Straight
Quote float64
ExpectedIRR float64
ExpectedSpread float64
}{
{
// bond details
// ISIN CH0224396983 (quote per 2021-04-01)
B: &bond.Straight{
Schedule: maturity.Schedule{
Settlement: time.Date(2021, 4, 1, 0, 0, 0, 0, time.UTC),
Maturity: time.Date(2026, 5, 28, 0, 0, 0, 0, time.UTC),
Frequency: 1,
},
Redemption: 100.0,
Coupon: 1.25,
},
Quote: 109.70,
ExpectedIRR: -0.574,
ExpectedSpread: 0.2,
},
{
// ISIN CH0193265995 (quote per 2021-04-16)
B: &bond.Straight{
Schedule: maturity.Schedule{
Settlement: time.Date(2021, 4, 15, 0, 0, 0, 0, time.UTC),
Maturity: time.Date(2022, 9, 21, 0, 0, 0, 0, time.UTC),
Frequency: 1,
},
Redemption: 100.0,
Coupon: 1.00,
},
Quote: 102.22,
ExpectedIRR: -0.54,
ExpectedSpread: 24.8,
},
}
// term structure (parameters per 2021-04-01 for CH govt bonds)
term := term.NelsonSiegelSvensson{
-0.266372,
-0.471343,
5.68789,
-5.12324,
5.74881,
4.14426,
0.0, // spread
}
// loop over tests
for nr, test := range testData {
// IRR
irr, err := fixedincome.Irr(test.Quote+test.B.Accrued(), test.B)
// fmt.Println(irr)
if err != nil {
fmt.Println(err)
t.Errorf("irr failed for test nr %d", nr)
}
// fmt.Println("Remaining years:", test.B.RemainingYears())
if math.Abs(irr-test.ExpectedIRR) > 0.1 {
t.Errorf("wrong IRR for test nr %d, got %f, expected %f", nr, irr, test.ExpectedIRR)
}
// Z-Spread
spread, err := fixedincome.Spread(test.Quote+test.B.Accrued(), test.B, &term)
// fmt.Println(zspread)
if err != nil {
fmt.Println(err)
t.Errorf("zspread failed for test nr %d", nr)
}
if math.Abs(spread-test.ExpectedSpread) > 0.1 {
t.Errorf("wrong Z-Spread for test nr %d, got %f, expected %f", nr, spread, test.ExpectedSpread)
}
}
}
func TestImpliedVola(t *testing.T) {
testOption := option.European{
option.Call,
110.0,
100.0,
2.0,
0.0,
math.Pi,
}
ts := term.Flat{2.0, 0.0}
expected := 0.3
tests := []struct {
OptionType int
Price float64
}{
{
OptionType: option.Call,
Price: 25.1291,
},
{
OptionType: option.Put,
Price: 11.2080,
},
}
for _, test := range tests {
testOption.Type = test.OptionType
vola, err := fixedincome.ImpliedVola(test.Price, &testOption, &ts)
if err != nil || math.Abs(vola-expected) > 0.0001 {
t.Error("Type", test.OptionType, "Got", vola, "Expected", expected)
}
}
}