-
Notifications
You must be signed in to change notification settings - Fork 85
Add effective sample size estimators #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevinchern
wants to merge
9
commits into
dwavesystems:main
Choose a base branch
from
kevinchern:feature/ess
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bd4c400
Add ESS estimators
kevinchern 4a2d252
Rename functions to be consistent and active
kevinchern 8c102e4
Move ess out of sampleset.py
kevinchern 0919227
Add unit tests for estimating ESS
kevinchern 04f46eb
Add release notes
kevinchern 151ed60
Fix aesthetics and type-hints
kevinchern 2cbdc9b
Validate parameter in public function
kevinchern 4f1ce5e
Improve error message
kevinchern fb04a97
Fix copyright header
kevinchern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright 2026 D-Wave | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from math import floor | ||
|
|
||
| import numpy as np | ||
|
|
||
| __all__ = ['estimate_effective_sample_size'] | ||
|
|
||
|
|
||
| def estimate_effective_sample_size(x: np.ndarray, b: int | None = None) -> float: | ||
| """Estimates the effective sample size of ``x``. | ||
|
|
||
| The effective sample size (ESS) is the number of effectively independent samples drawn from | ||
| Markov chains' stationary distribution. The univariate estimator implemented here is the | ||
| (multivariate) estimator defined in the first equation at the top of page 14 in | ||
| `<Revisiting the Gelman-Rubin Diagnostic https://arxiv.org/abs/1812.09384>`_. | ||
|
|
||
| Args: | ||
| x: An (m, n) matrix where rows index independent Markov chains and columns index | ||
| time steps. | ||
| b: Batch size of the estimator. If ``None``, then ``b`` is set to the floor of the | ||
| square root of ``n``. Defaults to None. | ||
|
|
||
| Returns: | ||
| float: An estimate of the effective sample size of ``x``. | ||
| """ | ||
| if x.ndim != 2: | ||
| raise ValueError("The input matrix ``x`` should have shape (m, n) where m indexes " | ||
| f"independent Markov chains and n indexes time. ``x`` has shape {x.shape}") | ||
| m, n = x.shape | ||
| if b is None: | ||
| b = int(floor(n**0.5)) | ||
| if b > n or b < 3: | ||
| raise ValueError( | ||
| f"Batch size should be at least three but no more than the chain length of the Markov " | ||
| f"chain. Batch size is {b} and chain length is {n}. If size was not given, it defaults" | ||
| f"to the floor of square-root of the chain length." | ||
| ) | ||
|
|
||
| s_squared = x.var(1, ddof=1).mean() | ||
| # = second equation at the top of page 7 | ||
| # = average of "sample variance within series" | ||
|
|
||
| # This estimator $\hat{\tau}^2_L$ is defined in equation (5) of | ||
| # Revisiting the Gelman-Rubin Diagnostic (https://arxiv.org/abs/1812.09384) | ||
| tau_squared = (2 * _estimate_replicated_batch_means(x, b) | ||
| - _estimate_replicated_batch_means(x, b // 3)) | ||
| # = equation (5) | ||
| # = nVar(xbar_i.) = total variance of the mean-within-series | ||
|
|
||
| sigma_squared = ((n - 1) / n) * s_squared + tau_squared / n | ||
| # = first equation at the top of page 10 | ||
| # = estimate of the distribution's variance | ||
|
|
||
| ess = m * n * sigma_squared / tau_squared | ||
| # = estimate of the effective sample size | ||
| # = first equation at the top of page 14 | ||
| return ess.item() | ||
|
|
||
|
|
||
| def _estimate_replicated_batch_means(x: np.ndarray, b: int) -> float: | ||
| """Computes the replicated batch means estimate. | ||
|
|
||
| This estimator (:math:`\\hat\\tau^2_b`) is defined in the equation above equation (5) of | ||
| `<Revisiting the Gelman-Rubin Diagnostic https://arxiv.org/abs/1812.09384>`_. | ||
|
|
||
| The estimator batches each Markov chain into batches of size ``b``, estimates the mean of each | ||
| batch, and computes the sample variance of these batched means. | ||
|
|
||
| The first few columns of ``x`` may be dropped in the estimation process in order to satisfy the | ||
| requirement that the length of the Markov chain is divisible by the batch size. | ||
|
|
||
| Args: | ||
| x: An (m, n) matrix where rows index independent Markov chains and columns index | ||
| time steps. | ||
| b: Batch size of the estimator. | ||
|
|
||
| Returns: | ||
| float: Replicated batch means estimate. | ||
| """ | ||
| n = x.shape[1] | ||
|
|
||
| n_batches = n // b | ||
| trimmed_length = b * n_batches | ||
|
|
||
| x = x[:, (n - trimmed_length):] | ||
| ybar = np.mean(np.split(x, n_batches, axis=1), axis=2).mT | ||
|
|
||
| res = b*np.var(ybar, ddof=1) | ||
| # NOTE: this is equivalent to | ||
| # res = ((ybar - muhat) ** 2).sum() * b / (n_batches * m - 1) | ||
| # where | ||
| # muhat = x.mean() | ||
| return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| features: | ||
| - | | ||
| Add estimator for effective sample size based on | ||
| `<Revisiting the Gelman-Rubin Diagnostic https://arxiv.org/abs/1812.09384>`_. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Copyright 2026 D-Wave | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Test effective sample size estimation.""" | ||
| import unittest | ||
| from math import isnan | ||
|
|
||
| import numpy as np | ||
|
|
||
| from dimod.ess import _estimate_replicated_batch_means | ||
| from dimod.ess import estimate_effective_sample_size as estimate_ess | ||
|
|
||
|
|
||
| class TestEffectiveSampleSize(unittest.TestCase): | ||
|
|
||
| def test_estimate_ess(self): | ||
| with self.subTest("ESS estimate should be undefined for constant input"): | ||
| self.assertTrue(isnan(estimate_ess(np.ones((100, 1000))))) | ||
|
|
||
| with self.subTest("Null batch size should raise an error."): | ||
| self.assertRaisesRegex(ValueError, "Batch size should be at least three", | ||
| estimate_ess, np.ones((100, 8))) | ||
|
|
||
| with self.subTest("Null batch size should raise an error."): | ||
| self.assertRaisesRegex(ValueError, "Batch size should be at least three", | ||
| estimate_ess, np.ones((100, 1000)), 0) | ||
|
|
||
| with self.subTest("Batch size larger than chain length should raise an error."): | ||
| self.assertRaisesRegex(ValueError, "Batch size should be at least three", | ||
| estimate_ess, np.ones((100, 1000)), 1001) | ||
|
|
||
| with self.subTest("Inputs that are not 2D should raise an error."): | ||
| self.assertRaisesRegex(ValueError, "The input matrix ``x`` should have shape", | ||
| estimate_ess, np.ones((123, 100, 1000)), 234) | ||
|
|
||
| with self.subTest("Single-batch estimates are incorrect."): | ||
| x = np.array([[0, 1, 2], | ||
| [0, 2, 4]]) | ||
| s_squared = (np.var([0, 1, 2], ddof=1) + np.var([0, 2, 4], ddof=1))/2 | ||
| tau_squared = (2 * _estimate_replicated_batch_means(x, 3) | ||
| - _estimate_replicated_batch_means(x, 1)) | ||
| sigma_squared = 2/3*s_squared + tau_squared/3 | ||
| answer = 2*3*sigma_squared/tau_squared | ||
| self.assertAlmostEqual(answer, estimate_ess(x, 3)) | ||
|
|
||
| with self.subTest("Two-batch estimatse are incorrect."): | ||
| x = np.array([[999, 0, 1, 2, 3, 6, 7], | ||
| [999, 0, 2, 4, 4, 6, 7]]) | ||
| s_squared = (np.var([999, 0, 1, 2, 3, 6, 7], ddof=1) | ||
| + np.var([999, 0, 2, 4, 4, 6, 7], ddof=1))/2 | ||
| tau_squared = (2 * _estimate_replicated_batch_means(x, 3) | ||
| - _estimate_replicated_batch_means(x, 1)) | ||
| sigma_squared = 6/7*s_squared + tau_squared/7 | ||
| answer = 2*7*sigma_squared/tau_squared | ||
| self.assertAlmostEqual(answer, estimate_ess(x, 3)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.