Skip to content

Instantly share code, notes, and snippets.

View assuncaolfi's full-sized avatar
🏠
Working from home

Luís Assunção assuncaolfi

🏠
Working from home
View GitHub Profile
@assuncaolfi
assuncaolfi / ewm.R
Created May 4, 2019 17:52
Vectorized R implementation of exponentially weighted moving average and standard deviation. Outputs the same as Python's default pandas.Series.ewm().
ewma <- function(x, alpha) {
n <- length(x)
sapply(
1:n,
function(i, x, alpha) {
y <- x[1:i]
m <- length(y)
weights <- (1 - alpha)^((m - 1):0)
ewma <- sum(weights * y) / sum(weights)
},