KDB:加权中位数

发布于 2025-01-16 16:06:37 字数 256 浏览 2 评论 0原文

如何计算 KDB 中的加权中位数?

我可以看到有一个函数 med 用于简单的中位数,但我不能找到类似于 wavgwmed

非常感谢您的帮助!

How can one compute weighted median in KDB?

I can see that there is a function med for a simple median but I could not find something like wmed similar to wavg.

Thank you very much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2025-01-23 16:06:39

您可以通过复制(使用 where)来有效地对中位数进行加权:

q)med 10 34 23 123 5 56 where 4 1 1 1 1 1
10f
q)med 10 34 23 123 5 56 where 1 1 1 1 1 4
56f
q)med 10 34 23 123 5 56 where 1 2 1 3 2 1
34f

如果您的权重是百分比(例如 0.15 0.10 0.20 0.30 0.25),则将它们转换为等效的整数/计数数字

q)med 1 2 3 4 5 where "i"$100*0.15 0.10 0.20 0.30 0.25
4f

You could effectively weight the median by duplicating (using where):

q)med 10 34 23 123 5 56 where 4 1 1 1 1 1
10f
q)med 10 34 23 123 5 56 where 1 1 1 1 1 4
56f
q)med 10 34 23 123 5 56 where 1 2 1 3 2 1
34f

If your weights are percentages (e.g. 0.15 0.10 0.20 0.30 0.25) then convert them to equivalent whole/counting numbers

q)med 1 2 3 4 5 where "i"$100*0.15 0.10 0.20 0.30 0.25
4f
流年里的时光 2025-01-23 16:06:38

对于值 v 和权重 wmed v 其中 w 会为较大的 w 值吞噬空间。

相反,将 wv 的升序排序,并查找累计总和达到其总和一半的位置。

q)show v:10?100
17 23 12 66 36 37 44 28 20 30
q)show w:.001*10?1000
0.418 0.126 0.077 0.829 0.503 0.12 0.71 0.506 0.804 0.012
q)med v where "j"$w*1000
36f

q)w iasc v / sort w into ascending order of v
0.077 0.418 0.804 0.126 0.506 0.012 0.503 0.12 0.71 0.829
q)0.5 1*(sum;sums)@\:w iasc v / half the sum and cumulative sums of w
2.0525
0.077 0.495 1.299 1.425 1.931 1.943 2.446 2.566 3.276 4.105
q).[>]0.5 1*(sum;sums)@\:w iasc v / compared
1111110000b
q)v i sum .[>]0.5 1*(sum;sums)@\:w i:iasc v / weighted median
36

q)\ts:1000 med v where "j"$w*1000
18 132192
q)\ts:1000 v i sum .[>]0.5 1*(sum;sums)@\:w i:iasc v
2 2576

q)wmed:{x i sum .[>]0.5 1*(sum;sums)@\:y i:iasc x}

一些值得注意的矢量技术:

  • 将两个函数与 Each 应用左 (sum;sums)@\: 并使用 申请 . 以及结果上的运算符,而不是设置变量,例如 (0.5*sum yi)>sums yi:y i 或定义内部 lambda { sums[x]<0.5*sum x}y i
  • 使用
  • noreferrer">iasc通过并置对另一个多个映射

For values v and weights w, med v where w gobbles space for larger values of w.

Instead, sort w into ascending order of v and look for where cumulative sums reach half their sum.

q)show v:10?100
17 23 12 66 36 37 44 28 20 30
q)show w:.001*10?1000
0.418 0.126 0.077 0.829 0.503 0.12 0.71 0.506 0.804 0.012
q)med v where "j"$w*1000
36f

q)w iasc v / sort w into ascending order of v
0.077 0.418 0.804 0.126 0.506 0.012 0.503 0.12 0.71 0.829
q)0.5 1*(sum;sums)@\:w iasc v / half the sum and cumulative sums of w
2.0525
0.077 0.495 1.299 1.425 1.931 1.943 2.446 2.566 3.276 4.105
q).[>]0.5 1*(sum;sums)@\:w iasc v / compared
1111110000b
q)v i sum .[>]0.5 1*(sum;sums)@\:w i:iasc v / weighted median
36

q)\ts:1000 med v where "j"$w*1000
18 132192
q)\ts:1000 v i sum .[>]0.5 1*(sum;sums)@\:w i:iasc v
2 2576

q)wmed:{x i sum .[>]0.5 1*(sum;sums)@\:y i:iasc x}

Some vector techniques worth noticing:

  • Applying two functions with Each Left (sum;sums)@\: and using Apply . and an operator on the result, rather than setting a variable, e.g. (0.5*sum yi)>sums yi:y i or defining an inner lambda {sums[x]<0.5*sum x}y i
  • Grading one list with iasc to sort another
  • Multiple mappings through juxtaposition: v i sum ..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文