stan-dev / stan-dev/math

Feature request: `row_centred_matrix` and `column_centred_matrix`

Open
#3,361 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature good first issue
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

To facilitate working with the sum-to-zero constraint in multivariate contexts, it would be valuable to be able to specify not only individual sum_to_zero_vectors but also matrices for which all rows or all columns are constrained to sum to zero. To my knowledge, there is no universally standard name for such a mathematical object (nor to Claude's knowledge, which only produced a list of suggestions), but for the sake of starting a conversation, I would propose something like row_centered_matrix and col_centered_matrix (and perhaps row_centred_matrix and col_centred_matrix as aliases for spelling-dialect neutrality).

The primary use case for me would be hierarchical modelling with correlated multivariate random effects, e.g., random-slopes models or multivariate item-response models. To reduce problems with funnel geometries during sampling, it can be wise to write such models using ‘raw’ parameters with standard normal priors and then construct the actual parameters in the transformed parameters block via matrix multiplication with standard-deviation and correlation-matrix hyperpriors (e.g., the User's Guide chapter on ‘Regression Models’).

That's awkward to do using the sum_to_zero_vector type alone. The N raw parameter vectors (let's say of dimension K) have to be stored as array[K] sum_to_zero_vector[N]. This structure needs to be pulled apart and restructured as matrix[K, N] or array[N] vector[K] in transformed parameters in order to be multiplied by a cholesky_factor_corr[K]. In practice, the problem is mostly that this restructuring step is ugly and confusing to read, but in theory, it is also memory inefficient and could cause slowdowns at large model scale.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing sum_to_zero_vector type and the related matrix, array, vector, and cholesky_factor_corr constructs described in the issue. Read the User's Guide regression chapter for the motivating model pattern, then clarify the naming and API design; done should include an agreed approach for row- and column-constrained matrices and their spelling aliases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.