0

how to use maxLik() to set the constraints? For example,the beta and alphh should >0, and sigma should >=0.0001 .The following is my code:

library(openxlsx)
data<-read.xlsx("E:\\1.my construction\\vasicek\\mle.xlsx",sheet=2)# This is my data
rt<-data$`BMJP02Y(RA)`;rt<-rt[-length(rt)]/100;rT<-data$`BMJP02Y(RA)`[-1]/100;n<-length(rt);pai<-3.14159;e<-exp(1)# This means the valuse of rt,tT,n,pai,e in function below
LL<-function(par)
{
  beta<-par[1];alphh<-par[2];sigma<-par[3]
  f<-0.5*(1-n)*(log(2*pai)+log((1-e^(-2*alphh))*sigma^2/(2*alphh)))-sum(rT-beta-(rt-beta)*e^(-alphh))^2/((1-e^(-2*alphh))*sigma^2/(alphh))
  return(f)
}
library(maxLik)
mle<-maxLik(LL, start=c(beta=0.00101, alphh=0.03452,sigma=0.0007))#This means the method of MLE
summary(mle)
XUN ZHANG
  • 83
  • 11
  • Have you read the documentation of `maxLik()`. You need to use the `constraints=` parameter which receives a list. In your case where you have inequality constraints, the list should contain the `ineqA` and `ineqB` elements whose values should be matrices such that `ineqA %*% theta + ineqB > 0`, where `theta` is your vector of parameters to estimate, i.e. `theta = par`, the argument received by the `LL()` function. Since you have 3 parameters and 3 constraints `ineqA` should be a 3x3 matrix and `ineqB` should be a 3x1 vector. See also last example of the doc for `maxBFGS` in `maxLik` library. – mastropi Jan 13 '20 at 06:57
  • @mastropi thank you for your help.The code below, is that right? library(maxLik); A<-matrix(c(1,0,0, 0,1,0, 0,0,1),3,3,byrow=TRUE); B<-c(0,0,0.0001); ineqCon<-list(ineqA=A,ineqB=B); cat("Inequality constraints, analytic gradient & Hessian\n"); mle<-maxLik(LL,start=c(beta=0.00101,alphh=0.03452,sigma=0.0007),constraints=ineqCon);summary(mle) I can't get a good estimated sigma as I thought, do you think the problem is the constraints setting about sigma or the LL funtion that I use is wrong? Because I am thinking about changing my LL function – XUN ZHANG Jan 14 '20 at 15:39
  • To begin with, `B` should be `B<-c(0,0,-0.0001)`, i.e. the lower bound for sigma should be negative, since `B` appears on the left hand side of the inequality. `A` is correctly defined. Regarding your estimation of sigma: (i) how do you define "good"? (ii) it's difficult to say whether the LL is correctly defined with so little context...; where does the LL come from? what is the data? – mastropi Jan 16 '20 at 08:54
  • Hi,@mastropi ,the function is log likelihood fuction for vasicek model, it comes from "MAXIMUM LIKELIHOOD ESTIMATION USING PRICE DATA OF THE DERIVATIVE CONTRACT" in (3.5), JIN CHUAND DUAN .The data is monthly data of 2year Japan government bond's yield to marturity from year 1988 to 2019. But use the maxLik() function in R, i just can't get the reult I want.So I find the solution at https://www.statisticshowto.datasciencecentral.com/wp-content/uploads/2016/01/Calibrating-the-Ornstein.pdf, page 9, I can get the estimated mu,lambda and sigma, which means beta,alphh and sigma in my code above – XUN ZHANG Jan 16 '20 at 23:40
  • @mastropi but now I don't know how to get the p-value for each parameter, could you give me some advice? – XUN ZHANG Jan 16 '20 at 23:40
  • You could use a likelihood-based method. It is mentioned here under section "Methods of derivation": https://en.wikipedia.org/wiki/Confidence_interval You can also check out this paper for a more general setting: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5863926/ Otherwise, you could use bootstrapping to estimate the distribution of the _estimate_ of each parameter: https://en.wikipedia.org/wiki/Bootstrapping_%28statistics%29 – mastropi Mar 08 '20 at 08:03
  • @mastropi Hi,mastropi.Sorry to disturb you again. If I need four constraints,like:beta and alphh should >0, sigma should >=0.0001,and beta also need to<0.01,i.e.,three parameters and four constraints,can I make it? – XUN ZHANG Jun 14 '20 at 17:07
  • Yes, the matrix defining the constraints doesn't have to be a square matrix. – mastropi Jun 15 '20 at 08:40

0 Answers0