0

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

enter image description here

  • What programming language is that? In any case, I suspect the substitution `(1..degree)` for `(0..degree)` will do it. Whether it is advisable to do so is another matter. – Matthew Drury Jun 26 '20 at 23:16
  • It’s Ruby, snippet from here. If you know the answer in plan math I will translate by self. https://rosettacode.org/wiki/Polynomial_regression#Ruby – user1028100 Jun 26 '20 at 23:17

0 Answers0