Post Reply 
Multivariable Polynomial Regression
10-13-2014, 10:28 PM (This post was last modified: 11-10-2014 08:18 PM by Han.)
Post: #2
RE: Polynomial Regression
Here is a program that is best used in conjunction with the Stat2Var app. Eventually this will evolve into a full-blown app that will enable users to create 3D surface regressions (using polynomials of a degree specified by the user).
Usage as follows:
\[ \mathrm{multireg}(\text{list_of_data},\text{degree}) \rightarrow
\left\{
a_0, a_1, \dotsm, a_{k(d)-1}
\right\} \]
where \( k(d) = (d+1)(d+2)/2 \) and the 2-variable polynomial is of the form
\[ f(x,y) = a_0 + a_1 x + a_2 y + a_3 x^2 + a_4 xy + a_5 y^2
+ \dotsm + a_{k(d)-2} xy^{d-1} +
a_{k(d)-1} y^d \]
and the list_of_data parameter is a list of three lists (for the \( x_i \), \(y_i\), and \( z_i \) values). Typically the data would be pulled from C0 through C9 in the Stat2Var app. So list_of_data may be something like: { C1, C2, C3 }.

The algorithm is essentially the same as that for 1-variable polynomial regression. Both cases use an appropriate Vandermonde matrix to solve for the critical point in the least sum-of-squared-residuals equation.

(Please note that error checking for valid input data is very minimal.)

Code:
mrerr:={
"Invalid input",
"Insufficient data",
"Singular data"
};

export multireg(m,d)
begin
local i,n;
local s1,s2,s3;
local xi,yi,zi;
local l1:={},l2:={};

xi:=m(1); yi:=m(2); zi:=m(3);
s1:=size(xi); s2:=size(yi); s3:=size(zi);

if (s1-s2) OR (s2-s3) OR (s1-s3) then
  msgbox(mrerr(1));
  return([[0]]);
end;

if (s1 < (d+1)*(d+2)/2) then
  msgbox(mrerr(2));
  return([[0]]);
end;

local tmat:=makelist(1,X,1,s1);
local l:=tmat;

for i from 1 to d do
  for n from 0 to i do
    if n then l2:=yi^n; else l2:=l; end;
    if (i-n) then l1:=xi^(i-n); else l1:=l; end;
    tmat:=concat(tmat,l1*l2);
  end; 
end; 

local tvm:=list2mat(tmat,s1);
local vm:=transpose(tvm);

iferr
  vm:=(tvm*vm)^(-1);
then
  msgbox(mrerr(3));
  return([[0]]);
else
  return(mat2list(vm*tvm*list2mat(zi,1)));
end;

end;

export MULTIREG(m,d)
begin
  multireg(m,d);
end;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Multivariable Polynomial Regression - Han - 10-09-2014, 08:27 PM
RE: Polynomial Regression - Han - 10-13-2014 10:28 PM
RE: Polynomial Regression - Han - 11-07-2014, 09:43 PM



User(s) browsing this thread: 1 Guest(s)