Skip to content

Commit

Permalink
Merge pull request #392 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #374
  • Loading branch information
spsanderson authored Jan 7, 2024
2 parents c246042 + 5fcc109 commit c8922e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dplyr in favor of using `dplyr::pick()`
5. Fix #385 - For `tidy_multi_dist_autoplot()` the `.plot_type = "quantile"` did
not work.
6. Fix #383 - Update all autoplot functions to use linewidth instead of size.
7. Fix #375 - Update `cskewness()` to take advantage of vectorization with a speedup
of 124x faster.

# TidyDensity 1.2.6

Expand Down
27 changes: 24 additions & 3 deletions R/vec-cumulative-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,31 @@ cvar <- function(.x) {
#'

cskewness <- function(.x) {
skewness <- function(.x) {
sqrt(length(.x)) * sum((.x - mean(.x))^3) / (sum((.x - mean(.x))^2)^(3 / 2))
n <- length(.x)

if (n == 0L) {
return(.x)
} else if (n == 1L) {
return(0)
}

m2 <- m3 <- term1 <- 0
out <- numeric(n)
out[1] <- NaN
m1 <- .x[1]

for (i in 2:n) {
n0 <- i - 1
delta <- x[i] - m1
delta_n <- delta/i
m1 <- m1 + delta_n
term1 <- delta*delta_n*n0
m3 <- m3 + term1*delta_n*(n0 - 1) - 3*delta_n*m2
m2 <- m2 + term1
out[i] <- sqrt(i)*m3/m2^1.5
}
sapply(seq_along(.x), function(k, z) skewness(z[1:k]), z = .x)

out
}

#' Cumulative Kurtosis
Expand Down

0 comments on commit c8922e7

Please sign in to comment.