File: links.go

package info (click to toggle)
golang-github-kshedden-statmodel 0.0~git20210519.ee97d3e-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 892 kB
  • sloc: makefile: 3
file content (237 lines) | stat: -rw-r--r-- 4,539 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package glm

import (
	"fmt"
	"math"
)

// VecFunc is a function with two float64 array arguments.
type VecFunc func([]float64, []float64)

// Link specifies a GLM link function.
type Link struct {
	Name string

	TypeCode LinkType

	// Link calculates the link function (usually mapping the mean
	// value to the linear predictor).
	Link VecFunc

	// InvLink calculates the inverse of the link function
	// (usually mapping the linear predictor to the mean value).
	InvLink VecFunc

	// Deriv calculates the derivative of the link function.
	Deriv VecFunc

	// Deriv2 calculates the second derivative of the link function.
	Deriv2 VecFunc
}

// LinkType is used to specify a GLM link function.
type LinkType uint8

// LogLink, ... are used to specify GLM link functions.
const (
	LogLink LinkType = iota
	IdentityLink
	LogitLink
	CloglogLink
	RecipLink
	RecipSquaredLink
	PowerLink
)

// NewLink returns a link function object corresponding to the given
// name.  Supported values are log, identity, cloglog, logit, recip,
// and recipsquared.
func NewLink(link LinkType) *Link {

	switch link {
	case LogLink:
		return &logLink
	case IdentityLink:
		return &idLink
	case CloglogLink:
		return &cLogLogLink
	case LogitLink:
		return &logitLink
	case RecipLink:
		return &recipLink
	case RecipSquaredLink:
		return &recipSquaredLink
	default:
		msg := fmt.Sprintf("Link unknown: %v\n", link)
		panic(msg)
	}
}

var logLink = Link{
	Name:     "Log",
	TypeCode: LogLink,
	Link:     logFunc,
	InvLink:  expFunc,
	Deriv:    logDerivFunc,
	Deriv2:   logDeriv2Func,
}

var idLink = Link{
	Name:     "Identity",
	TypeCode: IdentityLink,
	Link:     idFunc,
	InvLink:  idFunc,
	Deriv:    idDerivFunc,
	Deriv2:   idDeriv2Func,
}

var cLogLogLink = Link{
	Name:     "CLogLog",
	TypeCode: CloglogLink,
	Link:     cloglogFunc,
	InvLink:  cloglogInvFunc,
	Deriv:    cloglogDerivFunc,
	Deriv2:   cloglogDeriv2Func,
}

var logitLink = Link{
	Name:     "Logit",
	TypeCode: LogitLink,
	Link:     logitFunc,
	InvLink:  expitFunc,
	Deriv:    logitDerivFunc,
	Deriv2:   logitDeriv2Func,
}

var recipLink = Link{
	Name:     "Recip",
	TypeCode: RecipLink,
	Link:     genPowFunc(-1, 1),
	InvLink:  genPowFunc(-1, 1),
	Deriv:    genPowFunc(-2, -1),
	Deriv2:   genPowFunc(-3, 2),
}

var recipSquaredLink = Link{
	Name:     "RecipSquared",
	TypeCode: RecipSquaredLink,
	Link:     genPowFunc(-2, 1),
	InvLink:  genPowFunc(-0.5, 1),
	Deriv:    genPowFunc(-3, -2),
	Deriv2:   genPowFunc(-4, 6),
}

// NewPowerLink returns the power link eta = mu^pw.  If
// pw = 0 returns the log link.
func NewPowerLink(pw float64) *Link {

	if pw == 0 {
		return &logLink
	}

	return &Link{
		Name:     fmt.Sprintf("PowerLink(%f)", pw),
		TypeCode: PowerLink,
		Link:     genPowFunc(pw, 1),
		InvLink:  genPowFunc(1/pw, 1),
		Deriv:    genPowFunc(pw-1, pw),
		Deriv2:   genPowFunc(pw-2, pw*(pw-1)),
	}
}

func logFunc(x []float64, y []float64) {
	for i := range x {
		y[i] = math.Log(x[i])
	}
}

func logDerivFunc(x []float64, y []float64) {
	for i := range x {
		y[i] = 1 / x[i]
	}
}

func logDeriv2Func(x []float64, y []float64) {
	for i := range x {
		y[i] = -1 / (x[i] * x[i])
	}
}

func expFunc(x []float64, y []float64) {
	for i := range x {
		y[i] = math.Exp(x[i])
	}
}

func logitFunc(x []float64, y []float64) {
	for i := range x {
		r := x[i] / (1 - x[i])
		y[i] = math.Log(r)
	}
}

func logitDerivFunc(x []float64, y []float64) {
	for i := range x {
		y[i] = 1 / (x[i] * (1 - x[i]))
	}
}

func logitDeriv2Func(x []float64, y []float64) {
	for i := range x {
		v := x[i] * (1 - x[i])
		y[i] = (2*x[i] - 1) / (v * v)
	}
}

func expitFunc(x []float64, y []float64) {
	for i := range x {
		y[i] = 1 / (1 + math.Exp(-x[i]))
	}
}

func idFunc(x []float64, y []float64) {
	// Assumes lengths match
	copy(y, x)
}

func idDerivFunc(x []float64, y []float64) {
	one(y)
}

func idDeriv2Func(x []float64, y []float64) {
	zero(y)
}

func cloglogFunc(x []float64, y []float64) {
	for i, v := range x {
		y[i] = math.Log(-math.Log(1 - v))
	}
}

func cloglogDerivFunc(x []float64, y []float64) {
	for i, v := range x {
		y[i] = 1 / ((v - 1) * math.Log(1-v))
	}
}

func cloglogDeriv2Func(x []float64, y []float64) {
	for i, v := range x {
		f := math.Log(1 - v)
		r := -1 / ((1 - v) * (1 - v) * f)
		y[i] = r * (1 + 1/f)
	}
}

func cloglogInvFunc(x []float64, y []float64) {
	for i, v := range x {
		y[i] = 1 - math.Exp(-math.Exp(v))
	}
}

func genPowFunc(p float64, s float64) VecFunc {
	return func(x []float64, y []float64) {
		for i := range x {
			y[i] = s * math.Pow(x[i], p)
		}
	}
}