问题
Goal: Estimate sigma1 and sigma2 with the "optim" function, while sigma2 must be greater than sigma1
Simulate data (y)
I have the following kind of data y:
N<-50
delta<-matrix(rep(0, N*N), nrow=N, ncol=N)
for(i in 1:(N )){
for (j in 1:N)
if (i == j+1 | i == j-1){
delta[i,j] <- 1;
}
}
sigma1<-5
sigma2<-10
diagonal=2*sigma1^2+sigma2^2
nondiag<--sigma1^2*delta
Lambda_i<-(diag(diagonal,N)+-nondiag)/diagonal
sig<-as.matrix(diagonal*Lambda_i)
sig
mu<-rep(0, N)
y<-as.vector(mvnfast::rmvn(1,mu, sig))
Create maximum likelihood function
mle<-function(par){
sigma1<-par[1]
sigma2<-par[2]
diagonal=2*sigma1^2+sigma2^2
nondiag<--sigma1^2*delta
Lambda_i<-(diag(diagonal,N)+-nondiag)/diagonal
sig<-as.matrix(diagonal*Lambda_i)
#lokli
loglik<--as.numeric(mvnfast::dmvn(matrix(y, byrow=T, ncol=N),mu, sig, log=T))
loglik
}
Optimization
par <- c(5,5)
fit<-optim(par,mle,hessian=T,
method="L-BFGS-B",lower=c(0.01,0.01),
upper=c(30,30))
fit$par
Question: How I can set the constraint: "sigma2 always greater sigma1" in the optimization procedure?
回答1:
Just to follow-up on my comment. We can use two tricks:
- Replace all occurrences of sigma2 in your likelihood with (sigma1 + sigma2) to ensure that sigma2 now represents the amount that is added to sigma1
- Use
exp
on sigma2 to ensure that it is non-negative.
The likelihood becomes
mlenew<-function(par){
sigma1<-par[1]
sigma2<-par[2]
diagonal=2*sigma1^2+(sigma1 + exp(sigma2))^2
nondiag<--sigma1^2*delta
Lambda_i<-(diag(diagonal,N)+-nondiag)/diagonal
sig<-as.matrix(diagonal*Lambda_i)
#lokli
loglik<--as.numeric(mvnfast::dmvn(matrix(y, byrow=T, ncol=N),mu, sig, log=T))
loglik
}
If I run you code I get
> fit<-optim(par,mle,hessian=T,
+ method="L-BFGS-B",lower=c(0.01,0.01),
+ upper=c(30,30))
> fit$par
[1] 1.738656 12.672040
With the new code I get
> fit<-optim(par,mlenew,hessian=T,
+ method="L-BFGS-B",lower=c(0.01,0.01),
+ upper=c(30,30))
> fit$par
[1] 1.737843 2.391921
and then you need to "back-transform": The actual value of the old version of sigma2 using the new code is
> exp(2.391921) + 1.737843
[1] 12.67232
Hope this helps.
来源:https://stackoverflow.com/questions/44440657/r-customized-constraints-optim-function