1

I would like to maximize the funcToOpt in the code.

Description of the data:

  1. wb and X1 are T x N matrix, and Nt a vector of length N
  2. the sum of each row of wb is 1
  3. X1 is cross-sectionally standardized (mean=1 sd=0)

The standardization implies that the cross-sectional average of theta*X1 is zero, which means that rowSums(wb + (theta1*X1) / Nt) is always 1. This happens to me just when i work with a X1 matrix without NAs values, is there someone that know how i can have row sum of wi equal to 1 when using a matrix with NAs as well??

CODE OF THE MAXIMIZATION:

X1 <- t(scale(t((X1))))    # X1 STANDARDIZATION
N <- dim(X1)[2]
T <- dim(X1)[1]

M <- wb + r + X1                  # Nt, counting the NAs values
Nt <- rowSums(!is.na(M)) 

funcToOpt <- function (theta1)
{
  inner <- rowSums((wb + (theta1*X1) / Nt) * r,na.rm = TRUE)  

  return (sum(inner, na.rm=TRUE) / T)
}

max<-optimize(funcToOpt, c(-50, 50), maximum = TRUE)

theta1=max[[1]]                                 

wi=wb+(theta1*X1) / Nt     
rowSums(wi,na.rm=T)

MATRIX 5X5 WITHOUT NA

r=matrix(c(0.1,0.3,0.4,0.3,0.4,0.5,0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.2,0.1,0.2,0.2,0.4,0.4,0.5,0.1),ncol = 5)
wb=matrix(c(0.3,0.3,-0.4,-0.3,0.4,-0.5,-0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.6,0.5,0.2,0.2,0.6,0.4,0.5,0.1),ncol = 5)
X1=matrix(c(1:20,0,4,5,0,3),ncol = 5)

MATRIX 5x5 with NA

r=matrix(c(0.1,0.3,0.4,0.3,0.4,0.5,0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.2,0.1,0.2,0.2,0.4,0.4,0.5,0.1),ncol = 5)
wb=matrix(c(0.3,0.3,-0.4,-0.3,0.4,-0.5,-0.3,0.3,0.2,0.1,0.1,0.1,0.1,0.1,0.2,0.9,0.3,0.6,0.5,0.2,0.2,0.6,0.4,0.5,0.1),ncol = 5)
X1=matrix(c(1:20,NA,4,5,NA,3),ncol = 5)

0 Answers0