-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
183 lines (156 loc) · 4.02 KB
/
types.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
177
178
179
180
181
182
183
package openforecast
import (
"fmt"
"sort"
"strconv"
"strings"
"sync"
)
type DataPoint interface {
SetDependentValue(v float64)
DependentValue() float64
SetIndependentValue(k string, v float64)
IndependentValue(k string) (v float64, ok bool)
IndependentVariableNames() []string
Equals(DataPoint) bool
fmt.Stringer
}
type DataSet struct {
Points []DataPoint
timeVariable string
periodsPerYear int
}
func (ds *DataSet) TimeVariable() string { return ds.timeVariable }
func (ds *DataSet) PeriodsPerYear() int { return ds.periodsPerYear }
func NewDataSetCopy(dataSet *DataSet) *DataSet {
return NewDataSet(dataSet.TimeVariable(), dataSet.PeriodsPerYear(), dataSet.Points)
}
func NewDataSet(timeVariable string, periodsPerYear int, points []DataPoint) *DataSet {
ds := &DataSet{
timeVariable: timeVariable,
periodsPerYear: periodsPerYear,
Points: make([]DataPoint, 0, len(points)),
}
// deep copy DataPoint which is reference
for _, dp := range points {
ds.Points = append(ds.Points, NewObservationCopy(dp))
}
return ds
}
func (ds *DataSet) IndependentVariables() []string {
m := make(map[string]struct{})
for _, dp := range ds.Points {
for _, k := range dp.IndependentVariableNames() {
m[k] = struct{}{}
}
}
names := make([]string, 0, len(m))
for k := range m {
names = append(names, k)
}
return names
}
func (ds *DataSet) Sort(independentVariable string) {
sort.Slice(ds.Points, func(i, j int) bool {
vi, _ := ds.Points[i].IndependentValue(independentVariable)
vj, _ := ds.Points[j].IndependentValue(independentVariable)
return vi < vj
})
}
type ForecastingModel interface {
Type() string
Train(*DataSet) error
Forecast(DataPoint) (float64, error)
ForecastAll(*DataSet) (*DataSet, error)
AIC() float64
Bias() float64
MAD() float64
MAPE() float64
MSE() float64
SAE() float64
NumberOfPredictors() int
}
type StreamingModel interface {
// TODO(StitchCula): DataSet or DataPoint?
Update(DataPoint) error
}
type Observation struct {
dependentValue float64
independentValues map[string]float64
mu *sync.RWMutex
}
func NewObservation(dependentValue float64) *Observation {
o := &Observation{
dependentValue: dependentValue,
independentValues: make(map[string]float64),
mu: new(sync.RWMutex),
}
return o
}
func NewObservationCopy(dataPoint DataPoint) *Observation {
o := NewObservation(dataPoint.DependentValue())
for _, k := range dataPoint.IndependentVariableNames() {
o.independentValues[k], _ = dataPoint.IndependentValue(k)
}
return o
}
func (o *Observation) SetDependentValue(v float64) {
o.dependentValue = v
}
func (o *Observation) DependentValue() float64 {
return o.dependentValue
}
func (o *Observation) SetIndependentValue(k string, v float64) {
o.mu.Lock()
o.independentValues[k] = v
o.mu.Unlock()
}
func (o *Observation) IndependentValue(k string) (v float64, ok bool) {
o.mu.RLock()
v, ok = o.independentValues[k]
o.mu.RUnlock()
return
}
func (o *Observation) IndependentVariableNames() []string {
o.mu.RLock()
defer o.mu.RUnlock()
ks := make([]string, 0, len(o.independentValues))
for k := range o.independentValues {
ks = append(ks, k)
}
return ks
}
// Equals goroutine-unsafe
func (o *Observation) Equals(dataPoint DataPoint) bool {
if o.DependentValue() != dataPoint.DependentValue() {
return false
}
ks1 := o.IndependentVariableNames()
ks2 := dataPoint.IndependentVariableNames()
if len(ks1) != len(ks2) {
return false
}
for _, k := range ks1 {
v1, _ := o.IndependentValue(k)
if v2, ok := dataPoint.IndependentValue(k); !ok || v1 != v2 {
return false
}
}
return true
}
func (o *Observation) String() string {
buf := strings.Builder{}
buf.WriteByte('(')
o.mu.RLock()
for k, v := range o.independentValues {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
buf.WriteString(", ")
}
o.mu.RUnlock()
buf.WriteString("dependentValue=")
buf.WriteString(strconv.FormatFloat(o.dependentValue, 'f', -1, 64))
buf.WriteByte(')')
return buf.String()
}