Kray
Kray

Reputation: 51

Double Variable Summation

So I've been attempting to do double summations on RStudio for awhile and I'm getting nowhere fast.... Does anyone know how to solve equations such as these:

enter image description here

So far, the code I did for the first summation is as followed:

IndexStart = 1

i = seq(IndexStart, 17, 1)

j = seq(IndexStart, 13, 1)

resultb = sum(i*j)

print(resultb)

For the second, since pi and pj have distinct values, I was going to list p1 and p2 as separate variables maybe with the above style of code?

Any input would be helpful

Upvotes: 1

Views: 309

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

The first summation can be computed with the help of outer.

i <- 1:17
j <- 1:13
sum(outer(i, j))
#[1] 13923

And do something similar for the second summation.
I will create test data, since you have posted none.

set.seed(1)    # Make the rsults reproducible
p.i <- runif(2)
p.j <- runif(2)

p.ij <- outer(p.i, p.j)
logp.ij <- log(p.ij)
sum(p.ij*logp.ij)
#[1] -1.325546

Upvotes: 4

Related Questions