The interpolate_ma Module

interpolate_ma(num_vec: array, k: int)[source]

Python implementation of the logic described in the function located at: https://github.com/SteffenMoritz/imputeTS/blob/master/R/na_ma.R

If all inputs are missing, we return only missing values

Parameters
  • num_vec – Numeric vector with some missing values

  • k – minimum number of observations on each side of hte missing value to include in the weighted mean.

Returns

a np.array where the missing values have been replaced by new values calculated from a rolling average imputation method.

get_indices(vec, index, k)[source]

Return a range object of the indices to be used to calculate a rolling mean If the window defined by k doesn’t contain at least 2 non-missing values, k is increased by one until the window has at least two non-missing values.

Parameters
  • vec – np.array with missing data

  • k – minimum window on either side

  • index – Index to center the window around

Returns

np.array containing the indices defining the window

calc_weights(vec, indices, i)[source]

Create an array of weights for an array of indices, with the weight decreasing exponentially with distance from index of interest i. Indices pointing to missing data have their weight set to 0.

Parameters
  • vec – np.array of data being interpolated, known to have missingness.

  • indices – array of indices generated by get_indicies for index i.

  • i – index of a value to be replaced by a weighted mean of nearby values

Returns

np.array of weights