moving-average

Rolling Mean/standard deviation with conditions

一世执手 提交于 2019-12-01 00:32:50
I have a bit of a question about computing the Rolling Mean/standard deviation based on conditions. To be honest it is more of a syntax question, but since I think it is slowing down my code quite a bit I thought I should ask it here to find out what's going on. I have some finance data with columns such as Stock Name , Midquotes etc. and I would like to compute the rolling mean and rolling standard deviation based on the stock. Right now I wish to compute the volatility of each stock, and this is done by taking the rolling standard deviation of the previous 20 midquotes. To this end, after

Rolling Mean/standard deviation with conditions

荒凉一梦 提交于 2019-11-30 19:44:51
问题 I have a bit of a question about computing the Rolling Mean/standard deviation based on conditions. To be honest it is more of a syntax question, but since I think it is slowing down my code quite a bit I thought I should ask it here to find out what's going on. I have some finance data with columns such as Stock Name , Midquotes etc. and I would like to compute the rolling mean and rolling standard deviation based on the stock. Right now I wish to compute the volatility of each stock, and

SQL moving average

谁说胖子不能爱 提交于 2019-11-30 12:00:55
How do you create a moving average in SQL? Current table: Date Clicks 2012-05-01 2,230 2012-05-02 3,150 2012-05-03 5,520 2012-05-04 1,330 2012-05-05 2,260 2012-05-06 3,540 2012-05-07 2,330 Desired table or output: Date Clicks 3 day Moving Average 2012-05-01 2,230 2012-05-02 3,150 2012-05-03 5,520 4,360 2012-05-04 1,330 3,330 2012-05-05 2,260 3,120 2012-05-06 3,540 3,320 2012-05-07 2,330 3,010 One way to do this is to join on the same table a few times. select (Current.Clicks + isnull(P1.Clicks, 0) + isnull(P2.Clicks, 0) + isnull(P3.Clicks, 0)) / 4 as MovingAvg3 from MyTable as Current left

Cumulative sums, moving averages, and SQL “group by” equivalents in R

為{幸葍}努か 提交于 2019-11-30 09:24:39
What's the most efficient way to create a moving average or rolling sum in R? How do you do the rolling function along with a "group by"? While zoo is great, sometimes there are simpler ways. If you data behaves nicely, and is evenly spaced, the embed() function effectively lets you create multiple lagged version of a time series. If you look inside the VARS package for vector auto-regression, you will see that the package author chooses this route. For example, to calculate the 3 period rolling average of x, where x = (1 -> 20)^2: > x <- (1:20)^2 > embed (x, 3) [,1] [,2] [,3] [1,] 9 4 1 [2,]

How to efficiently calculate a moving Standard Deviation

穿精又带淫゛_ 提交于 2019-11-30 01:04:04
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 additional loop to calculate the moving average over the last n periods. This one I could remove by adding the new point value to total_average at the beginning of the loop and removing the i - n point value at the end of the loop. My question now is basically: Can I remove the remaining inner loop in a similar way I managed with the moving average?

pyspark: rolling average using timeseries data

给你一囗甜甜゛ 提交于 2019-11-29 22:35:36
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 bins the data by week. Here's an example: %pyspark import datetime from pyspark.sql import functions as F df1 = sc.parallelize([(17,"2017-03-11T15:27:18+00:00"), (13,"2017-03-11T12:27:18+00:00"), (21,"2017-03-17T11:27:18+00:00")]).toDF(["dollars", "datestring"]) df2 = df1.withColumn('timestampGMT', df1.datestring.cast('timestamp')) w = df2.groupBy(F

SQL moving average

試著忘記壹切 提交于 2019-11-29 17:27:54
问题 How do you create a moving average in SQL? Current table: Date Clicks 2012-05-01 2,230 2012-05-02 3,150 2012-05-03 5,520 2012-05-04 1,330 2012-05-05 2,260 2012-05-06 3,540 2012-05-07 2,330 Desired table or output: Date Clicks 3 day Moving Average 2012-05-01 2,230 2012-05-02 3,150 2012-05-03 5,520 4,360 2012-05-04 1,330 3,330 2012-05-05 2,260 3,120 2012-05-06 3,540 3,320 2012-05-07 2,330 3,010 回答1: One way to do this is to join on the same table a few times. select (Current.Clicks + isnull(P1

Numpy Root-Mean-Squared (RMS) smoothing of a signal

折月煮酒 提交于 2019-11-29 08:53:00
问题 I have a signal of electromyographical data that I am supposed (scientific papers' explicit recommendation) to smooth using RMS. I have the following working code, producing the desired output, but it is way slower than I think it's possible. #!/usr/bin/python import numpy def rms(interval, halfwindow): """ performs the moving-window smoothing of a signal using RMS """ n = len(interval) rms_signal = numpy.zeros(n) for i in range(n): small_index = max(0, i - halfwindow) # intended to avoid

Bigquery SQL for sliding window aggregate

旧街凉风 提交于 2019-11-29 02:20:15
Hi I have a table that looks like this Date Customer Pageviews 2014/03/01 abc 5 2014/03/02 xyz 8 2014/03/03 abc 6 I want to get page view aggregates grouped by week but showing aggregates for past 30 days - (sliding window aggregates with window-size of 30 days for every week) I am using google bigquery EDIT: Gordon - re your comment about "Customer", Actually what I need is slightly more complicated thats why I included customer in the table above. I am looking to get the number of customers who had >n pageviews in a 30day window every week. something like this Date Customers>10 pageviews in

How to calculate simple moving average faster in C#?

人盡茶涼 提交于 2019-11-29 01:30:43
What is the fastest library/algorithm for calculating simple moving average? I wrote my own, but it takes too long on 330 000 items decimal dataset. period / time(ms) 20 / 300; 60 / 1500; 120 / 3500. Here is the code of my method: public decimal MA_Simple(int period, int ii) { if (period != 0 && ii > period) { //stp.Start(); decimal summ = 0; for (int i = ii; i > ii - period; i--) { summ = summ + Data.Close[i]; } summ = summ / period; //stp.Stop(); //if (ii == 1500) System.Windows.Forms.MessageBox.Show((stp.ElapsedTicks * 1000.0) / Stopwatch.Frequency + " ms"); return summ; } else return -1; }