mushi.composition.multiplicative_replacement

multiplicative_replacement(mat, delta=None)[source]

Replace all zeros with small non-zero values.

It uses the multiplicative replacement strategy [1] , replacing zeros with a small positive \(\delta\) and ensuring that the compositions still add up to 1.

Parameters:
  • mat (array_like) – a matrix of proportions where rows = compositions and columns = components

  • delta (float, optional) – a small number to be used to replace zeros If delta is not specified, then the default delta is \(\delta = \frac{1}{N^2}\) where \(N\) is the number of components

Returns:

A matrix of proportions where all of the values are nonzero and each composition (row) adds up to 1

Return type:

numpy.ndarray, np.float64

Raises:

ValueError – Raises an error if negative proportions are created due to a large delta.

Notes

This method will result in negative proportions if a large delta is chosen.

References

Examples

>>> import numpy as np
>>> import mushi.composition as cmp
>>> X = np.array([[.2,.4,.4, 0],[0,.5,.5,0]])
>>> cmp.multiplicative_replacement(X)
Array([[0.1875, 0.375 , 0.375 , 0.0625],
       [0.0625, 0.4375, 0.4375, 0.0625]], dtype=float64)