Is this a thing? Does this look right?
Thanks!
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 08 18:36:43 2019
@author: User
"""
import numpy as np
def ewm_median(arr, alpha):
Final = []
for col in arr.T:
# ! important?
col = np.copy(col)
col[np.isnan(col)] = np.nanmedian(col, axis=0)
print col
inds = col.argsort()[::-1]
sorted_col = col[inds]
print sorted_col
weights = np.array([(1-alpha)**t for t in range(len(col))])[inds]
weights /= np.nansum(weights, axis=0)
weights = np.cumsum(weights, axis=0)
weights -= .5 * np.nanmin(weights, axis=0)
print weights
m = np.nanargmin(np.abs(weights - .5), axis=0)
m_1 = m - np.int(np.sign(weights[m] - .5))
print m, m_1
final = ((weights[m]-.5) * sorted_col[m_1] + \
(.5-weights[m_1]) * sorted_col[m]) \
/ (weights[m] - weights[m_1])
Final.append(final)
return np.array(Final)
arr = np.array([[2,3,5,np.nan,3.9]]).T
ewm = ewm_median(arr, .01)
arr2 = np.copy(arr)
arr2[np.isnan(arr2)] = ewm
ewm2 = ewm_median(arr2, .01)
arr3 = np.copy(arr)
arr3[np.isnan(arr3)] = ewm2
ewm3 = ewm_median(arr3, .01)
arr4 = np.copy(arr)
arr4[np.isnan(arr4)] = ewm3
ewm4 = ewm_median(arr4, .01)
print ewm
print ewm2-ewm
print ewm3-ewm2
print ewm4-ewm3