Skip to main content

Moving averages

Project description

Why??

Numpy does not include a built-in moving average function as of yet. Most solutions are tedious and complicated. This solution is very nice. This operates similar to the Wolfram Language’s MovingAverage[] function, but has the advantage that it can specify axis (Wolfram gets away with f[#]&/@, but that’s a story for another day).

Usage

See docs for examples. But essentially, the usage is just:

from mvgavg import mvgavg

mvgavg(array, n, axis=0, weights = [list of weights])
mvgavg(array, n, axis=0, binning = bool)

What This Code Does

Consider the array [a,b,c,d, … x, y, z]. Taking the moving average of length n=3 results in a new array wherein the first element is (a+b+c)/3, the second (b+c+d)/3 and so on. This can be done for arrays of arbitrary depth, so if you put in a time series [[t1,x1],[t2,x2],…], a moving average of along axis=0 (the default) will give you back the moving average of all t’s along side all x’s.

The output is a vector that is the same size and shape but has been shortened on the axis axis by a length of n-1.

Axis and Weight Options

Axis: The axis lets you operate at an increased depth, so using the axis=1 parameter, you can operate horizontally across columns with your moving average. You can do this as deep as the array itself.

Weights allow you to customize the function. The default weight (None) is described above (all values are given the same weight). One other weight you can use is ‘pascal’. This gives a nth-line pascal’s triangle weight, so for instance, for n = 5, the ith item in the array is (i + 4j + 6k + 4l + m) / 16, since the 5th line in pascal’s triangle is [1,4,6,4,1]. The other default weight options are triangle (i.e. triangle(5)=[1,2,3,2,1], and quadratic is triangle squared. Or make your own list and put whatever you want in here (make sure len(weights) == n)

Binning

Binning greatly shortens the array and loses some precision. This is desirable if you have an enormous amount of data and don’t need to preserve every point. The difference between a default moving average and a binned moving average is that, for an input array [a b c d e f…] and an output [A B C D E F…] over distance 3, the default moving average looks like this:

A = [a b c]/3
B =   [b c d]/3
C =     [c d e]/3
D =       [d e f]/3
E =         [e f g]/3
....

but if binning = True:

A = [a b c]/3
B =       [d e f]/3
C =             [g h i]/3
.....

As you can see the output array is greatly shortened. As arrays get very large, binning can become orders of magnitude faster to compute, and if you don’t need the resolution of a moving average, are a much more efficient way of handling data, because you may end up throwing most of your data away later when you go to plot it up anyways.

Why Is This Function Important?

Moving averages smooth data and illuminate trends that otherwise may not be as apparent. They also help with reverse interpolation when different x’s yield the same y. The reasons for using moving averages are myriad, so a decent arbitrary-depth moving average function with numpy-speed and arbitrary weighting needed to be written.

Acknowledgements

Credit to @fnjn on github for the sliding window function.

Misc

If you have issues or questions, open an issue on Github at https://github.com/NGeorgescu/python-moving-average or if you think there’s some functionality that you would like to see, or if you have a faster algorithm

Thanks and Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mvgavg-0.0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

mvgavg-0.0.2-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file mvgavg-0.0.2.tar.gz.

File metadata

  • Download URL: mvgavg-0.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for mvgavg-0.0.2.tar.gz
Algorithm Hash digest
SHA256 57a559389a8ade11066d105bafd473cdaad845b6096bda0d185d5d9b72aea246
MD5 b0f7abb1c84aa4a18ff06338a94f23aa
BLAKE2b-256 89952013d8738e75d4b1251ac26b9bfdb058ab60b4fd7cf73f9e9e37adf9ab4b

See more details on using hashes here.

File details

Details for the file mvgavg-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mvgavg-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for mvgavg-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d98f784bfe5490e83a56415f27f3f1376ff54c0ae19bcda49b401f4f04b244cf
MD5 4608a3c9cba2739ff1e5608d95d8d649
BLAKE2b-256 c59c96857d16a7824b1a68e7f0e6bb6aee293bfad91955c86ec9971efd9b70e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page