-
Notifications
You must be signed in to change notification settings - Fork 0
/
logarithmic.go
55 lines (44 loc) · 1.2 KB
/
logarithmic.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
package regressions
import "math"
type logarithmic struct {
base Regression
LogFunc func(float64) float64
}
// NewLogarithmic returns a new Regression for logarithmic regression.
func NewLogarithmic() Regression {
return NewLogarithmicWithLogFunc(
math.Log, // natural logarithm
)
}
// NewLogarithmicWithLogFunc returns a new Regression for logarithmic regression.
func NewLogarithmicWithLogFunc(log func(float64) float64) Regression {
return &logarithmic{
base: NewLinear(),
LogFunc: log,
}
}
// Fit implements the Fitter interface.
func (r *logarithmic) Fit(dps ...DataPoint) error {
logDps := make([]DataPoint, len(dps))
for i, dp := range dps {
x := dp.GetX()
logX := r.LogFunc(x)
if math.IsNaN(logX) || math.IsInf(logX, 0) {
return ErrLogUndefined(x)
}
logDps[i] = dataPoint{logX, dp.GetY()}
}
return r.base.Fit(logDps...)
}
// Predict implements the Predicter interface.
func (r *logarithmic) Predict(x float64) (float64, error) {
logX := r.LogFunc(x)
if math.IsNaN(logX) || math.IsInf(logX, 0) {
return 0, ErrLogUndefined(x)
}
return r.base.Predict(logX)
}
// GetR2 implements the Regression interface.
func (r *logarithmic) GetR2() float64 {
return r.base.GetR2()
}