stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
140 stars 44 forks source link

[FR] NaN values for `write_array` #1171

Closed SteveBronder closed 2 years ago

SteveBronder commented 2 years ago

Is your feature request related to a problem? Please describe.

I'd like to propose a fix for https://github.com/stan-dev/stan/issues/3114 that defaults the values of the vector we return from write_array to NaN values. This is nice because parameters will always be filled in but if write_array throws an error anywhere then additional transformed parameters / generated quantities will just be NaN

I'd also like to have an addition that slightly changes the behavior of write_array such that if the user passes a vector that is longer than what is needed, the bottom N values are of the matrix are filled out. This allows for users of write_array to do things like

Eigen::VectorXd unconstrained_values = //... fill in unconstrained values
Eigen::VectorXd constrained_values = Eigen::VectorXd(num_constrained_values + 1);
constrained_values(0) = model.log_prob(unconstrained_values);
model.write_array(constrained_values, unconstrained_values);

where currently we need to make a whole separate vector for the values such as the below.

Eigen::VectorXd unconstrained_values = //... fill in unconstrained values
Eigen::VectorXd constrained_values;
model.write_array(constrained_values, unconstrained_values);

Eigen::VectorXd write_to_csv_constrained_values = Eigen::VectorXd(num_constrained_values + 1);
write_to_csv_constrained_values(0) = model.log_prob(unconstrained_values);
write_to_csv_constrained_values.tail(num_constrained_values) = constrained_values;
writer(write_to_csv_constrained_values);

Though this is tangential to the NaN filling and we can just do this in a separate PR. Now that I'm thinking about it maybe we should have a seperate write_array that does this and the user can specify whether they want to write to the top or bottom of the array.

WardBrian commented 2 years ago

I'd also like to have an addition that slightly changes the behavior of write_array such that if the user passes a vector that is longer than what is needed, the bottom N values are of the matrix are filled out. This allows for users of write_array to do things like

Is it possible to do this as two separate PRs? I think it's very difficult to review with them both happening at the same time

SteveBronder commented 2 years ago

Actually @bob-carpenter @WardBrian yes I went through removed the second part of this. Now it's just the first part that fills in the NaN values. I'd like to do the second part (in particular for pathfinder) but can wait for a separate PR