【发布时间】:2020-07-29 22:59:46
【问题描述】:
我是 kdb 的新手,我正在研究一个用例,以使用包含各种函数输入的表生成时间序列数据。表的每一行都包含由 id 和段作为键的函数输入,每行将调用一个函数。尽管使用暴力嵌套条件,但我已经弄清楚了如何识别哪个函数。
我的问题分为两部分
- 如何开始执行这些功能?
- 为每个 id 和段生成时间序列数据后,如何最好地将输出编译成一个单一的表(示例输出如下所述 - 我曾考虑为每个 id 一个表,然后分两步编译也可以,但我们会有数千个 ID)
下面是一个示例表和一些添加元数据的条件,包括应用哪个函数
//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
下面的示例函数使用表 t 中的输入来生成时间序列数据(例如这里限制为 5 个周期)并使用 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
理想情况下,结果将是一个类似于下面的示例表
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
..
【问题讨论】:
标签: kdb