Corner case in `dirichlet_lpdf`: error when both alpha - 1 = theta = 0

未关闭
#3,186 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cpp

调研方向

从 stan/math/prim/prob/dirichlet_lpdf.hpp 中链接的 theta_log 计算开始,然后检查相应的 partials_vec<1> 处理。确定 alpha - 1 = theta = 0 情况是否应在不产生错误的情况下贡献零,并确保相关的 autodiff 行为一致。

由索引模型根据 Issue 内容生成。

描述

In the code there is currently:

  const auto& theta_log
      = to_ref_if<!is_constant_all<T_prior_size>::value>(theta_dbl.log());

  if (include_summand<propto, T_prob, T_prior_size>::value) {
    lp += (theta_log * alpha_m_1).sum();
  }

I understand that by the convention (and also by Stan documentation) for Dirichlet distribution, we usually require theta > 0. But when you think about it, for the corner case when alpha -1 == theta == 0, it is reasonable to define the lpdf to be 0 by the same logic why we define pow(0,0)=1. This is relevant in my application, where I may have on input an information that certain index of theta is a forbidden state and may thus be represented as hard constrained to be equal to 0, but then the Dirichlet prior crashes no matter what. This would be very inefficient for me to workaround by deleting the offending array element as I need to do this over a matrix with many rows each of which may have different constraints, so currently I needed to make my custom implementation of dirichlet_lpdf just to deal with that. I thus propose we change the code to something like:

  if (include_summand<propto, T_prob, T_prior_size>::value) {
    lp += lmultiply(alpha_m_1, theta).sum();
  }

The partials_vec<1> may need a corresponding adjustment.

主要语言
C++
星标
845
派生
225
平均合并
4 天 21 小时
30 天内合并 PR
13

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

stan-dev/math 的其他 Issue

查看 stan-dev/math 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。