---
title: "Alternating optimization"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Alternating optimization}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: ref.bib
link-citations: true
---

```{r, setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "ao-"
)
```

## Background

### What is alternating optimization?

Alternating optimization is an iterative procedure for optimizing some function jointly over all parameters by alternating restricted optimization over individual parameter subsets.

More precisely, consider minimizing or maximizing $f:\mathbb{R}^n \to \mathbb{R}$ over the set of feasible points $x \in X \subseteq \mathbb{R}^n$. The underlying algorithm of alternating optimization is as follows:

1. Assign an initial value for $x$.

2. Optimize $f$ with respect to a subset of parameters $\tilde{x}$ while holding the other parameters constant. Note that alternating optimization is a generalization of joint optimization, where the only parameter subset would be the whole set of parameters.

3. Replace the values in $x$ by the optimal values for $\tilde{x}$ found in step 2.

4. Repeat from step 2 with another parameter subset.

5. Stop when the process has converged or reached the iteration limit.

### When is alternating optimization a good idea?

- When the joint optimization is numerically difficult or not feasible.

- When there is a natural division of the parameters. That is the case for likelihood functions, where the parameter space naturally divides into parameter subsets, e.g. corresponding to linear effects, variances and covariances with different influence on the likelihood value.

- To improve optimization time in some cases, see [@hu:2002] for an example.

- Compared to joint optimization, alternating optimization may be better in bypassing local optima, see [@bezdek:2002].

### What are the properties of alternating optimization?

Alternating optimization under certain conditions on $f$ can convergence to the global optimum. However, the set of possible solutions also contains saddle points [@bezdek:1987]. @bezdek:2003 provides additional details on the convergence rate.

## Application

As an application, we consider minimizing the [Himmelblau's function](https://en.wikipedia.org/wiki/Himmelblau%27s_function) in $n = 2$ dimensions with parameter constraints $-5 \leq x_1, x_2 \leq 5$:

```{r, ao demo}
library("ao")
himmelblau <- function(x) (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
ao(
  f = himmelblau, p = c(0, 0), partition = list(1, 2),
  base_optimizer = optimizeR::Optimizer$new(
    which = "stats::optim", lower = -5, upper = 5, method = "L-BFGS-B"
  )
)
```

The call minimizes `f` by alternating optimizing with respect to each parameter separately, where the parameters all are initialized at the value 0. 

## References
