I have a function which I am using to calculate the sum of square differences between a 3 component normal mixture distribution function to a set of given quantiles. I want to use this function to optimize using the Nelder-Mead method in the optim function. The first 3 parameters to be optimized are mean parameters for the 3 components. The last 2 parameters are weight parameters for the components.
I already enforce that the parameters w1 and w2 are between 0 and 1 by using the inverse-logit function. Two other constraints that I want to enforce are mean1 > mean2 > mean3 and w1 + w2 + w3 = 1. How can these constraints be enforced using this setup?
opt3 <- function(quantiles,levels,par) {
mean1 <- par[1]
mean2 <- par[2]
mean3 <- par[3]
w1 <- inv.logit(par[4])
w2 <- inv.logit(par[5])
w3 <- 1 - (w1+w2)
sum((pnorm(quantiles,mean1,sd)*w1 +
pnorm(quantiles,mean2,sd)*w2 +
pnorm(quantiles,mean3,sd)*w3 -
levels)^2)
}
optim(par=par, opt3, quantiles=quantiles, levels=levels)