Skip to content
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

Create sampler module and gaussian mixture sampling function #793

Merged
merged 17 commits into from
May 25, 2023

Conversation

timothy-glover
Copy link
Contributor

Introduced the sampler module for producing samples from distributions. ParticleSampler is the main class contributed for this and it accepts a callable sampling function such as numpy.random.multivariate_normal or scipy.stats.uniform.rvs, and a dictionary of parameters to be used as kwargs for the callable and returns a ParticleState containing the samples. GaussianDetectionParticleSampler is a child class of ParticleSampler that specifically samples from Detection or sets of Detection. DetectionSampler is a general Detection sampler that is able to switch between a detection based sampler object such as GaussianDetectionParticleSampler and a non-detection backup sampler such as ParticleSampler that is used if no detections are available.

No single callable has been found for sampling from Gaussian mixture distributions which lead to the introduction of gm_sample method in stonesoup.functions. This method can be used as a callable for ParticleSampler and is the sampler used by GaussianDetectionParticleSampler.

@timothy-glover timothy-glover requested a review from a team as a code owner April 24, 2023 10:25
@timothy-glover timothy-glover requested review from sdhiscocks and nperree-dstl and removed request for a team April 24, 2023 10:25
@jmbarr jmbarr self-requested a review April 27, 2023 18:26

Returns
-------
: :class:`np.ndarray` of shape (:attr:`size`, num_dims)"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Should return a StateVectors type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was torn with this when I wrote the function but I think you are right. Changed in latest commit.



class Sampler(Base):
"""Sampler base class"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Should give a description of what a general sampler does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Description added

stonesoup/sampler/detection.py Show resolved Hide resolved
stonesoup/sampler/particle.py Show resolved Hide resolved
stonesoup/sampler/particle.py Outdated Show resolved Hide resolved
@@ -509,6 +509,47 @@ def rotz(theta):
[zero, zero, one]])


def gm_sample(means, covars, size, weights=None):
"""Sample from a mixture of multi-variate Gaussians

Copy link
Contributor

Choose a reason for hiding this comment

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

You might wish to explain how this is done, as for non-statisticians it might not be obvious. (First do a multinomial sample from the weight distribution to get the number of times you should sample from each component, then sample from those components that many times.. I think.)

jmbarr added 2 commits May 10, 2023 10:54
Modified Sampler base class description
Copy link
Contributor

@jmbarr jmbarr left a comment

Choose a reason for hiding this comment

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

Only thing I'd suggest is perhaps producing/referencing a gist where you can demonstrate pictorially that the sampling happens correctly. Say create 2 overlapping Gaussian components and then plot a few thousand sample points?

Copy link
Contributor

@nperree-dstl nperree-dstl left a comment

Choose a reason for hiding this comment

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

Looks good to me. As discussed it probably makes sense to have your DetectionSampler and GaussianDetectionParticleSampler both inheriting from a "DetectionSampler" class. Happy for that to be amended in a future PR but it may be worth renaming your DetectionSampler here if you do plan on restructuring slightly.

@codecov
Copy link

codecov bot commented May 15, 2023

Codecov Report

Patch coverage: 97.12% and project coverage change: +0.02 🎉

Comparison is base (78c88a4) 94.90% compared to head (61bed83) 94.92%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #793      +/-   ##
==========================================
+ Coverage   94.90%   94.92%   +0.02%     
==========================================
  Files         176      183       +7     
  Lines        9757     9919     +162     
  Branches     1938     1970      +32     
==========================================
+ Hits         9260     9416     +156     
- Misses        353      355       +2     
- Partials      144      148       +4     
Flag Coverage Δ
integration 67.87% <47.84%> (-0.42%) ⬇️
unittests 90.03% <97.12%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
stonesoup/types/state.py 98.53% <89.09%> (-1.25%) ⬇️
stonesoup/functions/__init__.py 94.40% <100.00%> (+0.53%) ⬆️
stonesoup/models/measurement/nonlinear.py 99.05% <100.00%> (-0.01%) ⬇️
stonesoup/predictor/particle.py 92.95% <100.00%> (ø)
stonesoup/regulariser/__init__.py 100.00% <100.00%> (ø)
stonesoup/regulariser/base.py 100.00% <100.00%> (ø)
stonesoup/regulariser/particle.py 100.00% <100.00%> (ø)
stonesoup/resampler/particle.py 100.00% <100.00%> (ø)
stonesoup/sampler/__init__.py 100.00% <100.00%> (ø)
stonesoup/sampler/base.py 100.00% <100.00%> (ø)
... and 3 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

stonesoup/sampler/detection.py Show resolved Hide resolved
stonesoup/sampler/detection.py Show resolved Hide resolved
@sdhiscocks sdhiscocks merged commit 683600a into dstl:main May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants