Questions tagged [moving-average]

A calculation of the average value of the most recent (within some window) values in a time series, rather than the average of the entire series.

Moving average is a calculation of the average value of the most recent (within some window) values in a time series, rather than the average of the entire series.

1034 questions
293
votes
30 answers

Moving average or running mean

Is there a SciPy function or NumPy function or module for Python that calculates the running mean of a 1D array given a specific window?
Shejo284
  • 4,541
  • 6
  • 32
  • 44
245
votes
18 answers

Calculating moving average

I'm trying to use R to calculate the moving average over a series of values in a matrix. There doesn't seem to be a built-in function in R that will allow me to calculate moving averages. Do any packages provide one? Or do I need to write my own?
Jared
  • 39,513
  • 29
  • 110
  • 145
226
votes
19 answers

How to calculate rolling / moving average using python + NumPy / SciPy?

There seems to be no function that simply calculates the moving average on numpy/scipy, leading to convoluted solutions. My question is two-fold: What's the easiest way to (correctly) implement a moving average with numpy? Since this seems…
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
169
votes
8 answers

How to calculate moving average without keeping the count and data-total?

I am trying to find a way to calculate a moving cumulative average without storing the count and total data that is received so far. I came up with two algorithms but both need to store the count: new average = ((old count * old data) + next data)…
user1705674
  • 1,691
  • 2
  • 11
  • 4
109
votes
4 answers

Moving Average Pandas

I would like to add a moving average calculation to my exchange time series. Original data from Quandl Exchange = Quandl.get("BUNDESBANK/BBEX3_D_SEK_USD_CA_AC_000", authtoken="xxxxxxx") # Value # Date …
Martin598
  • 1,491
  • 3
  • 13
  • 20
93
votes
2 answers

Understanding NumPy's Convolve

When calculating a simple moving average, numpy.convolve appears to do the job. Question: How is the calculation done when you use np.convolve(values, weights, 'valid')? When the docs mentioned convolution product is only given for points where the…
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
66
votes
3 answers

How to efficiently compute average on the fly (moving average)?

I come up with this n=1; curAvg = 0; loop{ curAvg = curAvg + (newNum - curAvg)/n; n++; } I think highlights of this way are: - It avoids big numbers (and possible overflow if you would sum and then divide) - you save one register (not need to…
Vit Bernatik
  • 3,566
  • 2
  • 34
  • 40
54
votes
11 answers

Calculate rolling / moving average in C++

I know this is achievable with boost as per: Using boost::accumulators, how can I reset a rolling window size, does it keep extra history? But I really would like to avoid using boost. I have googled and not found any suitable or readable…
goji
  • 6,911
  • 3
  • 42
  • 59
49
votes
4 answers

pyspark: rolling average using timeseries data

I have a dataset consisting of a timestamp column and a dollars column. I would like to find the average number of dollars per week ending at the timestamp of each row. I was initially looking at the pyspark.sql.functions.window function, but that…
Bob Swain
  • 3,052
  • 3
  • 17
  • 28
49
votes
5 answers

Smoothing values over time: moving average or something better?

I'm coding something at the moment where I'm taking a bunch of values over time from a hardware compass. This compass is very accurate and updates very often, with the result that if it jiggles slightly, I end up with the odd value that's wildly…
Henry Cooke
  • 2,563
  • 2
  • 23
  • 22
42
votes
3 answers

Apache Spark Moving Average

I have a huge file in HDFS having Time Series data points (Yahoo Stock prices). I want to find the moving average of the Time Series how do I go about writing the Apache Spark job to do that .
Ahmed Shabib
  • 687
  • 1
  • 8
  • 16
41
votes
6 answers

Python Pandas: Calculate moving average within group

I have a dataframe containing time series for 100 objects: object period value 1 1 24 1 2 67 ... 1 1000 56 2 1 59 2 2 46 ... 2 1000 64 3 1 54 ... 100 1 …
Alexandr Kapshuk
  • 1,380
  • 2
  • 13
  • 29
32
votes
2 answers

Moving average in postgresql

I have the following table in my Postgresql 9.1 database: select * from ro; date | shop_id | amount -----------+----------+-------- 2013-02-07 | 1001 | 3 2013-01-31 | 1001 | 2 2013-01-24 | 1001 | 1 2013-01-17 | …
Glicious
  • 421
  • 1
  • 5
  • 13
31
votes
2 answers

SQL Query for 7 Day Rolling Average in SQL Server

I have a table of hourly product usage (how many times the product is used) data – ID (bigint)| ProductId (tinyint)| Date (int - YYYYMMDD) | Hour (tinyint)| UsageCount (int) #|1 | 20140901 | 0 | 10 #|1 | 20140901 | 1 | 15 #|1 | 20140902 | 5 |…
Andy T
  • 1,355
  • 2
  • 19
  • 30
24
votes
5 answers

How to efficiently calculate a moving Standard Deviation

Below you can see my C# method to calculate Bollinger Bands for each point (moving average, up band, down band). As you can see this method uses 2 for loops to calculate the moving standard deviation using the moving average. It used to contain an…
1
2 3
68 69