| ao-package | R Documentation |
Implementation of an iterative process that optimizes a function by alternately performing restricted optimization over parameter subsets. Instead of solving one joint optimization problem, alternating optimization breaks it into smaller sub-problems. This approach can make optimization feasible when joint optimization is too difficult.
Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com (ORCID)
Authors:
Lennart Oelschläger oelschlaeger.lennart@gmail.com (ORCID)
Other contributors:
Siddhartha Chib chib@wustl.edu [contributor]
Useful links:
Report bugs at https://github.com/loelschlaeger/ao/issues
# Example 1: Minimization of Himmelblau's function --------------------------
himmelblau <- function(x) (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
ao(f = himmelblau, initial = c(0, 0))
# Example 2: Maximization of 2-class Gaussian mixture log-likelihood --------
normal_mixture_loglik_uc <- function(mu, logsd, eta, data) {
sd <- exp(logsd)
e <- exp(eta[1])
den <- 1 + e
q1 <- e / den
q2 <- 1 / den
l1 <- log(q1) + dnorm(data, mu[1], sd[1], log = TRUE)
l2 <- log(q2) + dnorm(data, mu[2], sd[2], log = TRUE)
m <- pmax(l1, l2)
sum(m + log(exp(l1 - m) + exp(l2 - m)))
}
set.seed(123)
data <- datasets::faithful$eruptions
fit <- ao(
f = normal_mixture_loglik_uc,
initial = c(mean(data) + c(-1, 1), rep(log(sd(data)), 2), 0),
target = c("mu", "logsd", "eta"),
npar = c(2, 2, 1),
data = data,
partition = "random",
base_optimizer = optimizeR::Optimizer$new("stats::nlm"),
minimize = FALSE,
add_details = FALSE
)
(muhat <- fit$estimate_split$mu)
(sdhat <- exp(fit$estimate_split$logsd))
e <- exp(fit$estimate_split$eta)
den <- 1 + e
(qhat <- c(e / den, 1 / den))
# Example 3: Constrained Optimization in the Setting of Example 2 -----------
# target arguments:
# - class means mu (2, unrestricted)
# - class standard deviations sd (2, must be non-negative)
# - class proportion lambda (only 1 for identification, must be in [0, 1])
normal_mixture_loglik <- function(mu, sd, lambda, data) {
c1 <- lambda * dnorm(data, mu[1], sd[1])
c2 <- (1 - lambda) * dnorm(data, mu[2], sd[2])
sum(log(c1 + c2))
}
set.seed(123)
ao(
f = normal_mixture_loglik,
initial = runif(5),
target = c("mu", "sd", "lambda"),
npar = c(2, 2, 1),
data = datasets::faithful$eruptions,
partition = list("sequential", "random", "none"),
minimize = FALSE,
lower = c(-Inf, -Inf, 0, 0, 0),
upper = c(Inf, Inf, Inf, Inf, 1),
add_details = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.