2016-12-05 23:44:36 +01:00
|
|
|
func regress(x, y, degree) {
|
2019-09-12 10:33:56 -07:00
|
|
|
var A = Matrix.build(x.len, degree+1, {|i,j|
|
|
|
|
|
x[i]**j
|
|
|
|
|
})
|
2016-12-05 23:44:36 +01:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
var B = Matrix.column_vector(y...)
|
|
|
|
|
((A.transpose * A)**(-1) * A.transpose * B).transpose[0]
|
|
|
|
|
}
|
2016-12-05 23:44:36 +01:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
func poly(x) {
|
|
|
|
|
3*x**2 + 2*x + 1
|
2016-12-05 23:44:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
var coeff = regress(
|
|
|
|
|
10.of { _ },
|
|
|
|
|
10.of { poly(_) },
|
2016-12-05 23:44:36 +01:00
|
|
|
2
|
2017-09-23 10:01:46 +02:00
|
|
|
)
|
2016-12-05 23:44:36 +01:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
say coeff
|