Search code examples
kdb

How do I solve a linear model in KDB using BFGS?


I've got a toy linear model:

\l ml/ml.q
.ml.loadfile`:optimize/init.q

xx: 9h$til 10
yy: ((xx)*3) + 4

x0: 1 1    
error:{sum xexp[(yy - (xx*x) + y);2]}

q).ml.optimize.BFGS[error;x0;();::]
\
'type
  [4]  /home/chris/anaconda3/q/ml/optimize/utils.q:467: .ml.i.gradEval:
  // Evaluate the gradient
  (i.funcEval[func;xk;args]-fk)%eps
                           ^
  }

I'm hoping it will minimize the error function, and recover 3;4 from the model.

It doesn't seem to go though, despite having followed the docs as best I can:

https://code.kx.com/q/ml/toolkit/optimize/

What am I doing wrong?


Solution

  • The problem was related to the error function; it should be unary and take a list as a parameter.

    error:{sum xexp[(yy - (xx*x[0]) + x[1]);2]}