I need to fit a quadratic surface to multidimensional data, one of the methods mentioned is using a polynomial basis function,
$$ \phi(x) = [ 1, x_1, ..., x_n x_1^2, ..., x_n^2 x_1 x_2, ..., x_{n-1}x_n ] $$
If we massage the data into this form,
$$ \Phi(Y) = \left[\begin{array}{cccccccccc} 1 & y_1^1 & \dots & y_n^1 & (y_1^1)^2 & \dots & (y_n^1)^2 & y_1^1y_2^1 & \dots & y_{n-1}^1 y_n^1\\ \vdots & & & & & & & & & \vdots \\ 1 & y_1^p & \dots & y_n^p & (y_1^p)^2 & \dots & (y_n^p)^2 & y_1^py_2^p & \dots & y_{n-1}^p y_n^p \end{array}\right] $$
We can run a regression to find the coefficients for each term of the quadratic expansion.
My question is can I use a library that can do this regression for any dimension (but always order=2, that is quadratic).
Also is there a way to obtain the Hessian matrix easily, from the epression above perhaps, once the fit is computed ? A way to represent a multivariate quadratic expression is known to be,
$$ f(x) = x^T A x $$
where $x = [x_1, x_2, ... , x_n]$ and the Hessian of this expression would be $A + A^T$. But how would the coefficients found above map to the matrix A? Is there a way to use the matrix $A$ directly in the regression so I also end up with the Hessian automaticaly?
My goal is to be able to sample values from a known function around a point, approximate it with a quadratic function at that point, and use this quadratic function to calculate a gradient and a Hessian.
The basis for two dimensions would be
$$ p(x) = \left[\begin{array}{ccccc} x_1 & x_2 & x_1^2 & x_1x_2 & x_2^2 \end{array}\right]^T $$
$f(x) = p(x)^T a$
where $a = [a_0, a_1, ...]$. This would be akin to calculating
$$ f(x) = a_0 + a_1 x_1 + a_2 x_2 + a_3 x_1 x_2 + a_4 x_2^2 $$
Then with given data points
$$ \left[\begin{array}{ccccc} (x_1^1) & (x_2^1) & (x_1^1)^2 & (x_1^1)(x_2^1) & (x_2^1)^2 \\ \vdots & & & & \vdots \\ (x_1^n) & (x_2^n) & (x_1^n)^2 & (x_1^n)(x_2^n) & (x_2^n)^2 \\ \end{array}\right] \mathbf{a} = \left[\begin{array}{c} y^1 \\ \vdots \\ y^n \end{array}\right] $$
and solve for $a$.
But I need the
$$ f(x) = x^T A x $$
form, so I can easily get $\nabla f (x) = 2 A x$ and the Hessian $\nabla^2 f(x) = 2 A$.
It turns out if we start with 3 dimensions, and use $x_3 = 1$ this gives the necessary form in two dimensions,
$$ x^T A x = \left[\begin{array}{ccc} x_1 & x_2 & x_3 \end{array}\right] \left[\begin{array}{ccc} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{array}\right] \left[\begin{array}{c} x_1 \\ x_2 \\ x_3 \end{array}\right] $$
$$ = \left[\begin{array}{c} x_1 a_{11} + x_2 a_{21} + x_3 a_{31} \\ x_1 a_{12} + x_2 a_{22} + x_3 a_{32} \\ x_1 a_{13} + x_2 a_{23} + x_3 a_{33} \end{array}\right]^T \left[\begin{array}{c} x_1 \\ x_2 \\ x_3 \end{array}\right] $$
$$ = x_1 x_1 a_{11} + x_1 x_2 a_{21} + x_1 x_3 a_{31} + $$ $$ x_1 x_2 a_{12} + x_2 x_2 a_{22} + x_3 x_2 a_{32} + $$ $$ x_1 x_3 a_{13} + x_2 x_2 a_{23} + x_3 x_3 a_{33} $$
Using $x_3 = 1$
$$ x^T A x = \left[\begin{array}{ccc} x_1 & x_2 & 1 \end{array}\right] \left[\begin{array}{ccc} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{array}\right] \left[\begin{array}{c} x_1 \\ x_2 \\ 1 \end{array}\right] $$
$$ = a_{11} x_1^2 + a_{21} x_1 x_2 + a_{31}x_1 + $$ $$ a_{12}x_1 x_2 + a_{22}x_2^2 + a_{32} x_2+ $$ $$ a_{13} x_1 + a_{23} x_2 x_3 + a_{33} $$