Skip to content

export srs_diff_est #333

Open
Open

Description

@avehtari

loo_subsample() can sometimes be over-complex. srs_diff_est() is a non-exported internal function used by loo_subsample. srs_diff_est() implements difference estimator Equations 7-9 of Magnusson et al., 2020. It would be useful to export this function and provide user targeted function with documentation.

Activity

  1. jgabry commented on Mar 2, 2026

    @jgabry
    Member

    Sounds good

  2. vinniott commented on Mar 11, 2026

    @vinniott
    Contributor

    Hello,

    would this be a good first issue/contribution?
    I.e. roxygenise, #‘ @export respectively, this:

    Image

    and also add 1 or 2 examples for users as well as the Magnusson et al. (2020) citation?

    Kind regards

  3. MansMeg commented on Mar 11, 2026

    @MansMeg
    Collaborator

    Sounds good to me too.

  4. jgabry commented on Mar 11, 2026

    @jgabry
    Member

    @vinniott thanks for your interest. Yes I think this would be a good first issue and what you described adding sounds good. We also probably want a bit more detail in the documentation when moving this to an exported user-facing function. For example, we should mention what type/shape the inputs should be and what the contents of the returned list are.

  5. vinniott commented on Mar 12, 2026

    @vinniott
    Contributor

    @jgabry thank you for elaborating.
    If you want, then I could give it a try and update you here in a week?

  6. jgabry commented on Mar 12, 2026

    @jgabry
    Member

    Great, thank you!

  7. vinniott commented on Mar 19, 2026

    @vinniott
    Contributor

    Hi again,

    while I have made progress, I think it might be better if I look into stan-dev/bayesplot now and return to loo later on.
    Again, thank you for explaining this issue to me.

    Maybe you could tag this as "good-first-issue" now so that others can find it easier?

    In case this is helpful for someone, here is my initial roxygen2 skeleton for the export:
    master...vinniott:loo:export-srs-diff-est

  8. avehtari commented on Mar 20, 2026

    @avehtari
    MemberAuthor

    Thanks @vinniott for the skeleton. I think this issue requires also contribution from me. I can write the technical description I'd like to have included in the documentation.

  9. avehtari commented on Mar 20, 2026

    @avehtari
    MemberAuthor

    Here is my proposoal for the documentation

    #' Difference estimator with simple random sampling without replacement.
    #'
    #' The difference estimator `srs_diff()` estimates
    #' the expectation $nE[y]$ when we have $n$ approximate values $\tilde{y}_i$,
    #' $i=1,\ldots,n$ and $m<n$ accurate values $y_j$, $j \in
    #' \mathcal{S}$, where $m$ is the subsample size and $\mathcal{S}$ is
    #' a simple random subsample without replacement.  The original
    #' approach is by Cochran (1977) and we follow the equations 7--9 by
    #' Magnusson et al. (2020).
    #' 
    #' @param y_approx (numeric) `n` approximated values.
    #' @param y (numeric) `m<n` subsampled values.
    #' @param y_idx (integerish) The index of `y` in `y_approx`.
    #' @return A named list containing numeric values:
    #' * `y_hat`: estimated mean of $y$ (Eq 7),
    #' * `v_y_hat`: variance of the mean estimate (Eq 8), and
    #' * `hat_v_y`: estimated variance of $y$ (Eq 9).
    #' 
    #' @details Magnusson et al. (2020) Eq (9) first row second `+` should
    #'   be `-`. Supplementary material Eq (6) has this correct.
    #'   As `srs_diff_est()` in `loo` package is used for $nE[y]$, there is 
    #'   a proportional difference of $1/n$ compared to the paper.
    

    As srs_diff_est() has been used for elpd_loo which is in sum scale, srs_diff_est() is actually estimating $nE[y]$, while the equations in the paper are for $E[y]$. The question whether we should change the exposed function to estimate $E[y]$ and then adjust other code in loo_subsample.R

  10. added a commit that references this issue on Mar 22, 2026
  11. vinniott commented on Mar 22, 2026

    @vinniott
    Contributor

    Thank you for the technical documentation! I have incorporated it and made a PR #340 .
    However, there is no @example yet because I am not fully familiar with the package as a whole.

  12. added a commit that references this issue on Apr 26, 2026
  13. added a commit that references this issue on May 10, 2026
  14. added this to the v2.11.0 milestone on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions