<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/discrete-or-continuous-modeling/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://freakonometrics.hypotheses.org/53197"> R-english – Freakonometrics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Tuesday, we got our conference “Insurance, Actuarial Science, Data &amp; Models” and <a rel="nofollow" href="https://sites.google.com/site/possamaidylan/" target="_blank">Dylan Possamaï</a> gave a very interesting concluding talk. In the introduction, he came back briefly on a nice discussion we usually have in economics on the kind of model we should consider. It was about optimal control. In many applications, we start with a one period economy, then a two period economy, and pretend that we can extend it to <img alt="n" title="n" src="https://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex"> period economy. And then, the continuous case can also be considered. A few years ago, I was working on sports game as an optimal effort startegy (within in a game – fixed time). It was with a discrete model, I was running simulations to get an efficient frontier, where coaches might say “ok, now we have enough (positive) difference, and we get closer to the end of the game, so we can ‘lower the effort’ i.e. top players can relax a little bit” (it was on basket-ball games). I asked a good friend of mine, <a rel="nofollow" href="http://perso-math.univ-mlv.fr/users/elie.romuald/elie.html" target="_blank">Romuald</a>, to help me on some technical parts of proofs, but he did not like so much my discrete-time model, and wanted to move to continuous time. And for now six years, we keep saying that someday we should get back to that paper….</p>
<p>My initial thoughts were that the difference was really “cultural”: you are either a continuous-time sort of guy, or a discrete-time one (or maybe none of the two, but that’s another problem). He works with stochastic processes, I work with time series. Of course, we can find connections, but most of the time, the techniques are very different. And tuesday, <a rel="nofollow" href="https://sites.google.com/site/possamaidylan/" target="_blank">Dylan</a> mentioned a very nice illustration that it’s not necessarily a cultural difference, and sometimes, it is great to move to continuous time. So I wanted to illustrate that idea.</p>
<p>Consider for instance the following curve.</p>
<div><table><tr><td><pre>1
2
3
</pre></td><td><pre>vu <span>=</span> <span>seq</span><span>(</span><span>0</span>,<span>1</span>,<span>length</span><span>=</span><span>601</span><span>)</span>
vv <span>=</span> <span>sin</span><span>(</span>vu<span>*</span><span>pi</span><span>)</span>
<span>plot</span><span>(</span>vu,vv,type<span>=</span><span>&quot;l&quot;</span>,lwd<span>=</span><span>2</span><span>)</span></pre></td></tr></table></div>
<p><img src="https://i2.wp.com/f.hypotheses.org/wp-content/blogs.dir/253/files/2018/06/Capture-d%E2%80%99e%CC%81cran-2018-06-13-a%CC%80-14.04.17.png?resize=307%2C269&ssl=1" alt="" width="307" data-recalc-dims="1" height="269"></p>
<p>The goal is to find the value of the maximum, numerically. And here, there are two (very) different strategies</p>
<ul>
<li>the discrete one: we see a (finite) collection of points – for instance, the graph above is a collection of 601 points (connected with a straight line) – and in that case, we need a standard algorithm (in <img alt="O(n)" title="O(n)" src="https://s0.wp.com/latex.php?latex=O%28n%29&bg=ffffff&fg=000&s=0" class="latex">) to get the value of the maximum</li>
<li>the continuous one: we see a function <img alt="x\mapsto \sin(\pi x)" title="x\mapsto \sin(\pi x)" src="https://s0.wp.com/latex.php?latex=x%5Cmapsto+%5Csin%28%5Cpi+x%29&bg=ffffff&fg=000&s=0" class="latex">, and in that case, we use optimization routines</li>
</ul>
<p>In the second case, use for instance</p>
<div><table><tr><td><pre>1
2
3
4
5
6
</pre></td><td><pre><span>optim</span><span>(</span><span>0</span>,<span>function</span><span>(</span>x<span>)</span> <span>-</span><span>sin</span><span>(</span><span>pi</span><span>*</span>x<span>)</span><span>)</span>
$par
<span>[</span><span>1</span><span>]</span> <span>0.5</span>
 
