Skip to content

Commit

Permalink
- updated optimizeAPA tutorial post
Browse files Browse the repository at this point in the history
  • Loading branch information
mrparker909 committed Jun 26, 2020
1 parent ed78623 commit 6692d2d
Show file tree
Hide file tree
Showing 24 changed files with 18,373 additions and 22 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 47 additions & 10 deletions content/post/2020-04-05-optimizeAPA-tutorial/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ library(ggplot2)
library(optimizeAPA)
library(gmp)
library(Rmpfr)
library(knitr)
library(rgl)
```

<tt>optimizeAPA</tt> is an R package which allows for multi-parameter optimization. That means you can use it to find the maximum (or the minimum) value of a function with many input values. What makes <tt>optimizeAPA</tt> unique? It works with arbitrary precision arithmetic.
Expand Down Expand Up @@ -105,8 +107,9 @@ remotes::install_github("mrparker909/optimizeAPA")
For the folowing examples, we will use these libraries:

```{r eval=F}
library(ggplot2)
library(optimizeAPA)
library(ggplot2) # for 2d plots
library(optimizeAPA) # for functional optimization
library(rgl) # for interactive 3d plots
```

### Single Parameter Function
Expand Down Expand Up @@ -150,8 +153,8 @@ op1 <- optimizeAPA::optim_DFP_NAPA(starts=3.5,

Let's look at the path to convergence:

```{r fig.cap="Plot of convergence for single parameter function F1 (NAPA).", fig.width=6, fig.height=6, warning=F, message=F}
optimizeAPA::plotConvergence(op1)
```{r fig.cap="Plot of convergence for single parameter function F1 (NAPA).", fig.width=10, fig.height=4, warning=F, message=F}
optimizeAPA::plotConvergence(op1, flip_axes=T)
```

