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

Add KS tests for weighted sampling #1530

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

dhardy
Copy link
Member

@dhardy dhardy commented Nov 18, 2024

  • Added a CHANGELOG.md entry

Motivation

Some of these are non-trivial distributions we didn't really test before.

To validate solution of #1476.

Details

Single-element weighted sampling is simple enough.

fn choose_two_iterator is also simple enough: there are no weights, so we can just assign each pair of results a unique index in the list of 100 * 99 / 2 possibilities (nothing that we sort pairs since the order of chosen elements is not specified).

fn choose_two_weighted_indexed gets a bit more complicated; I choose to approach it by building a table for the CDF of size num*num including impossible variants. Most of the tests don't pass, so there must be a mistake here.

Aside: using let key = rng.random::<f64>().ln() / weight; (src/seq/index.rs:392) may help with #1476 but does not fix the above.

@dhardy
Copy link
Member Author

dhardy commented Nov 19, 2024

I can confirm that choose_multiple_weighted has a significant problem, since sampling two elements from 0, 1, 2 with weights 1, 1/2, 1/3 a million times and sorting yields 532298 counts of (0, 1), 338524 counts of (0, 2) and 129178 counts of (1, 2). (Unlike #1476, this example does not require very small weights.)

This is sampling without replacement, so expected samples are:

  • (0,1) or (1, 0): 531818
  • (0, 2) or (2, 0): 339393
  • (1, 2) or (2, 1): 128788

@dhardy dhardy marked this pull request as ready for review November 19, 2024 11:02
@dhardy
Copy link
Member Author

dhardy commented Nov 19, 2024

I fixed my calculation of the CDF, found a variant which failed like #1476, fixed this by taking the logarithm of keys, and applied some optimisation to the Efraimidis-Spirakis algorithm.

@dhardy dhardy mentioned this pull request Nov 23, 2024
1 task
Copy link
Collaborator

@benjamin-lieser benjamin-lieser left a comment

Choose a reason for hiding this comment

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

Looks correct. We might want to test how the performance is for big amount

distr_test/tests/weighted.rs Show resolved Hide resolved
}

#[test]
fn choose_two_weighted_indexed() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is probably more complex than needed, but looks correct.
It's probably worth implementing chi squared at some point, but this should also be quite sensitive.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably more complex than needed, but looks correct.

You mean the use of an Adapter? Yes, but I'd sooner do this than revise the KS test API (which is well adapted for other usages).

It's probably worth implementing chi squared at some point, but this should also be quite sensitive.

A fair point.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean using KS for these distributions (chi squared would be more straight forward), Adapter I think is fine.

src/seq/index.rs Outdated Show resolved Hide resolved
src/seq/index.rs Show resolved Hide resolved
let t = core::f64::consts::E.powf(candidates[0].key * weight);
let key = rng.random_range(t..1.0).ln() / weight;
candidates[0] = Element { index, key };
candidates.sort_unstable();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess it is very likely that a tree data structure would perform much faster if amount is big enough. Not sure where the threshold is. Depending on sort_unstable is could even perform particularly worse on an almost sorted slice.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, though I won't address it now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html
should be always faster as it also stores its elements in a Vec should be an easy change.

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