phis <- c(1.7,-.72)
library(tswge)
psi.weights.wge(phi = phis, lag.max = 4)
# [1] 1.7000 2.1700 2.4650 2.6281
phis <- c(1.7,-.72)
l = 1
psi = as.numeric(1)
psi
# [1] 1
n <- phis[1:l] * psi[1:l]
# [1] 1.7
psi <- rlang::prepend(psi,n)
# [1] 1.7 1.0
l = 2
n <- phis[1:l] * psi[1:l]
n <- sum(n)
# [1] 2.17
psi <- rlang::prepend(psi,n)
# [1] 2.17 1.70 1.00
l = 3
n <- c(phis,0) * psi[1:l]
n <- sum(n)
# [1] 2.465
psi <- rlang::prepend(psi,n)
# [1] 2.465 2.170 1.700 1.000
psii <- function(phi,l){
if (l>length(phi)){
phi <- append(phi,rep(0,l-length(phis)))
}
if (l==1) return(phi[1])
else return(sum(phi[1:l]*(rlang::prepend(1,psii(phi,l-1)))))
}
Test:
psip <- function(phi,l){
as.numeric(lapply(1:l, psii, phi = phi))
}
psip(phis,4)
# [1] 1.7000 2.1700 2.9690 4.3273
Why I think it broke: We are not making a vector of psi's, we are only successfully getting the first two values
For some reason, this code fails to output a vector. I am not sure why because i think we are growing a vector:
phitest <- function(phi,l){
if (l>length(phi)){
return( append(phi,rep(0,l-length(phis))))
}
else{
return(phi)
}
}
psic <- function(phi,l, psi = as.numeric(1)){
phi <- phitest(phi,l)
if (l==1) return(phi[1:l] * psi[1:l])
else return(psic(phi, l - 1, rlang::prepend(psi, phi[1:l] * psi[1:l]))
)
}
psic(phis,1)
# [1] 1.7
psic(phis,2)
# [1] 2.89
How to proceed????
We dont want to be doing this for loop because for loop bad, we want to vectorize or recursive
multfun <- function(phi,l,psi){
sum(phi[1:l] * psi[1:l])
}
phitest <- function(phi,l){
if (l>length(phi)){
return( append(phi,rep(0,l-length(phis))))
}
else{
return(phi)
}
}
psiloop <- function(phi,l){
phi <- phitest(phi,l)
psi <- as.numeric(1)
for (i in 2:l){
psi[i] <- multfun(phi,i-1,rev(psi))
}
psi
}
psiloop(phis,5)
# [1] 1.0000 1.7000 2.1700 2.4650 2.6281
multfun <- function(phi,l,psi){
sum(phi[1:l] * psi[1:l])
}
phitest <- function(phi,l){
if (l>length(phi)){
return( append(phi,rep(0,l-length(phis))))
}
else{
return(phi)
}
}
psiloop <- function(phi,l, theta = as.numeric(0)){
phi <- phitest(phi,l)
psi <- as.numeric(1)
theta <- c(0,theta)
theta <- phitest(theta,l)
for (i in 2:l){
psi[i] <- multfun(phi,i-1,rev(psi)) - theta[i]
}
psi
}