1

Here is the image showing what i need to calculate. Sorry total_r in the image is actually test_gain in the code and gamma in the pic refers to alpha in the code (Sorry). Also in the image i stop calculating at t=3 but actually i want to calculate it until the last value which is 0.6.

I am fairly new to using the apply family of functions. I normally use loops but i have heard using the apply function instead of nested for loops is a lot quicker. I tried a a couple tutorials but still failed to replace my nested for loop with the apply function. Any help will be greatly appreciated and below is the code i'm trying to change.

Basically this is what i'm trying to do: first row of the data: take value from a column + alpha * nextvalue of this column (row 2) + alpha ^ 2 * nextvalue of column (row3) + alpha ^ 3 * next value of column (row4) and so on untill the last row. Each time i am increasing the power to Alpha.

All this calculation was for the first row. Now for the 2nd row i will ignore the first value from the column but will take all the subsequent values the same way. Below is my code which works fine but it just take too long to execute.

#value of alpha
alpha <- 0.85

# test_gain is a vector containing values from a column belonging to a data frame
test_gain <- testdata$Total_rew

# initialise the variables 
s = 0
d = rep(0,nrow(testdata))

for (i in 1:nrow(testdata[1:4999,])){
  d[i] = test_gain[i]
  for (s in (i+1):nrow(testdata)){
    d[i] = d[i] + alpha^(s-i) * test_gain[s]
    if (alpha^(s-i) < (10^-5)) {next()}

  }
}
5
  • What is gamma, and why do you not print if gamma is less than 10^-5? Commented Aug 15, 2016 at 15:04
  • Generally in R you're going to want to vectorize your code and avoid loops whenever possible. The apply family has loops in its code. Check out articles on vectorization Commented Aug 15, 2016 at 15:09
  • sorry that was a typo. gamma was actually alpha. i have changed it now. The only reason why i skip the iteration when alpha is less then 10^-5 is just to speed the whole process up. Because when alpha reaches 10^-5, anything after that will be so small that i don't need to add it to my sums Commented Aug 15, 2016 at 15:10
  • @AliJawaad: I'm not sure that will speed it up because I believe you only go to the next s not i; you should probably use break instead? Anyways, you would want a better solution. Commented Aug 15, 2016 at 15:17
  • cheers @aichao. i have also attached a image showing the calculation i want to do. Total_r in the image refers to test_gain in the code Commented Aug 15, 2016 at 15:24

1 Answer 1

0

The key is to generate an upper-triangular matrix of size N of the power series of alpha where N is the number of rows in testdata that looks like this:

1 alpha alpha^2 alpha^3 ... alpha^(N-1)
0   1   alpha   alpha^2 ... alpha^(N-2)
0   0     1     alpha   ... alpha^(N-3)
0   0     0       1     ... alpha^(N-4)
...                            ...
0   0     0       0             1

Then, the computation is simply a matrix multiply with the column testdata$Total_rew.

To generate this upper triangular matrix (adapted from this SO question/answer):

## This will work for both nrow(testdata) is odd and even
nr <- ceiling(nrow(testdata)/2 - 1)
mat <- outer(alpha^(-nr:nr), alpha^(-nr:nr))
## reverse the columns
mat <- mat[,rev(seq.int(ncol(mat)))]
## what we want is in the lower triangular, so set the upper triangular to zeroes
mat[upper.tri(mat)] <- 0
## take the transpose so what we want is now upper triangular
mat <- t(mat)

Then,

d <- mat %*% testdata$Total_rew

Note also that you do not need to transpose mat in the last step above. If you do not transpose mat in the last step above, this will also give the same result:

d <- testdata$Total_rew %*% mat

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

It certainly does help, thanks. I will try the way you suggested and see if the results match the values i earlier extracted using the nested loop. If this way works i can see it reducing my computation time dramatically
may I suggest that for the comparison you do not stop the series when alpha^(s-i) is less than 10^-5? I'll also be editing the answer, but that will not affect the results.
Yeah your right, i will take out this line of code and leave it running over night. Thanks
may I suggest you try a smaller number of rows of testdata first to convince yourself that it works?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.