vignettes/mixsqp-intro.Rmd
mixsqp-intro.Rmd
In this vignette, we illustrate the use of the sequential quadratic programming (SQP) algorithm implemented in mixsqp
, and we compare its runtime and accuracy against an interior-point (IP) solver implemented by the MOSEK commercial software (it is called by the “KWDual” function in the REBayes package).
If you do not have the Rmosek and REBayes packages installed on your computer, you may skip over these steps in the vignette.
Load the mixsqp
package.
library(mixsqp)
Next, initialize the sequence of pseudorandom numbers.
set.seed(1)
We begin with a small example to show how mixsqp
works.
L <- simulatemixdata(1000,20)$L
dim(L)
# [1] 1000 20
This call to simulatemixdata
created an \(n \times m\) conditional likelihood matrix for a mixture of zero-centered normals, with \(n = 1000\) and \(m = 20\). By default, simulatemixdata
normalizes the rows of the likelihood matrix so that the maximum entry in each row is 1.
Now we fit the mixture model using the SQP algorithm:
fit.sqp <- mixsqp(L)
# Running mix-SQP algorithm 0.3-17 on 1000 x 20 matrix
# convergence tol. (SQP): 1.0e-08
# conv. tol. (active-set): 1.0e-10
# zero threshold (solution): 1.0e-08
# zero thresh. (search dir.): 1.0e-10
# l.s. sufficient decrease: 1.0e-02
# step size reduction factor: 7.5e-01
# minimum step size: 1.0e-08
# max. iter (SQP): 1000
# max. iter (active-set): 20
# number of EM iterations: 10
# Computing SVD of 1000 x 20 matrix.
# Matrix is not low-rank; falling back to full matrix.
# iter objective max(rdual) nnz stepsize max.diff nqp nls
# 1 +6.825854577e-01 -- EM -- 20 1.00e+00 3.43e-02 -- --
# 2 +6.608901451e-01 -- EM -- 20 1.00e+00 1.12e-02 -- --
# 3 +6.501638059e-01 -- EM -- 20 1.00e+00 8.83e-03 -- --
# 4 +6.441429919e-01 -- EM -- 20 1.00e+00 7.64e-03 -- --
# 5 +6.405380236e-01 -- EM -- 20 1.00e+00 6.44e-03 -- --
# 6 +6.382624101e-01 -- EM -- 20 1.00e+00 5.36e-03 -- --
# 7 +6.367521107e-01 -- EM -- 20 1.00e+00 4.46e-03 -- --
# 8 +6.357010188e-01 -- EM -- 20 1.00e+00 3.75e-03 -- --
# 9 +6.349367202e-01 -- EM -- 20 1.00e+00 3.18e-03 -- --
# 10 +6.343585101e-01 -- EM -- 20 1.00e+00 2.73e-03 -- --
# 1 +6.343585101e-01 +2.161e-02 20 ------ ------ -- --
# 2 +6.331717425e-01 +8.779e-03 4 1.00e+00 4.36e-01 20 1
# 3 +6.282364710e-01 +8.715e-05 4 1.00e+00 3.39e-03 2 1
# 4 +6.281978492e-01 +9.817e-09 4 1.00e+00 4.13e-05 2 1
# Optimization took 0.01 seconds.
# Convergence criteria met---optimal solution found.
In this example, the SQP algorithm converged to a solution in a small number of iterations.
By default, mixsqp
outputs information on its progress. It begins by summarizing the optimization problem and the algorithm settings used. (Since we did not change these settings in the mixsqp
call, all the settings shown here are the default settings.)
After that, it outputs, at each iteration, information about the current solution, such as the value of the objective (“objective”) and the number of nonzeros (“nnz”).
The “max(rdual)” column shows the quantity used to assess convergence. It reports the maximum value of the “dual residual”; the SQP solver terminates when the maximum dual residual is less than conv.tol
, which by default is \(10^{-8}\). In this example, we see that the dual residual shrinks rapidly toward zero.
Another useful indicator of convergence is the “max.diff” column—it reports the maximum difference between the solution estimates at two successive iterations. We normally expect these differences to shrink as we approach the solution, which is precisely what we see in this example.
This information is also provided in the return value, which we can use, for example, to create a plot of the objective value at each iteration of the SQP algorithm:
numiter <- nrow(fit.sqp$progress)
plot(1:numiter,fit.sqp$progress$objective,type = "b",
pch = 20,lwd = 2,xlab = "SQP iteration",
ylab = "objective",xaxp = c(1,numiter,numiter - 1))
To assess the accuracy of the SQP solution, we can compare against the solution computed by the IP algorithm. (If you do not have the REBayes package installed, you can skip this step.)
fit.ip <- mixkwdual(L)
If you run the IP algorithm, you should see that the IP and SQP solutions achieve nearly the same objective value.
We observed that the SQP and IP methods achieve nearly the same solution quality in the example above. Here, we explore the computational properties of the SQP and IP algorithms in a larger data set.
As before, we compute the \(n \times m\) likelihood matrix for a mixture of zero-centered normals. This time, we use a finer grid of \(m = 100\) normal densities, as well as many more samples.
L <- simulatemixdata(1e5,100)$L
dim(L)
# [1] 100000 100
Now we fit the model using the SQP algorithm:
timing <- system.time(fit.sqp <- mixsqp(L))
cat(sprintf("Computation took %0.2f seconds\n",timing["elapsed"]))
# Running mix-SQP algorithm 0.3-17 on 100000 x 100 matrix
# convergence tol. (SQP): 1.0e-08
# conv. tol. (active-set): 1.0e-10
# zero threshold (solution): 1.0e-08
# zero thresh. (search dir.): 1.0e-10
# l.s. sufficient decrease: 1.0e-02
# step size reduction factor: 7.5e-01
# minimum step size: 1.0e-08
# max. iter (SQP): 1000
# max. iter (active-set): 20
# number of EM iterations: 10
# Computing SVD of 100000 x 100 matrix.
# SVD computation took 3.31 seconds.
# Rank of matrix is estimated to be 24.
# iter objective max(rdual) nnz stepsize max.diff nqp nls
# 1 +6.740173241e-01 -- EM -- 100 1.00e+00 8.05e-03 -- --
# 2 +6.466245907e-01 -- EM -- 100 1.00e+00 2.22e-03 -- --
# 3 +6.355057462e-01 -- EM -- 100 1.00e+00 1.58e-03 -- --
# 4 +6.297197672e-01 -- EM -- 100 1.00e+00 1.45e-03 -- --
# 5 +6.263196344e-01 -- EM -- 100 1.00e+00 1.27e-03 -- --
# 6 +6.241608434e-01 -- EM -- 100 1.00e+00 1.09e-03 -- --
# 7 +6.227044220e-01 -- EM -- 100 1.00e+00 9.48e-04 -- --
# 8 +6.216699411e-01 -- EM -- 100 1.00e+00 8.28e-04 -- --
# 9 +6.209017685e-01 -- EM -- 100 1.00e+00 7.31e-04 -- --
# 10 +6.203090889e-01 -- EM -- 100 1.00e+00 6.52e-04 -- --
# 1 +8.245148018e-01 +9.539e-01 20 ------ ------ -- --
# 2 +8.571386134e-01 +5.699e-01 20 7.50e-01 5.85e-01 20 2
# 3 +7.965014609e-01 +2.635e-01 4 1.00e+00 4.26e-01 20 1
# 4 +6.443970186e-01 +5.486e-02 8 1.00e+00 1.89e-01 20 1
# 5 +6.161076010e-01 +5.271e-03 7 1.00e+00 1.18e-01 20 1
# 6 +6.150624351e-01 +8.525e-05 7 1.00e+00 1.27e-01 11 1
# 7 +6.150581385e-01 +4.132e-08 7 1.00e+00 7.18e-04 2 1
# 8 +6.150581378e-01 +1.808e-08 7 5.62e-01 2.61e-07 2 3
# 9 +6.150581375e-01 +7.909e-09 7 5.62e-01 1.14e-07 2 3
# Optimization took 3.59 seconds.
# Convergence criteria met---optimal solution found.
# Computation took 7.82 seconds
If you have the REBayes package, you can also run the IP method:
timing <- system.time(fit.ip <- mixkwdual(L))
cat(sprintf("Computation took %0.2f seconds\n",timing["elapsed"]))
If you run the IP algorithm, you should find that the SQP algorithm was considerably faster than the IP solver, and it converged to a solution with nearly the same objective value as the IP solution.
This next code chunk gives information about the computing environment used to generate the results contained in this vignette, including the version of R and the packages used.
sessionInfo()
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS Catalina 10.15.2
#
# Matrix products: default
# BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] mixsqp_0.3-18
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 knitr_1.26 magrittr_1.5 REBayes_1.8
# [5] MASS_7.3-51.4 lattice_0.20-38 R6_2.4.1 rlang_0.4.2
# [9] stringr_1.4.0 tools_3.6.2 grid_3.6.2 xfun_0.11
# [13] irlba_2.3.3 htmltools_0.4.0 yaml_2.2.0 assertthat_0.2.1
# [17] digest_0.6.23 rprojroot_1.3-2 pkgdown_1.4.1 crayon_1.3.4
# [21] Matrix_1.2-18 fs_1.3.1 memoise_1.1.0 evaluate_0.14
# [25] rmarkdown_2.0 stringi_1.4.3 compiler_3.6.2 desc_1.2.0
# [29] backports_1.1.5 Rmosek_9.0.96