nutpie icon indicating copy to clipboard operation
nutpie copied to clipboard

Stan: Represent complex values in traces as complex value instead of as array

Open aseyboldt opened this issue 7 months ago • 3 comments

aseyboldt avatar May 06 '25 10:05 aseyboldt

Another thing to note (due to what is largely a historical mistake made by an intern) is that the real/imaginary index is output in a different order than most others.

e.g., the first few columns of an array[2,3] complex zs are

zs.1.1.real, zs.1.1.imag, zs.2.1.real, zs.2.1.imag, zs.1.2.real, zs.1.2.imag, zs.2.2.real, zs.2.2.imag

(notice how the rightmost index [real, imag] moves the fastest, but the other columns move faster to the left...).

I think this is actually currently broken in nutpie, even in the 'output an array of two elements' code.

Consider:

  array[2, 3] complex zs = {{3, 4i, 5}, {1i, 2i, 3i}};

I would expect trace.posterior.zs[0,0,0,1] to be [0,4], but it's currently reporting [0,0]...

I have two compliance test models that generate some nasty output but with each value being able to be asserted just based on the order they appear: https://github.com/stan-dev/stanio/blob/main/test/data/rectangles/output.stan https://github.com/stan-dev/stanio/blob/main/test/data/tuples/output.stan

WardBrian avatar May 06 '25 14:05 WardBrian

Damn, I always had a bad feeling about that code, it just blindly trusts that the variables are in the expected order. I think I'll add a second pass over the variable names to verify that the order is what we expect. But good catch, thanks! :-)

aseyboldt avatar May 06 '25 14:05 aseyboldt

It's a mistake I've personally made, which made catching it easier the second time

WardBrian avatar May 06 '25 15:05 WardBrian