-
Notifications
You must be signed in to change notification settings - Fork 11
feat(sampling): Implement VarOptItemsSketch core functionality #102
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
Conversation
- Add VarOptItems family (ID=13) to internal/family.go - Implement VarOptItemsSketch[T] with full C++/Java-compatible algorithm: - Warmup mode with h > k transition - growCandidateSet/downsampleCandidateSet for variance-optimal sampling - chooseDeleteSlot with weighted random selection - Min-heap operations (heapify, siftUp, siftDown) - Add Go 1.23 iter.Seq2-based Samples() iterator - Add 8 unit tests matching C++ test coverage: - NewSketch, WarmupPhase, TransitionToEstimation, EstimationMode - InvalidWeight, Reset, UniformWeights, CumulativeWeight Implements PR1 of apache#98
sampling/varopt_items_sketch.go
Outdated
| } | ||
|
|
||
| // NewVarOptItemsSketchWithResizeFactor creates a new VarOpt sketch with specified k and resize factor. | ||
| func NewVarOptItemsSketchWithResizeFactor[T any](k int, rf ResizeFactor) (*VarOptItemsSketch[T], error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you consider functional options pattern? so you can integrate with "NewVarOptItemsSketch"
sampling/varopt_items_sketch.go
Outdated
| // for item, weight := range sketch.Samples() { | ||
| // // process item and weight | ||
| // } | ||
| func (s *VarOptItemsSketch[T]) Samples() iter.Seq2[T, float64] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returing struct like
type Sample[T] struct {
data T
Weight float64
}
is more clear API.
NOTE: convention is using All. ref: https://pkg.go.dev/iter#hdr-Naming_Conventions
sampling/varopt_items_sketch.go
Outdated
| // chooseDeleteSlot randomly selects which item to delete from candidates. | ||
| func (s *VarOptItemsSketch[T]) chooseDeleteSlot(wtCands float64, numCands int) int { | ||
| if s.r == 0 { | ||
| panic("choosing delete slot while in exact mode") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not panic. can you returning error?
- Use functional options pattern for constructor - Return Sample[T] struct from All() iterator - Return error instead of panic in chooseDeleteSlot
|
Thanks for the great feedback @proost! I've addressed all three points. I learned a lot about Go design philosophy from your review - still have much to learn about idiomatic Go patterns :) |
1d9436d to
fe39ed5
Compare
proost
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary
This PR implements the core
VarOptItemsSketch[T]for variance-optimal weighted sampling, following the C++ and Java implementations exactly. ref #98Changes
internal/family.goVarOptItemsfamily (ID=13, MaxPreLongs=4)sampling/varopt_items_sketch.go(new file, ~520 lines)transitionFromWarmup()growCandidateSet()/downsampleCandidateSet()for variance-optimal samplingchooseDeleteSlot()/chooseWeightedDeleteSlot()for weighted random selectionheapify(),siftUp(),siftDown()Samples() iter.Seq2[T, float64]for elegant range iterationsampling/varopt_items_sketch_test.go(new file, ~218 lines)8 unit tests matching C++ test coverage:
TestVarOptItemsSketch_NewSketch- constructor and k validationTestVarOptItemsSketch_WarmupPhase- exact mode behaviorTestVarOptItemsSketch_TransitionToEstimation- h > k transitionTestVarOptItemsSketch_EstimationMode- sampling mode invariantsTestVarOptItemsSketch_InvalidWeight- negative/zero weight handlingTestVarOptItemsSketch_Reset- reset state verificationTestVarOptItemsSketch_UniformWeights- reservoir-like behaviorTestVarOptItemsSketch_CumulativeWeight- weight sum preservation (matches C++ test withexp(5*N(0,1))distribution and 1e-13 tolerance)API Usage
Note
The current implementation includes detailed comments for clarity. If the reviewers prefer, we can simplify the comments.