stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.6k stars 370 forks source link

Method `max_abs_deviation` in stan/mcmc/chainset should be *median* not max #3316

Open mitzimorris opened 10 hours ago

mitzimorris commented 10 hours ago

Summary:

Naming mistake during implementation - assumed the "M" in "MAD" was "max". Wrong - it's "median".

Description:

The name of this function is inaccurate. The code shows what's going on:

  /**
   * Compute maximum absolute deviation (mad) for specified parameter.
   *
   * Follows R implementation:  constant * median(abs(x - center))
   * where the value of center is median(x) and the constant is 1.4826,
   * a scale factor for asymptotically normal consistency: `1/qnorm(3/4)`.
   * (R stats version 3.6.2)
   *
   * @param index parameter index
   * @return sample mad
   */
  double max_abs_deviation(const int index) const {
    Eigen::MatrixXd draws = samples(index);
    auto center = median(index);
    Eigen::MatrixXd abs_dev = (draws.array() - center).abs();
    Eigen::Map<Eigen::VectorXd> map(abs_dev.data(), abs_dev.size());
    return 1.4826 * stan::math::quantile(map, 0.5);
  }

Current Version:

v2.35.0

mitzimorris commented 10 hours ago

fixing this requires changing the function name (everywhere, including CmdStan's stansummary utility) and the docstring. it won't actually change the code. worth doing before the release?