How to calculate the weighted sum of columns in tables stored in a dictionary in DolphinDB?

30 Views Asked by At

There are N tables with the same structure, consisting of three columns: “datetime“, ”symbol“, and ”faN“.

I store these tables in a dictionary named datadict, where the keys match the names in the third column of each table “fa1”, “fa2”, ..., “faN”, and the values are these tables.

enter image description here

I want to calculate the weighted sum of the third columns from all these tables, where the weights are a vector of length N. What’s the most convenient way to achieve this in DolphinDB?

1

There are 1 best solutions below

0
carbonhydrate On BEST ANSWER

Refer to the following script:

datetime = 2020.01.01..2020.01.09
symbol = take(4, 9)
t1 = table(datetime, symbol, rand(10, 9) as fa1)
t2 = table(datetime, symbol, rand(10, 9) as fa2)
t3 = table(datetime, symbol, rand(10, 9) as fa3)
x = `fa1`fa2`fa3
y = (t1,t2,t3)
d = dict(x,y, ordered=true)
w=[2\6, 3\6, 1\6]
rowWsum(each(at, d.values(), d.keys()), w)