-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.go
176 lines (149 loc) · 3.43 KB
/
model.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/json"
"errors"
"io"
)
// Model represents an immutable target weighted allocation of assets
type Model interface {
Symbols() Symbols
TargetProportion(s Symbol) float64
Equivalents(s Symbol) Symbols
Contains(s Symbol) bool
IsRelevant(s Symbol) bool
Validate() error
}
type modelImpl struct {
Assets map[Symbol]modelAsset `json:"assets"`
}
func LoadModelFromJson(r io.Reader) (Model, error) {
m := new(modelImpl)
if err := json.NewDecoder(r).Decode(m); err != nil {
return nil, err
}
if err := m.Validate(); err != nil {
return nil, err
}
return m, nil
}
func MustLoadModelFromJson(r io.Reader) Model {
m, err := LoadModelFromJson(r)
if err != nil {
panic(err)
}
return m
}
func (m modelImpl) Symbols() Symbols {
ss := NewSymbols()
for s := range m.Assets {
ss = ss.Add(s)
}
return ss
}
func (m modelImpl) TargetProportion(s Symbol) float64 {
if m.Contains(s) {
return m.Assets[s].Weight / m.totalWeight()
}
return 0
}
func (m modelImpl) totalWeight() (totalWeight float64) {
for _, a := range m.Assets {
totalWeight += a.Weight
}
return
}
func (m modelImpl) Equivalents(s Symbol) Symbols {
if m.Contains(s) {
return NewSymbols().Add(m.Assets[s].Equivalents...)
}
return NewSymbols()
}
func (m modelImpl) Contains(s Symbol) bool {
_, ok := m.Assets[s]
return ok
}
func (m modelImpl) IsRelevant(s Symbol) bool {
return m.relevantSymbols().Contains(s)
}
func (m modelImpl) relevantSymbols() Symbols {
rs := m.Symbols()
for _, a := range m.Assets {
for _, e := range a.Equivalents {
rs = rs.Add(e)
}
}
return rs
}
func (m modelImpl) equivalentSymbols() Symbols {
ss := NewSymbols()
for _, a := range m.Assets {
for _, e := range a.Equivalents {
ss = ss.Add(e)
}
}
return ss
}
var (
ErrModelIsEmpty = errors.New("model is empty")
ErrContainsEquivalent = errors.New("model contains equivalent")
ErrDuplicatedEquivalents = errors.New("an equivalent symbol is linked to more than one symbol in the asset")
ErrNonPositiveWeight = errors.New("model contains non-positive weight")
)
func (m modelImpl) Validate() error {
var validationFns = []func() error{
m.checkNotEmpty,
m.checkPositiveWeights,
m.checkNoEquivalent,
m.checkSymbolEquivalentOneToMany,
}
for _, fn := range validationFns {
if err := fn(); err != nil {
return err
}
}
return nil
}
func (m modelImpl) checkNoEquivalent() error {
for _, a := range m.Assets {
for _, e := range a.Equivalents {
if m.Contains(e) {
return ErrContainsEquivalent
}
}
}
return nil
}
func (m modelImpl) checkSymbolEquivalentOneToMany() error {
equivalentsSeen := NewSymbols()
for _, a := range m.Assets {
for _, e := range a.Equivalents {
if equivalentsSeen.Contains(e) {
return ErrDuplicatedEquivalents
}
equivalentsSeen = equivalentsSeen.Add(e)
}
}
return nil
}
func (m modelImpl) checkPositiveWeights() error {
for _, a := range m.Assets {
if a.Weight < 0 {
return ErrNonPositiveWeight
}
}
return nil
}
func (m modelImpl) checkNotEmpty() error {
if len(m.Assets) == 0 {
return ErrModelIsEmpty
}
return nil
}
// modelAsset represents an asset in a Model
type modelAsset struct {
// Equivalents of an asset are considered to be the same as itself
// if a Symbol appears as an modelAsset's Equivalents, it is not allowed
// to appear as a modelAsset by itself in the same Model.
Equivalents []Symbol `json:"equivalents"`
Weight float64 `json:"weight"`
}