如何向量化:根据上次二进制向量为 1 的时间设置一个值

发布于 2024-10-24 00:47:17 字数 1101 浏览 4 评论 0原文

我还有另一个 R 初学者问题...

我如何矢量化(避免 for 循环)以下代码:

# algorithm for getting entry prices (when signal > 0): look back from current
# position until you find first signal > 0,
# `mktdataclose` at that time is entry price
# `entryPrices` is an xts object representing entry prices
# if entryPrices are not available (is.null == TRUE) then wee need to reconstruct
# them from signal (xts object with 1 when entry signal triggered and 0 
# otherwise) and close prices available in mktdataclose (an xts object with the
# same length as signal and same dates just that it represents closing prices)

EntryPrices <- entryPrices
if (is.null(EntryPrices)) {
    # get entryprices as close prices on buy signal
    EntryPrices <- ifelse(signal > 0, mktdataclose, 0)
    entryPrice <- 0
    for (i in 1:NROW(signal)) {
        if (signal[i] > 0) entryPrice <- mktdataclose[i]
        EntryPrices[i] <- entryPrice
    }
}

我一直在思考 SAS 数据步骤方式,并绝望地寻找保留等。我在哪里可以找到一些简单的例子来理解 sapply等等(不幸的是,通过 ?sapply 提供的帮助对我来说太复杂了... :( )

谢谢您的帮助。

最好的, 萨摩。

I have another R beginner question...

How can I vectorize (avoid for loop in) following code:

# algorithm for getting entry prices (when signal > 0): look back from current
# position until you find first signal > 0,
# `mktdataclose` at that time is entry price
# `entryPrices` is an xts object representing entry prices
# if entryPrices are not available (is.null == TRUE) then wee need to reconstruct
# them from signal (xts object with 1 when entry signal triggered and 0 
# otherwise) and close prices available in mktdataclose (an xts object with the
# same length as signal and same dates just that it represents closing prices)

EntryPrices <- entryPrices
if (is.null(EntryPrices)) {
    # get entryprices as close prices on buy signal
    EntryPrices <- ifelse(signal > 0, mktdataclose, 0)
    entryPrice <- 0
    for (i in 1:NROW(signal)) {
        if (signal[i] > 0) entryPrice <- mktdataclose[i]
        EntryPrices[i] <- entryPrice
    }
}

I am stuck at thinking SAS data step way and desperatley looking for retain etc. Where can I find some simple exaples in order to understand sapply etc (r help via ?sapply is unfortunately to complicated for me... :( )

Thank you for your kind help.

Best,
Samo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-10-31 00:47:17

如果我理解正确,你的问题是:你有两个长度为 n 的向量 signal 和 mktdataclose ,并且你想创建一个新向量 < code>EntryPrices 长度为 n,使得 mktdataclose[i]mktdataclose 最后 的值> 时间 signal 在时间 i 或之前为 1。您可以在没有 for 循环的情况下使用 cummax 来完成此操作,这是一个经常出乎意料的有用函数(请注意,这个问题与您之前的一些问题类似,使用此函数和 类似地解决了这些问题>cumsum)。在这里,我们使用 Gavin 的数据:

set.seed(123)
signal <- sample(0:1, 10, replace = TRUE)
mktdataclose <- runif(10, 1, 10)  

我们的问题实际上是将信号向量转换为适当索引的向量:

indices <- cummax( seq_along(signal) * signal)

这正是我们想要的索引,除了0. 现在,我们通过从 mktdataclose 中提取非零索引处的值来设置EntryPrices

EntryPrices <- c( rep(0, sum(indices==0)), mktdataclose[ indices ])

>   cbind(signal, indices, mktdataclose, EntryPrices)
      signal indices mktdataclose EntryPrices
 [1,]      0       0     9.611500    0.000000
 [2,]      1       2     5.080007    5.080007
 [3,]      0       2     7.098136    5.080007
 [4,]      1       4     6.153701    6.153701
 [5,]      1       5     1.926322    1.926322
 [6,]      0       5     9.098425    1.926322
 [7,]      1       7     3.214790    3.214790
 [8,]      1       8     1.378536    1.378536
 [9,]      1       9     3.951286    3.951286
[10,]      0       9     9.590533    3.951286

If I understood correctly, your problem is: you have two vectors signal and mktdataclose of length n, and you want to create a new vector EntryPrices of length n such that mktdataclose[i] is the value of mktdataclose the last time signal was 1 at or before time i. You can do this without a for loop, using cummax, an often unexpectedly useful function (Note that this question is similar in flavor to some of your earlier questions, which were similarly solved using this function and cumsum). Here we go, using Gavin's data:

set.seed(123)
signal <- sample(0:1, 10, replace = TRUE)
mktdataclose <- runif(10, 1, 10)  

Our problem is really to convert the signal vector into a vector of the appropriate indices:

indices <- cummax( seq_along(signal) * signal)

This is exactly the indices we want, except for the 0. Now we set EntryPrices by extracting the values at the non-zero indices from mktdataclose:

EntryPrices <- c( rep(0, sum(indices==0)), mktdataclose[ indices ])

>   cbind(signal, indices, mktdataclose, EntryPrices)
      signal indices mktdataclose EntryPrices
 [1,]      0       0     9.611500    0.000000
 [2,]      1       2     5.080007    5.080007
 [3,]      0       2     7.098136    5.080007
 [4,]      1       4     6.153701    6.153701
 [5,]      1       5     1.926322    1.926322
 [6,]      0       5     9.098425    1.926322
 [7,]      1       7     3.214790    3.214790
 [8,]      1       8     1.378536    1.378536
 [9,]      1       9     3.951286    3.951286
[10,]      0       9     9.590533    3.951286
又爬满兰若 2024-10-31 00:47:17

由于信号是 0 和 1 我想你可以用以下方法进行矢量化:

EntryPrices * signal

since signal is 0 and 1 i presume you can vectorize with:

EntryPrices * signal
冰雪梦之恋 2024-10-31 00:47:17

您可能会发现另一种更直接的解决方案。我正在使用普拉萨德的伪数据。

> EntryPrices <- ifelse(signal > 0, mktdataclose, NA)
> EntryPrices <- na.locf(EntryPrices, na.rm=FALSE)
> cbind(signal,mktdataclose,EntryPrices)
      signal mktdataclose EntryPrices
 [1,]      0     9.611500          NA
 [2,]      1     5.080007    5.080007
 [3,]      0     7.098136    5.080007
 [4,]      1     6.153701    6.153701
 [5,]      1     1.926322    1.926322
 [6,]      0     9.098425    1.926322
 [7,]      1     3.214790    3.214790
 [8,]      1     1.378536    1.378536
 [9,]      1     3.951286    3.951286
[10,]      0     9.590533    3.951286

Here's another solution that you may find more straight-forward. I'm using Prasad's psudo-data.

> EntryPrices <- ifelse(signal > 0, mktdataclose, NA)
> EntryPrices <- na.locf(EntryPrices, na.rm=FALSE)
> cbind(signal,mktdataclose,EntryPrices)
      signal mktdataclose EntryPrices
 [1,]      0     9.611500          NA
 [2,]      1     5.080007    5.080007
 [3,]      0     7.098136    5.080007
 [4,]      1     6.153701    6.153701
 [5,]      1     1.926322    1.926322
 [6,]      0     9.098425    1.926322
 [7,]      1     3.214790    3.214790
 [8,]      1     1.378536    1.378536
 [9,]      1     3.951286    3.951286
[10,]      0     9.590533    3.951286
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文