So the maximum value of the function (found after 11 iterations of the algorithm) is $F_0 = F1(3.2543) = 4.235$.
Expand Down Expand Up @@ -181,8 +184,8 @@ op2 <- optimizeAPA::optim_DFP_APA(starts=3.5,

Let's look at the path to convergence:

```{r fig.cap="Plot of convergence for single parameter function F2 (APA).", fig.width=6, fig.height=6}
optimizeAPA::plotConvergence(op2)
```{r fig.cap="Plot of convergence for single parameter function F2 (APA).", fig.width=10, fig.height=4}
optimizeAPA::plotConvergence(op2, flip_axes = T)
```

So the convergence plots look the same, but let's compare the values found by the two algorithms:
Expand Down Expand Up @@ -247,10 +250,10 @@ op3 <- optimizeAPA::optim_DFP_NAPA(starts=c(x[29],y[29]),

Again we can look at the convergence plot:

```{r fig.cap="Plot of convergence for multi-parameter function F3 (NAPA).", fig.width=9, fig.height=9}
```{r fig.cap="Plot of convergence for multi-parameter function F3 (NAPA).", fig.width=10, fig.height=4.5}
# this time we disable the function labels for legibility
# using: labels = F
optimizeAPA::plotConvergence(op3, labels = F)
optimizeAPA::plotConvergence(op3, labels = F, flip_axes = T)
```

The algorithm converged after `r op3$steps` iterations. We can get at the maximum function value and at the values of $x$ and $y$ which jointly maximize the function:
Expand All @@ -262,6 +265,40 @@ op3$x[[1]] # note that "x" here denotes c(x,y)

The maximum value of $F3$ is `r -1*op3$f[[1]]`, and that value is attained by $x=$ `r op3$x[[1]][1]`, and $y=$ `r op3$x[[1]][2]`.

We can also visualize what has happened by plotting the convergence path on the 3D plot of the function:


```{r eval=F}
# here we define an x,y coordinate grid, and we
# calculate z=F3(x,y) at each point of the grid
x = seq(3.23,3.29,length=50)
y = seq(2.23,2.29,length=50)
z = outer(x,y,FUN = Vectorize(function(x,y) { F3(par=c(x,y)) }))
# using rgl
persp3d(x,y,z,col="dodgerblue",border="navy",
theta=30,phi=22,shade=0.75,
xaxs="i",yaxs="i",
ltheta=-55,lphi=30, alpha=0.45)
lines3d(x=unlist(op3$x)[c(T,F)],
y=unlist(op3$x)[c(F,T)],
z=-1*unlist(op3$f),
col="blue", size=5)
points3d(x=unlist(op3$x)[c(T,F)],
y=unlist(op3$x)[c(F,T)],
z=-1*unlist(op3$f),
col="red", size=5)
rgl.postscript(filename = "img/rgl-snapshot.pdf", fmt = "pdf")
```


```{r echo=F, fig.cap="Plot of multi-parameter function (zoomed near maximum). Visited points shown in red, path of convergence traced in blue."}
knitr::include_graphics("img/convergence-path-zoomed.png")
```



Let's make $F3$ APA:


Expand All @@ -286,10 +323,10 @@ op4 <- optimizeAPA::optim_DFP_APA(starts=c(x[29],y[29]),

This time the convergence plot is slightly different, and the convergence took `r op4$steps` steps to achieve:

```{r fig.cap="Plot of convergence for multi-parameter function F4 (APA).", fig.width=9, fig.height=9}
```{r fig.cap="Plot of convergence for multi-parameter function F4 (APA).", fig.width=10, fig.height=4.5}
# this time we disable the function labels for legibility
# using: labels = F
optimizeAPA::plotConvergence(op4, labels = F)
optimizeAPA::plotConvergence(op4, labels = F, flip_axes = T)
```

```{r}
Expand Down
52 changes: 40 additions & 12 deletions content/post/2020-04-05-optimizeAPA-tutorial/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ <h2>Install <tt>optimizeAPA</tt></h2>
<div id="examples" class="section level2">
<h2>Examples</h2>
<p>For the folowing examples, we will use these libraries:</p>
<pre class="r"><code>library(ggplot2)
library(optimizeAPA)</code></pre>
<pre class="r"><code>library(ggplot2) # for 2d plots
library(optimizeAPA) # for functional optimization
library(rgl) # for interactive 3d plots</code></pre>
<div id="single-parameter-function" class="section level3">
<h3>Single Parameter Function</h3>
<p>First, let’s define a regular function <tt>F1</tt> to optimize:</p>
Expand Down Expand Up @@ -107,9 +108,9 @@ <h3>Single Parameter Function</h3>
keepValues = T,
tolerance=10^-3)</code></pre>
<p>Let’s look at the path to convergence:</p>
<pre class="r"><code>optimizeAPA::plotConvergence(op1)</code></pre>
<pre class="r"><code>optimizeAPA::plotConvergence(op1, flip_axes=T)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-9"></span>
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-9-1.png" alt="Plot of convergence for single parameter function F1 (NAPA)." width="576" />
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-9-1.png" alt="Plot of convergence for single parameter function F1 (NAPA)." width="960" />
<p class="caption">
Figure 2: Plot of convergence for single parameter function F1 (NAPA).
</p>
Expand All @@ -130,9 +131,9 @@ <h3>Single Parameter Function</h3>
tolerance=10^-3,
precBits = 64)</code></pre>
<p>Let’s look at the path to convergence:</p>
<pre class="r"><code>optimizeAPA::plotConvergence(op2)</code></pre>
<pre class="r"><code>optimizeAPA::plotConvergence(op2, flip_axes = T)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-12"></span>
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-12-1.png" alt="Plot of convergence for single parameter function F2 (APA)." width="576" />
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-12-1.png" alt="Plot of convergence for single parameter function F2 (APA)." width="960" />
<p class="caption">
Figure 3: Plot of convergence for single parameter function F2 (APA).
</p>
Expand Down Expand Up @@ -207,9 +208,9 @@ <h3>Multi-Parameter Function</h3>
<p>Again we can look at the convergence plot:</p>
<pre class="r"><code># this time we disable the function labels for legibility
# using: labels = F
optimizeAPA::plotConvergence(op3, labels = F)</code></pre>
optimizeAPA::plotConvergence(op3, labels = F, flip_axes = T)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-18"></span>
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-18-1.png" alt="Plot of convergence for multi-parameter function F3 (NAPA)." width="864" />
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-18-1.png" alt="Plot of convergence for multi-parameter function F3 (NAPA)." width="960" />
<p class="caption">
Figure 5: Plot of convergence for multi-parameter function F3 (NAPA).
</p>
Expand All @@ -222,6 +223,33 @@ <h3>Multi-Parameter Function</h3>
## [1,] 3.254288
## [2,] 2.254288</code></pre>
<p>The maximum value of <span class="math inline">\(F3\)</span> is 17.9352205, and that value is attained by <span class="math inline">\(x=\)</span> 3.2542878, and <span class="math inline">\(y=\)</span> 2.2542877.</p>
<p>We can also visualize what has happened by plotting the convergence path on the 3D plot of the function:</p>
<pre class="r"><code># here we define an x,y coordinate grid, and we
# calculate z=F3(x,y) at each point of the grid
x = seq(3.23,3.29,length=50)
y = seq(2.23,2.29,length=50)
z = outer(x,y,FUN = Vectorize(function(x,y) { F3(par=c(x,y)) }))

