Skip to content

Implement posterior::ess_mean with coda fallback (Issue #45) (#5)#46

Open
GiorgioMB wants to merge 11 commits intoquentingronau:masterfrom
GiorgioMB:posterior-ess-computation-pr
Open

Implement posterior::ess_mean with coda fallback (Issue #45) (#5)#46
GiorgioMB wants to merge 11 commits intoquentingronau:masterfrom
GiorgioMB:posterior-ess-computation-pr

Conversation

@GiorgioMB
Copy link
Contributor

@GiorgioMB GiorgioMB commented Dec 3, 2025

As discussed in Issue #45, with the following, an internal helper, .bs_compute_ess() does the following to compute the effective sample size:

  1. Tries posterior::ess_mean() if the package is installed and the global option bridgesampling.use_posterior_ess (default TRUE) is enabled.
  2. Falls back silently to median(coda::effectiveSize()).
  3. Falls back again to raw nrow(samples) if both methods fail.
  4. Replaces existing ESS code paths with calls to the helper.
  5. Includes some basic checks to ensure the internal function works as intended (test_ess.R)
  6. Adds posterior to Suggest:

Additionally, it also renames all use_neff and neff with use_ess and ess respectively.

…onau#45) (#5)

* Prepare Pull Request (#4)

* Implement effective sample size computation function

Added a helper function to compute effective sample size for iterative schemes, with fallback options for different methods.

* Refactor effective sample size calculation

Replaced effective sample size calculation with a custom function for consistency.

* Implement tests for .bs_compute_neff function

Add tests for .bs_compute_neff function behavior with and without posterior ESS.

* Add messages for fallback cases in ESS computation

* Add 'posterior' dependency to DESCRIPTION

Added 'posterior' to the list of dependencies in DESCRIPTION.

* Rename parameter 'use_neff' to 'use_ess'

* Rename parameter for effective sample size function

Updated parameter name from 'use_neff' to 'use_ess' in the helper function for effective sample size calculation.

* Rename function and update effective sample size calculation

* Replace neff computation with ess in bridge_sampler

* Replace 'neff' with 'ess' in bridge sampler

* Rename 'neff' to 'ess' in bridge_sampler_normal.R

* Rename neff to ess for effective sample size

* Rename tests for .bs_compute_ess function
@singmann
Copy link
Collaborator

Sorry for the delay at looking at that, the last two weeks of term were of course very busy again. I have two thoughts:

  1. Sorry for only coming with this now, but can you rename the option to bridgesampling.ess_function which can have two values, either "posterior" or "coda". This way we keep the possibility open to add further ESS functions in the future and do not have to rename the option.
  2. Can you set the option when loading the package through .onLoad()? This function should be a in a new R file called zzz.R. For more see here. In particular, my suggestion is to run use requireNamespace("posterior", quietly = TRUE) in this function and if TRUE set the option to "posterior" (and "coda" otherwise). Then we only produce a message once at startup if posterior is not found and not every time when bridgesampling() is called.

Makes sense?

Add .onLoad function to set ESS function based on posterior package availability.
Refactor effective sample size computation to allow user-defined method and improve error handling.
Updated the documentation for the 'use_ess' parameter to clarify its default behavior and the method for effective sample size computation.
@GiorgioMB
Copy link
Contributor Author

Dear Professor Singmann,
thank you for the suggestions, I have now implemented them. The option has been renamed to bridgesampling.ess_function, the selection logic has been moved to .onLoad() in a new zzz.R file, and all the relevant documentation has been updated.
At this point, the ESS computation and option handling should behave as you outlined, and in addition, I modified other tests to ensure full compatibility with the new ess naming scheme (replacing neff/use_neff throughout).

@singmann
Copy link
Collaborator

Please have a look at my comments for function .bs_compute_ess

Refactor ESS calculation logic to improve validation and error handling.
@GiorgioMB
Copy link
Contributor Author

Dear Professor,
I have implemented the requested changes, the function now behaves as follows:

  • If the selected method is neither posterior nor coda, it defaults to coda.
  • If the method is posterior, the function first attempts to compute the ESS using the posterior package; if this fails, it falls back to the coda implementation, and this fallback structure is intentionally generic, so that additional methods can be inserted later in the same sequence if desired.
  • If use_ess is FALSE, the function immediately returns n
  • If all ESS computations fail, the function falls back to returning n.
    Please let me know if you would like me to document the extensibility of the fallback logic more explicitly in the code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants