cmdstanpy: unexpected output for parameter matrices
Summary:
When I was using cmdstanpy to read a parameter matrix from CSV, it put the values in an unexpected order. In Numpy terms, I think that the data was being read in “c order” when it should be read in “fortran order”.
Thanks in advance for taking a look at this!
Description:
Below, I’ve included a small Stan program that should output a matrix whose values match their row number. So the first row should be all 1’s, the second row should be all 2’s, etc.
The stan CSV output looks correct to me: Each entry of x lines up with its corresponding row number. I’ve aligned the output below to make this easier to confirm visually.
lp__ ,x.1.1 ,x.2.1 ,x.3.1 ,x.4.1 ,x.1.2 ,x.2.2 ,x.3.2 ,x.4.2 ,x.1.3 ,x.2.3 ,x.3.3 ,x.4.3
0 , 1 ,2 ,3 ,4 ,1 ,2 ,3 ,4 ,1 ,2 ,3 ,4
However, when I read this data in with cmdstanpy, I see this output:
array([[1., 2., 3.],
[4., 1., 2.],
[3., 4., 1.],
[2., 3., 4.]])
I think that cmdstanpy could get the expected output by telling Numpy to expect the numbers to be in Fortran’s preferred ordering with order=F. For this example, the code might look like this:
np.array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]).reshape(4, 3, order = "F")
Additional Information:
Stan code, saved as weird.stan:
transformed parameters {
matrix[4, 3] x;
for (row_num in 1:4) {
for (col_num in 1:3) {
x[row_num, col_num] = row_num;
}
}
}
Python code:
from cmdstanpy import CmdStanModel
model = CmdStanModel(stan_file="weird.stan")
fit = model.optimize()
print(fit.stan_variable("x"))
Current Version:
stanc3 v2.27.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 26 (21 by maintainers)
All interface are affected even when there is one row of data
e.g. in the csv file one row
needs to be reshaped to a matrix shape. Sure there is no draw dimension needed (but maybe xr method should still contain it?).