How to calculate the time-weighted average value of kdb?

Calculating weighted averages is easy in kdb thanks to the wavg function. How can I calculate the delay between two consecutive rows of times and use these values ​​as a parameter of weights for wavg?

Sort of:

`date`ts xasc select date,ts,mytw:(next ts - ts) wavg mycolumnval from pmd where date=2013.05.22 

next gives the next column of rows in the query, but I can't figure out how to subtract the two ts values ​​and use them in wavg .

UPDATE The solution was to use parentheses: ((next ts) - ts) . Please note that I cannot use deltas[ts] . This will result in different weights:

 08:15:19.811 00:00:00.000 08:15:19.811 00:00:00.001 08:15:19.812 00:00:00.014 08:15:19.826 00:00:07.305 08:15:27.131 NULL 

This is deltas[ts] :

 08:15:19.811 08:15:19.811 08:15:19.811 00:00:00.000 08:15:19.812 00:00:00.001 08:15:19.826 00:00:00.014 08:15:27.131 00:00:07.305 
+4
source share
3 answers

The necessary bracket around "next ts":

 `date`ts xasc select date,ts,mytw:((next ts) - ts) wavg mycolumnval from pmd where date=2013.05.22 
+1
source

What about:

 `date`ts xasc select date,ts,mytw:deltas[ts] wavg mycolumnval from pmd where date=2013.05.22 
+1
source

It should not be: date ts xasc select date, ts, mytw: (1 _ deltas ts, 0Nv) wavg mycolumnval from pmd, where date = 2013.05.22. Use 0Np if your ts is datetime

0
source

Source: https://habr.com/ru/post/1482232/


All Articles