-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (154 loc) · 3.97 KB
/
main.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 (
"bufio"
"flag"
"fmt"
"image/color"
"log"
"os"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg/draw"
)
// Use bash to run this with an increasing number of iterations:
// ```
// for n in $(seq 0 100)
// do
// go run main.go -n $n; sleep 1
// done
// ```
var iterations int
func main() {
flag.IntVar(&iterations, "n", 1000, "number of iterations")
flag.Parse()
xys, err := readData("data.txt")
if err != nil {
log.Fatalf("could not read data.txt: %v", err)
}
err = plotData("out.png", xys)
if err != nil {
log.Fatalf("could not plot data: %v", err)
}
}
type xy struct{ x, y float64 }
func readData(path string) (plotter.XYs, error) {
// read line by line, not all at once (like ioutil.ReadFile)
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var xys plotter.XYs
s := bufio.NewScanner(f)
for s.Scan() {
var x, y float64
_, err := fmt.Sscanf(s.Text(), "%f,%f", &x, &y)
if err != nil {
log.Printf("discarding bad data point: %q: %v", s.Text(), err)
}
xys = append(xys, plotter.XY{X: x, Y: y})
}
if err := s.Err(); err != nil {
return nil, fmt.Errorf("could not scan: %v", err)
}
return xys, nil
}
func plotData(path string, xys plotter.XYs) error {
// Create a file to write the plot to
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("could not create %s: %v", path, err)
}
// Plot the data
p, err := plot.New()
if err != nil {
return fmt.Errorf("could not create plot: %v", err)
}
// create scatter with all data points
s, err := plotter.NewScatter(xys)
s.GlyphStyle.Shape = draw.CrossGlyph{}
s.Color = color.RGBA{R: 255, A: 255}
if err != nil {
return fmt.Errorf("could not create scatter: %v", err)
}
p.Add(s)
// create linear regression result
m, c := linearRegression(xys, 0.01)
l, err := plotter.NewLine(plotter.XYs{
{X: 3, Y: (3 * m) + c}, {X: 20, Y: (20 * m) + c},
})
if err != nil {
return fmt.Errorf("could not create line: %v", err)
}
p.Add(l)
wt, err := p.WriterTo(512, 512, "png")
if err != nil {
return fmt.Errorf("could not create writer: %v", err)
}
_, err = wt.WriteTo(f)
if err != nil {
return fmt.Errorf("could not write to %s: %v", path, err)
}
// Make sure the file closes properly,
// otherwise the data might not have plotted correctly
if err = f.Close(); err != nil {
return fmt.Errorf("could not close %s: %v", path, err)
}
return nil
}
func linearRegression(xys plotter.XYs, alpha float64) (m, c float64) {
// NOTE: This is a simple way of finding the line of best fit
// ...BUT, it takes 4004001 iterations
// const (
// min = -100.0
// max = 100.0
// delta = 0.1
// )
// count := 0
// minCost := math.MaxFloat64
// for im := min; im < max; im += delta {
// for ic := min; ic < max; ic += delta {
// count++
// cost := computeCost(xys, im, ic)
// if cost < minCost {
// minCost = cost
// m, c = im, ic
// dm, dc := computeGradient(xys, m, c)
// fmt.Printf("grad(%.2f, %.2f) = (%.2f, %.2f)\n", m, c, dm, dc)
// }
// }
// }
// fmt.Printf("cost(%.2f, %.2f) = %.2f\n", m, c, computeCost(xys, m, c))
// fmt.Printf("tried %d times\n", count)
// NOTE: Instead, lets use gradient descent:
for i := 0; i < iterations; i++ {
dm, dc := computeGradient(xys, m, c)
m += -dm * alpha
c += -dc * alpha
// fmt.Printf("grad(%.2f, %.2f) = (%.2f, %.2f)\n", m, c, dm, dc)
}
fmt.Printf("cost(%.2f, %.2f) = %.2f\n", m, c, computeCost(xys, m, c))
return m, c
}
func computeCost(xys plotter.XYs, m, c float64) float64 {
// cost = 1/N * sum((y - (m*x+c))^2)
s := 0.0
for _, xy := range xys {
d := xy.Y - (xy.X*m + c)
s += d * d
}
return s / float64(len(xys))
}
func computeGradient(xys plotter.XYs, m, c float64) (dm, dc float64) {
// cost = 1/N sum((y - (m*x+c))^2)
// d = (y - (m*x+c))
// cost/dm = 2/N * sum(-x * d)
// cost/dc = 2/N * sum(-d)
for _, xy := range xys {
d := xy.Y - (xy.X*m + c)
dm += -xy.X * d
dc += -d
}
n := float64(len(xys))
return 2 / n * dm, 2 / n * dc
}