How to find multivariable regression equation in javascript

余生颓废 提交于 2019-12-04 04:01:32

The problem as you've stated it is equivalent, under transformation, to a linear regression problem. You said in comments that you have fixed exponents k_1, k_2, and k_3. The transformation takes a tuple {w, x, z ,y} to the tuple {w^k_1, x^k_2, z^k_2, y} = {w', x', z' ,y}. Use linear regression on the primed variables to get your coefficients.

For example, if k_1 = 2, k_2 = 3, and k_3 = 1, then here's a single example of the transform:

{"w" : 4, "x" : 3, "z" : 5, "y" : 15} 
==> {"w*" : 16, "x*" : 27, "z*" : 5, "y" : 15}

This is just a special case of how you convert a polynomial regression problem into a linear regression one. In your case the polynomial forms you are considering are particularly simple.

Use any JavaScript library you like to solve the linear regression problem; there are a number of them.

I think if it is the case that there are four equations and only 3 variables (As you already determined the powers, plugin and make it a linear equation), the linear equation is over complete, and there does not exist an exact answer that will satisfy all four equations.

What you can do is to minimize the residual error and get a best approximation.

Assume you have coefficients a b and c for the w x and z,

define matrix

M=[w1,x1,z1;w2,x2,z2;w3,x3,z3;w4,x4,z4]. 

and define the vector

v=[a;b;c], 

define vector

r=[y1;y2;y3;y4]. 

Then the problem is

M*v=r solve v. 

1. If rank(M)>variable number, you have to minimize the residual error

||M*v-r||_2. 

Since this is convex, take derivative on it and make it zero:

M^T*M*v-M^T*r=0 => v=(M^T*M)\M^T*r. 

(M^T*M)\M^T is MP-inverse of M, if rank(M)>variable number, then (M^T*M) is inversible.

2. If the rank(M)<=variable number, you can get infinitely many exact solution to the equation.

M*v=r. 

Let singular value decomposition of M:

M=U*S*V^T, 

then

v=V*S^-1*U^T*r 

is one of the solutions.

V*S^-1*U^T is pseudo inverse of M.

If you use a linear algebra library, it is very easy to get closed form solution without iterating. http://sylvester.jcoglan.com/

I would suggest using least squares for getting a linear equation. Further, you could use non-linear least squares, given that you know in advance the function you want to fit.

(http://en.wikipedia.org/wiki/Least_squares)

There are several links for linear LS in javascript and you can probably adapt these to 3 dimensions (e.g. http://dracoblue.net/dev/linear-least-squares-in-javascript/159/ from a quick Google search). For the non-linear case it would need some more work though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!