-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
711 lines (576 loc) · 23.8 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
```{r setup,include=FALSE}
# set the knitr options ... for everyone!
# if you unset this, then vignette build bonks. oh, joy.
#opts_knit$set(progress=TRUE)
opts_knit$set(eval.after='fig.cap')
# for a package vignette, you do want to echo.
# opts_chunk$set(echo=FALSE,warning=FALSE,message=FALSE)
opts_chunk$set(warning=FALSE,message=FALSE)
#opts_chunk$set(results="asis")
opts_chunk$set(cache=TRUE,cache.path="cache/")
#opts_chunk$set(fig.path="figure/",dev=c("pdf","cairo_ps"))
opts_chunk$set(fig.path="tools/figure/",dev=c("png"))
opts_chunk$set(fig.width=7,fig.height=6,dpi=100,out.width='700px',out.height='600px')
# doing this means that png files are made of figures;
# the savings is small, and it looks like shit:
#opts_chunk$set(fig.path="figure/",dev=c("png","pdf","cairo_ps"))
#opts_chunk$set(fig.width=4,fig.height=4)
# for figures? this is sweave-specific?
#opts_knit$set(eps=TRUE)
# this would be for figures:
#opts_chunk$set(out.width='.8\\textwidth')
# for text wrapping:
options(width=124,digits=2)
opts_chunk$set(size="small")
opts_chunk$set(tidy=TRUE,tidy.opts=list(width.cutoff=50,keep.blank.line=TRUE))
library(ggplot2)
library(fromo)
library(dplyr)
library(moments)
library(microbenchmark)
# chicken and egg dept:
# [![CRAN](http://www.r-pkg.org/badges/version/fromo)](http://cran.rstudio.com/package=fromo)
# [![Downloads](http://cranlogs.r-pkg.org/badges/fromo?color=brightgreen)](http://www.r-pkg.org/pkg/fromo)
# [![Total](http://cranlogs.r-pkg.org/badges/grand-total/fromo?color=brightgreen)](http://www.r-pkg.org/pkg/fromo)
```
# fromo
[![Build Status](https://github.com/shabbychef/fromo/workflows/R-CMD-check/badge.svg)](https://github.com/shabbychef/fromo/actions)
[![codecov.io](https://codecov.io/github/shabbychef/fromo/graph/badge.svg?token=aUUQLyasWG)](https://app.codecov.io/github/shabbychef/fromo)
![fromo pkg](https://rpkg.net/pub-age-y/fromo/green)
[![Downloads](https://cranlogs.r-pkg.org/badges/fromo)](https://r-pkg.org/pkg/fromo)
[![Total Downloads](https://cranlogs.r-pkg.org/badges/grand-total/fromo?color=brightgreen)](https://r-pkg.org/pkg/fromo)
![RCpp](https://img.shields.io/badge/RCpp-inside-blue.svg)
![is true](https://img.shields.io/badge/saying%20%22monoid%22-makes%20you%20cool-dd33ff.svg)
_Fast Robust Moments -- Pick Three!_
Fast, numerically robust, higher order moments in R, computed via Rcpp, mostly as an exercise to learn Rcpp.
Supports computation on vectors and matrices, and Monoidal append (and unappend) of moments.
Computations are via the Welford-Terriberry algorithm, as described by
[Bennett _et al._](https://www.osti.gov/biblio/1426900)
-- Steven E. Pav, [email protected]
## Installation
This package can be installed
from CRAN,
via [drat](https://github.com/eddelbuettel/drat "drat"), or
from github:
```{r install,eval=FALSE,echo=TRUE}
# via CRAN:
install.packages("fromo")
# via drat:
if (require(drat)) {
drat:::add("shabbychef")
install.packages("fromo")
}
# get snapshot from github (may be buggy)
if (require(devtools)) {
install_github("shabbychef/fromo")
}
```
# Basic Usage
Currently the package functionality can be divided into the following:
* Functions which reduce a vector to an array of moments.
* Functions which take a vector to a matrix of the running moments.
* Functions which transform a vector to some normalized form, like a centered, rescaled, z-scored sample,
or a summarized form, like the running Sharpe or t-stat.
* Functions for computing the covariance of a vector robustly.
* Object representations of moments with join and unjoin methods.
## Summary moments
A function which computes, say, the kurtosis, typically also computes the mean and standard deviation, and has
performed enough computation to easily return the skew. However, the default functions in R for higher order moments
discard these lower order moments. So, for example, if you wish to compute Merten's form for the standard error
of the Sharpe ratio, you have to call separate functions to compute the kurtosis, skew, standard deviation, and mean.
The summary functions in *fromo* return _all_ the moments up to some order, namely the
functions `sd3`, `skew4`, and `kurt5`.
The latter of these, `kurt5` returns an array of length 5 containing
the _excess_ kurtosis, the skewness, the standard deviation, the mean,
and the observation count. (The number in the function name denotes the length of the output.)
Along the same lines, there are summarizing functions that compute centered moments, standardized moments,
and 'raw' cumulants:
* `cent_moments`: return a `k+1`-vector of the `k`th centered moment, the `k-1`th, all the way down to the 2nd (the variance),
then _the mean_ and the observation count.
* `std_moments`: return a `k+1`-vector of the `k`th standardized moment, the `k-1`th, all the way down to the 3rd, then the
_standard deviation_, the mean, and the observation count.
* `cent_cumulants`: computes the centered cumulants (yes, this is redundant, but they are _not_ standardized).
return a `k+1`-vector of the `k`th raw cumulant, the `k-1`th, all the way down to the second, then the mean,
and the observation count.
* `std_cumulants`: computes the standardized (and, of course, centered) cumulants.
return a `k+1`-vector of the `k`th standardized cumulant, all the way down to the third, then the variance, the mean,
and the observation count.
```{r basic_usage,eval=TRUE,echo=TRUE}
library(fromo)
set.seed(12345)
x <- rnorm(1e3,mean=10.0,sd=2.0)
show(cent_moments(x,max_order=4,na_rm=TRUE))
show(std_moments(x,max_order=4,na_rm=TRUE))
show(cent_cumulants(x,max_order=4,na_rm=TRUE))
show(std_cumulants(x,max_order=4,na_rm=TRUE))
```
### Speed
In theory these operations should be just as fast as the default functions, but faster than calling multiple
default functions. Here is a speed comparison of the basic moment computations:
```{r babysteps,eval=TRUE,echo=TRUE}
library(fromo)
library(moments)
library(microbenchmark)
set.seed(1234)
x <- rnorm(1e3)
dumbk <- function(x) { c(kurtosis(x) - 3.0,skewness(x),sd(x),mean(x),length(x)) }
microbenchmark(kurt5(x),
skew4(x),
sd3(x),
dumbk(x),
kurtosis(x),
skewness(x),
sd(x),
mean(x))
x <- rnorm(1e7,mean=1e12)
microbenchmark(kurt5(x),
skew4(x),
sd3(x),
dumbk(x),
kurtosis(x),
skewness(x),
sd(x),
mean(x),times=10L)
# clean up
rm(x)
```
## Weight! Weight!
Many of the methods now support the computation of _weighted_ moments. There
are a few options around weights: whether to check them for negative values,
whether to normalize them to unit mean.
```{r maybesteps,eval=TRUE,echo=TRUE}
library(fromo)
library(moments)
library(microbenchmark)
set.seed(987)
x <- rnorm(1e3)
w <- runif(length(x))
# no weights:
show(cent_moments(x,max_order=4,na_rm=TRUE))
# with weights:
show(cent_moments(x,max_order=4,wts=w,na_rm=TRUE))
# if you turn off weight normalization, the last element is sum(wts):
show(cent_moments(x,max_order=4,wts=w,na_rm=TRUE,normalize_wts=FALSE))
# let's compare for speed!
x <- rnorm(1e7)
w <- runif(length(x))
slow_sd <- function(x,w) {
n0 <- length(x)
mu <- weighted.mean(x,w=w)
sg <- sqrt(sum(w * (x-mu)^2)/(n0 - 1))
c(sg,mu,n0)
}
microbenchmark(sd3(x,wts=w),
slow_sd(x,w))
# clean up
rm(x,w)
```
## Monoid mumbo-jumbo
The `as.centsums` object
performs the summary (centralized) moment computation, and stores the centralized sums.
There is a print method that shows raw, centralized, and standardized moments of the ingested
data.
This object supports concatenation and unconcatenation.
These should satisfy 'monoidal homomorphism', meaning that concatenation
and taking moments commute with each other.
So if you have two vectors, `x1` and `x2`, the following should be equal:
`c(as.centsums(x1,4),as.centsums(x2,4))` and `as.centsums(c(x1,x2),4)`.
Moreover, the following should also be equal:
`as.centsums(c(x1,x2),4) %-% as.centsums(x2,4))` and `as.centsums(x1,4)`.
This is a small step of the way towards fast machine learning
methods (along the lines of Mike Izbicki's [Hlearn library](https://github.com/mikeizbicki/HLearn)).
Some demo code:
```{r monoid1,eval=TRUE,echo=TRUE}
set.seed(12345)
x1 <- runif(1e2)
x2 <- rnorm(1e2,mean=1)
max_ord <- 6L
obj1 <- as.centsums(x1,max_ord)
# display:
show(obj1)
# join them together
obj1 <- as.centsums(x1,max_ord)
obj2 <- as.centsums(x2,max_ord)
obj3 <- as.centsums(c(x1,x2),max_ord)
alt3 <- c(obj1,obj2)
# it commutes!
stopifnot(max(abs(sums(obj3) - sums(alt3))) < 1e-7)
# unjoin them, with this one weird operator:
alt2 <- obj3 %-% obj1
alt1 <- obj3 %-% obj2
stopifnot(max(abs(sums(obj2) - sums(alt2))) < 1e-7)
stopifnot(max(abs(sums(obj1) - sums(alt1))) < 1e-7)
```
We also have 'raw' join and unjoin methods, not nicely wrapped:
```{r monoid2,eval=TRUE,echo=TRUE}
set.seed(123)
x1 <- rnorm(1e3,mean=1)
x2 <- rnorm(1e3,mean=1)
max_ord <- 6L
rs1 <- cent_sums(x1,max_ord)
rs2 <- cent_sums(x2,max_ord)
rs3 <- cent_sums(c(x1,x2),max_ord)
rs3alt <- join_cent_sums(rs1,rs2)
stopifnot(max(abs(rs3 - rs3alt)) < 1e-7)
rs1alt <- unjoin_cent_sums(rs3,rs2)
rs2alt <- unjoin_cent_sums(rs3,rs1)
stopifnot(max(abs(rs1 - rs1alt)) < 1e-7)
stopifnot(max(abs(rs2 - rs2alt)) < 1e-7)
```
### For multivariate input
There is also code for computing co-sums and co-moments, though as of this writing only up to order 2.
Some demo code for the monoidal stuff here:
```{r multi_monoid1,eval=TRUE,echo=TRUE}
set.seed(54321)
x1 <- matrix(rnorm(100*4),ncol=4)
x2 <- matrix(rnorm(100*4),ncol=4)
max_ord <- 2L
obj1 <- as.centcosums(x1,max_ord,na.omit=TRUE)
# display:
show(obj1)
# join them together
obj1 <- as.centcosums(x1,max_ord)
obj2 <- as.centcosums(x2,max_ord)
obj3 <- as.centcosums(rbind(x1,x2),max_ord)
alt3 <- c(obj1,obj2)
# it commutes!
stopifnot(max(abs(cosums(obj3) - cosums(alt3))) < 1e-7)
# unjoin them, with this one weird operator:
alt2 <- obj3 %-% obj1
alt1 <- obj3 %-% obj2
stopifnot(max(abs(cosums(obj2) - cosums(alt2))) < 1e-7)
stopifnot(max(abs(cosums(obj1) - cosums(alt1))) < 1e-7)
```
## Running moments
Since an online algorithm is used, we can compute cumulative running moments. Moreover, we can
_remove_ observations, and thus compute moments over a fixed length lookback window. The code
checks for negative even moments caused by roundoff, and restarts the computation to correct;
periodic recomputation can be forced by an input parameter.
A demonstration:
```{r run1,eval=TRUE,echo=TRUE}
library(fromo)
library(moments)
library(microbenchmark)
set.seed(1234)
x <- rnorm(20)
k5 <- running_kurt5(x,window=10L)
colnames(k5) <- c('excess_kurtosis','skew','stdev','mean','nobs')
k5
# trust but verify
alt5 <- sapply(seq_along(x),function(iii) {
rowi <- max(1,iii - 10 + 1)
kurtosis(x[rowi:iii]) - 3.0 },simplify=TRUE)
cbind(alt5,k5[,1])
```
### See also
If you like rolling computations, do also check out the following packages (I believe they
are all on CRAN):
* [RollingWindow](https://github.com/andrewuhl/rollingwindow), available only from github.
* [RcppRoll](https://github.com/kevinushey/RcppRoll).
* [roll](https://cran.r-project.org/package=roll).
Of these three, it seems that `RollingWindow` implements the optimal algorithm
of reusing computations, while the other two packages gain efficiency from
parallelization and implementation in C++.
## Running adjustment operations
Through template magic, the same code was modified to perform running centering, scaling, z-scoring and so on:
```{r run2,eval=TRUE,echo=TRUE}
library(fromo)
library(moments)
library(microbenchmark)
set.seed(1234)
x <- rnorm(20)
xz <- running_zscored(x,window=10L)
# trust but verify
altz <- sapply(seq_along(x),function(iii) {
rowi <- max(1,iii - 10 + 1)
(x[iii] - mean(x[rowi:iii])) / sd(x[rowi:iii]) },
simplify=TRUE)
cbind(xz,altz)
```
A list of the available running functions:
* `running_centered` : from the current value, subtract the mean over the trailing window.
* `running_scaled`: divide the current value by the standard deviation over the trailing window.
* `running_zscored`: from the current value, subtract the mean then divide by the standard deviation over the trailing window.
* `running_sharpe`: divide the mean by the standard deviation over the trailing window. There is a boolean flag to
also compute and return the Mertens' form of the standard error of the Sharpe ratio over the trailing window in the second
column.
* `running_tstat`: compute the t-stat over the trailing window.
* `running_cumulants`: computes cumulants over the trailing window.
* `running_apx_quantiles`: computes approximate quantiles over the trailing window based on the cumulants and the Cornish-Fisher approximation.
* `running_apx_median`: uses `running_apx_quantiles` to give the approximate median over the trailing window.
### Lookahead
The functions `running_centered`, `running_scaled` and `running_zscored` take an optional `lookahead` parameter that
allows you to peek ahead (or behind if negative) to the computed moments for comparing against the current value. These
are not supported for `running_sharpe` or `running_tstat` because they do not have an idea of the 'current value'.
Here is an example of using the lookahead to z-score some data, compared to a purely time-safe lookback. Around a timestamp
of 1000, you can see the difference in outcomes from the two methods:
```{r toy_zscore,cache=TRUE,eval=TRUE,echo=TRUE,dpi=200,out.width='600px',out.height='500px'}
set.seed(1235)
z <- rnorm(1500,mean=0,sd=0.090)
x <- exp(cumsum(z)) - 1
xz_look <- running_zscored(x,window=301,lookahead=150)
xz_safe <- running_zscored(x,window=301,lookahead=0)
df <- data.frame(timestamp=seq_along(x),raw=x,lookahead=xz_look,lookback=xz_safe)
library(tidyr)
gdf <- gather(df,key='smoothing',value='x',-timestamp)
library(ggplot2)
ph <- ggplot(gdf,aes(x=timestamp,y=x,group=smoothing,colour=smoothing)) +
geom_line()
print(ph)
```
### Running Bivariate Computations
The package now supports operations on a running window of two aligned input
series, $x$ and $y$, and can compute the correlation, covariance, and OLS
regression on the two over a running windo. The following are supported:
* `running_correlation`: the correlation over the trailing window.
* `running_covariance`: the covariance of the two series.
* `running_covariance3`: the full variance-covariance of the two series,
returning matrix with three columns.
* `running_regression_intercept`: the intercept of the OLS regression fit over the
trailing window.
* `running_regression_slope`: the slope of the OLS regression fit over the
trailing window.
* `running_regression_fit`: a matrix of the intercept and slope of the
OLS regression fit over the trailing window.
* `running_regression_diagnostics`: a matrix of the intercept and slope of the
OLS regression fit, the regression standard error and the standard errors of
the intercept and slope, over the trailing window.
### Time-Based Running Computations
The standard running moments computations listed above work on a running window
of a fixed number of observations. However, sometimes one needs to compute
running moments over a different kind of window. The most common form of this
is over time-based windows. For example, the following computations:
* Compute the total sales over the past six months, as of every day.
* Compute the volatility of an asset's daily returns, over a yearly window,
computed at the end of every trading month.
These are now supported in `fromo` via the `t_running` class of functions,
which are like the `running` functions, but accept also the 'times' at which
the input are marked, and optionally also the times at which one will
'look back' to perform the computations. The times can be computed implicitly
as the cumulative sum of given (non-negative) time deltas.
The `t_running` functions now also include the bivariate computations
`t_running_correlation`, `t_running_covariance`, `t_running_covariance3`,
`t_running_regression_intercept`, `t_running_regression_slope`, `t_running_regression_fit`,
`t_running_regression_diagnostics`.
Here is an example of computing the volatility of daily 'returns' of the
Fama French Market factor, based on a one year window, computed at month ends:
```{r trun_sd_testing,eval=TRUE,echo=TRUE}
# devtools::install_github('shabbychef/aqfb_data')
library(aqfb.data)
library(fromo)
# daily 'returns' of Fama French 4 factors
data(dff4)
# compute month end dates:
library(lubridate)
mo_ends <- unique(lubridate::ceiling_date(index(dff4),'month') %m-% days(1))
res <- t_running_sd3(dff4$Mkt,time=index(dff4),window=365.25,min_df=180,lb_time=mo_ends)
df <- cbind(data.frame(mo_ends),data.frame(res))
colnames(df) <- c('date','sd','mean','num_days')
knitr::kable(tail(df),row.names=FALSE)
```
And the plot of the time series:
```{r trun_testing,dependson=c('trun_sd_testing'),eval=TRUE,echo=TRUE}
library(ggplot2)
library(scales)
ph <- df %>%
ggplot(aes(date,1e-2 * sd)) +
geom_line() + geom_point(alpha=0.1) +
scale_y_continuous(labels=scales::percent) +
labs(x='lookback date',y='standard deviation of percent returns',
title='rolling 1 year volatility of daily Mkt factor returns, computed monthly')
print(ph)
```
Now consider the running correlation of the "SMB" factor against the "Mkt"
factor over time:
```{r trun_corr_testing,dependson=c('trun_sd_testing'),eval=TRUE,echo=TRUE}
rho <- t_running_correlation(x=dff4$Mkt,y=dff4$SMB,time=index(dff4),window=365.25,min_df=180,lb_time=mo_ends)
library(ggplot2)
library(scales)
ph <- cbind(data.frame(mo_ends),data.frame(rho)) %>%
setNames(c('date','rho')) %>%
ggplot(aes(date,rho)) +
geom_line() + geom_point(alpha=0.1) +
geom_hline(yintercept=0,linetype=2,alpha=0.5) +
labs(x='lookback date',y=expression(rho),
title='Rolling 1 year correlation of daily SMB and Mkt factor returns, computed monthly')
print(ph)
```
---------------
## Efficiency
We make every attempt to balance numerical robustness, computational efficiency and memory usage. As a bit of
strawman-bashing, here we microbenchmark the running Z-score computation against the naive algorithm:
```{r strawman_timing,eval=TRUE,echo=TRUE}
library(fromo)
library(moments)
library(microbenchmark)
set.seed(4422)
x <- rnorm(1e4)
dumb_zscore <- function(x,window) {
altz <- sapply(seq_along(x),function(iii) {
rowi <- max(1,iii - window + 1)
xrang <- x[rowi:iii]
(x[iii] - mean(xrang)) / sd(xrang)
},simplify=TRUE)
}
val1 <- running_zscored(x,250)
val2 <- dumb_zscore(x,250)
stopifnot(max(abs(val1-val2),na.rm=TRUE) <= 1e-14)
microbenchmark(running_zscored(x,250),
dumb_zscore(x,250))
```
### Timing against the roll package
More seriously, here we compare the `running_sd3` function, which computes
the standard deviation, mean and number of elements with the
`roll_sd` and `roll_mean` functions from the
[roll](https://cran.r-project.org/package=roll) package.
```{r some_more_timing,eval=TRUE,echo=TRUE}
# dare I?
library(fromo)
library(microbenchmark)
library(roll)
set.seed(4422)
x <- rnorm(1e5)
xm <- matrix(x)
v1 <- running_sd3(xm,250)
rsd <- roll::roll_sd(xm,250)
rmu <- roll::roll_mean(xm,250)
# compute error on the 1000th row:
stopifnot(max(abs(v1[1000,] - c(rsd[1000],rmu[1000],250))) < 1e-14)
# now timings:
microbenchmark(running_sd3(xm,250),
roll::roll_mean(xm,250),
roll::roll_sd(xm,250))
```
OK, that's not a fair comparison: `roll_mean` is optimized to work columwise on
a matrix. Let's unbash this strawman. I create a function using
`fromo::running_sd3` to compute a running mean or running standard deviation
columnwise on a matrix, then compare _that_ to `roll_mean` and `roll_sd`:
```{r some_more_timing_again,eval=TRUE,echo=TRUE}
library(fromo)
library(microbenchmark)
library(roll)
set.seed(4422)
xm <- matrix(rnorm(4e5),ncol=100)
fromo_sd <- function(x,wins) {
apply(x,2,function(xc) { running_sd3(xc,wins)[,1] })
}
fromo_mu <- function(x,wins) {
apply(x,2,function(xc) { running_sd3(xc,wins)[,2] })
}
wins <- 1000
v1 <- fromo_sd(xm,wins)
rsd <- roll::roll_sd(xm,wins,min_obs=3)
v2 <- fromo_mu(xm,wins)
rmu <- roll::roll_mean(xm,wins)
# compute error on the 2000th row:
stopifnot(max(abs(v1[2000,] - rsd[2000,])) < 1e-14)
stopifnot(max(abs(v2[2000,] - rmu[2000,])) < 1e-14)
# now timings:
# note fromo_mu and fromo_sd do exactly the same work, so only time one of them
microbenchmark(fromo_sd(xm,wins),
roll::roll_mean(xm,wins),
roll::roll_sd(xm,wins),
times=50L)
```
I suspect, however, that `roll_mean` is literally recomputing moments over the
entire window for every cell of the output, instead of reusing computations,
which `fromo` mostly does:
```{r catch_roll_at_four,eval=TRUE,echo=TRUE}
library(roll)
library(microbenchmark)
set.seed(91823)
xm <- matrix(rnorm(2e5),ncol=10)
fromo_mu <- function(x,wins,...) {
apply(x,2,function(xc) { running_sd3(xc,wins,...)[,2] })
}
microbenchmark(roll::roll_mean(xm,10,min_obs=3),
roll::roll_mean(xm,100,min_obs=3),
roll::roll_mean(xm,1000,min_obs=3),
roll::roll_mean(xm,10000,min_obs=3),
fromo_mu(xm,10,min_df=3),
fromo_mu(xm,100,min_df=3),
fromo_mu(xm,1000,min_df=3),
fromo_mu(xm,10000,min_df=3),
times=100L)
```
The runtime for operations from `roll` grow with the window size.
The equivalent operations from `fromo` also consume more time for longer windows.
In theory they would be invariant with respect to window size, but I coded them
to 'restart' the computation periodically for improved accuracy. The user has control
over how often this happens, in order to balance speed and accuracy. Here I set
that parameter very large to show that runtimes need not grow with window size:
```{r catch_fromo_at_five,eval=TRUE,echo=TRUE}
library(fromo)
library(microbenchmark)
set.seed(91823)
xm <- matrix(rnorm(2e5),ncol=10)
fromo_mu <- function(x,wins,...) {
apply(x,2,function(xc) { running_sd3(xc,wins,...)[,2] })
}
rp <- 1L + nrow(xm)
microbenchmark(
fromo_mu(xm,10,min_df=3,restart_period=rp),
fromo_mu(xm,100,min_df=3,restart_period=rp),
fromo_mu(xm,1000,min_df=3,restart_period=rp),
fromo_mu(xm,10000,min_df=3,restart_period=rp),
times=100L)
```
Here are some more benchmarks, also against the `rollingWindow` package, for
running sums:
```{r time_running_sums,eval=TRUE,echo=TRUE}
library(microbenchmark)
library(fromo)
library(RollingWindow)
library(roll)
set.seed(12345)
x <- rnorm(10000)
xm <- matrix(x)
wins <- 1000
# run fun on each wins sized window...
silly_fun <- function(x,wins,fun,...) {
xout <- rep(NA,length(x))
for (iii in seq_along(x)) {
xout[iii] <- fun(x[max(1,iii-wins+1):iii],...)
}
xout
}
vals <- list(running_sum(x,wins,na_rm=FALSE),
RollingWindow::RollingSum(x,wins,na_method='ignore'),
roll::roll_sum(xm,wins),
silly_fun(x,wins,sum,na.rm=FALSE))
# check all equal?
stopifnot(max(unlist(lapply(vals[2:length(vals)],function(av) {
err <- vals[[1]] - av
max(abs(err[wins:length(err)]),na.rm=TRUE) }))) < 1e-12)
# benchmark it
microbenchmark(running_sum(x,wins,na_rm=FALSE),
RollingWindow::RollingSum(x,wins),
running_sum(x,wins,na_rm=TRUE),
RollingWindow::RollingSum(x,wins,na_method='ignore'),
roll::roll_sum(xm,wins))
```
And running means:
```{r time_running_means,eval=TRUE,echo=TRUE}
library(microbenchmark)
library(fromo)
library(RollingWindow)
library(roll)
set.seed(12345)
x <- rnorm(10000)
xm <- matrix(x)
wins <- 1000
vals <- list(running_mean(x,wins,na_rm=FALSE),
RollingWindow::RollingMean(x,wins,na_method='ignore'),
roll::roll_mean(xm,wins),
silly_fun(x,wins,mean,na.rm=FALSE))
# check all equal?
stopifnot(max(unlist(lapply(vals[2:length(vals)],function(av) {
err <- vals[[1]] - av
max(abs(err[wins:length(err)]),na.rm=TRUE) }))) < 1e-12)
# benchmark it:
microbenchmark(running_mean(x,wins,na_rm=FALSE,restart_period=100000),
RollingWindow::RollingMean(x,wins),
running_mean(x,wins,na_rm=TRUE,restart_period=100000),
RollingWindow::RollingMean(x,wins,na_method='ignore'),
roll::roll_mean(xm,wins))
```