# Fitting of convex quadratic form to data

Hi, I’m new to using CVX and have a question on a problem that I want to solve, but have no idea on how to type down so that CVX understands it. It’s fitting a convex quadratic form to a data set (x_i,y_i),\ i=1,...,N with x_i\in\mathbb{R^n}, y_i\in\mathbb{R}, i.e

\underset{H,f,r}{\text{min}} \quad \sum_i^N(x_i^T Hx_i + f^Tx_i + r - y_i)^2 \\ \ \text{s.t.} \quad H\succeq 0

where the variables are H\in S^n, \ f\in\mathbb{R}^n, \ r\in\mathbb{R} .
Specifically, I’m having a hard time figuring out how to handle the objective function. Any ideas?

I’d probably do something like this:

cvx_begin sdp
variables f(n) r
variable H(n,n) symmetric
minimize(norm(sum(X.*(H*X))+X'*f+r-y))
H >= 0
cvx_end


sum(X.*(H*X)) is the same as diag(X'*H*X), but cheaper to compute. And avoiding a for loop on N is always a good thing, but both of these forms should be equivalent to this as well:

cvx_begin sdp
variables z(N) f(n) r
variable H(n,n) symmetric
for k = 1 : N
z(k) == x(:,k)'*H*x(:,k)+f'*x(:,k)+r-y(k)
end
minimize(norm(z))
H >= 0
cvx_end


These models all minimize the square root of the objective you have above, but it gives the same result; that is, the same values of H, f, and r. You can always use sum_square instead of norm, but I recommend against it.

Thanks, the help is much appreciated! And just FYI: the alternative with diag(X’HX) worked out nicely, the norm(sum(X.(HX))+X’*f+r-y), however didn’t work due to dimension mismatches. Just to be clear, in your description I interpret X as

X = [x_1, ..., x_N]

i.e. so that X\in \mathbb{R}^{n \times N}. The “looped” alternative failed (returned infeasible) for some reason.

I anyways got what I wanted, so thanks again!

Ah, it should be sum(X.*(H*X))'. Note the transpose. Sorry about that. Not sure why the equality constraint version would be infeasible, it’s internally going to be the same.