<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Matthieu Brucher's blog</title>
	
	<link>http://matt.eifelle.com</link>
	<description />
	<lastBuildDate>Thu, 13 Jun 2013 07:59:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/eifelle/CPPV" /><feedburner:info uri="eifelle/cppv" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Book review: Numpy Beginner’s Guide</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/g0UBdHwaemk/</link>
		<comments>http://matt.eifelle.com/2013/06/13/book-review-numpy-beginners-guide/#comments</comments>
		<pubDate>Thu, 13 Jun 2013 07:59:36 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Book review]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Packt Publishing]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[scipy]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2207</guid>
		<description><![CDATA[I had the opportunity from Packt Publishing to review the second edition of Numpy Beginner&#8217;s Guide. Many thanks to the publisher for this and let&#8217;s go to the review. Content and opinions The first chapter introduces the reader to the scientific ecosystem. The main modules are covered, but there are some small mistakes (ipython &#8211;pylab [...]]]></description>
				<content:encoded><![CDATA[<p>I had the opportunity from Packt Publishing to review the second edition of <a href="http://www.packtpub.com/numpy-mathematical-2e-beginners-guide/book">Numpy Beginner&#8217;s Guide</a>. Many thanks to the publisher for this and let&#8217;s go to the review.</p>
<p><span id="more-2207"></span></p>
<h4>Content and opinions</h4>
<p>The first chapter introduces the reader to the scientific ecosystem. The main modules are covered, but there are some small mistakes (ipython &#8211;pylab does not import Matplotlib, Numpy ans Scipy, just a subset of the first that gives a Matlab-like interface aith some renamed Numpy functions) and what I think is a bad habit (importing everything from Pylab and using it as is). Nothing is lost because in the second chapter, Numpy is properly introduced and imported explicitly. There is a link missing between the two chapters because I didn&#8217;t understand why Numpy was used this way and not with pylab.</p>
<p>So the second chapter is about the core Numpy data structure: the multidimensional array. The author browses through different ways of creating them, by stacking them, flattening them&#8230; The next chapter deals with universal functions, or put it in an other way, functions that run on array element by elements. There are many different way to do so, with specifics that are tackled properly.</p>
<p>Before the chapter of matrices, there is a useful chapter on correlation, convolution and polynomials. Although one may want to go up to Scipy for signal processing, more often than not, Numpy is enough. There enough examples to understand how everything work. Then, the much hated matrix class is introduced. There are many discussions online on whether this class is actually useful, and I won&#8217;t delve on this. Suffice to say that the power of this class can be seen in the examples.</p>
<p>The following chapter is about the different submodules inside Numpy: linear algebra, fft, random number. The proper pointers to Scipy are provided, as well as explicit samples. I only can regret the time shifting example is not perfect, as a filter is applied on the amplitude and the inverse FFT is applied on the amplitudes only. So the transformed signal loses all the phase information, which may be why the image is similar but not that similar to the original one.</p>
<p>The seventh chapter tackles different extra functions, mainly finance-related, and I have to say that I don&#8217;t know their use enough to comment. Of course, this is why they are in the middle of the book and not in the first pages as the other Numpy functions. Still, there are some useful functions here (some I didn&#8217;t know about), as sorting, searching&#8230; </p>
<p>When one code scientific code, one often forget about testing. And Numpy has a nice module for scientific testing. It is nice to know that this aspect of science is not forgotten here and has a proper introduction. (also don&#8217;t forget about version control!)</p>
<p>The last three chapters introduces additional modules. The first one was partial addressed before, Matplotlib, and if you need something more advanced, there is always the Matplotlib book also published by Packt. Then there are some examples with Scipy, Scikits (soon a new book on Machine Learning with scikit-learn will be available, also by Packt, for which I was a technical reviewer, and it is really great) and other tools. The final chapter is about Pygame, but I don&#8217;t code games 100% in Python, so I didn&#8217;t really read it!</p>
<h4>Conclusion</h4>
<p>It&#8217;s hard to be mad at the author for the <em>import</em> issue. But I find it also difficult not to, as the philosophy changes depending on the chapter without saying why. Still, for an introductory book to Numpy, it is great if not excellent. A lot of simple examples, a lot of checks, the good pointers to write efficient code&#8230;</p>
<p><iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=masbl03-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins=1782166084" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe> <iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=masbl03-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins=B00CITNP76" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/g0UBdHwaemk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2013/06/13/book-review-numpy-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2013/06/13/book-review-numpy-beginners-guide/</feedburner:origLink></item>
		<item>
		<title>Annoucement: scikits.optimization 0.3</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/11vFIaLnWGA/</link>
		<comments>http://matt.eifelle.com/2013/05/11/annoucement-scikits-optimization-0-3/#comments</comments>
		<pubDate>Sat, 11 May 2013 06:58:05 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Generic optimizers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Annoucement]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Scientific computing]]></category>
		<category><![CDATA[scikit]]></category>
		<category><![CDATA[scipy]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2199</guid>
		<description><![CDATA[I&#8217;m please to announce a new version for scikits.optimization. The main focus of this iteration was to finish usual unconstrained optimization algorithms. Changelog Fixes on the Simplex state implementation Added several Quasi-Newton steps (BFGS, rank 1 update&#8230;) The scikit can be installed with pip/easy_install or downloaded from PyPI Old announces: 0.2 0.1 Buy Me a [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m please to announce a new version for scikits.optimization. The main focus of this iteration was to finish usual unconstrained optimization algorithms.</p>
<p>Changelog</p>
<ul>
<li>Fixes on the Simplex state implementation</li>
<li>Added several Quasi-Newton steps (BFGS, rank 1 update&#8230;)</li>
</ul>
<p>The scikit can be installed with pip/easy_install or downloaded from <a href="https://pypi.python.org/pypi/scikits.optimization/0.3">PyPI</a></p>
<p>Old announces:</p>
<ul>
<li><a href="http://matt.eifelle.com/2012/11/15/annoucement-scikits-optimization-0-2/" title="0.2">0.2</a></li>
<li><a href="http://matt.eifelle.com/2010/02/02/annoucement-scikits-optimization-0-1/" title="0.1">0.1</a></li>
</ul>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/11vFIaLnWGA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2013/05/11/annoucement-scikits-optimization-0-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2013/05/11/annoucement-scikits-optimization-0-3/</feedburner:origLink></item>
		<item>
		<title>Comparison of optimization algorithms</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/Yp6YUtJntbg/</link>
		<comments>http://matt.eifelle.com/2013/05/02/comparison-of-optimization-algorithms/#comments</comments>
		<pubDate>Thu, 02 May 2013 07:03:59 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Generic optimizers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[scikit]]></category>
		<category><![CDATA[scipy]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2152</guid>
		<description><![CDATA[In the next version of scikits.optimization, I&#8217;ve added some Quasi-Newton steps. Before this version is released, I thought I would compare several methods of optimizing the Rosenbrock function. Optimizers What is great with the Rosenbrock cost function can be summed up in a few points: It is hard to optimize Gradient can be easily computed [...]]]></description>
				<content:encoded><![CDATA[<p>In the next version of scikits.optimization, I&#8217;ve added some Quasi-Newton steps. Before this version is released, I thought I would compare several methods of optimizing the Rosenbrock function.<br />
<span id="more-2152"></span></p>
<h4>Optimizers</h4>
<p>What is great with the Rosenbrock cost function can be summed up in a few points:</p>
<ol>
<li>It is hard to optimize</li>
<li>Gradient can be easily computed</li>
</ol>
<p>I&#8217;ve decided to compare the number of function and gradient calls as well as the cost behavior for several usual optimization algorithms. So the contestants will be:</p>
<ul>
<li>A Nelder-Mead/Polytope/simplex optimizer</li>
<li>SSA, a simplex with simulated annealing (think of amotsa from Numerical Recipes)</li>
<li>GA, a genetic algorithm</li>
<li>a gradient-based optimizer</li>
<li>CG, a conjugate-gradient optimizer</li>
<li>BFGS, a quasi-Newton optimizer</li>
</ul>
<p>The first 4 are from scikits.optimization, the last 2 are based on proprietary code that cannot be published, but it&#8217;s interesting to compare with other tools that are used to compare gradient-free complex cost functions.</p>
<h4>Results</h4>
<p>I&#8217;ve made a small slideshow with the derivative-free algorithms. First you have for each of the three algorithms the number of function calls versus iteration, the cost versus iteration and finally the location of testing parameters.<br />
<a href="http://matt.eifelle.com/2013/05/02/comparison-of-optimization-algorithms/#gallery-2152-1-slideshow">Click to view slideshow.</a></p>
<p>This slideshow is for the derivative-based algorithms.<br />
<a href="http://matt.eifelle.com/2013/05/02/comparison-of-optimization-algorithms/#gallery-2152-2-slideshow">Click to view slideshow.</a></p>
<h4>Conclusion</h4>
<p>I was quite surprised by some algorithm behaviors. Clearly, the conjugate gradient algorithm behaves far better than the simple gradient, but the BFGS followed the Rosenbrock valley far better. A good quasi-Newton can be really efficient (not a brent because it needs to solve a linear equation), although a conjugate gradient can be enough in some cases.</p>
<p>For the gradient-free algorithms, SSA really behaved badly. This is mainly due because the hyperparameters that must be adequately tuned. This function is quite simple, but my first trial at setting these parameters was far more efficient for GA or the simplex than for SSA. So I would go for GA for gradient-free optimization: few and easy hyper parameters and a good browse of the search space.</p>
<p><a href="https://github.com/mbrucher/scikit-optimization-tutorials">The code for the 4 first tests and display plots</a></p>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/Yp6YUtJntbg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2013/05/02/comparison-of-optimization-algorithms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2013/05/02/comparison-of-optimization-algorithms/</feedburner:origLink></item>
		<item>
		<title>Book review: Numpy Cookbook</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/U2cwum60wBk/</link>
		<comments>http://matt.eifelle.com/2013/01/22/book-review-numpy-cookbook/#comments</comments>
		<pubDate>Tue, 22 Jan 2013 08:06:32 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Book review]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Packt Publishing]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[scipy]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2135</guid>
		<description><![CDATA[I had the opportunity from Packt Publishing to review Numpy Cookbook. Many thanks to the publisher for this and let&#8217;s go to the review. Content and opinions Numpy Cookbook is a list of several dozens recipes to use Numpy. Or so the title and subtitle say, but in fact it&#8217;s nothing like that. More or [...]]]></description>
				<content:encoded><![CDATA[<p>I had the opportunity from Packt Publishing to review <a href="http://www.packtpub.com/numpy-for-python-cookbook/book">Numpy Cookbook</a>. Many thanks to the publisher for this and let&#8217;s go to the review.</p>
<p><span id="more-2135"></span></p>
<h4>Content and opinions</h4>
<p><strong>Numpy Cookbook</strong> is a list of several dozens recipes to use Numpy. Or so the title and subtitle say, but in fact it&#8217;s nothing like that. More or less than 75% of the book are not about Numpy, but about the Numpy ecosystem, which is far better. This also means that the audience for the book is not Python beginners, but people who know about how the language works and are looking for advice on the scientific side.</p>
<p>The first chapter tackles IPython and more precisely a new feature called the notebook. The specific scientific side of it is the support of Matplotlib, the notebook being a good way of sharing scripts with plots between people. It is indeed a common problem that one may encounter. The next two chapters cover Numpy aspects: how arrays, indexing work and how functions can be applied on them. It is worth mentioning that the last recipe (the Sieve of Erathosthenes) from these chapters doesn&#8217;t work. I actually don&#8217;t know how the author expected it to work, the algorithm being wrong. There are also several installation recipes, but they tend to be slightly different each time. Why? No real explanation. This happens a lot with all the different installations. Sometimes there are even packages that don&#8217;t have such recipe. So no coherency here.</p>
<p>The next chapter handles communication between other Python modules, other languages or frameworks. A small overview of what is called the buffer protocol and the array interface is provided. They are both sides of the same coin, the inner Numpy way of working. Then other languages are covered: Matlab and Octave (but it seems that the author forgot going back from Matlab to Numpy and the fact that Matlab changed its I/O format and went to HDF5), R, Java and the Google App Engine. Mainly these recipes are about installing the interfaces, not about using them and then, there is only a small example of using them. I would have expecting something deeper. One still has to read the manual of these interfaces to use them even for something very basic.</p>
<p>Next two chapters are actual scientific recipes. Based on 1D and 2D signals, the first one tackles some modules in Scipy in addition to <strong>memmap</strong>, a way of using data from the disk without string it in memory. Unfortunately, this is not dealt in-depth, memmap may have needed more that just a few lines. The second chapter presents more Numpy classes that can be useful, especially masked arrays, as well as universal functions. It is advanced Numpy use and regularly helpful in complex algorithms.</p>
<p>Before the last chapter on Scikits, the author spends two chapters on something scientists often forget: profiling, debugging and quality insurance. Almost all the main modules in these domains are introduced, safe for <strong>nosetests</strong>, currently the main test framework. It&#8217;s a shame that it isn&#8217;t one of the recipes, as it supersedes all the modules of the tests recipes. Still, I really appreciate the presence of all these tips that all scientists should apply as they are too often overlooked on scientific books.</p>
<p>As I&#8217;ve said, the last chapter presents some scikits, application specific packages (machine learning, image), and <strong>pandas</strong>, a statistics module. I think of them as threads that readers can follow if they want to dig in a specific direction.</p>
<h4>Conclusion</h4>
<p>Let&#8217;s start with saying that the book is neither a good nor a bad book. It could have been a good book if the author had gone further into the content, and more precisely Numpy. My point of view is that the title, subtitle and everything on the cover cannot represent the book. It doesn&#8217;t solve common problems (finding prime number or a palindrome is not a common problem, it&#8217;s a pet problem but not an actual real-life problem) and too many recipes are installation recipes. One would have sufficed.</p>
<p>But the book is not a bad one. If the reader doesn&#8217;t know much of Numpy, it can help starting using it and as I&#8217;ve said, threads can be followed if a specific direction is of interest. It can also a sort of exercise book for a Numpy crash course, provided that some errors are fixed.</p>
<p>Final word: even if I would have liked more structure is the code (class and function), at least the infamous <strong>from pylab import *</strong> is not advocated in the book.</p>
<p><iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=masbl03-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins=1849518920" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe> <iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=masbl03-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins=B009X5KIH8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/U2cwum60wBk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2013/01/22/book-review-numpy-cookbook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2013/01/22/book-review-numpy-cookbook/</feedburner:origLink></item>
		<item>
		<title>Optimization scikit: Polytope (Simplex/Nelder-Mead) optimization</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/a2WfZWP9_vA/</link>
		<comments>http://matt.eifelle.com/2012/12/18/optimization-scikit-polytope-simplexnelder-mead-optimization/#comments</comments>
		<pubDate>Tue, 18 Dec 2012 08:13:59 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Generic optimizers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[scikit]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2124</guid>
		<description><![CDATA[Now that version 0.2 of scikit.optimization is out, here is a tutorial on the gradient-free optimizer based on the simplex algorithm. When the only thing you have is the cost function and when you don&#8217;t have dozens of parameters, the first thing that can be tried is a simplex algorithm. Usage The only specific thing [...]]]></description>
				<content:encoded><![CDATA[<p>Now that <a href="http://matt.eifelle.com/2012/11/15/annoucement-scikits-optimization-0-2/">version 0.2 of scikit.optimization</a> is out, here is a tutorial on the gradient-free optimizer based on the simplex algorithm.</p>
<p>When the only thing you have is the cost function and when you don&#8217;t have dozens of parameters, the first thing that can be tried is a simplex algorithm.</p>
<p><span id="more-2124"></span></p>
<h4>Usage</h4>
<p>The only specific thing you need to do before creating the optimizer is create the first simplex. With the Rosenbrock cost function in 2D, this means setting up 3 vectors of 2 parameters:</p>
<pre>
  startPoint = np.empty((3, 2), np.float)
  startPoint[:,0] = 1.
  startPoint[:,1] = 0.
  startPoint[1,0] -= .1
  startPoint[2,1] += .1</pre>
<p>Then, the optimizer instance can be built and called:</p>
<pre>
  optimi = optimizer.PolytopeOptimizer(function = Rosenbrock(), criterion = criterion.criterion(ftol = .0001, iterations_max=50), x0 = startPoint)
  print optimi.optimize()
</pre>
<p>As usual, you can use a recorder to display how the optimizer behaved, which is the purpose of next section.</p>
<h4>Interactive results check</h4>
<p>In the tutorial folder on github, I&#8217;ve put an <a href="https://github.com/mbrucher/scikit-optimization-tutorials/blob/master/0.2/tut1/tutorial_simplex.py">interactive application</a> with Traits and Chaco. I would have prefered that the optimization took place in a background thread and that the main GUI updates itself with new iteration states, but I couldn&#8217;t get it to work (I need to practice Traits a bit more). Still, you can explore the results with this app.</p>
<p>The other way to check the results is to save each state, display it with matplotlib and make it a movie:</p>
<p><span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='420' height='315' src='http://www.youtube.com/embed/D-VTab-pOTU?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span></p>
<p>As usual with the Rosenbrock function, the optimizer finds quickly the minimum valley but then takes a lot of iterations to find the actual solution. As we need to move the simplex in this bent valley, the number of iterations increases rapidly. Still, the optimal value is found in the end.</p>
<h4>Conclusion</h4>
<p>The Simplex algorithm is a very crude one but it can be effective when the cost function is too complex to derive or if the gradient is too costly to compute. It is still very sensitive to the stopping criterion as well as the starting polytope, but it can be a first way to check your result before trying something different like a gradient-based optimization, genetic algorithms&#8230;</p>
<p>Next tutorial will be about Quasi-Newton methods, and then the following one may be on optimization comparisons.</p>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/a2WfZWP9_vA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/12/18/optimization-scikit-polytope-simplexnelder-mead-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/12/18/optimization-scikit-polytope-simplexnelder-mead-optimization/</feedburner:origLink></item>
		<item>
		<title>Annoucement: scikits.optimization 0.2</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/MLNznpbYqd8/</link>
		<comments>http://matt.eifelle.com/2012/11/15/annoucement-scikits-optimization-0-2/#comments</comments>
		<pubDate>Thu, 15 Nov 2012 08:23:25 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Generic optimizers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Annoucement]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Scientific computing]]></category>
		<category><![CDATA[scikit]]></category>
		<category><![CDATA[scipy]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2119</guid>
		<description><![CDATA[It has been a while, too long for sure, since my last update on this scikit. I&#8217;m pleased to announce that some algorithms are finally fixed as well as some tests. Changelog: Fixed Polytope/Simplex/Nelder-Mead Fixed the Quadratic Hessian helper class Additional tutorials will be available in the next weeks. Old announces: 0.1 Buy Me a [...]]]></description>
				<content:encoded><![CDATA[<p>It has been a while, too long for sure, since my last update on this scikit. I&#8217;m pleased to announce that some algorithms are finally fixed as well as some tests.</p>
<p>Changelog:</p>
<ul>
<li>Fixed Polytope/Simplex/Nelder-Mead</li>
<li>Fixed the Quadratic Hessian helper class</li>
</ul>
<p>Additional tutorials will be available in the next weeks.</p>
<p>Old announces:</p>
<ul>
<li><a href="http://matt.eifelle.com/2010/02/02/annoucement-scikits-optimization-0-1/" title="0.1">0.1</a></li>
</ul>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/MLNznpbYqd8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/11/15/annoucement-scikits-optimization-0-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/11/15/annoucement-scikits-optimization-0-2/</feedburner:origLink></item>
		<item>
		<title>KdTree for nearest neighbors</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/_42yHYZ2Pjs/</link>
		<comments>http://matt.eifelle.com/2012/08/28/kdtree-for-nearest-neighbors/#comments</comments>
		<pubDate>Tue, 28 Aug 2012 07:30:57 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Profiling]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2101</guid>
		<description><![CDATA[Yes, because Cover Trees are sometimes too slow. In fact, I asked myself this question, not for the build time, but for the search time if the data has a structure. Imagine, what would happen if your data was more a less a regular grid? When I tried that, starting with a point at (0,0), [...]]]></description>
				<content:encoded><![CDATA[<p>Yes, because <a href="http://matt.eifelle.com/2012/03/27/cover-tree-for-nearest-neighbors/">Cover Trees</a> are sometimes too slow. In fact, I asked myself this question, not for the build time, but for the search time if the data has a structure. Imagine, what would happen if your data was more a less a regular grid? When I tried that, starting with a point at (0,0), then (1,0)&#8230; the first node (0,0) had references to all the last points (9,9), (9,8)&#8230; And I figured, it would be slower than a tree search. So I decided to give kd-trees a shot for this kind of search on a regular grid.<br />
<span id="more-2101"></span></p>
<h4>Implementation</h4>
<p>The implementation is not optimized, the only noticeable thing is that I don&#8217;t create subnodes for all points, I only split nodes when they have more than <em>n</em> points, and then according to the best dimension (best is defined by the biggest distance on one axis in the node, the root distance being set by hand). The search is of course really close to the cover tree implementation.</p>
<p>Also, when doing the search, I can override the original distance function with a new one. This is because when doing the search, some dimensions may be of greater interest than others. At least, this is the case I have. When creating the tree, I don&#8217;t have access to the actual distance, and it could even change during the process.</p>
<h4>Results</h4>
<p>OK, some comparisons now. I&#8217;ve changed the number of nodes to 1 million. I&#8217;ve relaunched the cover tree test, and this is the result:</p>
<pre>Build time 12:20:49.484375
Out time (linear) 00:00:04.500000
Out time (cover_tree) 00:00:00.062500</pre>
<p>The first figure is true, it is more than 12 hours. You have to search a lot to be profitable to use a cover tree.</p>
<p>The kd-tree:</p>
<pre>Build time 00:00:01.890625
Out time (linear) 00:00:02.921875
Out time (kd-tree) 00:00:00.875000</pre>
<p>Strangely the timing for the linear search is a bit different, but I guess this is due to the long time the program had to run. The timing is not as good, but it is only one test. I would need to test far more points, but due to the high cover tree build time, I didn&#8217;t try yet.</p>
<p>Now, if we use another distance (infinite instead of L2):</p>
<pre>Out time (linear) 00:00:04.718750
Out time (kd-tree) 00:00:00.890625</pre>
<p>The linear time is a bit higher, like the one from the cover tree. What is interesting is that the kdtree time is comparable to the one from the L2 search time. And this is what I wanted to check.</p>
<h4>Conclusion</h4>
<p>Kd-trees are not something earth-shaking, but sometimes it is still very relevant. Of course, I didn&#8217;t optimize any of the cover tree or kd tree implementation, so there is place for improvement. What I really like for the kdtree is that if you change the distance function, it can still work, whereas a cover tree, being implemented on a specific distance, cannot easily.</p>
<p>Kd-tree code on Github is <a href="https://github.com/mbrucher/CoverTree/">here</a>.</p>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/_42yHYZ2Pjs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/08/28/kdtree-for-nearest-neighbors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/08/28/kdtree-for-nearest-neighbors/</feedburner:origLink></item>
		<item>
		<title>Just a small example of numerical optimization in C++</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/knplS3aV-Io/</link>
		<comments>http://matt.eifelle.com/2012/07/17/just-a-small-example-of-numerical-optimization-in-c/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 07:34:19 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Generic optimizers]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2069</guid>
		<description><![CDATA[I had to port a simplex/Nelder-Mead optimizer that I already have in Python in C++. As for the Python version, I tried to be as generic as possible but as efficient as possible, so the state is no longer a dictionary, but a simple structure. I could have used the Numerical Recipes version, but the [...]]]></description>
				<content:encoded><![CDATA[<p>I had to port <a href="http://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method">a simplex/Nelder-Mead optimizer</a> that I already have in Python in C++. As for the Python version, I tried to be as generic as possible but as efficient as possible, so the state is no longer a dictionary, but a simple structure.</p>
<p>I could have used the Numerical Recipes version, but the licence cost is not worth it, and the code is not generic enough, not explained enough. And also there are some design decisions that are questionable (one method = one responsibility).</p>
<p><span id="more-2069"></span></p>
<h4>Design</h4>
<p>The main choice was to use Eigen for the inner computation in the code. This library is perfectly fitted for numerical computation in C++. The parameter set for the function could be any matrix class, but Eigen&#8217;s ones are ensured to be compatible inside the code. The simplex is stored as an Eigen dynamic array to cover any dimension possible cases.</p>
<p>There are several tools that are pretty much generic in a simplex algorithm: the <a href="https://github.com/mbrucher/CppOptimization/blob/master/state.h">state</a> (current parameters, iteration&#8230;) and the stopping <a href="https://github.com/mbrucher/CppOptimization/blob/master/criteria.h">criterion</a> (a conjunction of a maximum iteration and a relative value difference). So these concepts will be implemented as separate generic classes.</p>
<p>The <a href="https://github.com/mbrucher/CppOptimization/blob/master/simplex.h">main class</a>, <strong>Simplex</strong>, will encompass the actual simplex algorithm. There are three inner central methods: the one that creates the first state (<strong>initialize_polytope()</strong>), the one that will detect the best parameter set, the worst one and the worst second (<strong>find_best_worst_near_worst()</strong>)  and the one that create a new candidate based on the current simplex and the selected worst parameter set (<strong>create_new_parameters()</strong>).</p>
<p>The main method is <strong>optimize()</strong>, building on the class attributes that were set before and the main methods that were exposed before. The algorithm follows strictly the implementation on the Wikipedia page. The sizes of expansion, contraction could have been made template, or even modified at compile time, it is an exercise left for those that want a fully generic class.</p>
<p>A static builder was written to ease the creation of a new instance. It doesn&#8217;t do anything else than instantiating the class with the constructor, its only goal is to remove the need for knowing the exact template parameter and to fully specify them. This can be done because of the <strong>auto</strong> keyword in C++11.</p>
<h4>Using it</h4>
<p>Let&#8217;s start with a function to optimize. I&#8217;ve chosen <a href="http://en.wikipedia.org/wiki/Rosenbrock_function">the Rosenbrock function</a> as it is not complex to compute but still difficult to optimize. For the parameter kind, I&#8217;ve also used Eigen.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2069code4'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20694"><td class="code" id="p2069code4"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> Eigen<span style="color: #008080;">::</span><span style="color: #007788;">Vector2f</span> ParameterTypeRosenbrock<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Rosenbrock cost function</span>
<span style="color: #0000ff;">struct</span> Rosenbrock
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">float</span> DataType<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">typedef</span> ParameterTypeRosenbrock ParameterType<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> ParameterTypeRosenbrock<span style="color: #000040;">&amp;</span> parameters<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000040;">-</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000040;">-</span> parameters<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Now, I can create the optimizer with all the different parameters.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2069code5'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20695"><td class="code" id="p2069code5"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ParameterTypeRosenbrock start_point<span style="color: #008080;">;</span>
  start_point <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">10</span>, <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
  Rosenbrock fun<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">long</span> max_iterations <span style="color: #000080;">=</span> <span style="color: #0000dd;">1000</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> ftol <span style="color: #000080;">=</span> <span style="color:#800080;">0.00001</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">auto</span> optimizer <span style="color: #000080;">=</span> Optimization<span style="color: #008080;">::</span><span style="color: #007788;">Local</span><span style="color: #008080;">::</span><span style="color: #007788;">build_simplex</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">// Builder to generate the correct optimizer</span>
      fun,                                             <span style="color: #666666;">// Used to infer function type</span>
      Optimization<span style="color: #008080;">::</span><span style="color: #007788;">Local</span><span style="color: #008080;">::</span><span style="color: #007788;">make_and_criteria</span><span style="color: #008000;">&#40;</span>Optimization<span style="color: #008080;">::</span><span style="color: #007788;">Local</span><span style="color: #008080;">::</span><span style="color: #007788;">IterationCriterion</span><span style="color: #008000;">&#40;</span>max_iterations<span style="color: #008000;">&#41;</span>,
          Optimization<span style="color: #008080;">::</span><span style="color: #007788;">Local</span><span style="color: #008080;">::</span><span style="color: #007788;">RelativeValueCriterion</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">float</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>ftol<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Composite stoping criterion</span>
&nbsp;
  optimizer.<span style="color: #007788;">set_start_point</span><span style="color: #008000;">&#40;</span>start_point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// Starting parameters</span>
  optimizer.<span style="color: #007788;">set_delta</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                  <span style="color: #666666;">// Simplex size</span>
  optimizer.<span style="color: #007788;">optimize</span><span style="color: #008000;">&#40;</span>fun<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                 <span style="color: #666666;">// Optimization start</span>
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Starting point: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> start_point <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Starting value: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fun<span style="color: #008000;">&#40;</span>start_point<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Best point: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> optimizer.<span style="color: #007788;">get_best_parameters</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Retrieve the best parameters</span>
  std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Best value: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> optimizer.<span style="color: #007788;">get_best_value</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>      <span style="color: #666666;">// Retrieve the best value</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>With C++11, it is easy to find the type of the optimizer, thanks to the builder. It is still cumbersome as the builder function needs the function instance to infer the inner data type as well as the parameter set type.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2069code6'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20696"><td class="code" id="p2069code6"><pre class="shell" style="font-family:monospace;">Starting point: 10
10
Starting value: 8181
Best point: 1
1
Best value: 5.68434e-014</pre></td></tr></table></div>

<p>The simplex does not need such a complex generix framework. The implementation usually uses the criterion, so I didn&#8217;t have to implement it the way I did. Still, it is interesting for a more complex optimizer like a quasi-Newton or conjugate-gradient.</p>
<h4>Conclusion</h4>
<p>This code is not world shaking but it explains how I consider optimization. The other optimizers from scikits.optimization could be written in the same way and they could be better understood by someone versed in object oriented languages than usual procedural code.</p>
<p>As usual, it can be found on github: <a href="https://github.com/mbrucher/CppOptimization">https://github.com/mbrucher/CppOptimization</a></p>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/knplS3aV-Io" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/07/17/just-a-small-example-of-numerical-optimization-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/07/17/just-a-small-example-of-numerical-optimization-in-c/</feedburner:origLink></item>
		<item>
		<title>Book review: The Audio Effects Workshop</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/Miodrefrrl0/</link>
		<comments>http://matt.eifelle.com/2012/05/22/book-review-the-audio-effects-workshop/#comments</comments>
		<pubDate>Tue, 22 May 2012 07:31:45 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Book review]]></category>
		<category><![CDATA[Course PTR]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Digital Audio Workstation]]></category>
		<category><![CDATA[digital filter]]></category>
		<category><![CDATA[VST]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2057</guid>
		<description><![CDATA[How to explain the different kind of audio effects and how to understand what their use is? Although I learnt a lot by practice, there is sometimes the need for some theory and for experiments. I tried to find a book that matches these two points: good theory and proper practice. I&#8217;ve chosen this book, [...]]]></description>
				<content:encoded><![CDATA[<p>How to explain the different kind of audio effects and how to understand what their use is? Although I learnt a lot by practice, there is sometimes the need for some theory and for experiments. I tried to find a book that matches these two points: good theory and proper practice. I&#8217;ve chosen this book, with tracks on a CD for experimentation. Was it really what I was looking for?</p>
<p><span id="more-2057"></span></p>
<h4>Content and opinions</h4>
<p>The first three chapters help you set up your environment with your own sequencer. The book tries to be sequencer agnostic, although the author mainly resorts to Sonar or Reaper. So the first chapter defines some terms and install some free plugins that will be used through the book. The second one is about what sound is while the third explains how effects can interact with sound to create something.</p>
<p>Afterwards, the author deals with actual effects. The next three chapters are dedicated to frequency and dynamics effects. The different types of equalization processes and each instrument has specific frequency bands that need to be shaped. As the book comes with several EQ plugins, there is room for tests with the different types of equalization and different instruments. Then the first dynamic effect is noise gate. I think it is one of the effects you have to know how to use fast as it is mandatory for real recorded tracks. There are different levels in the noise gate (it can have side chains, more filters), but the simplest one is easy to use. The tutorial does not handle all the different combinations, only the most usual ones. The compressor chapter is a bit more complex, as compression can be used in different ways. Not only ways has a complete tutorial, but the reader is exhorted to test all of them. There are several &#8220;checkpoints&#8221; where another audio project is used than the ones in the tutorials. In this case, you don&#8217;t have a reference where you will apply the tutorial without thinking, you have to test all parameters and different ways of adding a plugin.</p>
<p>The next three chapters are about time effects. I think time effects are what makes the most innovation in tracks. The first plugins are always on all audio tracks we listen to (with the addition of reverberation, which is the object of the second chapter in this part), but the other are used with scarcity and it makes the plus that surprises the listener (in my opinion). So the first chapter is about delay and chorus. A delay can be very complex (from a simple tap to multi-tap with a spatial aspect) whereas the chorus is something more simple, but nonetheless complex to use. They can add a spatial dimension to the audio track, and as such they must be tuned with care. Reverberation is the subject of the second chapter, and it is one of the mandatory effects (as I&#8217;ve said before). The author spends several pages on synthetic reverberation, but I prefer convolution. The reason is that you can exactly specify reverberation you want and need, and the CPU cost is not a problem anymore. Convolution is the future, why spend so many pages on synthetic? The last chapter is about other time effects, like phaser, flanger, tremolo&#8230; They will be applied to different tracks depending of what you want to achieve. It&#8217;s perhaps the most complicated chapter because you need to remember what all these effects sound like and imagine what they sound like on your work.</p>
<p>Almost all the remaining chapters are dedicated to making all the pieces of the puzzle fit together. The tenth chapter is about effects that used for something different from their usual goal (like deessers) and plugin effects that chain several effects (possibly in the order you want). They are incredibly valuable, although they are sequencer specific or not free (so no tutorials, you have to try by yourself). One of the great benefit of a sequencer over traditional hardware is automation, the subject of the next chapter. The author warns of the different aspects of automation and what you can and cannot do as well as why. A small chapter is dedicated to stereo effects. It is quite short and as the other dimensions are tackled before (delay and reverb), it is strange to have the main dimension&#8217;s discussion now. The next two chapters help setting the threads between all the plugins. There is nothing actually new, but the tutorials help testing different serial and parallel combinations. It&#8217;s what makes a final mix, and it is properly explained.</p>
<p>The final chapter before the conclusion is about mastering effects. The author provides a guide to check your mix and prepare it for the mastering phase (on your own or by someone else), and then he reviews the effects you will need. There is no discussion on the loudness war, it could have been a good talk.</p>
<p>The conclusion gives you some hints to sort your plugins and choose the proper one, it is something one can easily forget.</p>
<h4>Conclusion</h4>
<p>With all the tracks and plugins on the CD, the explanations of each effect and then the tutorials (for each effect, even if I didn&#8217;t say it), this book is what I expected from a book on audio effects. If you want to know what audio effects are, go and buy the book, you won&#8217;t regret it.</p>
<p><iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=masbl03-20&o=1&p=8&l=as1&m=amazon&f=ifr&asins=1435456149" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/Miodrefrrl0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/05/22/book-review-the-audio-effects-workshop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/05/22/book-review-the-audio-effects-workshop/</feedburner:origLink></item>
		<item>
		<title>Cover tree for nearest-neighbors</title>
		<link>http://feedproxy.google.com/~r/eifelle/CPPV/~3/eN8ZuHOetUY/</link>
		<comments>http://matt.eifelle.com/2012/03/27/cover-tree-for-nearest-neighbors/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 08:30:12 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Profiler]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[Profiling]]></category>

		<guid isPermaLink="false">http://matt.eifelle.com/?p=2042</guid>
		<description><![CDATA[I&#8217;ve looked on github for a good C++ implementation of Cover Trees for nearest-neighbors search, but I didn&#8217;t find one. I may have overlooked some repositories, but in the end, implementing it myself wasn&#8217;t that difficult. Implementation Contrary to kdtrees, the cover tree nodes always represent one point. So you have as many nodes as [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve looked on github for a good C++ implementation of Cover Trees for nearest-neighbors search, but I didn&#8217;t find one. I may have overlooked some repositories, but in the end, implementing it myself wasn&#8217;t that difficult.<br />
<span id="more-2042"></span></p>
<h4>Implementation</h4>
<p>Contrary to kdtrees, the cover tree nodes always represent one point. So you have as many nodes as you have data points. Each of them can have several children, sorted by &#8220;level&#8221;, or distance hierarchy. Each level halves the distance between the node and its children. When constructing the tree instance, the distance you use is given as parameters (and its type is a template parameter, as well as the point type and the data type used for comparisons).</p>
<p>Construction of the tree is done by searching in the tree for the closest node and the lowest hierarchy possible. In all time, the min and max levels are tracked in the tree. On insertion, the max level is tried, and if it fails, the max level is increased by one. The failure occurs if the new point is furthest of the root point that the maximum distance on the max level. At each level, the set of the nodes that are closer to the new point than the current maximum distance are kept (function <strong>populate_set_from_node</strong>). If the least distance is greater than the next level maximum level (function <strong>find_min_dist</strong>, the recursion stops and the new node is inserted in the first node in the set.</p>
<p>When doing a search, a list of distances and points (of type <strong>NearestNodesStructure</strong>) is created and its size is kept at the number of neighbors k. On entrance of a new level (<strong>level_traversal</strong>), all nodes that have children that could be close enough (current distance less than maximum level distance + distance to the current k-th neighbors found with <strong>find_k_dist</strong>) are added to the list. In order to speed search, a map is not used, but a partial sort is done at each iteration (which makes <strong>find_k_dist</strong> work without sorting the container again). Without this, the performance is worse than a linear knn search.</p>
<p>I didn&#8217;t implement other functions like removal or traversal. I don&#8217;t need them, but feel free to add them if you need. Sidenote: I will try to find time to refactor the code, because it is not very clean at the moment (especially the <strong>knn</strong> method).</p>
<h4>Results</h4>
<p>So some figures. I&#8217;ve populated a tree with 100k elements, and then made a 10-neighbors search with the cover tree, and with a standard linear search. Without the construction time, the search is about ten times faster than a linear one. Of course, it all depends on the cover tree balance. You could design a cover tree that has too many levels (but also the data would be unusual) and that would result in an almost linear time.</p>
<p>I&#8217;ve done a profile with <a href="http://matt.eifelle.com/2009/04/07/profiling-with-valgrind/">callgrind</a>, but the majority of the time is in the tree construction. No point in checking the search and displaying it here as the following timings show:</p>
<pre>Build time 00:09:38.530397
Out time (linear) 00:00:00.238989
Out time (cover_tree) 00:00:00.012518
</pre>
<p>What must be optimized is clearly the build time.</p>
<p>The test code is in the main.cpp file on github.</p>
<p>Cover Tree code on Github is <a href="https://github.com/mbrucher/CoverTree/">here</a> and the publication with detailed algorithms <a href="http://hunch.net/~jl/projects/cover_tree/icml_final/final-icml.pdf">here</a>.</p>
<div id="accept_paypal_payment_form">
        <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="business" value="matthieu.brucher@gmail.com" /><input type="hidden" name="item_name" value="Buy Me a Coffee!" /><input type="hidden" name="currency_code" value="USD" /><span style="font-size:10.0pt"><strong> Buy Me a Coffee!</strong></span><br /><br /><select id="amount" name="amount" class=""><option value="3">Capuccino - 3$</option><option value="6">Frappuccino - 6$</option><option value="10">Hot Chocolate - 10$</option><option value="20">Expensive Coffee - 20$</option><option value="50">Alien Coffee - 50$</option></select><br /><br /><strong>Other Amount:</strong><br /><br /><input type="text" name="amount" size="10" title="Other donate" value="" /><br /><br /><strong> Your Email Address :</strong><input type="hidden" name="on0" value="Reference" /><br /><br /><input type="text" name="os0" maxlength="60" />
        <br /><br />
        <input type="hidden" name="no_shipping" value="0" />
        <input type="hidden" name="no_note" value="1" />
        <input type="hidden" name="mrb" value="3FWGC6LFTMTUG" />
        <input type="hidden" name="bn" value="IC_Sample" />
    <input type="hidden" name="return" value="http://matt.eifelle.com" /><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but11.gif" name="submit" alt="Make payments with payPal - it's fast, free and secure!" /></form></div>
<img src="http://feeds.feedburner.com/~r/eifelle/CPPV/~4/eN8ZuHOetUY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://matt.eifelle.com/2012/03/27/cover-tree-for-nearest-neighbors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://matt.eifelle.com/2012/03/27/cover-tree-for-nearest-neighbors/</feedburner:origLink></item>
	</channel>
</rss>
