Skip to content

Add alpha to the existing stop-gradient decorator - #285

Merged
AdrienCorenflos merged 8 commits into
state-space-models:mainfrom
kunyanghe21:dmop-alpha
Nov 18, 2026
Merged

AdrienCorenflos merged 8 commits into
state-space-models:mainfrom
kunyanghe21:dmop-alpha

Conversation

@kunyanghe21

@kunyanghe21 kunyanghe21 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This adds an optional alpha argument to stop_gradient_decorator. The default of 1 preserves the original behavior. Smaller values reduce the contribution of the selected log weights to the gradient at resampling.

The existing autodiff example shows how to use alpha. The resampling tests check that changing alpha preserves the forward results and scales the gradient correction as expected.

The code and documentation were prepared with AI assistance.

@AdrienCorenflos

Copy link
Copy Markdown
Contributor

Hi, thanks for the PR

Given this generalizes the stop_gradient_decorator fully, I think we should avoid adding a new function and simply modify the current one by adding a parameter alpha to (defaulting to the old behaviour which is alpha=0. Similarly, there's no need to create new examples, and you can document the behaviour of alpha in the existing autodiff example rather than create a new example altogether. The same can be said about unittesting, which can be done in the same place as the test for the old non-discounted version.

I don't mind you using AI for the code, but I would appreciate if you could avoid using AI to generate text for documentation and examples: these parts are supposed be readable by a human naturally, and at the moment, it's just LLM slang that's very hard to parse. Additionally, please avoid the automatic hardwrapping of prose text by Claude in the example markdown, it makes it unreadable.

@AdrienCorenflos AdrienCorenflos self-assigned this Nov 5, 2026
@kunyanghe21 kunyanghe21 changed the title Add discounted stop-gradient resampling Add alpha to the existing stop-gradient decorator Nov 5, 2026
@kunyanghe21

Copy link
Copy Markdown
Contributor Author

Thanks, Adrien. I added alpha to the existing stop_gradient_decorator and put the new tests alongside the original resampling tests. I edited the example text myself to make it easier to read. I’ll also revise the docs and examples on the IF2/Dacca branch by myself. I appreciate you pointing this out.

I used alpha=1 as the default because the original decorator applies the full weight correction. I checked that calling it without alpha gives the same outputs and gradients as before.

Comment thread cuthbertlib/resampling/autodiff.py Resolved
Comment thread cuthbertlib/resampling/autodiff.py Resolved
Comment thread cuthbertlib/resampling/autodiff.py Outdated
Returns:
A Resampling function implementing stop gradient resampling.
"""
if isinstance(alpha, complex) or not math.isfinite(alpha) or not 0 <= alpha <= 1:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be parsed by JAX at run time. Simply use jnp.clip and let JAX fail and return its own message if the object is not a scalar.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to jnp.clip. The scalar check also uses JAX now, so alpha can be passed at runtime under jit.

resampling_fn = adaptive.ess_decorator(resampling_fn, ess_threshold)
```

With the default `alpha=1`, Cuthbert applies the full gradient correction when it resamples. In [DMOP](https://arxiv.org/abs/2407.03085v2), `alpha` controls the bias–variance trade-off in the gradient estimates.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamDuffield have we settled on how we do references? I don't think we have. I'm not a big fan of hyperlinks like these. TBC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we haven't got any unified approach to references. This is how we've been doing it in other examples so I'm fine with this here

Comment thread docs/examples/diff_pf_resampling.md Outdated

With the default `alpha=1`, Cuthbert applies the full gradient correction when it resamples. In [DMOP](https://arxiv.org/abs/2407.03085v2), `alpha` controls the bias–variance trade-off in the gradient estimates.

The DMOP analysis assumes resampling at every observation, while this example resamples only when ESS falls below 70% of the particle count.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that relevant?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(specifically the second paragraph)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed that paragraph.

@@ -8,36 +8,51 @@
See [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314) for more details.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably add the reference here too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the Tan et al. reference to the module docstring.

Stop gradient resampling decorator.

This wrapper will call the provided resampling function, and then apply
the stop gradient trick of [Scibior and Wood (2021)](https://arxiv.org/abs/2106.10314).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to reference the paper

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the citation where the wrapper describes the discount.

Comment thread cuthbertlib/resampling/autodiff.py Outdated
log-likelihood with respect to model parameters) are unbiased,
corresponding to the classical Fisher estimate.
corresponding to the classical Fisher estimate. Values below 1 discount
the log-weight correction at each resampling step.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With alpha = 0...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clarified that alpha=0 removes the log-weight correction. Gradients still pass through the selected particle states.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main issue here is that you just didn't illustrate the new method at all. Ideally, the file should have been edited to show a similar figure, with the data and model here, as for Figure 1 in https://arxiv.org/abs/2407.03085v2

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a figure to the existing example showing how the bias and variance of the score estimates for log q and log r change with alpha.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make its background transparent please?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once done happy to approve

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Adrien. I’ve updated the figure with a transparent background.


@parameterized.parameters(-0.1, 1.1, float("nan"))
def test_invalid_discount(self, alpha):
with self.assertRaises(ValueError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail to raise for compiled code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe it would but for the wrong reason. See comment there

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced that test with a jax.jit test. It checks the gradient correction for alpha=-0.1, 0.5 and 1.1, and checks that JAX rejects a vector.

@AdrienCorenflos

Copy link
Copy Markdown
Contributor

Happy to merge. @SamDuffield anything on your end?

@SamDuffield

Copy link
Copy Markdown
Contributor

Happy to merge. @SamDuffield anything on your end?

Nope, looks great!

@AdrienCorenflos
AdrienCorenflos merged commit f3063de into state-space-models:main Nov 18, 2026
2 checks passed
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.

3 participants