gpt4 book ai didi

vega-lite - 如何在 vega 公式变换中使用阶乘 (!)

转载 作者:行者123 更新时间:2023-12-04 08:06:23 25 4
gpt4 key购买 nike

我正在尝试创建 binomial distribution PMF 的直方图使用 vega js 规范。
这通常是如何完成的? vega expressions不包括用于选择或阶乘的函数,也不包括 statistical functions 下的二项式分布.
我似乎也无法引用 vega 规范中的其他函数(即下面的 yval)。

  "data":[
{"name": "dataset",
"transform": [
{"type":"sequence", "start": 1, "stop": 50, "step": 1, "as": "seq" },
{"type": "formula", "as": "xval", "expr": "if(datum.seq<nval,datum.seq,NaN)"},
{"type": "formula", "as": "yval", "expr": "math.factorial(datum.xval)
" }
]}],
谢谢。

最佳答案

没有可用的阶乘运算,但一种合适的选择可能是用 Stirling's approximation 对其进行近似处理。 ,或者可能是 Stirling series如果需要更高的精度。
例如,在 Vega-Lite ( view in editor ) 中:

{
"data": {
"values": [
{"n": 0, "factorial": 1},
{"n": 1, "factorial": 1},
{"n": 2, "factorial": 2},
{"n": 3, "factorial": 6},
{"n": 4, "factorial": 24},
{"n": 5, "factorial": 120},
{"n": 6, "factorial": 720},
{"n": 7, "factorial": 5040},
{"n": 8, "factorial": 40320},
{"n": 9, "factorial": 362880},
{"n": 10, "factorial": 3628800}
]
},
"transform": [
{
"calculate": "datum.n == 0 ? 1 : sqrt(2 * PI * datum.n) * pow(datum.n / E, datum.n)",
"as": "stirling"
},
{"fold": ["factorial", "stirling"]}
],
"mark": "point",
"encoding": {
"x": {"field": "n", "type": "quantitative"},
"y": {"field": "value", "type": "quantitative", "scale": {"type": "log"}},
"color": {"field": "key", "type": "nominal"}
}
}
enter image description here

关于vega-lite - 如何在 vega 公式变换中使用阶乘 (!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66201742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com