This is an old version, view current version.

## 17.5 Matrices with Parameters and Constants

Suppose you have a $$3 x 3$$ matrix and know that two entries are zero but the others are parameters. Such a situation arises in missing data situations and in problems with fixed structural parameters.

Suppose a $$3 \times 3$$ matrix is known to be zero at indexes $$[1,2]$$ and $$[1,3]$$. The indexes for parameters are included in a “melted” data-frame or database format.

transformed data {
int<lower=1, upper=3> idxs[7, 2]
= { {1, 1},
{2, 1}, {2, 2}, {2, 3},
{3, 1}, {3, 2}, {3, 3} };
...

The seven remaining parameters are declared as a vector.

parameters {
vector A_raw;
...

Then the full matrix A is constructed in the model block as a local variable.

model {
matrix[3, 3] A;
for (i in 1:7)
A[idxs[i, 1], idxs[i, 2]] = A_raw[i];
A[1, 2] = 0;
A[1, 3] = 0;
...

This may seem like overkill in this setting, but in more general settings, the matrix size, vector size, and the idxs array will be too large to code directly. Similar techniques can be used to build up matrices with ad-hoc constraints, such as a handful of entries known to be positive.