Skip to content

Commit d392b41

Browse files
author
OVVO-Financial
committed
NNS 10.9.4 Beta
1 parent ce05056 commit d392b41

14 files changed

+267
-165
lines changed

NNS_10.9.4.tar.gz

315 KB
Binary file not shown.

NNS_10.9.4.zip

840 Bytes
Binary file not shown.

R/NNS_MC.R

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param exp numeric; \code{1} default will exponentially weight maximum rho value if \code{exp > 1}. Shrinks values towards \code{upper_rho}.
1111
#' @param type options("spearman", "pearson", "NNScor", "NNSdep"); \code{type = "spearman"}(default) dependence metric desired.
1212
#' @param drift logical; \code{drift = TRUE} (default) preserves the drift of the original series.
13-
#' @param target_drift numerical; code{NULL} (default) Specifies the desired drift when \code{drift = TRUE}, i.e. a risk-free rate of return.
13+
#' @param target_drift numerical; code{target_drift = NULL} (default) Specifies the desired drift when \code{drift = TRUE}, i.e. a risk-free rate of return.
1414
#' @param xmin numeric; the lower limit for the left tail.
1515
#' @param xmax numeric; the upper limit for the right tail.
1616
#' @param ... possible additional arguments to be passed to \link{NNS.meboot}.
@@ -52,13 +52,18 @@ NNS.MC <- function(x,
5252

5353
exp_rhos <- rev(c((neg_rhos^exp)*-1, pos_rhos^(1/exp)))
5454

55+
if(is.null(target_drift)){
56+
n <- length(x)
57+
orig_coef <- fast_lm(1:n, x)$coef
58+
orig_intercept <- orig_coef[1]
59+
orig_drift <- orig_coef[2]
60+
target_drift <- orig_drift
61+
}
5562

56-
samples <- suppressWarnings(NNS.meboot(x = x, reps = reps, rho = exp_rhos, type = type,
57-
drift = drift, target_drift = target_drift, xmin = xmin, xmax = xmax, ...))
5863

59-
replicates <- samples["replicates",]
60-
61-
rm(samples)
64+
replicates <- suppressWarnings(NNS.meboot(x = x, reps = reps, rho = exp_rhos, type = type, drift = drift,
65+
target_drift = target_drift, xmin = xmin, xmax = xmax, ...))["replicates",]
66+
6267

6368
ensemble <- Rfast::rowmeans(do.call(cbind, replicates))
6469

R/NNS_meboot.R

+27-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @param rho numeric [-1,1] (vectorized); A \code{rho} must be provided, otherwise a blank list will be returned.
88
#' @param type options("spearman", "pearson", "NNScor", "NNSdep"); \code{type = "spearman"}(default) dependence metric desired.
99
#' @param drift logical; \code{drift = TRUE} (default) preserves the drift of the original series.
10-
#' @param target_drift numerical; code{NULL} (default) Specifies the desired drift when \code{drift = TRUE}, i.e. a risk-free rate of return.
10+
#' @param target_drift numerical; code{target_drift = NULL} (default) Specifies the desired drift when \code{drift = TRUE}, i.e. a risk-free rate of return.
1111
#' @param trim numeric [0,1]; The mean trimming proportion, defaults to \code{trim = 0.1}.
1212
#' @param xmin numeric; the lower limit for the left tail.
1313
#' @param xmax numeric; the upper limit for the right tail.
@@ -41,6 +41,8 @@
4141
#' \item{kappa} scale adjustment to the variance of ME density.
4242
#' \item{elaps} elapsed time.
4343
#' }
44+
#'
45+
#' @note Vectorized \code{rho} and \code{drift} parameters will not vectorize both simultaneously. Also, do not specify \code{target_drift = NULL}.
4446
#'
4547
#' @references
4648
#' \itemize{
@@ -57,7 +59,7 @@
5759
#' @examples
5860
#' \dontrun{
5961
#' # To generate an orthogonal rank correlated time-series to AirPassengers
60-
#' boots <- NNS.meboot(AirPassengers, reps=100, rho = 0, xmin = 0)
62+
#' boots <- NNS.meboot(AirPassengers, reps = 100, rho = 0, xmin = 0)
6163
#'
6264
#' # Verify correlation of replicates ensemble to original
6365
#' cor(boots["ensemble",]$ensemble, AirPassengers, method = "spearman")
@@ -67,6 +69,17 @@
6769
#'
6870
#' # Plot ensemble
6971
#' lines(boots["ensemble",]$ensemble, lwd = 3)
72+
#'
73+
#'
74+
#' ### Vectorized drift with a single rho
75+
#' boots <- NNS.meboot(AirPassengers, reps = 100, rho = 0, xmin = 0, target_drift = c(1,7))
76+
#' matplot(do.call(cbind, boots["replicates", ]), type = "l")
77+
#' lines(1:length(AirPassengers), AirPassengers, lwd = 3, col = "red")
78+
#'
79+
#' ### Vectorized rho with a single target drift
80+
#' boots <- NNS.meboot(AirPassengers, reps = 100, rho = c(0, .5, 1), xmin = 0, target_drift = 3)
81+
#' matplot(do.call(cbind, boots["replicates", ]), type = "l")
82+
#' lines(1:length(AirPassengers), AirPassengers, lwd = 3, col = "red")
7083
#' }
7184
#' @export
7285

@@ -107,7 +120,14 @@
107120
ordxx <- order(x)
108121

109122

110-
### Fred Viole SUGGESTION PART 1 of 2
123+
if(is.null(target_drift)){
124+
orig_coef <- fast_lm(1:n, x)$coef
125+
orig_intercept <- orig_coef[1]
126+
orig_drift <- orig_coef[2]
127+
target_drift <- orig_drift
128+
}
129+
130+
111131

112132
if(rho < 1){
113133
if(rho < -0.5) ordxx_2 <- rev(ordxx) else ordxx_2 <- order(ordxx)
@@ -194,11 +214,8 @@
194214

195215
ensemble[ordxx,] <- qseq
196216

197-
198-
### Fred Viole SUGGESTION PART 2 of 2
199-
### Average two ordxx ensemble matrices
200217

201-
if(rho<1){
218+
if(any(rho<1)){
202219
matrix2 = matrix(0, nrow=length(x), ncol = reps)
203220
matrix2[ordxx_2,] = qseq
204221

@@ -240,9 +257,9 @@
240257

241258
new_coef <- apply(ensemble, 2, function(i) fast_lm(1:n, i)$coef)
242259
slopes <- new_coef[2,]
243-
260+
244261
if(drift){
245-
if(is.null(target_drift)) new_slopes <- (orig_drift - slopes) else new_slopes <- (target_drift - slopes)
262+
new_slopes <- (target_drift - slopes)
246263
ensemble <- ensemble + t(t(sapply(new_slopes, function(slope) cumsum(rep(slope, n)))))
247264

248265
new_intercepts <- orig_intercept - new_coef[1,]
@@ -303,4 +320,4 @@
303320
return(final)
304321
}
305322

306-
NNS.meboot <- Vectorize(NNS.meboot, vectorize.args = "rho")
323+
NNS.meboot <- Vectorize(NNS.meboot, vectorize.args = c("rho", "target_drift"))

doc/NNSvignette_Sampling.R

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ legend('left', legend = c('ecdf', 'LPM.ratio'), fill=c('black','red'), border=NA
151151
# rho = 1 rho = 0.5 rho = -0.5 rho = -1
152152
# 1.0000000 0.4989619 -0.4984818 -0.9779778
153153

154+
## ----tgt_drift, fig.align='center', fig.width=8, fig.height=8, eval=FALSE-----
155+
# boots = NNS.MC(x, reps = 1, lower_rho = -1, upper_rho = 1, by = .5, target_drift = 0.05)$replicates
156+
# reps = do.call(cbind, boots)
157+
#
158+
# plot(x, type = "l", lwd = 3, ylim = c(min(reps), max(reps)))
159+
# matplot(reps, type = "l", col = rainbow(length(boots)), add = TRUE)
160+
154161
## ----multisim, eval=FALSE-----------------------------------------------------
155162
# set.seed(123)
156163
# x <- rnorm(1000); y <- rnorm(1000); z <- rnorm(1000)

doc/NNSvignette_Sampling.Rmd

+16-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,22 @@ sapply(boots, function(r) cor(r, x, method = "spearman"))
227227
1.0000000 0.4989619 -0.4984818 -0.9779778
228228
```
229229

230-
More replicates and ensembles thereof can be generated for any number of $\rho$ values. Please see the full **`NNS.meboot`** and **`NNS.MC`** argument documentation.
230+
More replicates and ensembles thereof can be generated for any number of $\rho$ values.
231+
232+
### `target_drift` Specification
233+
We can also specify a target drift in our replicates with the `target_drift` parameter.
234+
235+
```{r tgt_drift, fig.align='center', fig.width=8, fig.height=8, eval=FALSE}
236+
boots = NNS.MC(x, reps = 1, lower_rho = -1, upper_rho = 1, by = .5, target_drift = 0.05)$replicates
237+
reps = do.call(cbind, boots)
238+
239+
plot(x, type = "l", lwd = 3, ylim = c(min(reps), max(reps)))
240+
matplot(reps, type = "l", col = rainbow(length(boots)), add = TRUE)
241+
```
242+
243+
![](images/NNSmc_1_tgt_drift.png)
244+
245+
Please see the full **`NNS.meboot`** and **`NNS.MC`** argument documentation.
231246

232247
## Simulating a Multivariate Dependence Structure
233248

doc/NNSvignette_Sampling.html

+83-72
Large diffs are not rendered by default.

man/NNS.MC.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/NNS.meboot.Rd

+16-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NNS.dll

0 Bytes
Binary file not shown.

vignettes/NNSvignette_Sampling.R

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ legend('left', legend = c('ecdf', 'LPM.ratio'), fill=c('black','red'), border=NA
151151
# rho = 1 rho = 0.5 rho = -0.5 rho = -1
152152
# 1.0000000 0.4989619 -0.4984818 -0.9779778
153153

154+
## ----tgt_drift, fig.align='center', fig.width=8, fig.height=8, eval=FALSE-----
155+
# boots = NNS.MC(x, reps = 1, lower_rho = -1, upper_rho = 1, by = .5, target_drift = 0.05)$replicates
156+
# reps = do.call(cbind, boots)
157+
#
158+
# plot(x, type = "l", lwd = 3, ylim = c(min(reps), max(reps)))
159+
# matplot(reps, type = "l", col = rainbow(length(boots)), add = TRUE)
160+
154161
## ----multisim, eval=FALSE-----------------------------------------------------
155162
# set.seed(123)
156163
# x <- rnorm(1000); y <- rnorm(1000); z <- rnorm(1000)

vignettes/NNSvignette_Sampling.Rmd

+16-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,22 @@ sapply(boots, function(r) cor(r, x, method = "spearman"))
227227
1.0000000 0.4989619 -0.4984818 -0.9779778
228228
```
229229

230-
More replicates and ensembles thereof can be generated for any number of $\rho$ values. Please see the full **`NNS.meboot`** and **`NNS.MC`** argument documentation.
230+
More replicates and ensembles thereof can be generated for any number of $\rho$ values.
231+
232+
### `target_drift` Specification
233+
We can also specify a target drift in our replicates with the `target_drift` parameter.
234+
235+
```{r tgt_drift, fig.align='center', fig.width=8, fig.height=8, eval=FALSE}
236+
boots = NNS.MC(x, reps = 1, lower_rho = -1, upper_rho = 1, by = .5, target_drift = 0.05)$replicates
237+
reps = do.call(cbind, boots)
238+
239+
plot(x, type = "l", lwd = 3, ylim = c(min(reps), max(reps)))
240+
matplot(reps, type = "l", col = rainbow(length(boots)), add = TRUE)
241+
```
242+
243+
![](images/NNSmc_1_tgt_drift.png)
244+
245+
Please see the full **`NNS.meboot`** and **`NNS.MC`** argument documentation.
231246

232247
## Simulating a Multivariate Dependence Structure
233248

vignettes/NNSvignette_Sampling.html

+83-72
Large diffs are not rendered by default.
159 KB
Loading

0 commit comments

Comments
 (0)