npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

online-autocovariance

v0.0.1

Published

Autocovariance (online algorithm)

Downloads

270

Readme

online-autocovariance

Usage

const Autocov = require('.')

const ac1 = Autocov(3)
const ac2 = Autocov(3)

;[1, 2, 3, 4, 5, 6, 7].forEach(v => { ac1(v) })

;[5, 1, 4, 7, 8, 1, -4, 0, 3, 7].forEach(v => { ac2(v) })
console.log(ac1.values, ac1.n) // ~> [ 4, 2.286, 0.714, -0.571 ]
console.log(ac2.values, ac2.n)

// Also works when calling with array argument:
// Keep in mind, it updates already defined ac1 values
console.log(ac1([8, 9, 10])) // ~> [8.25, 5.775, 3.4, 1.225]

How it works

Autocovariance online algorithm

Start with a formula for auto-covariance approximation:

covk = (1 / n) Σi=1..n-k(xi+k - μ)(xi - μ)

Then multiply values in brackets:

covk = (1 / n) Σi=1..n-k(xi+kxi + μ2 - μxi - μxi+k)

Multiply the result with (n-k)/(n-k) to get rid of Σ:

covk = α(β + μ2 - μμi - μμi+k)) = α(β + μ(μ - μi - μi+k))

Where:

α = (n - k) / n β = (Σi=1..n-k(xi+kxi)) / (n - k) = avg(xi+kxi) μi = (Σi=1..n-k xi) / (n - k) μi+k = (Σi=1..n-k xi+k) / (n - k)

μ is constant here so (Σi=1..n-k μ) / (n - k) = μ

Using the resulting formula, for each lag k from 0 to K we only need to accumulate:

  • μ - average of full (0, N) data interval; don't depend on k
  • μi - average of (0, N-k)
  • μi+k - average of (k, N) interval
  • β - average of the product of xi+k and xi

To update μ iteratively we can just use a simple algorithm: online-mean

μi is just a lagged (t-k) value of μ: ◆◆◆◆◆ μi(0, N-k), k=0 ◆◆◆◆◇ μi(0, N-k), k=1 ◆◆◆◇◇ μi(0, N-k), k=2 To update μi we just calculate μ and push it to μi and shift all its values to the right.

μi+k and β are "delayed": ◆◆◆◆◆ μi+k(k, N), k=0 ◇◆◆◆◆ μi+k(k, N), k=1 ◇◇◆◆◆ μi+k(k, N), k=2

To update their values we need to track last k observations of x (xlag) and their weights (wlag) using online-lag module xlag: [x[t], x[t-1], x[t-2], ...] wlag: [w[t], w[t-1], w[t-2], ...]

Until we get non-zero weight wlag[k], μi+k and β are zero. Each observation adds its weight to the wlag object shifting its values. So after, let's say 3 observation, 3rd weight in wlag will be 1, that gives us non-zero value for corresponding lag. I definitely should rewrite this to make everything more clear :)