Skip to content

Commit 36a624e

Browse files
authored
Merge pull request #306 from cicirello/static-sample-p
Methods for sampling by specifying probability of element inclusion
2 parents c2b9f27 + da02380 commit 36a624e

14 files changed

+758
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
* Added the SequenceSampler interface's methods to SequenceReservoirSampler.
1414
* Added the SequenceSampler interface's methods to SequencePoolSampler.
1515
* Added the SequenceSampler interface's methods to SequenceInsertionSampler.
16+
* Methods to sample by specifying probability of including element in sample to the classes that implement SequenceSampler
17+
including SequenceReservoirSampler, SequencePoolSampler, SequenceInsertionSampler, and SequenceCompositeSampler.
1618

1719
### Changed
1820
* SequenceSampler converted from a utility class of static methods to an interface, retaining the existing static

src/main/java/org/cicirello/sequences/SequenceCompositeSampler.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package org.cicirello.sequences;
2323

2424
import java.util.random.RandomGenerator;
25+
import org.cicirello.math.rand.RandomVariates;
2526

2627
/**
2728
* SequenceCompositeSampler generates random samples of array elements, without replacement.
@@ -153,6 +154,133 @@ public <T> T[] nextSample(T[] source, int k, T[] target) {
153154
return sample(source, k, target, r);
154155
}
155156

157+
/**
158+
* Generates a random sample, without replacement, from a given source array with a specified
159+
* probability of an element's inclusion in the sample.
160+
*
161+
* @param source The array from which to sample.
162+
* @param p The probability that element is included in the sample. The expected sample size is
163+
* source.length * p.
164+
* @param r The source of randomness.
165+
* @return An array containing the sample, whose sample size is simply the length of the array.
166+
*/
167+
public static int[] sample(int[] source, double p, RandomGenerator r) {
168+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
169+
}
170+
171+
/**
172+
* Generates a random sample, without replacement, from a given source array with a specified
173+
* probability of an element's inclusion in the sample.
174+
*
175+
* @param source The array from which to sample.
176+
* @param p The probability that element is included in the sample. The expected sample size is
177+
* source.length * p.
178+
* @param r The source of randomness.
179+
* @return An array containing the sample, whose sample size is simply the length of the array.
180+
*/
181+
public static long[] sample(long[] source, double p, RandomGenerator r) {
182+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
183+
}
184+
185+
/**
186+
* Generates a random sample, without replacement, from a given source array with a specified
187+
* probability of an element's inclusion in the sample.
188+
*
189+
* @param source The array from which to sample.
190+
* @param p The probability that element is included in the sample. The expected sample size is
191+
* source.length * p.
192+
* @param r The source of randomness.
193+
* @return An array containing the sample, whose sample size is simply the length of the array.
194+
*/
195+
public static short[] sample(short[] source, double p, RandomGenerator r) {
196+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
197+
}
198+
199+
/**
200+
* Generates a random sample, without replacement, from a given source array with a specified
201+
* probability of an element's inclusion in the sample.
202+
*
203+
* @param source The array from which to sample.
204+
* @param p The probability that element is included in the sample. The expected sample size is
205+
* source.length * p.
206+
* @param r The source of randomness.
207+
* @return An array containing the sample, whose sample size is simply the length of the array.
208+
*/
209+
public static byte[] sample(byte[] source, double p, RandomGenerator r) {
210+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
211+
}
212+
213+
/**
214+
* Generates a random sample, without replacement, from a given source array with a specified
215+
* probability of an element's inclusion in the sample.
216+
*
217+
* @param source The array from which to sample.
218+
* @param p The probability that element is included in the sample. The expected sample size is
219+
* source.length * p.
220+
* @param r The source of randomness.
221+
* @return An array containing the sample, whose sample size is simply the length of the array.
222+
*/
223+
public static double[] sample(double[] source, double p, RandomGenerator r) {
224+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
225+
}
226+
227+
/**
228+
* Generates a random sample, without replacement, from a given source array with a specified
229+
* probability of an element's inclusion in the sample.
230+
*
231+
* @param source The array from which to sample.
232+
* @param p The probability that element is included in the sample. The expected sample size is
233+
* source.length * p.
234+
* @param r The source of randomness.
235+
* @return An array containing the sample, whose sample size is simply the length of the array.
236+
*/
237+
public static float[] sample(float[] source, double p, RandomGenerator r) {
238+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
239+
}
240+
241+
/**
242+
* Generates a random sample, without replacement, from a given source array with a specified
243+
* probability of an element's inclusion in the sample.
244+
*
245+
* @param source The array from which to sample.
246+
* @param p The probability that element is included in the sample. The expected sample size is
247+
* source.length * p.
248+
* @param r The source of randomness.
249+
* @return An array containing the sample, whose sample size is simply the length of the array.
250+
*/
251+
public static char[] sample(char[] source, double p, RandomGenerator r) {
252+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
253+
}
254+
255+
/**
256+
* Generates a random sample, without replacement, from a given source String with a specified
257+
* probability of an element's inclusion in the sample.
258+
*
259+
* @param source The String from which to sample.
260+
* @param p The probability that element is included in the sample. The expected sample size is
261+
* source.length() * p.
262+
* @param r The source of randomness.
263+
* @return An array containing the sample, whose sample size is simply the length of the array.
264+
*/
265+
public static char[] sample(String source, double p, RandomGenerator r) {
266+
return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r);
267+
}
268+
269+
/**
270+
* Generates a random sample, without replacement, from a given source array with a specified
271+
* probability of an element's inclusion in the sample.
272+
*
273+
* @param source The array from which to sample.
274+
* @param p The probability that element is included in the sample. The expected sample size is
275+
* source.length * p.
276+
* @param r The source of randomness.
277+
* @param <T> The type of array elements.
278+
* @return An array containing the sample, whose sample size is simply the length of the array.
279+
*/
280+
public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
281+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
282+
}
283+
156284
/**
157285
* Generates a random sample of k elements, without replacement, from a given source array. All n
158286
* choose k combinations are equally likely, where n is the length of the source array.

src/main/java/org/cicirello/sequences/SequenceInsertionSampler.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.random.RandomGenerator;
2525
import org.cicirello.math.rand.RandomSampler;
26+
import org.cicirello.math.rand.RandomVariates;
2627
import org.cicirello.util.ArrayMinimumLengthEnforcer;
2728

2829
/**
@@ -157,6 +158,133 @@ public <T> T[] nextSample(T[] source, int k, T[] target) {
157158
return sample(source, k, target, r);
158159
}
159160

161+
/**
162+
* Generates a random sample, without replacement, from a given source array with a specified
163+
* probability of an element's inclusion in the sample.
164+
*
165+
* @param source The array from which to sample.
166+
* @param p The probability that element is included in the sample. The expected sample size is
167+
* source.length * p.
168+
* @param r The source of randomness.
169+
* @return An array containing the sample, whose sample size is simply the length of the array.
170+
*/
171+
public static int[] sample(int[] source, double p, RandomGenerator r) {
172+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
173+
}
174+
175+
/**
176+
* Generates a random sample, without replacement, from a given source array with a specified
177+
* probability of an element's inclusion in the sample.
178+
*
179+
* @param source The array from which to sample.
180+
* @param p The probability that element is included in the sample. The expected sample size is
181+
* source.length * p.
182+
* @param r The source of randomness.
183+
* @return An array containing the sample, whose sample size is simply the length of the array.
184+
*/
185+
public static long[] sample(long[] source, double p, RandomGenerator r) {
186+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
187+
}
188+
189+
/**
190+
* Generates a random sample, without replacement, from a given source array with a specified
191+
* probability of an element's inclusion in the sample.
192+
*
193+
* @param source The array from which to sample.
194+
* @param p The probability that element is included in the sample. The expected sample size is
195+
* source.length * p.
196+
* @param r The source of randomness.
197+
* @return An array containing the sample, whose sample size is simply the length of the array.
198+
*/
199+
public static short[] sample(short[] source, double p, RandomGenerator r) {
200+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
201+
}
202+
203+
/**
204+
* Generates a random sample, without replacement, from a given source array with a specified
205+
* probability of an element's inclusion in the sample.
206+
*
207+
* @param source The array from which to sample.
208+
* @param p The probability that element is included in the sample. The expected sample size is
209+
* source.length * p.
210+
* @param r The source of randomness.
211+
* @return An array containing the sample, whose sample size is simply the length of the array.
212+
*/
213+
public static byte[] sample(byte[] source, double p, RandomGenerator r) {
214+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
215+
}
216+
217+
/**
218+
* Generates a random sample, without replacement, from a given source array with a specified
219+
* probability of an element's inclusion in the sample.
220+
*
221+
* @param source The array from which to sample.
222+
* @param p The probability that element is included in the sample. The expected sample size is
223+
* source.length * p.
224+
* @param r The source of randomness.
225+
* @return An array containing the sample, whose sample size is simply the length of the array.
226+
*/
227+
public static double[] sample(double[] source, double p, RandomGenerator r) {
228+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
229+
}
230+
231+
/**
232+
* Generates a random sample, without replacement, from a given source array with a specified
233+
* probability of an element's inclusion in the sample.
234+
*
235+
* @param source The array from which to sample.
236+
* @param p The probability that element is included in the sample. The expected sample size is
237+
* source.length * p.
238+
* @param r The source of randomness.
239+
* @return An array containing the sample, whose sample size is simply the length of the array.
240+
*/
241+
public static float[] sample(float[] source, double p, RandomGenerator r) {
242+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
243+
}
244+
245+
/**
246+
* Generates a random sample, without replacement, from a given source array with a specified
247+
* probability of an element's inclusion in the sample.
248+
*
249+
* @param source The array from which to sample.
250+
* @param p The probability that element is included in the sample. The expected sample size is
251+
* source.length * p.
252+
* @param r The source of randomness.
253+
* @return An array containing the sample, whose sample size is simply the length of the array.
254+
*/
255+
public static char[] sample(char[] source, double p, RandomGenerator r) {
256+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
257+
}
258+
259+
/**
260+
* Generates a random sample, without replacement, from a given source String with a specified
261+
* probability of an element's inclusion in the sample.
262+
*
263+
* @param source The String from which to sample.
264+
* @param p The probability that element is included in the sample. The expected sample size is
265+
* source.length() * p.
266+
* @param r The source of randomness.
267+
* @return An array containing the sample, whose sample size is simply the length of the array.
268+
*/
269+
public static char[] sample(String source, double p, RandomGenerator r) {
270+
return sample(source, RandomVariates.nextBinomial(source.length(), p), null, r);
271+
}
272+
273+
/**
274+
* Generates a random sample, without replacement, from a given source array with a specified
275+
* probability of an element's inclusion in the sample.
276+
*
277+
* @param source The array from which to sample.
278+
* @param p The probability that element is included in the sample. The expected sample size is
279+
* source.length * p.
280+
* @param r The source of randomness.
281+
* @param <T> The type of array elements.
282+
* @return An array containing the sample, whose sample size is simply the length of the array.
283+
*/
284+
public static <T> T[] sample(T[] source, double p, RandomGenerator r) {
285+
return sample(source, RandomVariates.nextBinomial(source.length, p), null, r);
286+
}
287+
160288
/**
161289
* Generates a random sample of k elements, without replacement, from a given source array. All n
162290
* choose k combinations are equally likely, where n is the length of the source array.

0 commit comments

Comments
 (0)