forked from GoesToEleven/GolangTraining
/
stats.go
executable file
·111 lines (93 loc) · 1.93 KB
/
stats.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
package csvexample
import (
"encoding/csv"
"fmt"
"math"
"strconv"
"google.golang.org/appengine/urlfetch"
"golang.org/x/net/context"
)
func correlation(xs, ys []float64) float64 {
return covariance(xs, ys) / (standardDeviation(xs) * standardDeviation(ys))
}
func covariance(x, y []float64) float64 {
if len(x) != len(y) {
panic("Vector lengths must be the same")
}
n := len(x)
sum, xsum, xmean, ysum, ymean := 0.0, 0.0, 0.0, 0.0, 0.0
for i := 0; i < n; i++ {
xsum += x[i]
ysum += y[i]
}
xmean = xsum / float64(n)
ymean = ysum / float64(n)
for i := 0; i < n; i++ {
sum += (x[i] - xmean) * (y[i] - ymean)
}
return sum / float64(n-1)
}
func getData(ctx context.Context, symbol string) ([]float64, error) {
url := fmt.Sprintf(
"http://real-chart.finance.yahoo.com/table.csv"+
"?s=%s"+
"&d=6"+
"&e=1"+
"&f=2015"+
"&g=m"+
"&a=6"+
"&b=1"+
"&c=2014"+
"&ignore=.csv",
symbol,
)
client := urlfetch.Client(ctx)
result, err := client.Get(url)
if err != nil {
return nil, err
}
defer result.Body.Close()
// parse the csv file
reader := csv.NewReader(result.Body)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
// convert the rows to floats
const (
dateColumn = 0
closeColumn = 4
)
var data []float64
for i, row := range records {
if i == 0 || len(row) < 5 {
continue
}
val, _ := strconv.ParseFloat(row[closeColumn], 64)
data = append(data, val)
}
return relativize(data), nil
}
func standardDeviation(xs []float64) float64 {
return math.Sqrt(variance(xs))
}
func variance(vector []float64) float64 {
n := 0.0
mean := 0.0
S := 0.0
delta := 0.0
for _, v := range vector {
n++
delta = v - mean
mean = mean + (delta / n)
S += delta * (v - mean)
}
return S / (n - 1)
}
func relativize(data []float64) []float64 {
nv := make([]float64, len(data)-1)
for i := 1; i < len(data); i++ {
nv[i-1] = (data[i] - data[i-1]) / data[i-1]
}
return nv
}