Reputation: 125
I have a 3x3 matrix Omega whose elements are unknown, and a defined 3x1 parameter vector alpha. I want to define a 3x1 vector delta whose elements are still unknown but depend on alpha and Omega as shown in this image:
The term in the brackets to the power of 2 simplifies to a number K, so I wrote this function:
Alpha=c(-0.248,1.092,-0.518)
K=function(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13){
(1+Alpha[1]*(Alpha[1]+Alpha[2]*gamma12/(gamma1*gamma2)+Alpha[3]*gamma13/(gamma1*gamma3)))^(-1/2)
}
gamma1, gamma2, gamma3
are the elements in the diagonal of the 3x3 matrix Omega, whereas gamma12, gamma13, gamma23
are the off-diagonal elements (each elements repeats itself twice, e.g. gamma12=gamma21
). So, by putting 6 arbitrary values in K()
I get the scalar. So far so clear.
The rest I'm not sure about. I want R to return me a vector delta defined as shown above. How can I write a function that would perform this algebraic calculation, and return a 3x1 vector delta whose elements are the same unknowns as in Omega, but shifted/multiplied by the numbers in alpha?
Upvotes: 1
Views: 232
Reputation: 5152
Is that what you want?:
#generate the triangular matrix
set.seed(234)
og<-matrix(runif(9,1,2),3)
og[lower.tri(og)] = t(og)[lower.tri(og)]
#K(og[1,1],og[2,2],og[3,3],og[1,2],og[2,3],og[1,3])
#delta = (1 + alpha' * Omega * alpha)^(-1/2) * Omega * alpha
as.numeric((1+Alpha%*%og%*%Alpha)^(-1/2))*og%*%Alpha
> as.numeric((1+Alpha%*%og%*%Alpha)^(-1/2))*og%*%Alpha
[,1]
[1,] 0.7264204
[2,] -0.2365427
[3,] 0.5709528
If you want a function
Kf=function(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13,Alphav=Alpha){
ogm<-matrix(c(gamma1,gamma12,gamma13,gamma12,gamma2,gamma23,
gamma13,gamma23,gamma3),3)
KF<-as.numeric((1+t(Alphav)%*%ogm%*%Alphav)^(-1/2))
Delt<-KF*ogm%*%Alphav
list(Delta=Delt,K=KF,Om=ogm)
}
#taking the same random values for consistency
gamma1=og[1,1]; gamma2=og[2,2]; gamma3=og[3,3];
gamma12=og[1,2]; gamma23=og[2,3]; gamma13=og[1,3]
Kf(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13)$Delta
> Kf(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13)$Delta
[,1]
[1,] 0.7264204
[2,] -0.2365427
[3,] 0.5709528
Upvotes: 1