KDB - Referencing functions from a table

丶灬走出姿态 提交于 2021-01-06 03:29:27

问题


I am new to kdb and researching it for a use case to generate time series data using a table of various function inputs. Each row of the table consists of function inputs keyed by an id and segment and will call one function per row. I have figured out how to identify which function albeit using brute force nested conditions.

My question is 2 part

  1. How does one employ kicking off the execution of these functions?
  2. Once the time series data is generated for each id and segment, how best can the the output be compiled into a singular table (sample output noted below - I have thought about one table for each id and then compile in two steps which would work as well but we'll have thousands of ids)

Below is a sample table and some conditions to add meta data including which function to apply

        //Create sample table and add columns to identify unknown and desired function
        t:([id:`AAA`AAA`AAA`BBB`CCC;seg:1 2 3 1 1];aa: 1500 0n 400 40 900;bb:0n 200 30 40 0n;cc: .40 .25 0n 0n .35) 
        t: update Uknown:?[0N = aa;`aa;?[0N = bb;`bb;?[0N = cc;`cc;`UNK]]] from t
        t: update Call_Function:?[0N = aa;`Solveaa;?[0N = bb;`Solvebb;?[0N = cc;`Solvecc;`NoFunction]]] from t 

A sample function below uses the inputs from table t to generate time series data (limited to 5 periods for example here) and test using X

        //dummy function to generate output for first 5 time periods
        Solvebb:{[aa;cc]
             (aa%cc)*(1-exp(neg cc*1+til 5)) 
        }

        //test the function as an example for dummy output in result table below 
        x: flip enlist Solvebb[1500;.40]  //sample output for AAA seg1 from t for example

The result would ideally be a sample table similar to below

        t2: `id`seg xkey ("SIIIS";enlist",") 0:`:./Data/sampleOutput.csv 

        
id  seg| seg_idx tot_idx result  
-------| ------------------------
AAA 1  | 1       1       1,236.30
AAA 1  | 2       2       2,065.02
AAA 1  | 3       3       2,620.52
AAA 1  | 4       4       2,992.89
AAA 1  | 5       5       3,242.49
AAA 2  | 1       6
AAA 2  | 2       7
AAA 2  | 3       8
AAA 2  | 4       9
AAA 2  | 5       10
AAA 3  | 1       11
AAA 3  | 2       12
AAA 3  | 3       13
AAA 3  | 4       14
AAA 3  | 5       15
BBB 1  | 1       1
BBB 1  | 2       2
BBB 1  | 3       3
BBB 1  | 4       4
BBB 1  | 5       5
..

回答1:


It's difficult without more details, but something like the following may help.

First, it may be easier to define Solvebb so that it can take 3 inputs and simple ignores the middle one:

q)Solvebb:{[aa;bb;cc](aa%cc)*(1-exp(neg cc*1+til 5))}

And adding dummy functions for the other two in your table (NB. it's important for the use of ungroup later that the output of these functions are lists):

q)Solveaa:{[aa;bb;cc] (bb+cc;bb*cc)}
q)Solvecc:{[aa;bb;cc] (aa+bb;aa*bb)}

You can apply each call each function on all three vectors of input with:

q)update result:first[Call_Function]'[aa;bb;cc] by Call_Function from t
id  seg| aa   bb  cc   Uknown Call_Function result
-------| -------------------------------------------------------------------------------
AAA 1  | 1500     0.4  bb     Solvebb       1236.3 2065.016 2620.522 2992.888 3242.493
AAA 2  |      200 0.25 aa     Solveaa       200.25 50
AAA 3  | 400  30       cc     Solvecc       430 12000f
BBB 1  | 40   40       cc     Solvecc       80 1600f
CCC 1  | 900      0.35 bb     Solvebb       759.3735 1294.495 1671.589 1937.322 2124.581

and you can unravel this table by applying the ungroup function

q)ungroup update result:first[Call_Function]'[aa;bb;cc] by Call_Function from t
id  seg aa   bb  cc   Uknown Call_Function result
---------------------------------------------------
AAA 1   1500     0.4  bb     Solvebb       1236.3
AAA 1   1500     0.4  bb     Solvebb       2065.016
AAA 1   1500     0.4  bb     Solvebb       2620.522
AAA 1   1500     0.4  bb     Solvebb       2992.888
AAA 1   1500     0.4  bb     Solvebb       3242.493
AAA 2        200 0.25 aa     Solveaa       200.25
AAA 2        200 0.25 aa     Solveaa       50
AAA 3   400  30       cc     Solvecc       430
AAA 3   400  30       cc     Solvecc       12000
BBB 1   40   40       cc     Solvecc       80
BBB 1   40   40       cc     Solvecc       1600
CCC 1   900      0.35 bb     Solvebb       759.3735
CCC 1   900      0.35 bb     Solvebb       1294.495
CCC 1   900      0.35 bb     Solvebb       1671.589
CCC 1   900      0.35 bb     Solvebb       1937.322
CCC 1   900      0.35 bb     Solvebb       2124.581


来源:https://stackoverflow.com/questions/63163687/kdb-referencing-functions-from-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!