I have to perform a polynomial fit but I have to force it to pass through intercept zero. How can I do it?
Here the standard code for poly fit. If you have the answer in pure math it’s fine.
require 'matrix'
def regress x, y, degree
x_data = x.map { |xi| (0..degree).map { |pow| (xi**pow).to_r } }
mx = Matrix[*x_data]
my = Matrix.column_vector(y)
((mx.t * mx).inv * mx.t * my).transpose.to_a[0].map(&:to_f)
end
Basically, the same when in Excel I check "Set Intercept" to zero