Adds matrix sub package & adds polynomial regression series (#36)
* updates * updates * tests. * test coverage * fixing test * stride not rows + cols * lu decomp implementation. * poly regression! * poly regression works. * typo.
This commit is contained in:
parent
b3dc3fef3c
commit
a211e88530
10 changed files with 1292 additions and 9 deletions
45
matrix/regression.go
Normal file
45
matrix/regression.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package matrix
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrPolyRegArraysSameLength is a common error.
|
||||
ErrPolyRegArraysSameLength = errors.New("polynomial array inputs must be the same length")
|
||||
)
|
||||
|
||||
// Poly returns the polynomial regress of a given degree over the given values.
|
||||
func Poly(xvalues, yvalues []float64, degree int) ([]float64, error) {
|
||||
if len(xvalues) != len(yvalues) {
|
||||
return nil, ErrPolyRegArraysSameLength
|
||||
}
|
||||
|
||||
m := len(yvalues)
|
||||
n := degree + 1
|
||||
y := New(m, 1, yvalues...)
|
||||
x := Zero(m, n)
|
||||
|
||||
for i := 0; i < m; i++ {
|
||||
ip := float64(1)
|
||||
for j := 0; j < n; j++ {
|
||||
x.Set(i, j, ip)
|
||||
ip *= xvalues[i]
|
||||
}
|
||||
}
|
||||
|
||||
q, r := x.QR()
|
||||
qty, err := q.Transpose().Times(y)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := make([]float64, n)
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
c[i] = qty.Get(i, 0)
|
||||
for j := i + 1; j < n; j++ {
|
||||
c[i] -= c[j] * r.Get(i, j)
|
||||
}
|
||||
c[i] /= r.Get(i, i)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue