英文:
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_date、f1和f2。您可以使用update函数按trade_date列对表进行分组,并使用zscore()函数计算每个因子列的Z分数。结果包括新列f1_zscore和f2_zscore,它们是f1和f2的标准化值。
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论