-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (82 loc) · 2.66 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
package main
import (
"path/filepath"
"sort"
"github.com/gonum/matrix/mat64"
"github.com/jackfrye/MachineLearning/data"
"github.com/jackfrye/MachineLearning/io"
"github.com/jackfrye/MachineLearning/machine-learning-algorithms"
"github.com/tonnerre/golang-pretty"
)
//main function to the project.
func main() {
//Make absolute path filenames for your computer
filenames := []string{
makePath("res/ExportData.xlsx"),
makePath("res/S&PStockPrices.xlsx"),
makePath("res/US_GDP.xlsx")}
//Read from the files and populate structure
excelData := make([]map[string][]map[string]float64, 0, 10000)
for _, filename := range filenames {
excelData = append(excelData, read(filename))
}
//Flatten data to key by year
historicalMarketData := data.JoinOn("Year", excelData)
finalData := data.FilterIn(1977, 2015, historicalMarketData)
X, y := buildSystemOn("Price", finalData)
mat64.PrintDense(X)
//Now we perform linear regression using the least squares method
// theta = ((X*X^T)^-1)(X^T*y)
theta := mlearn.LeastSquares(X, y)
pretty.Print("HYPOTHESIS", theta)
delta := mlearn.CalculateTotalDelta(X, &theta, y)
pretty.Print("DELTA", delta)
}
//test1 tests the mat64 library. Playground for features.
func test1() {
var A = mat64.NewDense(2, 2, []float64{0.1, 3.4, 8.5, 22.0})
var b = mat64.NewVector(2, []float64{7.15, 73.75})
var x = mat64.NewVector(2, make([]float64, 2))
x.SolveVec(A, b)
}
//read is a wrapper for reading files. Should be deprecated.
func read(filename string) map[string][]map[string]float64 {
workBooks := io.ReadFile(filename)
return workBooks
}
func makePath(filename string) string {
a, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
return a
}
//buildSystemOn takes data and builds a design matrix and solution.
func buildSystemOn(key string, data map[int]map[string]float64) (*mat64.Dense, *mat64.Dense) {
rows := len(data)
cols := 0
length := rows
designMatrixData := make([]float64, 0, 100000)
solutionData := make([]float64, 0, 10000)
for _, associatedData := range data {
cols = len(associatedData)
keys := make([]string, 0, len(associatedData))
/*ensure you are going through the keys in the same order
every tme */
for k := range associatedData {
keys = append(keys, k)
}
sort.Strings(keys)
designMatrixData = append(designMatrixData, 1)
for _, sortedKey := range keys {
if sortedKey != key {
designMatrixData = append(designMatrixData, associatedData[sortedKey])
} else {
solutionData = append(solutionData, associatedData[key])
}
}
}
designMatrix := mat64.NewDense(rows, cols, designMatrixData)
y := mat64.NewDense(length, 1, solutionData)
return designMatrix, y
}