rolling-computation

CAPM.beta rollapply

喜欢而已 提交于 2019-12-08 13:31:41
问题 I have already successfully calculated my rolling correlations in my xts object with x <- cbind(market_return,stock_returns) rollcor_3year <- rollapplyr( x, width=width_cor,function(x) cor(x[,1],x[,-1], use="pairwise.complete.obs"),by.column=FALSE) The correlation was later used to calculate rolling Betas. Now I found the function CAPM.beta from the PerformanceAnalytics package and I wonder why I cannot use beta <- rollapplyr(x,width=width_cor,function(x) CAPM.beta(x[,1],x[,-1]),by.column

Pandas - Using `.rolling()` on multiple columns

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 01:03:45
问题 Consider a pandas DataFrame which looks like the one below A B C 0 0.63 1.12 1.73 1 2.20 -2.16 -0.13 2 0.97 -0.68 1.09 3 -0.78 -1.22 0.96 4 -0.06 -0.02 2.18 I would like to use the function .rolling() to perform the following calculation for t = 0,1,2 : Select the rows from t to t+2 Take the 9 values contained in those 3 rows, from all the columns. Call this set S Compute the 75th percentile of S (or other summary statistics about S ) For instance, for t = 1 we have S = { 2.2 , -2.16, -0.13,

Fast rolling mean + summarize

為{幸葍}努か 提交于 2019-12-07 08:47:40
问题 In R, I am trying to do a very fast rolling mean of a large vector (up to 400k elements) using different window widths, then for each window width summarize the data by the maximum of each year. The example below will hopefully be clear. I have tried several approaches, and the fastest up to now seems to be using roll_mean from the package RcppRoll for the running mean, and aggregate for picking the maximum. Please note that memory requirement is a concern: the version below requires very

“Rolling” Regression in R

二次信任 提交于 2019-12-06 15:07:50
问题 Say I want to run regressions per group whereby I want to use the last 5 year data as input for that regression. Then, for each next year, I would like to "shift" the input for that regression by one year (i.e., 4 observations). From those regressions I want to extract both the R2 and the fitted values/residuals, which I then need in subsequent regressions that follow similar notions. I have some code working using loops, but it is not really elegant nor efficient for large datasets. I assume

Pandas - Using `.rolling()` on multiple columns

老子叫甜甜 提交于 2019-12-06 14:08:57
Consider a pandas DataFrame which looks like the one below A B C 0 0.63 1.12 1.73 1 2.20 -2.16 -0.13 2 0.97 -0.68 1.09 3 -0.78 -1.22 0.96 4 -0.06 -0.02 2.18 I would like to use the function .rolling() to perform the following calculation for t = 0,1,2 : Select the rows from t to t+2 Take the 9 values contained in those 3 rows, from all the columns. Call this set S Compute the 75th percentile of S (or other summary statistics about S ) For instance, for t = 1 we have S = { 2.2 , -2.16, -0.13, 0.97, -0.68, 1.09, -0.78, -1.22, 0.96 } and the 75th percentile is 0.97. I couldn't find a way to make

Applying lambda function to a pandas rolling window series

。_饼干妹妹 提交于 2019-12-06 04:15:48
I have a function which takes an array and a value, and returns a value. I would like to apply it to my Series s on a rolling basis, so the array is always the rolling window. Here's a minimal example of what I've tried (unsuccessfully), using np.random.choice in place of my real function. I find lots of examples for finding rolling means and other built-in functions, but can't get it to work for my arbitrary lambda function. s = pd.Series([1,2,3,4,5,6,7,8,9]) rolling_window = s.rolling(3) First attempt: new_values = s.apply(lambda x: np.random.choice(rolling_window, size=1)) ValueError: a [a

Rolling percentage add along column

懵懂的女人 提交于 2019-12-05 23:59:55
I feel this should be easy in base R but I just can't figure it out. I have a simple dataframe, let's say it looks like this tbl <- read.table(text = "Field1 Field2 100 200 150 180 200 160 280 250 300 300 300 250", header = TRUE) Now, what I want to do is create a function that will apply a rolling % addition, something like: fn <- function(tbl, pct) {} which accepts the dataframe above as tbl . It adds a percentage fraction of the current row to the NEXT row down based on pct , and rolls this almost in a cumulative fashion. For example, fn(tbl$Field1, 0.1) would generate the following results

“Rolling” Regression in R

断了今生、忘了曾经 提交于 2019-12-04 20:00:53
Say I want to run regressions per group whereby I want to use the last 5 year data as input for that regression. Then, for each next year, I would like to "shift" the input for that regression by one year (i.e., 4 observations). From those regressions I want to extract both the R2 and the fitted values/residuals, which I then need in subsequent regressions that follow similar notions. I have some code working using loops, but it is not really elegant nor efficient for large datasets. I assume there must be a nice plyr way for resolving this issue. # libraries # library(dplyr) library(broom) #

Rolling Average to calculate rainfall intensity

拈花ヽ惹草 提交于 2019-12-04 15:19:14
I have some real rainfall data recorded as the date and time, and the accumulated number of tips on a tipping bucket rain-gauge. The tipping bucket represents 0.5mm of rainfall. I want to cycle through the file and determine the variation in intensity (rainfall/time) So I need a rolling average over multiple fixed time frames: So I want to accumulate rainfall, until 5minutes of rain is accumulated and determine the intensity in mm/hour. So if 3mm is recorded in 5min it is equal to 3/5*60 = 36mm/hr. the same rainfall over 10 minutes would be 18mm/hr... So if I have rainfall over several hours I

r calculating rolling average with window based on value (not number of rows or date/time variable)

为君一笑 提交于 2019-12-01 17:59:03
I'm quite new to all the packages meant for calculating rolling averages in R and I hope you can show me in the right direction. I have the following data as an example: ms <- c(300, 300, 300, 301, 303, 305, 305, 306, 308, 310, 310, 311, 312, 314, 315, 315, 316, 316, 316, 317, 318, 320, 320, 321, 322, 324, 328, 329, 330, 330, 330, 332, 332, 334, 334, 335, 335, 336, 336, 337, 338, 338, 338, 340, 340, 341, 342, 342, 342, 342) correct <- c(1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0) df <-