I am trying to fit an F distribution to a given set using optim
's L-BFGS-B method. For some reason, it is always converging at iteration 0, which obviously doesn't approximate the parameters I am looking for.
For reproduction purposes,
# A sample x ~ F(5, 8) with 5,000 observations. x <- rf(50000, 5, 8) # Sample density. f <- density(x, from=min(x), to=max(x)) # Error function (root mean squared error) to be minimized. # Noteworthy F's degrees of freedom are rounded since they # are required to be integers. rmse <- function(params) { sqrt(sum((df(f$x, round(params[1]), round(params[2])) - f$y)^2)) } # L-BFGS-B optimization. o <- optim(c(50, 50), # Initial parameters. rmse, lower=c(0, 0), # F's degrees of freedom's domain upper=c(2^16, 2^16), # is [0,Inf). method='L-BFGS-B', control=list(trace=3)) print(o)
Here we should expect o$par
$approx (5, 8)$ to correspond with our x
definition. Instead, the optimization does not take place and returns the initial parameters, $(50, 50)$, claiming
$message [1] "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL"
Any hint towards a successful optimization?
Contents
hide
Best Answer
The issue is with the round()
function as it allows infinite optimal solutions. That is, $(5.1,8.1)$ is optimal and so is $(4.9,7.9)$. Getting rid of the round()
function works.
# A sample x ~ F(5, 8) with 5,000 observations. x <- rf(50000, 5, 8) # Sample density. f <- density(x, from=min(x), to=max(x)) # Error function (root mean squared error) to be minimized. # Noteworthy F's degrees of freedom are rounded since they # are required to be integers. rmse <- function(params) { sqrt(sum((df(f$x, params[1], params[2]) - f$y)^2)) } # L-BFGS-B optimization. o <- optim(c(50, 50), # Initial parameters. rmse, lower=c(0, 0), # F's degrees of freedom's domain upper=c(2^16, 2^16), # is [0,Inf). method='L-BFGS-B', control=list(trace=3)) print(o)
Similar Posts:
- Solved – L-BFGS-B converging in iteration 0 in R
- Solved – How to write log-likelihood for beta-geometric with optim() in R
- Solved – How to write log-likelihood for beta-geometric with optim() in R
- Solved – Defining gradient function argument in optim function-R
- Solved – Defining gradient function argument in optim function-R