如何定义mcan向量的有序向量:如何定义

2024-06-16 18:34:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个有序的逻辑回归,其中一个参数是一个有序的切入点向量。我不知道该如何定义它们。在

我想出了一个非常愚蠢的方法,就是手动定义向量的每个分量,使用其中一个作为另一个的边界:

with pm.Model() as bound_model:
    a = pm.Normal('a', mu=0, sd=10)
    BoundedB = pm.Bound(pm.Normal, upper=a)
    b = BoundedB('b', mu=0, sd=10)
    BoundedC = pm.Bound(pm.Normal, upper=b)
    c = BoundedC('c', mu=0, sd=10)

    bound_trace = pm.sample(1000)

这几乎没有效率,我也不确定他们是否能如期工作。有更好的方法吗?在


Tags: 方法参数定义逻辑sdupper向量normal
1条回答
网友
1楼 · 发布于 2024-06-16 18:34:29

我想这是pymc3中缺少的特性。我可能会写一个请求请求,但同时你可以使用这样的方法:

class Ordered(pymc3.distributions.transforms.ElemwiseTransform):
    name = "ordered"

    def forward(self, x):
        out = tt.zeros(x.shape)
        out = tt.inc_subtensor(out[0], x[0])
        out = tt.inc_subtensor(out[1:], tt.log(x[1:] - x[:-1]))
        return out

    def backward(self, y):
        out = tt.zeros(y.shape)
        out = tt.inc_subtensor(out[0], y[0])
        out = tt.inc_subtensor(out[1:], tt.exp(y[1:]))
        return tt.cumsum(out)

    def jacobian_det(self, y):
        return tt.sum(y[1:])

像这样使用它:

^{pr2}$

编辑:这里发生的事情的简要说明:

我们定义了一个从$R^n$到有序序列集的映射

f(x_1) = x_1,\quad f(x_i) = f(x_{i - 1}) + exp(x_i)

因为这是一个很好的双目标函数,我们可以通过

P_{R^n}(x) = P_{ordered}(f(x)) \cdot |J_{f(x)}|

其中J是变换的雅可比矩阵。在

采样器将只看到未约束的值。这基本上就是Bound首先是如何实现的。在

如果需要更多详细信息,可以查看stan manual。它包含了对这些转换的很好的描述,pymc3和stan的数学是相同的。在

相关问题 更多 >