-
Notifications
You must be signed in to change notification settings - Fork 1
/
glm.go
223 lines (178 loc) · 4.69 KB
/
glm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package glasso
import (
"fmt"
"math"
"github.com/gonum/matrix/mat64"
)
const DefaultTolerance = .000001
// The GamConfig specifies the desired family, and other model configurations
type GLMConfig struct {
F Family // Distribution family for the GLM
MaxIt int64 // upper bound on number of model iterations
Tolerance float64 // tolerance for model training
}
func NewGLMConfig(fam Family, maxit int64, tol float64) *GLMConfig {
return &GLMConfig{
F: fam,
MaxIt: maxit,
Tolerance: tol,
}
}
type glmTrainer struct {
config *GLMConfig
}
func NewGlmTrainer(config *GLMConfig) Trainer {
return &glmTrainer{
config: config,
}
}
// Iterative Re-weighting Least Squares Estimation for Generalized Linear Models
func (l *glmTrainer) Train(df *DataFrame, b []float64) (Model, Summary, error) {
if l.config == nil {
return nil, nil, fmt.Errorf("config not set")
}
A := df.Data()
nrow, ncol := A.Dims()
x := mat64.NewDense(ncol, 1, rep(0.0, ncol))
var i int64
var err error
for ; i < l.config.MaxIt; i++ {
eta := matrixMult(A, x)
etaCol := mat64.Col(nil, 0, eta)
var (
g = make([]float64, nrow) // g = invLink(eta)
gprime = make([]float64, nrow) // g = derivativeFn(eta)
w = make([]float64, nrow) // w = gprime^2 / variance(g)
)
for i, val := range etaCol {
g[i] = l.config.F.LinkFn(val)
gprime[i] = l.config.F.DerivativeFn(val)
w[i] = math.Pow(gprime[i], 2.0) / l.config.F.VarianceFn(g[i])
}
// z = eta + (b - g) / gprime
z := mat64.NewDense(nrow, 1, nil)
eta.Clone(z)
z.Apply(func(i, j int, eta float64) float64 {
return eta + (b[i]-g[i])/gprime[i]
}, z)
// convert w = w * I
wMat := mat64.NewDense(nrow, nrow, rep(0.0, nrow*nrow))
for i := 0; i < nrow; i++ {
wMat.Set(i, i, w[i])
}
var (
wa = matrixMult(wMat, A)
cprod1 = matrixMult(wa.T(), A)
wz = matrixMult(wMat, z)
cprod2 = matrixMult(wz.T(), A)
)
// save xold for evaluating convergence
xold := mat64.NewDense(ncol, 1, nil)
x.Clone(xold)
// xnew = solve(crossprod(A,W*A), crossprod(A,W*z))
x = &mat64.Dense{}
err = x.Solve(cprod1, cprod2.T())
if err != nil {
return nil, nil, err
}
// convergence = sqrt(crossprod(x - xold)) <= tolerance
diff := &mat64.Dense{}
diff.Sub(x, xold)
conv := matrixMult(diff.T(), diff)
if math.Sqrt(conv.At(0, 0)) <= l.config.Tolerance {
break
}
}
coef := mat64.Col(nil, 0, x)
fmt.Printf("coef=%v x=%v", coef, x)
return nil, nil, nil
}
func matrixMult(a, b mat64.Matrix) *mat64.Dense {
out := &mat64.Dense{}
out.Mul(a, b)
return out
}
type linkFunc func(float64) float64
type fnType uint8
const (
Link fnType = iota
Derivative
Variance
)
type evalFn func(float64) float64
type Family struct {
LinkFn evalFn
VarianceFn evalFn
DerivativeFn evalFn
}
func NewFamily(l, d, v evalFn) Family {
return Family{
LinkFn: l,
VarianceFn: v,
DerivativeFn: d,
}
}
var (
Binomial = NewFamily(binomialLink, binomialDerivative, binomialVariance)
Poisson = NewFamily(poissonLink, poissonDerivative, poissonVariance)
Gamma = NewFamily(gammaLink, gammaDerivative, gammaVariance)
InvNormal = NewFamily(invnLink, invnDerivative, invnVariance)
)
// -------------------------- //
// Binomial
// -------------------------- //
// logistic link function
func binomialLink(x float64) float64 {
return 1 / (1 + math.Exp(-x))
}
// derivative of logistic f(x) = f(x) * (1 - f(x))
func binomialDerivative(x float64) float64 {
l := 1 / (1 + math.Exp(-x))
ans := l * (1 - l)
return ans
}
// mean = x variance = np(1 - p) = p - p^2
func binomialVariance(x float64) float64 {
return x - math.Pow(x, 2.0)
}
// -------------------------- //
// Poisson
// -------------------------- //
// exponential link function
func poissonLink(x float64) float64 {
return math.Exp(x)
}
func poissonDerivative(x float64) float64 {
return poissonLink(x)
}
func poissonVariance(x float64) float64 {
return x
}
// -------------------------- //
// Gamma
// -------------------------- //
// inverse link function: 1/x
func gammaLink(x float64) float64 {
return 1 / x
}
// derivative of link: 1/x^2
func gammaDerivative(x float64) float64 {
return 1 / math.Pow(x, 2.0)
}
// variance of gamma dist: kx^2, but k=1
func gammaVariance(x float64) float64 {
return math.Pow(x, 2.0)
}
// -------------------------- //
// Inverse Normal
// -------------------------- //
// Link function: 1 / sqrt(x)
func invnLink(x float64) float64 { return 1 / math.Sqrt(x) }
// Derivative of Link: - (x ^ 3/2) / 2
func invnDerivative(x float64) float64 {
return -0.5 * math.Pow(x, -1.5)
}
// Variance : mu^3 / lambda , lambda = 1
func invnVariance(x float64) float64 {
return math.Pow(x, 3.0)
}