/
selection.py
919 lines (733 loc) · 38 KB
/
selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
from abc import abstractmethod
from warnings import warn
import numpy
from scipy.integrate import quad
from . import irt
from .simulation import Selector, FiniteSelector
def _nearest(array: list, value) -> numpy.ndarray:
"""Returns the indexes of values in `array` that are closest to `value`
:param array: an array of numeric values
:param value: a numerical value
:return: an array containing the indexes of numbers in `array`,
according to how close their are to `value`
"""
array = numpy.asarray(array)
return numpy.abs(array - value).argsort()
class MaxInfoSelector(Selector):
"""Selector that returns the first non-administered item with maximum information, given an estimated theta"""
def __init__(self):
super().__init__()
def __str__(self):
return 'Maximum Information Selector'
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
# first, we'll order items by their information value
if irt.detect_model(items) <= 2:
# when the logistic model has the number of parameters <= 2,
# all items have highest information where theta = b
ordered_items = _nearest(items[:, 1], est_theta)
else:
# else, we'll have to calculate the theta value where information is maximum
inf_values = irt.max_info_hpc(items)
ordered_items = _nearest(inf_values, est_theta)
valid_indexes = [x for x in ordered_items if x not in administered_items]
if len(valid_indexes) == 0:
warn('There are no more items to be applied.')
return None
return valid_indexes[0]
class UrrySelector(Selector):
"""Selector that returns the item whose difficulty parameter is closest to the examinee's proficiency"""
def __init__(self):
super().__init__()
def __str__(self):
return 'Urry Selector'
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
ordered_items = _nearest(items[:, 1], est_theta)
valid_indexes = [x for x in ordered_items if x not in administered_items]
if len(valid_indexes) == 0:
warn('There are no more items to be applied.')
return None
return valid_indexes[0]
class LinearSelector(FiniteSelector):
"""Selector that returns item indexes in a linear order, simulating a standard
(non-adaptive) test.
:param indexes: the indexes of the items that will be returned in order"""
def __str__(self):
return 'Linear Selector'
def __init__(self, indexes: list):
super().__init__(len(indexes))
self._indexes = indexes
self._current = 0
@property
def indexes(self):
return self._indexes
@property
def current(self):
return self._current
def select(self, index: int = None, administered_items: list = None, **kwargs) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param administered_items: a list containing the indexes of items that were already administered
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None) and (administered_items is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if administered_items is None:
administered_items = self.simulator.administered_items[index]
if set(self._indexes) <= set(administered_items):
warn(
'A new index was asked for, but there are no more item indexes to present.\nCurrent item:\t\t\t{0}\nItems to be administered:\t{1} (size: {2})\nAdministered items:\t\t{3} (size: {4})'.
format(
self._current, sorted(self._indexes), len(self._indexes),
sorted(administered_items), len(administered_items)
)
)
return None
selected_item = [x for x in self._indexes if x not in administered_items][0]
return selected_item
class RandomSelector(Selector):
"""Selector that randomly selects items for application.
:param replace: whether to select an item that has already been selected before for this examinee."""
def __str__(self):
return 'Random Selector'
def __init__(self, replace: bool = False):
super().__init__()
self._replace = replace
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or
self.simulator is None) and (items is None or administered_items is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
if len(administered_items) >= items.shape[0] and not self._replace:
warn(
'A new item was asked for, but there are no more items to present.\nAdministered items:\t{0}\nItem bank size:\t{1}'.
format(len(administered_items), items.shape[0])
)
return None
if self._replace:
return numpy.random.choice(items.shape[0])
else:
return numpy.random.choice(list(set(range(items.shape[0])) - set(administered_items)))
class ClusterSelector(Selector):
"""Cluster-based Item Selection Method.
.. [Men15] Meneghetti, D. R. (2015). Metolodogia de seleção de itens em testes
adaptativos informatizados baseada em agrupamento por similaridade (Mestrado).
Centro Universitário da FEI. Retrieved from
https://www.researchgate.net/publication/283944553_Metodologia_de_selecao_de_itens_em_Testes_Adaptativos_Informatizados_baseada_em_Agrupamento_por_Similaridade
:param clusters: a list containing item cluster memberships
:param r_max: maximum exposure rate for items
:param method: one of the available methods for cluster selection. Given
the estimated theta value at each step:
``item_info``: selects the cluster which has the item
with maximum information;
``cluster_info``: selects the cluster whose items sum of
information is maximum;
``weighted_info``: selects the cluster whose weighted
sum of information is maximum. The weighted equals the
number of items in the cluster;
:param r_control: if `passive` and all items :math:`i` in the selected
cluster have :math:`r_i > r^{max}`, applies the item with
maximum information; if `aggressive`, applies the item
with smallest :math:`r` value.
"""
def __str__(self):
return 'Cluster Selector'
@property
def r_max(self):
return self._r_max
@property
def clusters(self):
return self._clusters
@property
def method(self):
return self._method
@property
def r_control(self):
return self._r_control
def __init__(
self,
clusters: list,
method: str = 'item_info',
r_max: float = 1,
r_control: str = 'passive'
):
super().__init__()
available_methods = ['item_info', 'cluster_info', 'weighted_info']
if method not in available_methods:
raise ValueError(
'{0} is not a valid cluster selection method; choose one from {1}'.format(
method, available_methods
)
)
available_rcontrol = ['passive', 'aggressive']
if r_control not in available_rcontrol:
raise ValueError(
'{0} is not a valid item exposure control method; choose one from {1}'.format(
r_control, available_rcontrol
)
)
self._clusters = clusters
self._method = method
self._r_max = r_max
self._r_control = r_control
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
selected_cluster = None
existent_clusters = set(self._clusters)
# this part of the code selects the cluster from which the item at
# the current point of the test will be chosen
if self._method == 'item_info':
# get the item indexes sorted by their information value
infos = _nearest(irt.max_info_hpc(items), est_theta)
evaluated_clusters = set()
# iterate over every item in order of information value
for i in range(items.shape[0]):
# get the current non-examined item
max_info_item = infos[i]
# if the cluster of the current item has already been fully examined, go to the next item
if self._clusters[max_info_item] in evaluated_clusters:
continue
# get the indexes of all items in the same cluster as the current item
items_in_cluster = numpy.nonzero(
[x == self._clusters[max_info_item] for x in self._clusters]
)[0]
# if all the items in the current cluster have already been administered (it happens, theoretically),
# add this cluster to the list of fully evaluated clusters
if set(items_in_cluster) <= set(administered_items):
evaluated_clusters.add(self._clusters[max_info_item])
# if all clusters have been evaluated, the loop ends and the method returns None somewhere below
if evaluated_clusters == existent_clusters:
break
# else, if there are still items and clusters to be explored, keep going
continue
# if the algorithm gets here, it means this cluster can be used
selected_cluster = self._clusters[max_info_item]
break
elif self._method in ['cluster_info', 'weighted_info']:
# calculates the cluster information, depending on the method
# selected
if self._method == 'cluster_info':
cluster_infos = ClusterSelector.sum_cluster_infos(est_theta, items, self._clusters)
else:
cluster_infos = ClusterSelector.weighted_cluster_infos(
est_theta, items, self._clusters
)
# sorts clusters descending by their information values
# this type of sorting was seem on
# http://stackoverflow.com/a/6618543
sorted_clusters = numpy.array(
[
cluster for (inf_value, cluster) in sorted(
zip(cluster_infos, set(self._clusters)),
key=lambda pair: pair[0],
reverse=True
)
],
dtype=float
)
# walks through the sorted clusters in order
for i in range(len(sorted_clusters)):
valid_indexes = numpy.nonzero([r == sorted_clusters[i] for r in items[:, 4]])[0]
# checks if at least one item from this cluster has not
# been administered to this examinee yet
if set(valid_indexes).intersection(administered_items) != set(valid_indexes):
selected_cluster = sorted_clusters[i]
break
# the for loop ends with the cluster that has a) the maximum
# information possible and b) at least one item that has not
# yet been administered
# if the test size gets larger than the item bank size, end the test
if selected_cluster is None:
warn("There are no more items to be applied.")
return None
# in this part, an item is chosen from the cluster that was
# selected above
# gets the indexes and information values from the items in the
# selected cluster that have not been administered
valid_indexes = [
index
for index in numpy.nonzero([cluster == selected_cluster
for cluster in self._clusters])[0]
if index not in administered_items
]
# gets the indexes and information values from the items in the
# selected cluster with r < rmax that have not been
# administered
valid_indexes_low_r = [
index for index in valid_indexes
if items[index, 4] < self._r_max and index not in administered_items
]
if len(valid_indexes_low_r) > 0:
# return the item with maximum information from the ones available
inf_values = irt.inf_hpc(est_theta, items[valid_indexes_low_r])
selected_item = valid_indexes_low_r[numpy.nonzero(inf_values == max(inf_values))[0][0]]
# if all items in the selected cluster have exceed their r values,
# select the one with smallest r, regardless of information
else:
if self._r_control == 'passive':
inf_values = irt.inf_hpc(est_theta, items[valid_indexes])
selected_item = valid_indexes[numpy.nonzero(inf_values == max(inf_values))[0][0]]
else:
selected_item = valid_indexes[items[:, 4].index(min(items[:, 4]))]
return selected_item
@staticmethod
def sum_cluster_infos(theta: float, items: numpy.ndarray, clusters: list) -> numpy.ndarray:
"""Returns the sum of item information values, separated by cluster
:param theta: an examinee's :math:`\\theta` value
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param clusters: a list containing item cluster memberships, represented by integers
:returns: array containing the sum of item information values for each cluster"""
cluster_infos = numpy.zeros((len(set(clusters))))
for cluster in set(clusters):
cluster_indexes = numpy.nonzero([c == cluster for c in clusters])[0]
for item in items[cluster_indexes]:
cluster_infos[cluster] += irt.inf(theta, item[0], item[1], item[2], item[3])
return cluster_infos
@staticmethod
def weighted_cluster_infos(theta: float, items: numpy.ndarray, clusters: list) -> numpy.ndarray:
"""Returns the weighted sum of item information values, separated by cluster.
The weight is the number of items in each cluster.
:param theta: an examinee's :math:`\\theta` value
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param clusters: a list containing item cluster memberships, represented by integers
:returns: array containing the sum of item information values for each cluster,
divided by the number of items in each cluster"""
cluster_infos = ClusterSelector.sum_cluster_infos(theta, items, clusters)
count = numpy.bincount(clusters)
for i in range(len(cluster_infos)):
cluster_infos[i] /= count[i]
return cluster_infos
@staticmethod
def sum_cluster_params(items: numpy.ndarray, c: list):
"""Returns the sum of item parameter values for each cluster
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param c: a list containing clustering memeberships.
:returns: a matrix containing the sum of each parameter by cluster. Lines are clusters, columns are parameters.
"""
averages = numpy.zeros((numpy.max(c) + 1, 4))
for i in numpy.arange(0, numpy.size(c)):
if c[i] == -1:
continue
averages[c[i], 0] += items[i, 0]
averages[c[i], 1] += items[i, 1]
averages[c[i], 2] += items[i, 2]
averages[c[i], 3] += items[i, 3]
return averages
@staticmethod
def avg_cluster_params(items: numpy.ndarray, c: list):
"""Returns the average values of item parameters by cluster
:param items:
:param c: a list containing clustering memeberships.
:returns: a matrix containing the average values of each parameter by cluster.
Lines are clusters, columns are parameters."""
averages = ClusterSelector.sum_cluster_params(items, c)
occurrences = numpy.bincount(numpy.delete(c, numpy.where(c == -1)).astype(numpy.int64))
for counter, i in enumerate(occurrences):
averages[counter, 0] /= i
averages[counter, 1] /= i
averages[counter, 2] /= i
averages[counter, 3] /= i
return averages
class StratifiedSelector(FiniteSelector):
def __str__(self):
return 'General Stratified Selector'
def __init__(self, test_size):
super().__init__(test_size)
self._organized_items = None
@staticmethod
@abstractmethod
def sort_items(items: numpy.ndarray) -> numpy.ndarray:
pass
def preprocess(self):
# sort item indexes by their discrimination value
self._organized_items = __class__.sort_items(self.simulator.items)
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters
:param administered_items: a list containing the indexes of items that were already administered
:returns: index of the next item to be applied or `None` if there are no more strata to get items from.
"""
if (index is None or
self.simulator is None) and (items is None or administered_items is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
# select the item in the correct layer, according to the point in the test the examinee is
slices = numpy.linspace(0, items.shape[0], self._test_size, endpoint=False, dtype='i')
try:
pointer = slices[len(administered_items)]
max_pointer = items.shape[0] if len(
administered_items
) == self._test_size - 1 else slices[len(administered_items) + 1]
except IndexError:
warn(
"{0}: test size is larger than was informed to the selector\nLength of administered items:\t{0}\nTotal length of the test:\t{1}\nNumber of slices:\t{2}".
format(self, len(administered_items), self._test_size, len(slices))
)
return None
organized_items = self._organized_items if self._organized_items is not None else self.sort_items(
items
)
# if the selected item has already been administered, select the next one
while organized_items[pointer] in administered_items:
pointer += 1
if pointer == max_pointer:
raise ValueError(
'There are no more items to be selected from stratum {0}'.format(
slices[len(administered_items)]
)
)
return organized_items[pointer]
class AStratSelector(StratifiedSelector):
"""Implementation of the :math:`\\alpha`-stratified selector proposed by
[Chang99]_, in which the item bank is sorted in ascending order according to the
items discrimination parameter and then separated into :math:`K` strata
(:math:`K` being the test size), each stratum containing gradually higher
average discrimination. The :math:`\\alpha`-stratified selector then selects the
first non-administered item from stratum :math:`k`, in which :math:`k`
represents the position in the test of the current item the examinee is being
presented.
.. image:: ../sphinx/alpha-strat.*
:param test_size: the number of items the test contains. The selector uses this parameter
to create the correct number of strata.
"""
def __str__(self):
return 'a-Stratified Selector'
def __init__(self, test_size):
super().__init__(test_size)
@staticmethod
def sort_items(items: numpy.ndarray) -> numpy.ndarray:
return items[:, 0].argsort()
class AStratBBlockSelector(StratifiedSelector):
"""Implementation of the :math:`\\alpha`-stratified selector with :math:`b`
blocking proposed by [Chang2001]_, in which the item bank is sorted in ascending
order according to the items difficulty parameter and then separated into
:math:`M` strata, each stratum containing gradually higher average difficulty.
Each of the :math:`M` strata is then again separated into :math:`K`
sub-strata (:math:`k` being the test size), according to their
discrimination. The final item bank is then ordered such that the first
sub-strata of each strata forms the first strata of the new ordered item
bank, and so on. This method tries to balance the distribution of both
parameters between all strata, after perceiving that they are correlated.
.. image:: ../sphinx/b-blocking.*
:param test_size: the number of items the test contains. The selector uses this parameter to
create the correct number of strata.
"""
def __str__(self):
return 'a-Stratified b-Blocking Selector'
def __init__(self, test_size):
super().__init__(test_size)
@staticmethod
def sort_items(items: numpy.ndarray) -> numpy.ndarray:
return numpy.lexsort((items[:, 0], items[:, 1]))
class MaxInfoStratSelector(StratifiedSelector):
"""Implementation of the maximum information stratification (MIS) selector
proposed by [Bar06]_, in which the item bank is sorted in ascending order
according to the items maximum information and then separated into :math:`K`
strata (:math:`K` being the test size), each stratum containing items with
gradually higher maximum information. The MIS selector then selects the first
non-administered item from stratum :math:`k`, in which :math:`k` represents the
position in the test of the current item the examinee is being presented.
.. image:: ../sphinx/mis.*
This method claims to work better than the :math:`a`-stratified method by
[Chang99]_ for the three-parameter logistic model of IRT, since item difficulty
and maximum information are not positioned in the same place in the proficiency
scale in 3PL.
:param test_size: the number of items the test contains. The selector uses this parameter to
create the correct number of strata.
"""
def __str__(self):
return 'Maximum Information Stratification Selector'
def __init__(self, test_size):
super().__init__(test_size)
@staticmethod
def sort_items(items: numpy.ndarray) -> numpy.ndarray:
maxinfo = irt.max_info_hpc(items)
return irt.inf_hpc(maxinfo, items).argsort()
class MaxInfoBBlockSelector(StratifiedSelector):
"""Implementation of the maximum information stratification with :math:`b`
blocking (MIS-B) selector proposed by [Bar06]_, in which the item bank is sorted
in ascending order according to the items difficulty parameter and then
separated into :math:`M` strata, each stratum containing gradually higher
average difficulty.
Each of the :math:`M` strata is then again separated into :math:`K`
sub-strata (:math:`k` being the test size), according to the items maximum
information. The final item bank is then ordered such that the first
sub-strata of each strata forms the first strata of the new ordered item
bank, and so on. This method tries to balance the distribution of both
parameters between all strata and works better than the :math:`a`-stratified
with :math:`b` blocking method by [Chang2001]_ for the three-parameter
logistic model of IRT, since item difficulty and maximum information are not
positioned in the same place in the proficiency scale in 3PL. This may also
apply, although not mentioned by the authors, for the 4PL.
.. image:: ../sphinx/mis-b.*
:param test_size: the number of items the test contains. The selector uses this parameter to
create the correct number of strata.
"""
def __str__(self):
return 'Maximum Information Stratification with b-Blocking Selector'
def __init__(self, test_size):
super().__init__(test_size)
@staticmethod
def sort_items(items: numpy.ndarray) -> numpy.ndarray:
maxinfo = irt.max_info_hpc(items)
return numpy.lexsort((irt.inf_hpc(maxinfo, items), maxinfo))
class The54321Selector(FiniteSelector):
"""Implementation of the 5-4-3-2-1 selector proposed by [McBride83]_, in which,
at each step :math:`k` of a test of size :math:`K`, an item is chosen from a bin
containing the :math:`K-k` most informative items in the bank, given the current
:math:`\\hat\\theta`. As the test progresses, the bin gets smaller and more
informative items have a higher probability of being chosen by the end of the
test, when the estimation of ':math:`\\hat\\theta` is more precise. The
5-4-3-2-1 selector can be viewed as a specialization of the
:py:class:`catsim.selection.RandomesqueSelector`, in which the bin size of most
informative items gets smaller as the test progresses.
:param test_size: the number of items the test contains. The selector uses
this parameter to set the bin size"""
def __str__(self):
return '5-4-3-2-1 Selector'
def __init__(self, test_size):
super().__init__(test_size)
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
# sort item indexes by their information value and remove indexes of administered items
organized_items = [
x for x in irt.inf_hpc(est_theta, items).argsort() if x not in administered_items
]
bin_size = self._test_size - len(administered_items)
if len(organized_items) == 0:
warn('There are no more items to apply.')
return None
return numpy.random.choice(organized_items[0:bin_size])
class RandomesqueSelector(Selector):
"""Implementation of the randomesque selector proposed by [Kingsbury89]_, in which,
at every step of the test, an item is randomly chosen from the :math:`n` most informative
items in the item bank, :math:`n` being a predefined value (originally 5, but user-defined
in this implementation)
:param bin_size: the number of most informative items to be taken into consideration when
randomly selecting one of them.
"""
def __str__(self):
return 'Randomesque Selector'
def __init__(self, bin_size):
super().__init__()
self._bin_size = bin_size
@property
def bin_size(self):
return self._bin_size
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
# sort item indexes by their information value and remove indexes of administered items
organized_items = [
x for x in irt.inf_hpc(est_theta, items).argsort() if x not in administered_items
]
if len(organized_items) == 0:
warn('There are no more items to apply.')
return None
return numpy.random.choice(list(organized_items)[:self._bin_size])
class IntervalInfoSelector(Selector):
"""A selector in which, at every step of the test, the item that maximizes
the integral of the information function at a predetermined ``interval``
:math:`\\delta` above and below the current :math:`\\hat\\theta` is chosen.
.. math:: argmax_{i \\in I} \\int_{\\hat\\theta - \\delta}^{\\hat\\theta - \\delta}I_i(\\hat\\theta)
:param interval: the interval of the integral. If no interval is passed, the
integral is calculated from :math:`[-\\infty, \\infty]`.
"""
def __str__(self):
return 'Interval Information Selector'
def __init__(self, interval: float = None):
super().__init__()
self._interval = interval if interval is not None else numpy.inf
@property
def interval(self):
return self._interval
def select(
self,
index: int = None,
items: numpy.ndarray = None,
administered_items: list = None,
est_theta: float = None,
**kwargs
) -> int:
"""Returns the index of the next item to be administered.
:param index: the index of the current examinee in the simulator.
:param items: a matrix containing item parameters in the format that `catsim` understands
(see: :py:func:`catsim.cat.generate_item_bank`)
:param administered_items: a list containing the indexes of items that were already administered
:param est_theta: a float containing the current estimated proficiency
:returns: index of the next item to be applied or `None` if there are no more items in the item bank.
"""
if (index is None or self.simulator is None
) and (items is None or administered_items is None or est_theta is None):
raise ValueError(
'Either pass an index for the simulator or all of the other optional parameters to use this component independently.'
)
if items is None and administered_items is None and est_theta is None:
items = self.simulator.items
administered_items = self.simulator.administered_items[index]
est_theta = self.simulator.latest_estimations[index]
# sort item indexes by the integral of the information function and remove indexes of administered items
organized_items = [
x for x in numpy.array(
[
quad(
irt.inf,
est_theta - self._interval,
est_theta + self._interval,
args=(item[0], item[1], item[2], item[3])
)[0] for item in items
]
).argsort() if x not in administered_items
]
if len(organized_items) == 0:
warn('There are no more items to apply.')
return None
return list(organized_items)[0]