API reference

andrey.data.missingness.draw#

andrey.data.missingness.draw(data, mechanism, *, seed)[source]#

Draw the missing values of mechanism over complete data.

The draw comes from its own stream of seed, the one missing= on sample() takes, so draw(ds.data, mechanism, seed=s) on a complete sample ds with seed s and no screen gives the mask of the sample with missing=mechanism. A draw that leaves a column that can go missing with fewer than 2 observed values is drawn again from the next stream of seed.

Parameters:
Returns:

The mask and the parents of each indicator. np.where(m.mask, np.nan, data) is the data with the missing values.

Return type:

Missingness

Raises:

ValueError – If data is not a 2-D array of finite numbers with at least 2 rows, parents names a column data lacks, the columns cannot hold the mechanism (a "mar" mask needs one always-observed column and one that can go missing, an "mnar" mask two that can go missing), a parent is constant, or no draw in 100 keeps 2 observed values in every column.

Examples

>>> import numpy as np
>>> from andrey.data.missingness import draw, mcar
>>> X = np.random.default_rng(0).standard_normal((200, 3))
>>> m = draw(X, mcar(0.1), seed=0)
>>> m.mask.shape, sorted(m.parents)
((200, 3), [0, 1, 2])