/
polyfit.go
205 lines (161 loc) · 3.58 KB
/
polyfit.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Package polyfit models a polynomial y from sample points xs and ys, to minimizes the squared residuals.
//
// See https://en.wikipedia.org/wiki/Least_squares#Linear_least_squares
//
// Since 0.5.4
package polyfit
import (
"fmt"
"strings"
"gonum.org/v1/gonum/mat"
)
// Fitting models a polynomial y from sample points xs and ys, to minimizes the squared residuals.
// It returns coefficients of the polynomial y:
//
// y = β₁ + β₂x + β₃x² + ...
//
// It use linear regression, which assumes y is in form of:
// m
// y = ∑ βⱼ Φⱼ(x)
// j=1
//
// In our case:
// Φⱼ(x) = x^(j-1)
//
// Then
// (Xᵀ × X) βⱼ = Xᵀ × Y
// Xᵢⱼ = [ Φⱼ(xᵢ) ]
//
// See https://en.wikipedia.org/wiki/Least_squares#Linear_least_squares
//
// Since 0.5.4
type Fitting struct {
N int
Degree int
// cache Xᵀ X
xtx []float64
// cache Xᵀ Y
xty []float64
}
// NewFitting creates a new polynomial fitting context.
//
// Since 0.5.4
func NewFitting(xs, ys []float64, degree int) *Fitting {
n := len(xs)
m := degree + 1
f := &Fitting{
N: 0,
Degree: degree,
xtx: make([]float64, m*m),
xty: make([]float64, m),
}
for i := 0; i < m*m; i++ {
f.xtx[i] = 0
}
for i := 0; i < m; i++ {
f.xty[i] = 0
}
for i := 0; i < n; i++ {
f.Add(xs[i], ys[i])
}
return f
}
// Add a point(x, y) into this fitting.
//
// Since 0.5.4
func (f *Fitting) Add(x, y float64) {
m := f.Degree + 1
xpows := make([]float64, m)
v := float64(1)
for i := 0; i < m; i++ {
xpows[i] = v
v *= x
}
for i := 0; i < m; i++ {
for j := 0; j < m; j++ {
f.xtx[i*m+j] += xpows[i] * xpows[j]
}
}
for i := 0; i < m; i++ {
f.xty[i] += xpows[i] * y
}
f.N++
}
// Merge Combines two sets of sample data.
//
// This can be done because:
// |X₁|ᵀ × |X₁| = X₁ᵀ × X₁ + X₂ᵀ × X₂
// |X₂| |X₂|
//
// Since 0.5.4
func (f *Fitting) Merge(b *Fitting) {
if f.Degree != b.Degree {
panic(fmt.Sprintf("different degree: %d %d", f.Degree, b.Degree))
}
f.N += b.N
m := f.Degree + 1
for i := 0; i < m; i++ {
f.xty[i] += b.xty[i]
for j := 0; j < m; j++ {
f.xtx[i*m+j] += b.xtx[i*m+j]
}
}
}
// Solve the equation and returns coefficients of result polynomial.
// The number of coefficients is f.Degree + 1.
//
// Since 0.5.4
func (f *Fitting) Solve(minimizeDegree bool) []float64 {
m := f.Degree + 1
coef := mat.NewDense(m, m, f.xtx)
right := mat.NewDense(m, 1, f.xty)
if minimizeDegree && f.Degree+1 > f.N {
m = f.N
coef = coef.Slice(0, m, 0, m).(*mat.Dense)
right = right.Slice(0, m, 0, 1).(*mat.Dense)
}
var beta mat.Dense
beta.Solve(coef, right)
rst := make([]float64, f.Degree+1)
for i := 0; i < m; i++ {
rst[i] = beta.At(i, 0)
}
for i := m; i < f.Degree+1; i++ {
rst[i] = 0
}
return rst
}
// String prints human readable info of a fitting.
// It includes:
// n: the number of points.
// degree: expected degree of polynomial.
// and two matrix.
//
// Since 0.5.4
func (f *Fitting) String() string {
m := f.Degree + 1
ss := []string{}
xtx := f.matrixStrings(f.xtx)
ss = append(ss, fmt.Sprintf("n=%d degree=%d", f.N, f.Degree))
ss = append(ss, xtx...)
ss = append(ss, "")
for i := 0; i < m; i++ {
s := fmt.Sprintf("%3.3f", f.xty[i])
ss = append(ss, s)
}
return strings.Join(ss, "\n")
}
func (f *Fitting) matrixStrings(mat []float64) []string {
m := f.Degree + 1
ss := []string{}
for i := 0; i < m; i++ {
line := []string{}
for j := 0; j < m; j++ {
s := fmt.Sprintf("%3.3f", mat[i*m+j])
line = append(line, s)
}
linestr := strings.Join(line, " ")
ss = append(ss, linestr)
}
return ss
}