R中for循环中的正确序列

时间:2015-10-26 06:02:10

标签: r loops for-loop iteration

在R中,我试图通过归纳过程构建矩阵'pi',我尝试了以下代码:

lambda<-0.2    #Tuning Parameter
T<-521

pi<-matrix(0,ncol=3,nrow=T-1)   #Empty matrix for portfolio pi, 3 columns for 3 stocks, T-1 time periods
pi[1,]<-1/3    #starting weights: Equal Weighted Portfolio

V<-matrix(0,ncol=1,nrow=T-1)    #Empty vector for portfolio relative value process
V[1] <- 1         #Starting wealth = $1

FE<-matrix(0,ncol=1,nrow=T-1) #Emptry vector for free energy at each time point

K<-matrix(0,ncol=1,nrow=T-1)  #Empty vector for kappa (convex parameter)
#will only be T-2 values for K=kappa, ignore the first to keep index the same as other variables

for( i in (1:T-1)){

  V[i+1]<-sum(pi[i,]*(1+r[i,]))/Vmu[i+1]

  FE[i]<-log(V[i+1]) - log(V[i]) - sum(pi[i,]*(mu[i+1,]/mu[i,]))

  K[i+1]<-min( 1 , lambda*FE[i]/abs( sum(1+(log(pi[i,]/mu[i+1]))) ) )                              

  pi[i+1,]<-pi[i,] + K[i+1]*(mu[i+1,]-pi[i,])
}

代码无效,我只是想知道for循环是否在以下步骤中起作用:

  1. 找到V [2]然后是FE [2]然后是K [2]然后是pi [2]然后重复3,4,......
  2. (这就是我想要的)

    或者它在做什么 2.找到V [2],V [3],........... V [T]然后FE [2],FE [3],....,FE [T] ,. .....等

    (这不是我想要的。)

    如果它是第二种方式,我怎样才能调整它以便它遵循第一种方法?

0 个答案:

没有答案