output | ||||
---|---|---|---|---|
|
Arbitrary Precision Arithmetic (APA) optimization algorithms implemented in R.
Throughout I will use the abreviations APA = "arbitrary precision arithmetic", and NAPA="non arbitrary precision arithmetic".
The APA in this package is achieved using the packages Rmpfr and gmp.
install.packages("devtools")
devtools::install_github("mrparker909/optimizeAPA")
- optim_DFP_NAPA (non-APA version of DFP algorithm)
- optim_DFP_APA (APA version of DFP algorithm)
- optim_BFGS_NAPA (non-APA version of BFGS algorithm)
- optim_BFGS_APA (APA version of BFGS algorithm)
library(optimizeAPA)
# notice that the function must be made using APA arithmetic from the package Rmpfr
# the function inputs are first: the parameter to be optimized over (x)
# and must also include the variable precBits, to be passed to all APA numbers.
# Any other function parameters (such as center) can be passed to the function
# when calling optim_DFP_APA
# function we would like to optimize:
quadratic <- function(x, center, precBits=64) {
x_apa <- Rmpfr::mpfr(x,precBits) # convert input to APA
(x_apa-center)^2
}
# APA optimization
optim_DFP_APA(starts=0, func=quadratic, center=10, precBits = 64)
## $x
## 'mpfrMatrix' of dim(.) = (1, 1) of precision 64 bits
## [,1]
## [1,] 9.99999999996157024670
##
## $f
## 'mpfrMatrix' of dim(.) = (1, 1) of precision 64 bits
## [,1]
## [1,] 1.47684593894512519704e-21
##
## $grad
## 1 'mpfr' number of precision 64 bits
## [1] -7.68595066064081677837e-11
##
## $inv_Hessian
## 'mpfrMatrix' of dim(.) = (1, 1) of precision 64 bits
## [,1]
## [1,] 0.500000000000000000000
##
## $steps
## [1] 4
##
## $converged
## [1] TRUE
The minimum is attained at
We can compare these results to the optim_DFP_NAPA results, and to the regular optim results:
# function we would like to optimize:
quadratic_NAPA <- function(x, center) {
(x-center)^2
}
# NAPA optimization
optim_DFP_NAPA(starts=0, func=quadratic_NAPA, center=10)
## $x
## [,1]
## [1,] 10
##
## $f
## [,1]
## [1,] 4.416905e-20
##
## $grad
## [1] 4.203287e-10
##
## $inv_Hessian
## [,1]
## [1,] 0.5
##
## $steps
## [1] 2
##
## $converged
## [1] TRUE
optim(par=0, fn=quadratic_NAPA, hessian=TRUE, method="BFGS", center=10)
## $par
## [1] 10
##
## $value
## [1] 2.839899e-29
##
## $counts
## function gradient
## 11 3
##
## $convergence
## [1] 0
##
## $message
## NULL
##
## $hessian
## [,1]
## [1,] 2
It can be desirable to retain the convergence path for diagnosis and visualization purposes. This can be done easily with both the APA and NAPA version of the optimization algorithm:
library(ggplot2) # ggplot for plotting
# just set keepValues to TRUE to keep every visited value
op <- optim_DFP_NAPA(starts=0, func=quadratic_NAPA, center=10, keepValues = TRUE)
# convergence path plot:
plotConvergence(op) + ggtitle("Convergence Path")
And the same for the APA version:
# just set keepValues to TRUE to keep every visited value
op <- optim_DFP_APA(starts=0, func=quadratic, center=10, keepValues = TRUE)
# convergence path plot:
plotConvergence(op)
Parker, M.R.P. (2020). optimizeAPA: An R Package for Arbitrary Precision Optimization of Multi-parameter Functions. R package version 1.0.1. https://github.com/mrparker909/optimizeAPA