[R] how to apply summation sign formula to R
Sebastian Hersberger
sebastian.hersberger at unibas.ch
Mon Aug 26 18:59:13 CEST 2013
Hi David
Thanks for your help.
I tried it for a simplified example with vectors instead of matrices.
Once again the formula:
C = (Σ(from i=0 to i) A^i ) x B x (Σ(from i=0 to i) A^i )’
I applied it at follows – but couldn’t figure out what’s missing:
library(expm)
i <- c(0,1,2,3,4,5,6,7,8,9,10)
A <- c(1,2,3)
B <- 5
res1 <- Reduce("+", lapply(1:i, function(i) A ^ i) )
res2 <- res1 * B
> res3 <- res2 * t(res1)
> res3
[,1] [,2] [,3]
[1,] 20 45 80
My expected result must be in this case a single number, which is the sum of i=0 to i=10:
i=0 ⇒ 5
i=1 ⇒ 1*5*1 + 1*5*2+1*5*3 + 2*5*1+2*5*2 + 2*5*3+3*5*1+3*5*2+3*5*3 = 180
i = 2 ⇒ ........... (=985)
i=3 ⇒ ....
...
i=10 ⇒ =............
After having adjusted the above provided „R-code“: what is necessary to get from the „vector-solution“ to the matrix-solution“: would it be enough to:
- define A and B as matrices
- and replace * by %*% ?
Or what else is required?
Thanks and regards
Sebastian
________________________________________
From: David Winsemius [dwinsemius at comcast.net]
Sent: Sunday, August 25, 2013 3:51 AM
To: David Winsemius
Cc: Sebastian Hersberger; r-help at r-project.org
Subject: Re: [R] how to apply summation sign formula to R
On Aug 24, 2013, at 6:43 PM, David Winsemius wrote:
>
> On Aug 24, 2013, at 2:13 PM, Sebastian Hersberger wrote:
>
>> Thanks. I restate my problem/question and hope its better understandable now.
>>
>> Let us define A and B as kxk matrices. C is the output (matrix), which I try to calculate for differnt i values.
>>
>> So for example: I want to caluclate the matrix C for the value i=10:
>>
>> Therefore, I set:
>>
>> i <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>
>> Finally, I have to define the summation formula in R. My question is how this following summation formula has to be applied to R.
>>
>> The arithmetic form of the formula equals:
>>
>> C = (Σ(from i=0 to i) A^i ) x B x (Σ(from i=0 to i) A^i )’
>>
>> Which means:
>> matrix C equals the sum from i=0 to i times matrix A to the power of i
>
> That first term might be (untested in absence of test data by questioner) :
>
> require(expm)
> res1 <- Reduce("+", lapply(1:i, function(i) powm(A, i) ) )
I did test it and both my memory about the functions name and its operation were wrong. Try:
res1 <- Reduce("+", lapply(1:i, function(i) A %^% i) )
>
>> times matrix B
>
> res2 <- res1 %*% B
>
>> times the transposed/invers of the sum from i=0 to i times matrix A to the power of i
>>
> Well, the transpose was not generally the inverse when I took linear algebra, but as I said I didn't make it to all the sessions. (I'm assuming it is the transpose.)
>
> res3 <- res2 %*% t(res1)
>
> You could probably do it all in one line, but I like to check my itermediates.
>
> --
> David.
>
>> I hope I were able to specify my problem in an understandable way. If not, please let me know.
>>
>> Thanks and regards
>> Sebastian
>>
>> ________________________________________
>> From: David Winsemius [dwinsemius at comcast.net]
>> Sent: Saturday, August 24, 2013 8:35 PM
>> To: Sebastian Hersberger
>> Cc: r-help at r-project.org
>> Subject: Re: [R] how to apply summation sign formula to R
>>
>> On Aug 24, 2013, at 6:37 AM, Sebastian Hersberger wrote:
>>
>>> Hi all
>>>
>>> I have a short question relating to the usage of the summation sign in R.
>>>
>>> Let's define A and B as two kxk matrice.
>>> My goal is to calculate the matrix C for the periods from 1 to 200 (n=1-200).
>>>
>>> C^(n) = Σ_(j=1)^n [(Σ_(i=1)^(j-1) A^i ) B (Σ_(i=1)^(j-1) A^i)’ ]
>>>
>>> How has that to be implemented in R (lets say for example for period = n = 150)?
>>
>> I don't follow all this notation but you might want to look at the expm package that has a powm function. It's possible that the expm function may do what you want, but as I said I was unclear what the equation was attempting to do. Perhaps if you explained the steps in mathematical language, then other viewers who actually woke up to attend their linear algebra course might have a better chance of offering assistance.
>>
>> ( When I saw the earlier copy yesterday, I had hopes that such wiser viewers might chime in and still have such hopes.)
>>
>> --
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>
> David Winsemius
> Alameda, CA, USA
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list