如何在DolphinDB中计算多列中因子的z分数?

huangapple go评论55阅读模式
英文:

How to calculate the z-scores of factors in multiple columns in DolphinDB?

问题

我有一张表格,其中包含一个名为 trade_date 的列和多个因子列。我想要使用函数 zscore() 对这些因子列进行标准化,以 交易日期 作为分组依据。

如何批量传递这些列?

英文:

I have a table with a column trade_date and multiple factor columns. I would like to standardize these factor columns using the funciton zscore(), with trade-date as the grouping basis.

How can I pass these columns in batch?

答案1

得分: 1

让我们假设您有一个名为tbl的表,其中包含列trade_datef1f2。您可以使用update函数按trade_date列对表进行分组,并使用zscore()函数计算每个因子列的Z分数。结果包括新列f1_zscoref2_zscore,它们是f1f2的标准化值。

update tbl by trade_date, f1_zscore:zscore(f1), f2_zscore:zscore(f2) from tbl
英文:

Let's say you have a table tbl with columns trade_date, f1, and f2. You can use update function to group the table by the trade_date column and calculate the z-scores for each factor column using the zscore() function. The result includes new columns f1_zscore and f2_zscore which are standardized values of f1 and f2.

update tbl by trade_date, f1_zscore:zscore(f1), f2_zscore:zscore(f2) from tbl

答案2

得分: 1

以下是已翻译好的部分:

对于单列:

从 t 按 trade_date 上下文选择 zscore(factor1)

对于多列:

(1) 内存表:

contextby(zscore, t, t.trade_date)

(2) 或者,使用元编程:

t = table(100:0, [`trade_date, `symbol, `f1,`f2,`f3,`f4,`f5], [DATE, SYMBOL, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE])
otherCol = [`trade_date, `symbol]
colName = t.colNames()
factorCol = colName[not colName in otherCol]
sql(select=sqlCol(factorCol, zscore, "re_" + factorCol), from=objByName("t"), groupBy=sqlCol("trade_date"), groupFlag=0)
英文:

You can refer to the following methods:

For single column:

select zscore(factor1) from t context by trade_date

For multiple columns:

(1) in-memory table:

contextby(zscore, t, t.trade_date)

(2) Otherwise, use meta-programming:

t = table(100:0, [`trade_date, `symbol, `f1,`f2,`f3,`f4,`f5], [DATE, SYMBOL, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE])
otherCol = [`trade_date, `symbol]
colName = t.colNames()
factorCol = colName[not colName in otherCol]
sql(select=sqlCol(factorCol, zscore, "re_" + factorCol), from=objByName("t"), groupBy=sqlCol("trade_date"), groupFlag=0)

huangapple
  • 本文由 发表于 2023年6月6日 09:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定