$value
<span>[</span><span>1</span><span>]</span> <span>-</span><span>1</span></pre></td></tr></table></div>
<p>For the first case, we can use the standard R function, and see how long it takes to use simulations to get an approximation of the maximum</p>
<div><table><tr><td><pre>1
2
3
4
5
</pre></td><td><pre><span>library</span><span>(</span>microbenchmark<span>)</span>
max_time <span>=</span> <span>function</span><span>(</span>n<span>)</span> <span>median</span><span>(</span>microbenchmark<span>(</span><span>max</span><span>(</span><span>sin</span><span>(</span><span>runif</span><span>(</span>n<span>)</span><span>*</span><span>pi</span><span>)</span><span>)</span><span>)</span>$time<span>)</span>
vn <span>=</span> <span>10</span><span>^</span><span>(</span><span>seq</span><span>(</span><span>1</span>,<span>6</span>,<span>length</span><span>=</span><span>21</span><span>)</span><span>)</span>
vt <span>=</span> <span>Vectorize</span><span>(</span>max_time<span>)</span><span>(</span>vn<span>)</span>
<span>plot</span><span>(</span>vn,vt<span>/</span>1e9,<span>col</span><span>=</span><span>&quot;blue&quot;</span>,pch<span>=</span><span>19</span>,type<span>=</span><span>&quot;b&quot;</span>,<span>log</span><span>=</span><span>&quot;xy&quot;</span><span>)</span></pre></td></tr></table></div>
<p>but of course, some home-made code can also be used</p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
8
</pre></td><td><pre>c_max <span>=</span> <span>function</span><span>(</span>n<span>=</span><span>100</span><span>)</span><span>{</span>
  x <span>=</span> <span>sin</span><span>(</span><span>runif</span><span>(</span>n<span>)</span><span>*</span><span>pi</span><span>)</span>
  y <span>=</span> x<span>[</span><span>1</span><span>]</span>
  <span>for</span><span>(</span>i <span>in</span> <span>2</span><span>:</span><span>length</span><span>(</span>x<span>)</span><span>)</span> <span>{</span> 
    <span>if</span><span>(</span>x<span>[</span>i<span>]</span> <span>&amp;</span>gt<span>;</span> y<span>)</span> <span>{</span> y <span>=</span> x<span>[</span>i<span>]</span> <span>}</span><span>}</span>
  <span>return</span><span>(</span>y<span>)</span><span>}</span>
max_time<span>=</span><span>function</span><span>(</span>n<span>)</span> <span>median</span><span>(</span>microbenchmark<span>(</span>c_max<span>(</span>n<span>)</span><span>)</span>$time<span>)</span>
<span>lines</span><span>(</span>vn,vt<span>/</span>1e9,type<span>=</span><span>&quot;b&quot;</span><span>)</span></pre></td></tr></table></div>
<p><img src="https://i2.wp.com/freakonometrics.hypotheses.org/files/2018/06/Capture-d%E2%80%99e%CC%81cran-2018-06-13-a%CC%80-21.20.30.png?resize=420%2C385&ssl=1" alt="" width="420" data-recalc-dims="1" height="385"></p>
<p>We can add that horizontal red line using</p>
<div><table><tr><td><pre>1
</pre></td><td><pre><span>abline</span><span>(</span>h<span>=</span><span>median</span><span>(</span>microbenchmark<span>(</span><span>optim</span><span>(</span>.5,<span>function</span><span>(</span>x<span>)</span> <span>sin</span><span>(</span><span>pi</span><span>*</span>x<span>)</span><span>)</span><span>)</span>$time<span>)</span><span>/</span>1e9,lty<span>=</span><span>2</span>,<span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span></pre></td></tr></table></div>
<p>So, indeed, it looks like computational time to find the maximum in a list of <img alt="n" title="n" src="https://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex"> elements is linear in <img alt="n" title="n" src="https://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex">, i.e. <img alt="O(n)" title="O(n)" src="https://s0.wp.com/latex.php?latex=O%28n%29&bg=ffffff&fg=000&s=0" class="latex">. And R code is faster than home-made code. But also, interestingly, using continus time (based on analysis techniques) can be much faster. So, sometimes, considering continuous time models can be much easier to solve, from a numerical perspective.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://freakonometrics.hypotheses.org/53197"> R-english – Freakonometrics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/MaiEAwm5nhM" height="1"></a></div>