How to find sum of i<j (i,j = 1 to 25) of i without using for loop in R language.
This equation is what I am trying to code exactly, I need to get the index of both i and j and calculate sum of determination from there.
{(x_i, j_i)}i = 1 to 25
We can use outer
sum(outer(i, j, FUN = `<`))
If we need to find the sum of 'x'
sum(matrix(x, 25, 25)[outer(x, x, FUN = `<`)])
data
i <- 1:25
j <- 1:25
x <- rnorm(25)
For vector x, you can try the code below
sum(cumsum(x)[-length(x)])
# DATA
set.seed(42)
n = 25
v = 1:n
x = rnorm(n)
sum(rep(v, n) < rep(v, each = n))
sum(rep(x, n)[rep(v, n) < rep(v, each = n)])
Related
Title's a little rough, open to suggestions to improve.
I'm trying to calculate time-average covariances for a 500 length vector.
This is the equation we're using
The result I'm hoping for is a vector with an entry for k from 0 to 500 (0 would just be the variance of the whole set).
I've started with something like this, but I know I'll need to reference the gap (i) in the first mean comparison as well:
x <- rnorm(500)
xMean <-mean(x)
i <- seq(1, 500)
dfGam <- data.frame(i)
dfGam$gamma <- (1/(500-dfGam$i))*(sum((x-xMean)*(x[-dfGam$i]-xMean)))
Is it possible to do this using vector math or will I need to use some sort of for loop?
Here's the for loop that I've come up with for the solution:
gamma_func <- function(input_vec) {
output_vec <- c()
input_mean <- mean(input_vec)
iter <- seq(1, length(input_vec)-1)
for(val in iter){
iter2 <- seq((val+1), length(input_vec))
gamma_sum <- 0
for(val2 in iter2){
gamma_sum <- gamma_sum + (input_vec[val2]-input_mean)*(input_vec[val2-val]-input_mean)
}
output_vec[val] <- (1/length(iter2))*gamma_sum
}
return(output_vec)
}
Thanks
Using data.table, mostly for the shift function to make x_{t - k}, you can do this:
library(data.table)
gammabar <- function(k, x){
xbar <- mean(x)
n <- length(x)
df <- data.table(xt = x, xtk = shift(x, k))[!is.na(xtk)]
df[, sum((xt - xbar)*(xtk - xbar))/n]
}
gammabar(k = 10, x)
# [1] -0.1553118
The filter [!is.na(xtk)] starts the sum at t = k + 1, because xtk will be NA for the first k indices due to being shifted by k.
Reproducible x
x <- c(0.376972124936433, 0.301548373935665, -1.0980231706536, -1.13040590360378,
-2.79653431987176, 0.720573498411587, 0.93912102300901, -0.229377746707471,
1.75913134696347, 0.117366786802848, -0.853122822287008, 0.909259181618213,
1.19637295955276, -0.371583903741348, -0.123260233287436, 1.80004311672545,
1.70399587729432, -3.03876460529759, -2.28897494991878, 0.0583034949929225,
2.17436525195634, 1.09818265352131, 0.318220322390854, -0.0731475581637693,
0.834268741278827, 0.198750636733429, 1.29784138432631, 0.936718306241348,
-0.147433193833294, 0.110431994640128, -0.812504663900505, -0.743702167768748,
1.09534507180741, 2.43537370755095, 0.38811846676708, 0.290627670295127,
-0.285598287083935, 0.0760147178373681, -0.560298603759627, 0.447188372143361,
0.908501134499943, -0.505059597708343, -0.301004012157305, -0.726035976548133,
-1.18007702699501, 0.253074712637114, -0.370711296884049, 0.0221795637601637,
0.660044122429767, 0.48879363533552)
I am a beginner in R and I am given the following problem to code:
Let
and
be the summations I am trying to recreate into R.
Right now this is my code for the first summation (code snippet):
z <- 1:J
L<-1000
D<-0
for(k in z){
for(j in D:D+L-1){
X[k] = 1/L*sum(X[j])
}
}
I had no idea how to create latex formulas in the questions so if you run the code snippets you see the formulas I am trying to recreate in R.
My question is, am I on the right path? I am not sure how to use the for loop to create the summation.
J <- 5
L <- 100
D <- 1 # in R we start to count at 1 (one). (thanks to Darren)
x <- matrix(1:(L*J), nrow = length(D:(D+L-1)), ncol = J)
funXj_ <- function(j, D, L) sum(x[D:(D+L-1), j], na.rm = T)
X_ <- sapply(1:J, funXj_, D = D, L = L)
#5050 15050 25050 35050 45050
I believe your x_j^t is some sort of 2-Dimensional array. (so I took a matrix as example)
we're of course free to alter our borders:
J as 4, D as 2, L as 80
sapply(1:4, funXj_, D = 2, L = 80)
I am trying to find a way to efficiently use permutations of a vector in R without blowing up my computer. This is what I am trying to do:
n = 3 # I would need n>1000 instead, this is just to show what I am trying to achieve
t = 3
library(gtools)
m <- permutations(n = n, r = t, repeats.allowed = F, v = 1:n)
mm <- as.numeric(m)
df = data.frame()
for (i in 1:nrow(m)) {
mat <- matrix(0, nrow = ncol(m), ncol = n)
idx = m[i,]
mat[cbind(seq_along(idx), idx)] = 1
df = rbind(df, mat)
}
However using permutations, it is too time/memory consuming to work with large n (e.g. >1000). It looks like using "sample" is a great solution (proposed here):
v = 1:n
N <- t(replicate(length(v)^4, sample(v, t)))
# compare with: permutations(n = n, r = t, repeats.allowed = F, v = 1:n)
sum(duplicated(N))
m <- N[!(duplicated(N)), ] # then continue with code above
However, I am still unsure about the number of samples to take to be sure to cover all the possibilities. Does anybody have any ideas on number of samples, or how to make sure that all possibilities are covered? Thank you!
I am trying to learn how the CVXR package works, and I was porting a
Python example
by Steve Diamond here:
https://groups.google.com/forum/#!topic/cvxpy/5hBSB9KVbuI
and
http://nbviewer.jupyter.org/github/cvxgrp/cvx_short_course/blob/master/intro/control.ipynb
The R equivalent of the code is below:
set.seed(1)
n = 8
m = 2
T1 = 50
alpha = 0.2
beta = 5
A = diag(n) + alpha*replicate(n, rnorm(n))
B = replicate(m, rnorm(n))
x_0 = beta*replicate(1, rnorm(n))
# Form and solve control problem.
x = Variable(n, T1+1)
u = Variable(m, T1)
states = c()
for (t in 1:T1) {
cost = sum_squares(x[,t+1]) + sum_squares(u[,t])
constr = list(x[, t+1] == A%*%x[, t] + B%*%u[, t],
norm_inf(u[,t]) <= 1)
states = c(states, Problem(Minimize(cost), constr) )
}
# sums problem objectives and concatenates constraints.
prob <- Reduce("+", states)
constraints(prob) <- c(constraints(prob), x[ ,T1] == 0)
constraints(prob) <- c(constraints(prob), x[ ,0] == x_0)
sol <- solve(prob)
I have a challenge with the second-to-last line (it throws an error):
constraints(prob) <- c(constraints(prob), x[ ,0] == x_0)
My guess is that x[ , 0] points to the zero-th index position of the
variable, x, which does not exist in R. But from Python which the
program is converted from, a zero-th index position exists from the
for loop (for t in range(T)). range(T) is a vector starting from 0
- 49.
But in R, the for loop (for (t in 1:T1) ) is for a vector of 1 - 50.
Please, any ideas to help will be much appreciated.
Thank you.
You need to bump up the index number by 1, so x[,1] == x_0 and x[,T1+1] == 0 in the second and third from the last line, respectively. Otherwise, you never set the T1+1 entry.
I have a data set dat and two lists x and y. I would like to calculate different combination of x and y with different value of k. I wrote the following code to find the value of function fun for these different combinations. but how can I get the value of k which maximize the function fun for these different combination? since in each iteration I have different lists of x and y and at the end I want to find the k which maximise the function fun.
dat = c(9, 2, 7)
k = seq(0, 1, length = 10)
x =list(a = 1, b = 8, c = 4)
y = list(a = .5, b = 5, c = 5)
matrix = cbind(unlist(x), unlist(y)) %*% rbind(1-k, k)
z = apply(matrix, 2, as.list)
fun = function(dat, vec) sum(vec$a * dat - vec$b * dat + vec$c * dat)
res = rep(0, length(k))
for (i in 1:(length(k))){
v = split(unlist(z[[i]]), sub("\\d+$", "", names(z[[i]])))
res[i] = fun(dat, v)
}
> res
[1] -54 -47 -40 -33 -26 -19 -12 -5 2 9
In this example, k = 10 , but how can I find for every different lists without loop?
I still can't make heads or tails of what you are trying to do, but your code seems to boil down to this:
colSums(matrix(rep(dat,nrow(matrix)),ncol=nrow(matrix)) %*% (matrix*c(1,-1,1)))
That will work for any size of k. It also does not require any of your names.
Some advice: Don't use list when a simple vector will do. You seem to understand how the %*% multiply works, you just need to get your matrices into the right form.