Item Bank – irtlib.ItemBank#
The item bank: the items of a test with their names, read from and written to a CSV file, or drawn at random.
A bank holds one row \([a, b, c, d]\) per item, the parameters of the four-parameter logistic model of
irtlib.irt: every parameter finite, with \(a > 0\) and \(0 \leq c < d \leq 1\). Its model is the
simplest that fits every item. The 3PL fixes \(d = 1\), the 2PL also \(c = 0\), and the 1PL also gives every
item the same discrimination (Magis and Raîche [2012], p. 3).
ItemBank.generate draws random banks. Barrada et al. [2010] (p. 445)
simulate banks of 500 items whose “parameter distributions were: a ~ N(1.2, .25), b ~ N(0, 1) and c ~ N(.25,
.02)”, with \(a\) and \(b\) uncorrelated or correlated at \(r_{ab} = .5\). Their item information
carries the scaling constant \(D = 1.7\) (p. 443, Equations 9 and 10), so their discriminations are converted to
irtlib’s models, which have none, as a bank built with D=1.7 converts them: multiplied by 1.7. The
page does not say whether the second number is a standard deviation or a variance, nor how the correlation was
induced, and it bounds neither \(a\) nor \(c\). The rest is irtlib’s choice:
the second numbers are read as standard deviations;
\(a\) and \(b\) are drawn from a bivariate normal with correlation
corr, and an item whose \(a\) is not positive is drawn again, \(a\) and \(b\) together, which truncates the bivariate normal to \(a > 0\);\(c\) is clipped at 0;
the upper asymptote of a 4PL bank is drawn from \(U(0.94, 1)\).
- class irtlib.ItemBank(items: NDArray[floating[Any]], names: Iterable[str] | None = None, *, D: float = 1.0)[source]#
The calibrated items of a test, with their names and the values derived from their parameters.
Every item has a name, a non-empty string unique within the bank, and is referred to by its row index, from 0 to
n_items - 1:names[item]is its name andindex()gives a name’s row. A bank checks every parameter and name when it is built, and caches the values that depend only on the parameters, such as each item’s ability of maximum information; its arrays are read-only, so those values cannot go stale. Two banks are equal when their parameters and names are. Read a bank withfrom_csv(), or draw one withgenerate(). Printed, a bank shows its model and a table of its items’ parameters, by row and name.- Parameters:
- itemsnumpy.ndarray
Item parameter matrix, one row per item, with columns [a, b, c, d]. A matrix with one column is read as [b], two as [a, b] and three as [a, b, c], filled with \(a = 1\), \(c = 0\) and \(d = 1\); a 1-D array is one item.
- namesiterable of str or None, optional
One name per item, in row order. Default is None, which names each item
item_followed by its index, zero-padded to the width of the last index:item_000toitem_499in a 500-item bank.- Dfloat, optional
The scaling constant \(D\) of the model the parameters are written for: a constant that some sources and programs multiply each discrimination by, such as 1.7, and that irtlib’s models do not have. The bank multiplies every discrimination by it, a filled-in \(a = 1\) included, and keeps the other parameters. Default is 1, for parameters written without the constant, which are kept as they are. The Item Response Functions page defines it.
- Attributes:
itemsnumpy.ndarrayGet the item parameter matrix.
n_itemsintGet the number of items in the bank.
namestuple of strGet the items’ names.
max_info_thetasnumpy.ndarrayGet the cached theta values where items have maximum information.
max_info_valuesnumpy.ndarrayGet the cached maximum information values for each item.
- Raises:
- ValueError
If the matrix does not have 1 to 4 columns, has no items, or has a parameter that is NaN, infinite or outside \(a > 0\) and \(0 \leq c < d \leq 1\); if
namesdoes not hold one name per item, or holds an empty or repeated name; or ifDis not a positive finite number.- TypeError
If items is not a numpy.ndarray, or
namesis a string or holds a value that is not one.
Examples
>>> import numpy as np >>> from irtlib import ItemBank >>> ItemBank(np.array([[1.0, 0.0, 0.0, 1.0], [1.2, -0.5, 0.1, 1.0]])) ItemBank(n_items=2, model=3PL) >>> bank = ItemBank(np.array([[0.0], [-0.5], [1.1]]), names=["q7", "q2", "q4"]) >>> bank.names ('q7', 'q2', 'q4') >>> bank.index("q2") 1 >>> ItemBank.generate(100, model="3PL", rng=7).names[:2] ('item_00', 'item_01')
Parameters written with the scaling constant 1.7 are converted as the bank is built:
>>> ItemBank(np.array([[1.2, -0.5, 0.2, 1.0]]), D=1.7).discrimination array([2.04])
A bank of another shape than
generate()draws is built from its parameters’ columns:>>> rng = np.random.default_rng(3) >>> a, b = rng.lognormal(0.0, 0.3, 200), rng.uniform(-3.0, 3.0, 200) >>> ItemBank(np.column_stack([a, b, np.full(200, 0.2)])) ItemBank(n_items=200, model=3PL)
- property difficulty: NDArray[floating[Any]]#
Get item difficulty parameters (b).
- Returns:
- numpy.ndarray
Array of difficulty values.
- property discrimination: NDArray[floating[Any]]#
Get item discrimination parameters (a).
- Returns:
- numpy.ndarray
Array of discrimination values.
- classmethod from_csv(path: str | PathLike[str], *, D: float = 1.0) Self[source]#
Read a bank from a CSV file in the format
to_csv()writes.The file is UTF-8 text, comma-separated, with numbers such as
-1.25or1e-05, a dot as the decimal mark. Its first row is a header naming the columns, in any order:a,b,candd, the item parameters.bis required; a missingais 1, a missingcis 0 and a missingdis 1.An optional name column, headed
nameor left empty, as R’swrite.csvheads row names. Without it, the items get generated names.
Each following row is one item, and only blank lines at the end of the file are skipped. A path ending in
.gzis read as a gzip-compressed file. The bank checks what it reads as it checks a matrix, and its errors start with the file’s path.- Parameters:
- pathstr or os.PathLike
The file to read.
- Dfloat, optional
The scaling constant \(D\) of the model the file’s parameters are written for, which every discrimination read, a missing
aincluded, is multiplied by, asItemBankdoes. Default is 1, for parameters written without the constant, which are kept as they are.
- Returns:
- ItemBank
The bank the file holds.
- Raises:
- ValueError
If
Dis not a positive finite number; if the file is empty; if its header names another column, names a column twice, or has nob; if a row, a blank line before the last item included, does not hold one value per column, or a parameter is not a number; or if the bank rejects the parameters or names read.
- classmethod generate(n_items: int, model: IrtModel | str = IrtModel.PL4, corr: float = 0.0, rng: int | Generator | None = None) Self[source]#
Draw a bank of
n_itemsitems of one logistic model.The parameters the model varies are drawn from these distributions; the others are \(a = 1\), \(c = 0\) and \(d = 1\). The module docstring gives their source and says which choices are irtlib’s.
discrimination: 1.7 times a draw from the normal with mean 1.2 and standard deviation 0.25, truncated to positive values;
difficulty: standard normal, with correlation
corrto the discrimination;guessing parameter: normal with mean 0.25 and standard deviation 0.02, clipped at 0;
upper asymptote: uniform on [0.94, 1).
- Parameters:
- n_itemsint
Number of items.
- modelIrtModel or str, optional
The logistic model of the items, an
IrtModelor its name in any case (“3pl”). Default isIrtModel.PL4.- corrfloat, optional
Correlation between discrimination and difficulty, from -1 to 1 for every model; the discriminations of a 1PL bank do not depend on it. Default is 0.
- rngint, numpy.random.Generator or None, optional
Where the parameters are drawn from. None, the default, draws fresh randomness, so every call gives another bank. An int seeds a new generator, so the same seed gives the same bank. A generator is drawn from as is, and the call advances it.
- Returns:
- ItemBank
The generated bank.
- Raises:
- ValueError
If
modelis neither anIrtModelnor one of the names “1PL”, “2PL”, “3PL” and “4PL”, in any case, or ifcorris NaN or outside [-1, 1].
Examples
>>> bank = ItemBank.generate(100, model="3PL", corr=0.3, rng=7) >>> bank.n_items 100 >>> bank.model <IrtModel.PL3: '3PL'>
- get_items(indices: ArrayLike) NDArray[floating[Any]][source]#
Return the parameter rows of the given items, in the order of
indices.Any sequence of item indices is read as a list of items, so a tuple gives its rows and an empty one gives no rows, whereas
bank[(1, 2)]is one parameter (row 1, column 2) andbank[()]is every item.- Parameters:
- indicesarray-like of int
Item indices: a list, tuple or integer array, possibly empty.
- Returns:
- numpy.ndarray
A copy of the rows, shape
(len(indices), 4).
Examples
>>> bank = ItemBank.generate(5, rng=0) >>> bank.get_items((1, 2)).shape (2, 4) >>> bank.get_items(()).shape (0, 4)
- icc(theta: float | NDArray[floating[Any]]) NDArray[floating[Any]][source]#
Return the probability of a correct response to every item, as
irtlib.irt.icc()gives it.- Parameters:
- thetafloat or numpy.ndarray
An ability, or an array of abilities.
- Returns:
- numpy.ndarray
Shape
(n_items,)for a floatthetaand(k, n_items)forkabilities.
- index(name: str) int[source]#
Return the row index of the item with the given name.
- Parameters:
- namestr
An item’s name.
- Returns:
- int
The item’s index, from 0 to
n_items - 1.
- Raises:
- ValueError
If no item of the bank has that name.
- information(theta: float | NDArray[floating[Any]]) NDArray[floating[Any]][source]#
Return the information of every item, as
irtlib.irt.information()gives it.- Parameters:
- thetafloat or numpy.ndarray
An ability, or an array of abilities.
- Returns:
- numpy.ndarray
Shape
(n_items,)for a floatthetaand(k, n_items)forkabilities.
- property items: NDArray[floating[Any]]#
Get the item parameter matrix.
- Returns:
- numpy.ndarray
The read-only item matrix with shape (n_items, 4) containing [a, b, c, d] for each item.
- property max_info_thetas: NDArray[floating[Any]]#
Get the cached theta values where items have maximum information.
- Returns:
- numpy.ndarray
Read-only array of theta values, one per item.
- property max_info_values: NDArray[floating[Any]]#
Get the cached maximum information values for each item.
- Returns:
- numpy.ndarray
Read-only array of maximum information values, one per item.
- property model: IrtModel#
Get the simplest logistic model that fits every item of the bank.
- Returns:
- IrtModel
PL4if an upper asymptote is below 1, elsePL3if a guessing parameter is above 0, elsePL2if the discriminations differ, elsePL1.
- property n_items: int#
Get the number of items in the bank.
- Returns:
- int
Number of items in the bank.
- property names: tuple[str, ...]#
Get the items’ names.
- Returns:
- tuple of str
One name per item, in row order, so
names[item]is the name of itemitem.
- property pseudo_guessing: NDArray[floating[Any]]#
Get item guessing parameters (c), also called pseudo-guessing parameters.
- Returns:
- numpy.ndarray
Array of guessing parameter values.
- to_csv(path: str | PathLike[str], *, D: float = 1.0) None[source]#
Write the bank to a CSV file that
from_csv()reads back.The header is
name,a,b,c,d, followed by one row per item. Each parameter is written with the fewest digits that read back as the same float, so the file reads back as an equal bank. Written and read back with anotherDthan 1, the discriminations come back within two units in the last place. A name is quoted where it holds a comma, a quote or a line break. A path ending in.gzis written gzip-compressed. An existing file is overwritten.- Parameters:
- pathstr or os.PathLike
The file to write.
- Dfloat, optional
The scaling constant \(D\) of the model the file is written for, which every discrimination is divided by; the other parameters are written as they are. Default is 1, for a file without the constant, which keeps the discriminations as they are.
- Raises:
- ValueError
If
Dis not a positive finite number.
- total_information(theta: float, indices: ArrayLike | None = None) float[source]#
Return the test information of the given items at
theta.It is
irtlib.irt.total_information()of the items’ rows.- Parameters:
- thetafloat
Ability value.
- indicesarray-like of int or None, optional
Item indices, as in
get_items(): a list, tuple or integer array, possibly empty. Default is None, every item.
- Returns:
- float
The sum of the items’ information at
theta.
- class irtlib.IrtModel(*values)[source]#
The logistic model of an item bank, named after the number of parameters its items vary.
A member equals its name, so
IrtModel("3PL")isIrtModel.PL3and either can be passed where a model is expected. A 1PL bank has one discrimination for every item, \(c = 0\) and \(d = 1\); a 2PL bank varies the discrimination, a 3PL bank also the guessing parameter \(c\), and a 4PL bank also the upper asymptote \(d\).Examples
>>> IrtModel("3PL") is IrtModel.PL3 True