<jupyter_start><jupyter_text>EWM> Exponentially weighted functions<jupyter_code>#hide
import random

from nbdev.showdoc import *
#export
import random

import numpy as np
from numba import njit  # type: ignore

from window_ops.utils import first_not_na
#export
@njit
def ewm_mean(input_array: np.ndarray, alpha: float) -> np.ndarray:
    n_samples = input_array.size
    output_array = np.full_like(input_array, np.nan)
    start_idx = first_not_na(input_array)
    if start_idx >= n_samples:
        return output_array
    output_array[start_idx] = input_array[start_idx]
    for i in range(start_idx + 1, n_samples):
        output_array[i] = alpha * input_array[i] + (1-alpha) * output_array[i-1]
    return output_array
import pandas as pd

y = np.random.rand(10)
ys = pd.Series(y)
alpha = random.random()

assert np.allclose(ewm_mean(y, alpha), ys.ewm(alpha=alpha, adjust=False).mean().values)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1054897739504844, 'fraction_numerical': 0.005382131324004306, 'mean_word_length': 4.8125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '87588', 'license': 'Apache-2.0', 'n_tokens_mistral': 342, 'n_tokens_neox': 336, 'n_words': 76, 'path': 'nbs/ewm.ipynb', 'repo_name': 'jose-moralez/window_ops', 'size': '929'}