Skip to content

Commit

Permalink
Moveing Average and Weighted Moving Average
Browse files Browse the repository at this point in the history
  • Loading branch information
ptyadana committed Jan 19, 2021
1 parent b875f25 commit 3516113
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/********** Moving Average *************/

/*
We want to know the hourly cpu utilization.
So we can create a sliding windows to get the average cpu utilization of 1 hour.
In hour case, each log is inserted per 5 minutes. So for 1 hour, we need to gather 12 logs (transactions).
NOTE:
OVER statements basically said order by the event time, then given the current row, go back 12 rows.
With that set of date, apply the average function.
*/

SELECT
event_time, server_id,
AVG(cpu_utilization) OVER (ORDER BY event_time ROWS BETWEEN 12 PRECEDING AND CURRENT ROW) AS hourly_cpu_util
FROM time_series.utilization;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********* Weighted Moving Average ************/

/*
The idea is you want to give MORE weight to the MORE RECENT events than the past events.
We want to weight the average temp of last 3 days. so we need to sum up of those last 3 days.
*/
WITH daily_avg_temp AS (
SELECT
DATE_TRUNC('day', event_time) as event_date,
ROUND(AVG(temp_celcius),2) AS avg_temp
FROM time_series.location_temp
GROUP BY DATE_TRUNC('day', event_time)
)
SELECT
event_date, avg_temp,
(SELECT ROUND(avg_temp,2) * 0.5
FROM daily_avg_temp d2
WHERE d2.event_date = d1.event_date - INTERVAL '1' day
) +
(SELECT ROUND(avg_temp,2) * 0.333
FROM daily_avg_temp d3
WHERE d3.event_date = d1.event_date - INTERVAL '2' day
) +
(SELECT ROUND(avg_temp,2) * 0.167
FROM daily_avg_temp d4
WHERE d4.event_date = d1.event_date - INTERVAL '3' day
) AS three_days_weighted_avg_temp
FROM daily_avg_temp d1

0 comments on commit 3516113

Please sign in to comment.