当我将
ols
函数与 context by 子句一起使用时,如下所示,会发生错误: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
以下是我的脚本:
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
}
}
出现错误是因为输入矩阵是非奇异的。为了解决这个问题,您可以添加一个逻辑来检查输入矩阵“b”是否是非奇异的,如以下脚本所示:
if(sum(0!=svd(b, computeUV=false)) == 0){
return 0
}