Fixing certain matrix elements in the parameter

stanc

#1

I have a parameter which is a matrix[3,3] m. Is there a mechanism for forcing m[0,0] = 0.0, and let the rest be solved?


#2

I have a parameter

Don’t treat m as a parameter.

  • Declare and initialise the matrix m in the transformed data {} block or
  • In your model section, just declare and initialise a matrix m.

#3

I want to make sure I explained my requirement properly. I want to fix only the diagonal elements of the 3 x 3 matrix. The other 6 elements need to be derived by the solver. If I declare in the transformed data block, will the solver fill in the values? Same question if I declare in the model block.

I tried declaring and initializing in the model block.

real<lower=0,upper=6> m[S, S];  		
for (s in 1:S)
	m[S, S] = 0.0;

I get
variable “real” does not exist.

declare in parameter block and initialize in model block fails as well.


#4

Yes. The mechanism is reading section 11.2 of the Stan User’s Manual.


#5

Sorry, I did not see that you only wanted to fix part of a matrix, but that is also easy enough.
Do be a bit careful, …

I get
variable “real” does not exist.

That is a syntax error. You have some lines above the “real”, so the parser decides that you are out of the variable declaration part of the model block.
If you move that “real” a bit higher, you will get rid of the syntax error. It is a different issue to your question about fixing values in a matrix.


#6

So what’s the answer to fixing part of the matrix? You just said it’s easy enough. That’s the only thing I care about.

Section 11.2 covers truncated data. There are only examples for real. How would you truncate selected elements of a matrix?


#7

I was referring to section 11.2 of the current Stan User’s Manual.


#8

ok, I was one version behind. I think its clear. I will test out.


#9

I tested this out
transformed parameters {
real nondiag_xi[N, S, S];
nondiag_xi = xi;
for (s in 1:S)
for (n in 1:N)
nondiag_xi[n, S, S] = 0.0;
}
but only the nondiag_xi[n, 3,3] ended up as 0.0. The other two positions [1,1] and [2,2] still copied over from xi.


#10

That would result in a parser error unless xi is declared as

parameters {
  real xi[N, S, S];
}

and, if so, that is not going to accomplish what you want to do. As to why it only put a zero into the 3, 3 element of nondiag_xi, that is because Stan is a case-sensitive language.


#11

yes, declaration of xi was correct, and no compilation error. good point about s vs. S.