When I use the ols function with the context by clause as follows, an error occurs: update factor set cor = moving(calOls{n}, (close, arc, vrc, src, krc), win) context by underlying => calOls: r = ols(nextRet, a) => The input matrix is singular.
update factor set o = ols(nextRet, (arc, vrc, src, krc))[0] context by underlying
The following is my script:
defg calOls(n, close, arc, vrc, src, krc)
{
tb = table(close as close, arc as arc, vrc as vrc, src as src, krc as krc)
update tb set nextRet = log(close.move(-n)/close)
b = matrix(tb[["arc", "vrc", "src", "krc"]])
try
{
r = ols(tb["nextRet"],b)
return r[0] + r[1] * arc[size(arc)-1] + r[2] * vrc[size(vrc)-1] + r[3] * src[size(src)-1] + r[4] * krc[size(krc)-1]
}
catch(ex)
{
print(b)
print("=========")
return 0
}
}
The error occurs because the input matrix is non-singular. To address this, you can add a logic to check whether the input matrix “b” is non-singular, as shown in the following script: