random_value_sampler 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *~
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ 0.1.4:
2
+ ----------------
3
+ * fixing bug if we get a uniform distribution with duplicates and try to sample_unique for number
4
+ of samples that's between # of distinct values and total # of values (had infinite loop :( )
5
+ * changed behavior when requesting to sample_unique with larger number than the total number
6
+ of values in a distribution. instead of raising exception, just return unique set of all the
7
+ valid values. this shouldn't break any existing implementations unless someone was depending
8
+ on the exceptions being raised (that would be bad form).
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ * update README with examples of actually sampling from the distributions...they just say how to create the distributions to begin with. ;)
2
+
@@ -1,3 +1,3 @@
1
1
  class RandomValueSampler
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -147,25 +147,26 @@ class RandomValueSampler
147
147
  def sample_unique(n = 1)
148
148
  raise "n must be 0 or greater to sample_unique" if n < 0
149
149
 
150
+ samples = nil
151
+
150
152
  # take care of edge cases: where they ask for more samples than there are
151
- # entries in the distribution (error)
153
+ # entries in the distribution
152
154
  if n > pmf.num_values
153
- raise("Invalid request to pull #{n} unique samples from a distribution " +
154
- "with only #{pmf.num_values} distinct values")
155
- end
156
-
157
- # use a set in case the calling code added multiple copies of the same
158
- # object into distribution
159
- samples = Set.new
160
- while samples.length < n
161
- if pmf.respond_to?(:sample_from_distribution_and_remove)
162
- samples << pmf.sample_from_distribution_and_remove
163
- else
164
- samples << pmf.sample_from_distribution
155
+ samples = pmf.all_values.uniq
156
+ else
157
+ # use a set in case the calling code added multiple copies of the same
158
+ # object into distribution
159
+ samples = Set.new
160
+ while samples.length < n && pmf.num_values > 0
161
+ if pmf.respond_to?(:sample_from_distribution_and_remove)
162
+ samples << pmf.sample_from_distribution_and_remove
163
+ else
164
+ samples << pmf.sample_from_distribution
165
+ end
165
166
  end
166
167
  end
167
168
 
168
- return samples.length == 1 ? samples.first : samples.to_a
169
+ return (n == 1 && samples.size == 1) ? samples.first : samples.to_a
169
170
  end
170
171
 
171
172
 
@@ -271,7 +272,7 @@ class RandomValueSampler
271
272
  end
272
273
 
273
274
  @values.delete(sample)
274
- @num_values -= 1
275
+ @num_values = @values.length
275
276
  @probability = nil # force recalculation of probability next time
276
277
 
277
278
  return sample
@@ -1,8 +1,9 @@
1
1
  require 'set'
2
2
  require 'test/unit'
3
- require 'random_value_sampler'
3
+ require 'timeout'
4
+
5
+ require File.join(File.dirname(File.expand_path(__FILE__)), '../lib/random_value_sampler')
4
6
 
5
- #
6
7
  # rough outline of this file:
7
8
  # * test cases: these just call helper methods to run tests on all of the
8
9
  # data cases created below in the setup() method
@@ -62,79 +63,70 @@ class RandomValueSamplerTest < Test::Unit::TestCase
62
63
  assert_raises(RuntimeError) { RandomValueSampler.new_non_uniform @nonuniform_arrayoftuples_error_all_zeros }
63
64
  end
64
65
 
65
- def test_uniform_exception_on_too_many_sample_unique
66
+ def test_uniform_sample_unique_more_than_distinct_values
66
67
  # singleton set
67
- assert_raises(RuntimeError) do
68
- rsampler = RandomValueSampler.new_uniform @uniform_set_single_string
69
- rsampler.sample_unique 2
70
- end
68
+ rsampler = RandomValueSampler.new_uniform @uniform_set_single_string
69
+ samples = rsampler.sample_unique 2
70
+ assert_equal @uniform_set_single_string.to_a.sort, samples.to_a.sort
71
71
 
72
72
  # singleton array
73
- assert_raises(RuntimeError) do
74
- rsampler = RandomValueSampler.new_uniform @uniform_array_single_numeric
75
- rsampler.sample_unique(@uniform_array_single_numeric.length + 1)
76
- end
73
+ rsampler = RandomValueSampler.new_uniform @uniform_array_single_numeric
74
+ samples = rsampler.sample_unique(@uniform_array_single_numeric.length + 1)
75
+ assert_equal @uniform_array_single_numeric.to_a.sort, samples.to_a.sort
77
76
 
78
77
  # singleton Range
79
- assert_raises(RuntimeError) do
80
- rsampler = RandomValueSampler.new_uniform @uniform_range_single_exclusive
81
- rsampler.sample_unique(@uniform_range_single_exclusive.to_a.length + 1)
82
- end
78
+ rsampler = RandomValueSampler.new_uniform @uniform_range_single_exclusive
79
+ samples = rsampler.sample_unique(@uniform_range_single_exclusive.to_a.length + 1)
80
+ assert_equal @uniform_range_single_exclusive.to_a.sort, samples.to_a.sort
83
81
 
84
82
  # singleton value
85
- assert_raises(RuntimeError) do
86
- rsampler = RandomValueSampler.new_uniform @uniform_single_zero
87
- rsampler.sample_unique 2
88
- end
83
+ rsampler = RandomValueSampler.new_uniform @uniform_single_zero
84
+ samples = rsampler.sample_unique 2
85
+ assert_equal 1, samples.size
86
+ assert_equal @uniform_single_zero, samples.first
89
87
 
90
88
  # size N set
91
- assert_raises(RuntimeError) do
92
- rsampler = RandomValueSampler.new_uniform @uniform_set_10_string
93
- rsampler.sample_unique(@uniform_set_10_string.length + 1)
94
- end
89
+ rsampler = RandomValueSampler.new_uniform @uniform_set_10_string
90
+ samples = rsampler.sample_unique(@uniform_set_10_string.length + 1)
91
+ assert_equal @uniform_set_10_string.to_a.sort, samples.to_a.sort
95
92
 
96
93
  # size N array
97
- assert_raises(RuntimeError) do
98
- rsampler = RandomValueSampler.new_uniform @uniform_array_10_numeric
99
- rsampler.sample_unique(@uniform_array_10_numeric.length + 1)
100
- end
94
+ rsampler = RandomValueSampler.new_uniform @uniform_array_10_numeric
95
+ samples = rsampler.sample_unique(@uniform_array_10_numeric.length + 1)
96
+ assert_equal @uniform_array_10_numeric.to_a.sort, samples.to_a.sort
101
97
 
102
98
  # size N Range inclusive
103
- assert_raises(RuntimeError) do
104
- rsampler = RandomValueSampler.new_uniform @uniform_range_10_inclusive
105
- rsampler.sample_unique(@uniform_range_10_inclusive.to_a.length + 1)
106
- end
99
+ rsampler = RandomValueSampler.new_uniform @uniform_range_10_inclusive
100
+ samples = rsampler.sample_unique(@uniform_range_10_inclusive.to_a.length + 1)
101
+ assert_equal @uniform_range_10_inclusive.to_a.sort, samples.to_a.sort
107
102
 
108
103
  # size N Range exclusive
109
- assert_raises(RuntimeError) do
110
- rsampler = RandomValueSampler.new_uniform @uniform_range_10_exclusive
111
- rsampler.sample_unique(@uniform_range_10_exclusive.to_a.length + 1)
112
- end
113
-
114
- # scalar defining Range size N
115
- assert_raises(RuntimeError) do
116
- rsampler = RandomValueSampler.new_uniform @uniform_single_nonzero
117
- rsampler.sample_unique(@uniform_single_nonzero + 2)
118
- end
104
+ rsampler = RandomValueSampler.new_uniform @uniform_range_10_exclusive
105
+ samples = rsampler.sample_unique(@uniform_range_10_exclusive.to_a.length + 1)
106
+ assert_equal @uniform_range_10_exclusive.to_a.sort, samples.to_a.sort
107
+
108
+ # scalar defining Range size N (samples on [0,N])
109
+ rsampler = RandomValueSampler.new_uniform @uniform_single_nonzero
110
+ samples = rsampler.sample_unique(@uniform_single_nonzero + 2)
111
+ assert_equal (0..@uniform_single_nonzero).to_a.sort, samples.to_a.sort
119
112
  end
120
113
 
121
114
  def test_non_uniform_exception_on_too_many_sample_unique
122
- assert_raises(RuntimeError) do
123
- rsampler = RandomValueSampler.new_non_uniform @nonuniform_hash_single_string
124
- rsampler.sample_unique 2
125
- end
126
- assert_raises(RuntimeError) do
127
- rsampler = RandomValueSampler.new_non_uniform @nonuniform_hash_10_sum_to_1
128
- rsampler.sample_unique(@nonuniform_hash_10_sum_to_1.length + 1)
129
- end
130
- assert_raises(RuntimeError) do
131
- rsampler = RandomValueSampler.new_non_uniform @nonuniform_arrayoftuples_single_string
132
- rsampler.sample_unique 2
133
- end
134
- assert_raises(RuntimeError) do
135
- rsampler = RandomValueSampler.new_non_uniform @nonuniform_arrayoftuples_10_sum_to_1
136
- rsampler.sample_unique(@nonuniform_arrayoftuples_10_sum_gt_1.length + 1)
137
- end
115
+ rsampler = RandomValueSampler.new_non_uniform @nonuniform_hash_single_string
116
+ samples = rsampler.sample_unique 2
117
+ assert_equal @nonuniform_hash_single_string.keys.map(&:to_s).sort, samples.to_a.map(&:to_s).sort
118
+
119
+ rsampler = RandomValueSampler.new_non_uniform @nonuniform_hash_10_sum_to_1
120
+ samples = rsampler.sample_unique(@nonuniform_hash_10_sum_to_1.length + 1)
121
+ assert_equal @nonuniform_hash_10_sum_to_1.keys.map(&:to_s).sort, samples.to_a.map(&:to_s).sort
122
+
123
+ rsampler = RandomValueSampler.new_non_uniform @nonuniform_arrayoftuples_single_string
124
+ samples = rsampler.sample_unique 2
125
+ assert_equal @nonuniform_arrayoftuples_single_string.map(&:first).map(&:to_s).sort, samples.to_a.map(&:to_s).sort
126
+
127
+ rsampler = RandomValueSampler.new_non_uniform @nonuniform_arrayoftuples_10_sum_to_1
128
+ samples = rsampler.sample_unique(@nonuniform_arrayoftuples_10_sum_gt_1.length + 1)
129
+ assert_equal @nonuniform_arrayoftuples_10_sum_to_1.map(&:first).map(&:to_s).sort, samples.to_a.map(&:to_s).sort
138
130
  end
139
131
 
140
132
  def test_negative_num_samples
@@ -142,6 +134,40 @@ class RandomValueSamplerTest < Test::Unit::TestCase
142
134
  assert_raises(RuntimeError) { RandomValueSampler.new_uniform([1,2,3,4]).sample_unique(-1) }
143
135
  end
144
136
 
137
+ ##################
138
+ # SOME EDGE CASES
139
+ ##################
140
+
141
+ def test_sample_unique_on_uniform_with_duplicates_no_dupes_in_result
142
+ 20.times do
143
+ rsampler = RandomValueSampler.new_uniform(@uniform_10_array_5_unique_vals)
144
+ samples = rsampler.sample_unique(5)
145
+ assert_equal samples.to_a.sort, samples.to_a.uniq.sort
146
+ end
147
+ end
148
+
149
+ def test_sample_unique_on_uniform_with_duplicates_dont_return_more_values_than_requested
150
+ # just in case we do some short-cutting for cases where we detect there are
151
+ # duplicates in the sample set
152
+ 100.times do
153
+ rsampler = RandomValueSampler.new_uniform(@uniform_10_array_5_unique_vals)
154
+ samples = rsampler.sample_unique(4)
155
+ assert samples.size <= 4
156
+ end
157
+ end
158
+
159
+ def test_sample_unique_on_uniform_with_fewer_uniques_than_num_values_requested
160
+ rsampler = RandomValueSampler.new_uniform(@uniform_10_array_5_unique_vals)
161
+ assert_nothing_raised do
162
+ samples = nil
163
+ Timeout::timeout(2) do
164
+ samples = rsampler.sample_unique(6)
165
+ end
166
+
167
+ assert_equal(@uniform_10_array_5_unique_vals.sort.uniq, samples.to_a.sort)
168
+ end
169
+ end
170
+
145
171
  ###################################################
146
172
  # VERIFYING VALIDITY, CONSISTENCY OF DISTRIBUTIONS
147
173
  ###################################################
@@ -378,29 +404,30 @@ class RandomValueSamplerTest < Test::Unit::TestCase
378
404
  @uniform_single_nonzero)
379
405
  end
380
406
 
381
- def test_non_uniform_sample_values_are_valid
382
- assert_equal(@nonuniform_hashes.length, 4)
383
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_single_string),
384
- @nonuniform_hash_single_string)
385
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_to_1),
386
- @nonuniform_hash_10_sum_to_1)
387
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_gt_1),
388
- @nonuniform_hash_10_sum_gt_1)
389
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_lt_1),
390
- @nonuniform_hash_10_sum_lt_1)
407
+ def test_uniform_sample_unique_values_are_valid1
408
+ assert_equal(@uniform_sets.length, 3)
409
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_set_single_string)
410
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_set_10_string)
411
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_set_10_numeric)
391
412
 
392
- assert_equal(@nonuniform_arrayoftuples.length, 4)
393
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_single_string),
394
- @nonuniform_arrayoftuples_single_string)
395
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_to_1),
396
- @nonuniform_arrayoftuples_10_sum_to_1)
397
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_gt_1),
398
- @nonuniform_arrayoftuples_10_sum_gt_1)
399
- verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_lt_1),
400
- @nonuniform_arrayoftuples_10_sum_lt_1)
413
+ assert_equal(@uniform_arrays.length, 3)
414
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_array_single_numeric)
415
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_array_10_string)
416
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_array_10_numeric)
417
+
418
+ assert_equal(@uniform_ranges.length, 4)
419
+
420
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_range_single_exclusive)
421
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_range_single_inclusive)
422
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_range_10_exclusive)
423
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_range_10_inclusive)
424
+
425
+ assert_equal(@uniform_singles.length, 2)
426
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_single_zero)
427
+ verify_sample_unique_values_are_valid_for_uniform(@uniform_single_nonzero)
401
428
  end
402
429
 
403
- def test_uniform_sample_values_are_valid
430
+ def test_uniform_sample_unique_values_are_valid2
404
431
  assert_equal(@uniform_sets.length, 3)
405
432
  verify_sample_unique_values_are_valid(RandomValueSampler.new_uniform(@uniform_set_single_string),
406
433
  @uniform_set_single_string)
@@ -435,7 +462,52 @@ class RandomValueSamplerTest < Test::Unit::TestCase
435
462
  @uniform_single_nonzero)
436
463
  end
437
464
 
465
+ def test_uniform_with_duplicates_no_nils_returned
466
+ verify_sample_values_are_valid(RandomValueSampler.new_uniform(@uniform_10_array_5_unique_vals),
467
+ @uniform_10_array_5_unique_vals)
468
+ 100.times do
469
+ rsampler = RandomValueSampler.new_uniform(@uniform_10_array_5_unique_vals)
470
+ assert(!rsampler.sample_unique(3).include?(nil))
471
+ end
472
+ end
473
+
438
474
  def test_non_uniform_sample_values_are_valid
475
+ assert_equal(@nonuniform_hashes.length, 4)
476
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_single_string),
477
+ @nonuniform_hash_single_string)
478
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_to_1),
479
+ @nonuniform_hash_10_sum_to_1)
480
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_gt_1),
481
+ @nonuniform_hash_10_sum_gt_1)
482
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_10_sum_lt_1),
483
+ @nonuniform_hash_10_sum_lt_1)
484
+
485
+ assert_equal(@nonuniform_arrayoftuples.length, 4)
486
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_single_string),
487
+ @nonuniform_arrayoftuples_single_string)
488
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_to_1),
489
+ @nonuniform_arrayoftuples_10_sum_to_1)
490
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_gt_1),
491
+ @nonuniform_arrayoftuples_10_sum_gt_1)
492
+ verify_sample_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_arrayoftuples_10_sum_lt_1),
493
+ @nonuniform_arrayoftuples_10_sum_lt_1)
494
+ end
495
+
496
+ def test_non_uniform_sample_unique_values_are_valid1
497
+ assert_equal(@nonuniform_hashes.length, 4)
498
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_hash_single_string)
499
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_hash_10_sum_to_1)
500
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_hash_10_sum_gt_1)
501
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_hash_10_sum_lt_1)
502
+
503
+ assert_equal(@nonuniform_arrayoftuples.length, 4)
504
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_arrayoftuples_single_string)
505
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_arrayoftuples_10_sum_to_1)
506
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_arrayoftuples_10_sum_gt_1)
507
+ verify_sample_unique_values_are_valid_for_non_uniform(@nonuniform_arrayoftuples_10_sum_lt_1)
508
+ end
509
+
510
+ def test_non_uniform_sample_unique_values_are_valid2
439
511
  assert_equal(@nonuniform_hashes.length, 4)
440
512
  verify_sample_unique_values_are_valid(RandomValueSampler.new_non_uniform(@nonuniform_hash_single_string),
441
513
  @nonuniform_hash_single_string)
@@ -564,16 +636,124 @@ class RandomValueSamplerTest < Test::Unit::TestCase
564
636
 
565
637
  valid_value_set = Set.new(vals_and_probs.keys)
566
638
 
639
+ # one sample
567
640
  (1..1000).each do
568
641
  sample = rsampler.sample
569
642
  assert(valid_value_set.include?(sample),
570
- "<#{sample}> is not a valid sample in raw values: <#{values}>")
643
+ "<#{sample.nil? ? 'nil' : sample}> is not a valid sample in raw values: <#{values}>")
571
644
  end
572
645
 
646
+ # two samples
647
+ (1..1000).each do
648
+ rsampler.sample(2).each do |s|
649
+ assert(valid_value_set.include?(s),
650
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
651
+ end
652
+ end
653
+
654
+ # ten samples
573
655
  (1..1000).each do
574
656
  rsampler.sample(10).each do |s|
575
657
  assert(valid_value_set.include?(s),
576
- "<#{s}> is not a valid multi-sample in raw values: <#{values}>")
658
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
659
+ end
660
+ end
661
+
662
+ # N-1 samples
663
+ unless rsampler.num_values <= 1
664
+ (1..1000).each do
665
+ rsampler.sample([rsampler.num_values - 1, 1].max).each do |s|
666
+ assert(valid_value_set.include?(s),
667
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
668
+ end
669
+ end
670
+ end
671
+ end
672
+
673
+ def verify_sample_unique_values_are_valid_for_uniform(values)
674
+ vals_and_probs = extract_hash_of_vals_and_probs(values)
675
+ vals_and_probs.delete_if { |val, prob| prob == 0 }
676
+
677
+ valid_value_set = Set.new(vals_and_probs.keys)
678
+
679
+ # one sample
680
+ (1..1000).each do
681
+ rsampler = RandomValueSampler.new_uniform(values)
682
+ sample = rsampler.sample_unique
683
+ assert(valid_value_set.include?(sample),
684
+ "<#{sample.nil? ? 'nil' : sample}> is not a valid sample in raw values: <#{values}>")
685
+ end
686
+
687
+ # two samples
688
+ (1..1000).each do
689
+ rsampler = RandomValueSampler.new_uniform(values)
690
+ rsampler.sample_unique(2).each do |s|
691
+ assert(valid_value_set.include?(s),
692
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
693
+ end
694
+ end
695
+
696
+ # ten samples
697
+ (1..1000).each do
698
+ rsampler = RandomValueSampler.new_uniform(values)
699
+ rsampler.sample_unique(10).each do |s|
700
+ assert(valid_value_set.include?(s),
701
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
702
+ end
703
+ end
704
+
705
+ # N-1 samples
706
+ if (values.is_a?(Array) || values.is_a?(Range)) && (values.to_a.size > 1)
707
+ (1..1000).each do
708
+ rsampler = RandomValueSampler.new_uniform(values)
709
+ rsampler.sample_unique([rsampler.num_values - 1, 1].max).each do |s|
710
+ assert(valid_value_set.include?(s),
711
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
712
+ end
713
+ end
714
+ end
715
+ end
716
+
717
+ def verify_sample_unique_values_are_valid_for_non_uniform(values)
718
+ vals_and_probs = extract_hash_of_vals_and_probs(values)
719
+ vals_and_probs.delete_if { |val, prob| prob == 0 }
720
+
721
+ valid_value_set = Set.new(vals_and_probs.keys)
722
+
723
+ # one sample
724
+ (1..1000).each do
725
+ rsampler = RandomValueSampler.new_non_uniform(values)
726
+ sample = rsampler.sample_unique
727
+ assert(valid_value_set.include?(sample),
728
+ "<#{sample.nil? ? 'nil' : sample}> is not a valid sample in raw values: <#{values}>")
729
+ end
730
+
731
+ # two samples
732
+ (1..1000).each do
733
+ rsampler = RandomValueSampler.new_non_uniform(values)
734
+ rsampler.sample_unique(2).each do |s|
735
+ assert(valid_value_set.include?(s),
736
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
737
+ end
738
+ end
739
+
740
+ # ten samples
741
+ (1..1000).each do
742
+ rsampler = RandomValueSampler.new_non_uniform(values)
743
+ rsampler.sample_unique(10).each do |s|
744
+ assert(valid_value_set.include?(s),
745
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
746
+ end
747
+ end
748
+
749
+ # N-1 samples
750
+ unless values.size <= 1
751
+ (1..1000).each do
752
+ rsampler = RandomValueSampler.new_non_uniform(values)
753
+ rsampler.sample_unique([rsampler.num_values - 1, 1].max).each do |s|
754
+ assert(valid_value_set.include?(s),
755
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values}>")
756
+ end
577
757
  end
578
758
  end
579
759
  end
@@ -594,7 +774,7 @@ class RandomValueSamplerTest < Test::Unit::TestCase
594
774
 
595
775
  sample = test_rsampler.sample_unique
596
776
  assert(valid_value_set.include?(sample),
597
- "<#{sample}> is not a valid sample in raw values: <#{values.inspect}>")
777
+ "<#{sample.nil? ? 'nil' : sample}> is not a valid sample in raw values: <#{values.inspect}>")
598
778
  end
599
779
 
600
780
  (1..1000).each do
@@ -603,12 +783,12 @@ class RandomValueSamplerTest < Test::Unit::TestCase
603
783
  if num_multi_samples > 1
604
784
  test_rsampler.sample_unique(num_multi_samples).each do |s|
605
785
  assert(valid_value_set.include?(s),
606
- "<#{s}> is not a valid multi-sample in raw values: <#{values.inspect}>")
786
+ "<#{s.nil? ? 'nil' : s }> is not a valid multi-sample in raw values: <#{values.inspect}>")
607
787
  end
608
788
  else
609
789
  sample = test_rsampler.sample_unique(num_multi_samples)
610
790
  assert(valid_value_set.include?(sample),
611
- "<#{sample}> is not a valid multi-sample in raw values: <#{values.inspect}>")
791
+ "<#{sample.nil? ? 'nil' : sample}> is not a valid multi-sample in raw values: <#{values.inspect}>")
612
792
  end
613
793
  end
614
794
  end
@@ -814,6 +994,8 @@ class RandomValueSamplerTest < Test::Unit::TestCase
814
994
  @uniform_single_nonzero
815
995
  ]
816
996
 
997
+ @uniform_10_array_5_unique_vals = [1,1,1,1,1,2,3,4,5,6]
998
+
817
999
  # error inputs
818
1000
 
819
1001
  @uniform_set_error_empty = Set.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random_value_sampler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-05-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2165317380 !ruby/object:Gem::Requirement
16
+ requirement: &2152713320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2165317380
24
+ version_requirements: *2152713320
25
25
  description: Class for sampling from arbitrary probability distributions
26
26
  email:
27
27
  - bpercival@goodreads.com
@@ -30,10 +30,12 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
+ - CHANGELOG
33
34
  - Gemfile
34
35
  - LICENSE
35
36
  - README.md
36
37
  - Rakefile
38
+ - TODO
37
39
  - lib/random_value_sampler.rb
38
40
  - lib/random_value_sampler/version.rb
39
41
  - random_value_sampler.gemspec