1

I am new at programming. I need to please ask 2 questions. My code is after the questions. Thank you very much!!!

1) I am trying to use fmincon in R but I keep getting the following mistake:

"Error in chol.default(ZHZ) : the leading minor of order 2 is not positive definite"

Does someone have any idea what this is about? My code is below.

2) Am I programming correctly? The function should be finding values to minimize "sqrterrors". Is the code I wrote doing that?

x0 <- c(-0.0084,0.0049,-0.0019,0);

#Function

fn <- function(x){



  #Get the parameters
  a0<-x[1];
  a1<-x[2];
  a2<-x[3];
  a3<-x[4];  


  daysmatrix=as.matrix(maturity)%*%as.matrix(ones(1,dim(datespayout)[2]))-as.matrix(ones(dim(maturity)[1],1))%*%as.matrix(datespayout)


  timetopayout<-(as.matrix(datespayout)--34)/365;


  discountrate<-a0+a1*as.matrix(timetopayout)+a2*as.matrix(timetopayout^2)+a3*as.matrix(timetopayout^3);


  priceZCB<-exp(-as.matrix(discountrate)*as.matrix(timetopayout))


  paymentbydaysmatrix=(daysmatrix==0)*100+0.5*as.matrix(coupon)%*%as.matrix(ones(1,dim(datespayout)[2]))*(daysmatrix>=0);

  paymentbydaysmatrix<-sweep(as.matrix(paymentbydaysmatrix), MARGIN=2, as.matrix(priceZCB), `*`);


  PVestimate=as.matrix(rowSums(paymentbydaysmatrix));

  sqrterrors=(price_obs-PVestimate)^2;
  sqrterrors=sum(sqrterrors);


}

# Upper and Lower Bounds
bmax0 = 0.01;
bmax1 = 0.01;
bmax2 = 0.001;
bmax3 = 0.001;
ub1 <- c(bmax0,bmax1,bmax2,bmax3);
lb1=-ub1;
A <- NULL;
b <- NULL;
Aeq <- NULL;
beq <- NULL;
hin <- NULL;
heq <- NULL;

x <- fmincon(x0, fn, gr = NULL, method = "SQP",A=NULL,b=NULL,Aeq=NULL,beq=NULL,lb=lb1,ub=ub1,hin=NULL,heq=NULL)```






  • The error you are getting is in cholesky decomposition, and it probably means that when you where calculating correlation matrix it came to some numerical errors and matrix is no longer postitive definite. To make correlation matrix pos. def again, you can use `Matrix::nearPD` function, that gives the closest approx. of PD matrix to your input. – JacobJacox Jan 20 '20 at 11:01

0 Answers0