@@ -252,7 +252,7 @@ pub enum ParticleResamplingStrategy {
252252///
253253/// # Arguments
254254/// * `weights` - Non-negative weights for each particle. Callers are responsible
255- /// for ensuring the weights sum to 1.0 before calling this function.
255+ /// for ensuring the weights sum to 1.0 before calling this function.
256256/// * `num_samples` - Number of samples to draw
257257/// * `rng` - Random number generator
258258///
@@ -302,7 +302,7 @@ pub fn multinomial_resample<R: Rng>(
302302///
303303/// # Arguments
304304/// * `weights` - Non-negative weights for each particle. Callers are responsible
305- /// for ensuring the weights sum to 1.0 before calling this function.
305+ /// for ensuring the weights sum to 1.0 before calling this function.
306306/// * `num_samples` - Number of samples to draw
307307/// * `rng` - Random number generator
308308///
@@ -319,11 +319,7 @@ pub fn multinomial_resample<R: Rng>(
319319/// let indices = systematic_resample(&weights, 4, &mut rng);
320320/// assert_eq!(indices.len(), 4);
321321/// ```
322- pub fn systematic_resample < R : Rng > (
323- weights : & [ f64 ] ,
324- num_samples : usize ,
325- rng : & mut R ,
326- ) -> Vec < usize > {
322+ pub fn systematic_resample < R : Rng > ( weights : & [ f64 ] , num_samples : usize , rng : & mut R ) -> Vec < usize > {
327323 let mut indices = Vec :: with_capacity ( num_samples) ;
328324 let step = 1.0 / num_samples as f64 ;
329325 let start: f64 = rng. random :: < f64 > ( ) * step;
@@ -355,7 +351,7 @@ pub fn systematic_resample<R: Rng>(
355351///
356352/// # Arguments
357353/// * `weights` - Non-negative weights for each particle. Callers are responsible
358- /// for ensuring the weights sum to 1.0 before calling this function.
354+ /// for ensuring the weights sum to 1.0 before calling this function.
359355/// * `num_samples` - Number of samples to draw
360356/// * `rng` - Random number generator
361357///
@@ -372,11 +368,7 @@ pub fn systematic_resample<R: Rng>(
372368/// let indices = stratified_resample(&weights, 4, &mut rng);
373369/// assert_eq!(indices.len(), 4);
374370/// ```
375- pub fn stratified_resample < R : Rng > (
376- weights : & [ f64 ] ,
377- num_samples : usize ,
378- rng : & mut R ,
379- ) -> Vec < usize > {
371+ pub fn stratified_resample < R : Rng > ( weights : & [ f64 ] , num_samples : usize , rng : & mut R ) -> Vec < usize > {
380372 let mut indices = Vec :: with_capacity ( num_samples) ;
381373 let step = 1.0 / num_samples as f64 ;
382374
@@ -407,7 +399,7 @@ pub fn stratified_resample<R: Rng>(
407399///
408400/// # Arguments
409401/// * `weights` - Non-negative weights for each particle. Callers are responsible
410- /// for ensuring the weights sum to 1.0 before calling this function.
402+ /// for ensuring the weights sum to 1.0 before calling this function.
411403/// * `num_samples` - Number of samples to draw
412404/// * `rng` - Random number generator
413405///
@@ -424,11 +416,7 @@ pub fn stratified_resample<R: Rng>(
424416/// let indices = residual_resample(&weights, 4, &mut rng);
425417/// assert_eq!(indices.len(), 4);
426418/// ```
427- pub fn residual_resample < R : Rng > (
428- weights : & [ f64 ] ,
429- num_samples : usize ,
430- rng : & mut R ,
431- ) -> Vec < usize > {
419+ pub fn residual_resample < R : Rng > ( weights : & [ f64 ] , num_samples : usize , rng : & mut R ) -> Vec < usize > {
432420 let mut indices = Vec :: with_capacity ( num_samples) ;
433421
434422 // Calculate expected number of copies for each particle
@@ -1226,20 +1214,20 @@ mod tests {
12261214 let weights = vec ! [ 0.1 , 0.3 , 0.4 , 0.2 ] ;
12271215 let mut rng = rand:: rngs:: StdRng :: seed_from_u64 ( 42 ) ;
12281216 let indices = multinomial_resample ( & weights, 100 , & mut rng) ;
1229-
1217+
12301218 assert_eq ! ( indices. len( ) , 100 ) ;
1231-
1219+
12321220 // All indices should be valid
12331221 for & idx in & indices {
12341222 assert ! ( idx < weights. len( ) ) ;
12351223 }
1236-
1224+
12371225 // With 100 samples, distribution should roughly match weights
12381226 let mut counts = vec ! [ 0 ; weights. len( ) ] ;
12391227 for & idx in & indices {
12401228 counts[ idx] += 1 ;
12411229 }
1242-
1230+
12431231 // Check that higher weight particles are sampled more often
12441232 assert ! ( counts[ 2 ] > counts[ 0 ] ) ; // 0.4 > 0.1
12451233 assert ! ( counts[ 1 ] > counts[ 0 ] ) ; // 0.3 > 0.1
@@ -1250,15 +1238,15 @@ mod tests {
12501238 let weights = vec ! [ 0.25 , 0.25 , 0.25 , 0.25 ] ; // Uniform weights
12511239 let mut rng = rand:: rngs:: StdRng :: seed_from_u64 ( 42 ) ;
12521240 let indices = systematic_resample ( & weights, 100 , & mut rng) ;
1253-
1241+
12541242 assert_eq ! ( indices. len( ) , 100 ) ;
1255-
1243+
12561244 // With uniform weights, each particle should be selected roughly equally
12571245 let mut counts = vec ! [ 0 ; weights. len( ) ] ;
12581246 for & idx in & indices {
12591247 counts[ idx] += 1 ;
12601248 }
1261-
1249+
12621250 // Each should be selected about 25 times
12631251 for count in counts {
12641252 let diff = if count > 25 { count - 25 } else { 25 - count } ;
@@ -1271,9 +1259,9 @@ mod tests {
12711259 let weights = vec ! [ 0.1 , 0.2 , 0.3 , 0.4 ] ;
12721260 let mut rng = rand:: rngs:: StdRng :: seed_from_u64 ( 42 ) ;
12731261 let indices = stratified_resample ( & weights, 50 , & mut rng) ;
1274-
1262+
12751263 assert_eq ! ( indices. len( ) , 50 ) ;
1276-
1264+
12771265 // All indices should be valid
12781266 for & idx in & indices {
12791267 assert ! ( idx < weights. len( ) ) ;
@@ -1285,15 +1273,15 @@ mod tests {
12851273 let weights = vec ! [ 0.1 , 0.3 , 0.4 , 0.2 ] ;
12861274 let mut rng = rand:: rngs:: StdRng :: seed_from_u64 ( 42 ) ;
12871275 let indices = residual_resample ( & weights, 10 , & mut rng) ;
1288-
1276+
12891277 assert_eq ! ( indices. len( ) , 10 ) ;
1290-
1278+
12911279 // Count occurrences
12921280 let mut counts = vec ! [ 0 ; weights. len( ) ] ;
12931281 for & idx in & indices {
12941282 counts[ idx] += 1 ;
12951283 }
1296-
1284+
12971285 // Particle with weight 0.4 should appear at least 4 times (deterministic part)
12981286 // Particle with weight 0.3 should appear at least 3 times
12991287 assert ! ( counts[ 2 ] >= 4 ) ; // 0.4 * 10 = 4.0
@@ -1305,17 +1293,17 @@ mod tests {
13051293 let weights = vec ! [ 0.1 , 0.2 , 0.3 , 0.4 ] ;
13061294 let num_samples = 100 ;
13071295 let mut rng = rand:: rngs:: StdRng :: seed_from_u64 ( 42 ) ;
1308-
1296+
13091297 // Test all resampling methods
13101298 let multinomial_indices = multinomial_resample ( & weights, num_samples, & mut rng) ;
13111299 assert_eq ! ( multinomial_indices. len( ) , num_samples) ;
1312-
1300+
13131301 let systematic_indices = systematic_resample ( & weights, num_samples, & mut rng) ;
13141302 assert_eq ! ( systematic_indices. len( ) , num_samples) ;
1315-
1303+
13161304 let stratified_indices = stratified_resample ( & weights, num_samples, & mut rng) ;
13171305 assert_eq ! ( stratified_indices. len( ) , num_samples) ;
1318-
1306+
13191307 let residual_indices = residual_resample ( & weights, num_samples, & mut rng) ;
13201308 assert_eq ! ( residual_indices. len( ) , num_samples) ;
13211309 }
@@ -1331,10 +1319,10 @@ mod tests {
13311319 ParticleAveragingStrategy :: WeightedMean ,
13321320 0.5 ,
13331321 ) ;
1334-
1322+
13351323 // Call resample directly
13361324 pf. resample ( ) ;
1337-
1325+
13381326 // Should still have 100 particles
13391327 assert_eq ! ( pf. num_particles( ) , 100 ) ;
13401328 }
0 commit comments