# using rgl
persp3d(x,y,z,col=&quot;dodgerblue&quot;,border=&quot;navy&quot;,
theta=30,phi=22,shade=0.75,
xaxs=&quot;i&quot;,yaxs=&quot;i&quot;,
ltheta=-55,lphi=30, alpha=0.45)
lines3d(x=unlist(op3$x)[c(T,F)],
y=unlist(op3$x)[c(F,T)],
z=-1*unlist(op3$f),
col=&quot;blue&quot;, size=5)
points3d(x=unlist(op3$x)[c(T,F)],
y=unlist(op3$x)[c(F,T)],
z=-1*unlist(op3$f),
col=&quot;red&quot;, size=5)
rgl.postscript(filename = &quot;img/rgl-snapshot.pdf&quot;, fmt = &quot;pdf&quot;)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-21"></span>
<img src="img/convergence-path-zoomed.png" alt="Plot of multi-parameter function (zoomed near maximum). Visited points shown in red, path of convergence traced in blue." />
<p class="caption">
Figure 6: Plot of multi-parameter function (zoomed near maximum). Visited points shown in red, path of convergence traced in blue.
</p>
</div>
<p>Let’s make <span class="math inline">\(F3\)</span> APA:</p>
<pre class="r"><code>F4 &lt;- function(par, precBits=53) {
x = par[1]
Expand All @@ -239,11 +267,11 @@ <h3>Multi-Parameter Function</h3>
<p>This time the convergence plot is slightly different, and the convergence took 38 steps to achieve:</p>
<pre class="r"><code># this time we disable the function labels for legibility
# using: labels = F
optimizeAPA::plotConvergence(op4, labels = F)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-22"></span>
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-22-1.png" alt="Plot of convergence for multi-parameter function F4 (APA)." width="864" />
optimizeAPA::plotConvergence(op4, labels = F, flip_axes = T)</code></pre>
<div class="figure"><span id="fig:unnamed-chunk-24"></span>
<img src="/post/2020-04-05-optimizeAPA-tutorial/index_files/figure-html/unnamed-chunk-24-1.png" alt="Plot of convergence for multi-parameter function F4 (APA)." width="960" />
<p class="caption">
Figure 6: Plot of convergence for multi-parameter function F4 (APA).
Figure 7: Plot of convergence for multi-parameter function F4 (APA).
</p>
</div>
<pre class="r"><code>knitr::kable(data.frame(algorithm = c(&quot;F3: NAPA&quot;, &quot;F4: APA&quot;),
Expand Down
24 changes: 24 additions & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="{{ site.LanguageCode | default "en-us" }}">

{{ partial "site_head.html" . }}
<body id="top" data-spy="scroll" data-offset="70" data-target="{{ if or .IsHome (eq .Type "widget_page") }}#navbar-main{{else}}#TableOfContents{{end}}" {{ if not (.Scratch.Get "light") }}class="dark"{{end}}>

{{ partial "search" . }}

{{ partial "navbar.html" . }}

{{ block "main" . }}{{ end }}

{{ partial "site_js.html" . }}

{{ if ne .Type "docs" }}
<div class="container">
{{ partial "site_footer.html" . }}
</div>
{{ end }}

{{ partial "citation.html" . }}

</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6692d2d

Please sign in to comment.