Skip to content

Include (unconstrained) parameter names along with the inverse metric? #3398

Description

@jgabry

CmdStan writes the inverse metric to a file, but it doesn't include the names of the unconstrained parameters corresponding to the rows/columns. See stan-dev/cmdstanr#716.

To do that I think it would just involve including the names returned by

  model.unconstrained_param_names(names, false, false);

in the JSON file, right? For example,

  {
    "stepsize": 0.1,
    "metric_type": "diag_e",
    "unconstrained_param_names": ["alpha", "beta.1", "beta.2"],
    "inv_metric": [0.1, 0.2, 0.3]
  }

This would make it much easier for users to figure out which unconstrained parameter elements correspond to which elements of the matrix.

Activity

  1. added theissue type on Jul 21, 2026
  2. WardBrian commented on Jul 21, 2026

    @WardBrian
    Member

    I think this is a very reasonable extension. The code that writes these files actually lives at

    void write_sampler_state_struct(callbacks::structured_writer& struct_writer) {
    struct_writer.begin_record();
    struct_writer.write("stepsize", get_nominal_stepsize());
    struct_writer.write("metric_type", z_.metric_type());
    struct_writer.write("inv_metric", z_.inv_e_metric_);
    struct_writer.end_record();
    }
    , so I'll move this to stan

  3. transferred this issue fromstan-dev/cmdstanon Jul 21, 2026
  4. bob-carpenter commented on Jul 21, 2026

    @bob-carpenter
    Member

    I'm confused about why the step size is included in the metric output. Is this meant to be more general? What's the function name in CmdStanPy or CmdStanR that gets this? What's the name of the saved file?

  5. WardBrian commented on Jul 21, 2026

    @WardBrian
    Member

    The Stan issue that led to these files was just to write 'tuning parameters' #3181 / #3230

    The default filename for them in cmdstan does end in _metric.json, I believe, but these files aren't directly exposed to the user (at least in cmdstanpy), the fields are read out individually

  6. jgabry commented on Jul 21, 2026

    @jgabry
    MemberAuthor

    What's the function name in CmdStanPy or CmdStanR that gets this? What's the name of the saved file?

    In cmdstanr if you do

    fit <- model$sample(..., save_metric = TRUE)
    

    Then cmdstanr can tell you where CmdStan wrote the metric files if you call fit$metric_files(). You can also directly get the inverse mass matrix as an R object by calling fit$inv_metric(). But the rows and columns have no names, which is why I opened this issue (or really someone opened an issue in CmdStanR a long time that I just came back to because I ran into it myself).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions