Skip to content

Feature request: column/row_sum_to_zero_matrix, sum_to_zero_row_vector #3354

Description

@mhollanders

Hi everyone,

The new sum_to_zero_vector and *_stochastic_matrix types are really useful for my work. The current sum_to_zero_matrix ensures both row and column sums are 0. Is it possible to get sum_to_zero_matrix types where only the rows or columns sum to zero, like the stochastic_matrix types?

Additionally, I think a sum_to_zero_row_vector also makes sense!

Cheers,

Matt

Activity

  1. WardBrian commented on Sep 5, 2025

    @WardBrian
    Member

    These would be reasonably easy to implement in Stan given the math implementations we already have.
    But, it would be reasonably easy to do this yourself now too:

    parameters {
      vector[N-1] raw;
    }
    transformed parameters {
      row_vector[N] sums_to_zero = sum_to_zero_jacobian(raw)';
    }

    The matrix versions would be essentially the same but would need a loop.

    The only differences between this and what we would supply built-in if we did are:

    1. Extra IO ('raw' would also get written out)
    2. User provided initialization is a bit more annoying, since an initial value would need to be specified for raw, not for sums_to_zero.

    If we had this framework a few versions ago we may not have added row/column stochastic, since they're similarly easy to do yourself. Doubly stochastic matrices or our current sum_to_zero_matrix that have the constraint in both axes are a different transform that cannot easily be built out of smaller parts

  2. mhollanders commented on Sep 10, 2025

    @mhollanders
    Author

    Hi Brian,

    Thanks for the explanation. I can see the logic about removing the row/column stochastic matrices and just using a transform in the transformed parameters block. The downside of course is that it's more verbose and required two objects, one in parameters and one in transformed, but I'll leave those choices up to you guys. Thanks again!

    Matt

  3. bob-carpenter commented on Sep 10, 2025

    @bob-carpenter
    Member

    I can see the logic about removing the row/column stochastic matrices and just using a transform in the transformed parameters block.

    I think this was just an explanation for why we didn't add these transforms. I think they could be useful and would be useful to add, because it's both error prone and tedious and also not the best performance to write this all yourself in transformed parameters (or as local variables in the transformed parameter blocks).

    It would help if we had these, which I think we should also add:

    matrix to_matrix(array[] vector x);
    matrix to_matrix(array[] row_vector u);
    
    array[] vector to_vector_array(matrix y);
    array[] row_vector to_row_vector_array(matrix v);
  4. mhollanders commented on Sep 10, 2025

    @mhollanders
    Author

    Hey Bob,

    The second function

    matrix to_matrix(array[] row_vector u);

    I'm pretty sure exists and works. This is another reason by sum_to_zero_row_vector would be useful because you could at least create a matrix of sum to zero rows with one function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions