DAX Poisson.dist 的 M 等效值

3

有人知道在DAX中,Poisson分布公式是否有M等效的版本吗?我有一个查询,在其中我想要基于两列计算该公式,但无法在任何地方找到解决方案。或者我应该将查询添加到数据模型中,并在那里使用DAX来计算它?


1
也许这个YouTube视频可以帮助 - 链接。我知道在SO上简单粘贴解决方案的链接是一个不好的做法,但我甚至不会假装我理解那里发生了什么...! - Justyna MK
2个回答

1
你可以根据定义将其定义为自定义函数。
以下是使用List.Accumulate实现累加和的示例:PoissonDist(k,mean,cdf)
(k as number, mean as number, cdf as logical) as number =>
let
    k = Number.RoundDown(k, 0),
    Poisson = Number.Exp(-mean) * Number.Power(mean, k) / Number.Factorial(k),
    Cumulative = List.Accumulate(List.Range({0..k},0), 0, (state, current) => state + Number.Exp(-mean) * Number.Power(mean, current) / Number.Factorial(current)),
    Switch = if cdf then Cumulative else Poisson,
    Result = if k < 0 or mean < 0 then error "Input cannot be negative." else Switch
in
    Result

这个版本更加紧凑,但本质上是相同的东西:

(k as number, m as number, cdf as logical) as number =>
let
    k = if k > 0 and m > 0 then Number.RoundDown(k,0) else error "Input cannot be < 0.",
    Poisson = List.Accumulate(List.Range({0..k}, if cdf then 0 else k), 0,
        (sum, i) => sum + Number.Exp(-m) * Number.Power(m, i) / Number.Factorial(i))
in
    Poisson

0

亚历克西斯的答案在我的情况下有一些缺陷:

  1. Poisson(0, 0, false) 在两个版本中都会因为Number.Power(0, 0)而抛出错误。相比之下,Excel返回正确值1(请参阅维基百科上的递归定义)。
  2. 计算累积值很耗费资源,因此为了更好的性能,列表操作应该只在真正需要时执行(第一个版本)。
  3. 同样是性能方面:表达式Number.Exp(-m)可以移至列表操作之外(第二个版本)。
  4. 同样是性能方面:幂和阶乘的重复计算可以用迭代乘法替代(第二个版本)。

所以这是我修改后的泊松分布版本:

(x as number, mean as number, cumulative as logical) as number =>
    let
        k = if x >= 0 then  Number.RoundDown(x, 0) else error "x must not be negative",
        m = if mean >= 0 then mean else error "mean must not be negative",
        result = Number.Exp(-m) * (if k = 0 then 1 else List.Accumulate({1..k}, [sum = 1, element = 1], (state, i) => let next = state[element]*m/i in [sum = if cumulative then state[sum] + next else next, element = next])[sum])
    in
        result

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接