<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Experiments in data science</title>
    <description>A blog about my experiments in programming &amp; machine learning.
</description>
    <link>http://blog.richardweiss.org/</link>
    <atom:link href="http://blog.richardweiss.org/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 24 Dec 2022 09:05:01 +0000</pubDate>
    <lastBuildDate>Sat, 24 Dec 2022 09:05:01 +0000</lastBuildDate>
    <generator>Jekyll v3.9.2</generator>
    
      <item>
        <title>Sintering bronze</title>
        <description>&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;This is just a quick post on how I sintered some bronze. 
This may be of interest to people who are thinking of moving into metal fabrication.
I’m interested in sintering as a way to 3d print metal.&lt;/p&gt;

&lt;p&gt;Bronze seems like the ideal metal to start with, for a few reasons&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s not very dangerous. The FDA allows it to be &lt;a href=&quot;https://www.accessdata.fda.gov/scripts/cdrh/cfdocs/cfcfr/CFRSearch.cfm?fr=73.1646&amp;amp;SearchTerm=bronze%20powder&quot;&gt;used in cosmetics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;It has a low sintering temperature, between 800°C &amp;amp; 950°C&lt;/li&gt;
  &lt;li&gt;It’s quite accessible. I bought mine from &lt;a href=&quot;https://www.ebay.com.au/itm/333403927012&quot;&gt;ebay&lt;/a&gt;. In particular, the types sold as makeup have very small grain size (40 micron), which is easier to sinter&lt;/li&gt;
  &lt;li&gt;It’s quite inert, so I coudl mix it with water to form a paste without causing a chemical reaction (e.g., oxidation)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;approach&quot;&gt;Approach&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;I prepared a mix of water, isopropyl alchohol and poly-vinyl alchohol. The ratio was roughly 45:45:10, respectively. The water &amp;amp; isopropyl alchohol were to form the bronze into a paste, while the PVA was a binder that would remain once the solvents had evaporated&lt;/li&gt;
  &lt;li&gt;I mixed a small amount of the liquid mixture with the bronze powder, as little as possible to form a workeable paste&lt;/li&gt;
  &lt;li&gt;I pushed this mixture into a small 4mm nylon standoff tube I had lying around, and used two 4mm pins to compress the mixture into a pellet. This compression was done by hand, and assuming I pushed with approx. 10kg of force, concentrated over a 4mm cylinder, it would have reached 2Mpa.&lt;/li&gt;
  &lt;li&gt;I let the mixture dry for 30 mins or so&lt;/li&gt;
  &lt;li&gt;I pushed the mixture out into a cruicble filled with previously dried sand.&lt;/li&gt;
  &lt;li&gt;I put the whole thing into a furnace and shot for 820°C.&lt;/li&gt;
  &lt;li&gt;Once the furnace reached temperature, I let it bake for 10 mins or so, then turned off the heat.&lt;/li&gt;
  &lt;li&gt;After an hour or so the system had cooled to ~300°C or so, and I took the item out of the crucible, and found it had sintered well, losing about 3% of its volume.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;/h2&gt;

&lt;h3 id=&quot;density&quot;&gt;Density&lt;/h3&gt;
&lt;p&gt;Sintering outcomes are apparently driven by density; specifically:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The denser the input, the denser the output&lt;/li&gt;
  &lt;li&gt;The hotter &amp;amp; longer the sintering, the denser the output&lt;/li&gt;
  &lt;li&gt;The denser the output, the better the pysical strength.
So the more you can compress the item in step 3, the better&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;top-temperature--sintering-time&quot;&gt;Top temperature &amp;amp; sintering time&lt;/h3&gt;

&lt;p&gt;I found a lot of different sintering recipes on the internet. The one I used was based on a &lt;a href=&quot;https://www.researchgate.net/figure/Bronze-sintering-profile-used-in-experiments_fig3_282835416&quot;&gt;paper&lt;/a&gt;. However, more research suggests&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bronze’s melting point is very dependent on percentage of tin, so definitely work out what your bronze alloy is and infer a temperature from the phase diagram. Mine was 90/10 bronze; 10% tin.&lt;/li&gt;
  &lt;li&gt;The link above has a 10 minute soak time, but many sources suggest longer and hotter. Probably something to experiment with&lt;/li&gt;
  &lt;li&gt;See the note on density - you should probably be measuring the density of your items after sintering to validate your time &amp;amp; temp.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;shapes&quot;&gt;Shapes&lt;/h3&gt;

&lt;p&gt;I made a little cylinder. It’s a bit useless. The next step is to try:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Extrueable shapes, where I’m easily able to compress the object&lt;/li&gt;
  &lt;li&gt;3d printed paste, without compression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My main interest is in the 3d printing 😀&lt;/p&gt;
</description>
        <pubDate>Mon, 31 Aug 2020 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2020/08/31/bronze-sintering.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2020/08/31/bronze-sintering.html</guid>
        
        
      </item>
    
      <item>
        <title>Deep learning applications on a raspberry pi using Onnx</title>
        <description>&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;I’ve recently made a tool to turn my smart lights on and off with an automated people counter, based on the &lt;a href=&quot;https://www.adafruit.com/product/3538&quot;&gt;AMG8833&lt;/a&gt;, 
and a raspberry pi zero. For details on how I made the pi talk to the AMG8833, see the &lt;a href=&quot;/2020/08/31/rust-amg8833-on-pizero.html&quot;&gt;previous blog post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post, I’ll outline:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;the general approach I’ve used to build the people counter, using &lt;em&gt;pytorch&lt;/em&gt; and &lt;em&gt;python&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;how I used &lt;a href=&quot;http://onnx.ai/&quot;&gt;onnx&lt;/a&gt; to embed my &lt;em&gt;pytorch&lt;/em&gt; model into a rust executable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By the end of the process, I have a single executable that I can run on my pi zero. It gets good FPS - each frame takes about 30-40ms to process, plenty fast enough for me.&lt;/p&gt;

&lt;p&gt;And this is the result!&lt;/p&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/XODomywp1lk&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;

&lt;h2 id=&quot;how-to-make-a-simple-people-counter-with-unsupervised-learning&quot;&gt;How to make a simple people counter with unsupervised learning&lt;/h2&gt;

&lt;p&gt;My goal is to make a simple classifier that will classify frames into one of three categories:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Nothing happening&lt;/li&gt;
  &lt;li&gt;Someone entering a zone&lt;/li&gt;
  &lt;li&gt;Someone leaving a zone&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I opted to use a neural networks approach, because it has a few valuable properties.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It’s easy to express interesting unsupervised learning ideas as neural networks. I knew I didn’t want to spend time labelling data, so unsupervised learning would definitely be part of my approach.&lt;/li&gt;
  &lt;li&gt;Pytorch supports Onnx export, so I knew I’d be able to run my code on the pi through a pi Onnx library.&lt;/li&gt;
  &lt;li&gt;If I needed to do any data cleanup, I knew that as long as it was expressed in Pytorch, it would be exported by Onnx.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the end I used the following architecture, and trained in on a small dataset of me walking back and forth.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A small autoencoder is trained on single frames from the AM8833&lt;/li&gt;
  &lt;li&gt;The encoded frames have dimensionality 3-5&lt;/li&gt;
  &lt;li&gt;The encoded frames were run through a K-means classifier, with three clusters. This corresponds to the three states above. Note that this was the &lt;strong&gt;sklearn&lt;/strong&gt; kmeans implementation.&lt;/li&gt;
  &lt;li&gt;I extracted the centroids from KMeans, and created a Pytorch version of a KMeans classifier (just the classifier, not the training)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, I put all this work end to end to make a single Pytorch module - that I could export with Onnx.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# The autoencoder
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PReLU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PReLU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;encoder&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# The KMeans implementation
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;KMeansLookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;centroids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;centroids&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;centroids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cls_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dists&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;centroids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;dists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dists&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# The export using Onnx
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mdl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KMeansLookup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster_centers_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;decoder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onnx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mdl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'test.onnx'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;training&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example_outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For all the code, check out the &lt;a href=&quot;https://gitlab.com/ririw/microprojects/-/blob/master/walker/Model%20training.ipynb&quot;&gt;notebook with all the code&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;model-outputs&quot;&gt;Model outputs&lt;/h2&gt;

&lt;p&gt;To get a sense for how this “looks”, I’ve extracted a few plots. The first is of the encoded frames from the autoencoder.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/thermaltracker/encoded.png&quot; alt=&quot;encoded outputs from the model&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The spikes are me walking in and out of frame - you can see why it might be easy for KMeans to sort these into three clusters! In fact, this is what KMeans produces:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/thermaltracker/kmeans.png&quot; alt=&quot;KMeans&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So all I need to do now is run this model in my rust executable, and then transform the state transitions into lights changing!&lt;/p&gt;

&lt;h2 id=&quot;how-to-compile-a-model-into-a-rust-executable&quot;&gt;How to compile a model into a rust executable&lt;/h2&gt;

&lt;p&gt;This was a little bit fiddly - mostly because it takes a bit of work to get data from the internal format my program uses, into the format that Onnx expects. 
However, with some experimentation I was able to do so, and you can take a look &lt;a href=&quot;https://gitlab.com/ririw/microprojects/-/blob/e3dad6281d869720debcae6bf1dfd56e15df3842/walker/src/model.rs#L25-40&quot;&gt;at the code&lt;/a&gt; to see how I did it.&lt;/p&gt;

&lt;p&gt;The other thing I &lt;strong&gt;really&lt;/strong&gt; liked was that I could compile my model into the executable itself, rather than keeping it as a separate file. The rust &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;include_bytes&lt;/code&gt; macro helped here. This keeps the deployment simple and easy!&lt;/p&gt;

&lt;p&gt;The only other piece of significant work was making a small state machine to manage the lights as people move around.&lt;/p&gt;

&lt;h2 id=&quot;wrapup&quot;&gt;Wrapup&lt;/h2&gt;

&lt;p&gt;I was quite happy with most of this process. The ML side needs some refinement, I think it still isn’t robust enough. I’m very happy with how Onnx worked out, and I’ll continue to push models into edge devices in this way in future projects.&lt;/p&gt;
</description>
        <pubDate>Mon, 31 Aug 2020 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2020/08/31/using-onnx-with-rust.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2020/08/31/using-onnx-with-rust.html</guid>
        
        
      </item>
    
      <item>
        <title>Running hardware on the Raspberry Pi with Rust</title>
        <description>&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Recently, I’ve been playing around with a little 8x8 infrared camera, the &lt;a href=&quot;https://www.adafruit.com/product/3538&quot;&gt;AMG8833&lt;/a&gt;. 
I’ve been running it on a raspberry pi zero, using Rust, so I thought it might be useful to share with the community:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;How I compiled rust code for the pi with minimal pain&lt;/li&gt;
  &lt;li&gt;How to access I2C in rust to talk to embedded peripherals&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I learned all this stuff because I’ve been building a device to turn my lights on and off in my house as people change rooms.
I’ll explain all of the work in several posts, covering topics including the ML techniques I used and how I ran deep learning code on the pi.&lt;/p&gt;

&lt;h2 id=&quot;compiling-code-with-cross&quot;&gt;Compiling code with Cross&lt;/h2&gt;

&lt;p&gt;The last time I tried to compile rust code for the pi, I found two options.
Either:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Spend forever mucking around with tool chains on my mac. I found this very difficult, and gave up.&lt;/li&gt;
  &lt;li&gt;Compile rust code on the pi itself, which is frustratingly slow, so I gave up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Luckily, I’ve found a convenient solution! The &lt;a href=&quot;https://github.com/rust-embedded/cross&quot;&gt;cross&lt;/a&gt; project by the rust embedded team was a joy to use.&lt;/p&gt;

&lt;p&gt;Simply run:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo install cross&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Ensure docker is running&lt;/li&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cross build --target arm-unknown-linux-gnueabihf&lt;/code&gt; (for my pi zero).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was the basis for the whole project, without the cross project I’m sure I’d have given up on rust quickly.&lt;/p&gt;

&lt;h2 id=&quot;talking-i2c-on-the-pi-in-rust&quot;&gt;Talking I2C on the pi in Rust&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rppal&lt;/code&gt; project provides I2C tools for the pi in rust.
These made it easy to talk to my AMG8833 sensor, simply by copying the approach shown in the open source C++ libraries for Arduino.&lt;/p&gt;

&lt;p&gt;You can access the code &lt;a href=&quot;https://gitlab.com/ririw/microprojects/-/blob/master/walker/src/amg8833.rs&quot;&gt;on gitlab&lt;/a&gt;, 
it provides a simple interface to the device:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMG8833::new()&lt;/code&gt; sets up the interface with the device&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_thermistor()&lt;/code&gt; reads the internal temperature sensor&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_pixels()&lt;/code&gt; reads the pixels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I particularly like the return type of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read_pixels&lt;/code&gt; where the array sizes (8x8) are encoded in the return type (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[f64, 8], 8]&lt;/code&gt;)!
Super nice!&lt;/p&gt;

&lt;h2 id=&quot;hardware-setup&quot;&gt;Hardware setup&lt;/h2&gt;
&lt;p&gt;For reference, here’s a photo of the hardware setup.
I 3D printed a small enclosure for the pi, along with a little swivel mount based camera head. 
I’ve also included photos of the wiring layout I used for the camera, in case you’re creating this setup for yourself.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/thermaltracker/sensor.jpg&quot; alt=&quot;the connections to the sensor&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/thermaltracker/connections.jpg&quot; alt=&quot;the connections to the pi&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can download all the 3D print files &lt;a href=&quot;https://gitlab.com/ririw/microprojects/-/tree/master/walker/hardware&quot;&gt;from the repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;

&lt;p&gt;Here’s a recording of the compilation process - much much faster than on the pi itself!
&lt;img src=&quot;/assets/thermaltracker/render.gif&quot; alt=&quot;gif of the compilation&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is what the output looks like - perfectly adequate for my use case.
&lt;img src=&quot;/assets/thermaltracker/output.png&quot; alt=&quot;image output the sensor&quot; /&gt;&lt;/p&gt;

</description>
        <pubDate>Mon, 31 Aug 2020 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2020/08/31/rust-amg8833-on-pizero.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2020/08/31/rust-amg8833-on-pizero.html</guid>
        
        
      </item>
    
      <item>
        <title>Upside down reinforcement learning</title>
        <description>&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;How to make an upside down RL. I used  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transform: rotateX(180deg)&lt;/code&gt;.
This has the downside of only working in a div. But it’s still something…&lt;/p&gt;

&lt;div style=&quot;transform: rotateX(180deg)&quot;&gt;
       RL
   &lt;/div&gt;

&lt;p&gt;But moving on, in this post I’m going to take you through building a simple example of reinforcement using &lt;em&gt;upside down RL&lt;/em&gt;.
Upside down reinforcement learning is detailed in &lt;a href=&quot;https://arxiv.org/abs/1912.02877&quot;&gt;this report&lt;/a&gt;, and I found &lt;a href=&quot;https://arxiv.org/abs/1912.02877&quot;&gt;this paper&lt;/a&gt; to be useful in implementation.  The broad outline is contained in the title of the original report: &lt;em&gt;Don’t Predict Rewards – Just Map Them to Actions&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Typically, most systems predict the returns of actions, and execution proceeds by picking the best action&lt;/li&gt;
  &lt;li&gt;In upside down RL, the system predicts reads in a state and a &lt;strong&gt;command&lt;/strong&gt; and predicts the action to take.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ve trained an example of upside down RL on the very easy cartpole problem.&lt;/p&gt;

&lt;video controls=&quot;&quot;&gt;
  &lt;source src=&quot;https://gitlab.com/ririw/upside-down-rl/raw/master/cartpole.webm&quot; type=&quot;video/webm&quot; /&gt;
&lt;/video&gt;

&lt;h2 id=&quot;running-it-yourself&quot;&gt;Running it yourself&lt;/h2&gt;
&lt;p&gt;If you’d like to run it &lt;em&gt;right now&lt;/em&gt;, all you need to do is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Install via pip: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip install upside-down-rl&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Run via python: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python -m &quot;upside_down_rl.cartpole&quot; --render&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Run tensorboard to monitor the output: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorboard --logdir=experiments serve&lt;/code&gt;. This will show the performance of the model improving over time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see a window with the cartpole, and you can watch as it slowly improves.&lt;/p&gt;

&lt;p&gt;Alternatively, you can run using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker&lt;/code&gt;, as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Either:
    &lt;ul&gt;
      &lt;li&gt;Clone the repo and cd into the deploy directory, or&lt;/li&gt;
      &lt;li&gt;Download the &lt;a href=&quot;https://gitlab.com/ririw/upside-down-rl/blob/master/deploy/docker-compose.yml&quot;&gt;docker compose file&lt;/a&gt; and put it into a directory somewhere&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MINIO_ACCESS_KEY=somethingyoumakeup MINIO_SECRET_KEY=somesecretkeyyoulike docker-compose up&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will run 25 runs of the cartpole tool. Model params are saved to minio (an S3 compatible data store), and you can access them by going to &lt;a href=&quot;http://localhost:80&quot;&gt;http://localhost:80/&lt;/a&gt;. The runs will also appear in tensorflow, accessible at &lt;a href=&quot;http://localhost:6006&quot;&gt;http://localhost:6006&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;approach&quot;&gt;Approach&lt;/h2&gt;

&lt;p&gt;In general, we want to train an algorithm to take a state and a command and tell us the action to take that would achieve that state and command. 
A command is of the form “Achieve reward &lt;em&gt;x&lt;/em&gt; in &lt;em&gt;n&lt;/em&gt; steps”.
To do this, we train a supervised algorithm to predict the action that will (eventually) lead to the desired command and state. 
To gather commands, states and actions, we use previous play episodes to build a dataset of commands and states, and train a model to predict the actions taken in those runs.
Finally, when running the model to gather data, we send commands that ask for progressively higher and higher rewards.&lt;/p&gt;

&lt;h1 id=&quot;overall-algorithm&quot;&gt;Overall algorithm&lt;/h1&gt;

&lt;p&gt;The core algorithm, as I’ve applied it is as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Play the game with random actions many times to initialise the system. Save each play into two buffers:
    &lt;ul&gt;
      &lt;li&gt;One is a FIFO buffer, that holds the most recent plays.&lt;/li&gt;
      &lt;li&gt;One is a priority queue, that keeps the best plays. Note that plays are compared first on total reward, then on length of play, in both cases, higher is better.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;For as many epochs as necessary:
    &lt;ol&gt;
      &lt;li&gt;Train a classifier to map from inputs and command to action, as described below&lt;/li&gt;
      &lt;li&gt;Execute plays using the classifier to pick actions based on the output of the classifier&lt;/li&gt;
      &lt;li&gt;Collect executed plays in the same FIFO and priority queue as described in step one&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The classifier takes two inputs:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The state, as reported by the game being played&lt;/li&gt;
  &lt;li&gt;A “command”, comprised of:
    &lt;ul&gt;
      &lt;li&gt;The reward we desire&lt;/li&gt;
      &lt;li&gt;And the number of steps to achieve it&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The classifier then suggests the action that would achieve that command, given the state.&lt;/p&gt;

&lt;h1 id=&quot;training-algorithm&quot;&gt;Training algorithm&lt;/h1&gt;

&lt;p&gt;This generates training data, from previous episodes in the FIFO and the priority queue. Note that an episode is a start to end play of the game in question, so it has multiple states and multiple rewards accumulated over the play.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;For all episodes in each of the buffers
    &lt;ol&gt;
      &lt;li&gt;Sample a starting time between the start and end of the episode&lt;/li&gt;
      &lt;li&gt;Sample an ending time. The ending time is:
        &lt;ul&gt;
          &lt;li&gt;With 75% probability, set to the end of the episode&lt;/li&gt;
          &lt;li&gt;With 25% probability, set to a random time between the start time and the end time&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Generate the command:
        &lt;ul&gt;
          &lt;li&gt;the Δt is end time minus start time&lt;/li&gt;
          &lt;li&gt;the reward is the sum of all rewards accumulated between the start time and end time&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Gather the state input, the game state at the sampled start time&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Train the model based on these X and Y values. My model as as follows
    &lt;ul&gt;
      &lt;li&gt;A state embedding:
        &lt;ul&gt;
          &lt;li&gt;A Linear layer, from state size (4) to a vector of length 64&lt;/li&gt;
          &lt;li&gt;A PReLU layer&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;A command embedding:
        &lt;ul&gt;
          &lt;li&gt;A Linear layer, from command size (2) to a vector of length 64&lt;/li&gt;
          &lt;li&gt;A PReLU layer&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Then, I take the elementwise product of state and command embeddings&lt;/li&gt;
      &lt;li&gt;And feed it through a network with structure:
        &lt;ul&gt;
          &lt;li&gt;Linear 64 → 64&lt;/li&gt;
          &lt;li&gt;PReLU&lt;/li&gt;
          &lt;li&gt;Linear 64 → num actions (2)&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Models were trained with Adam, for up to 100 epoch, with early stopping, patience 10.&lt;/li&gt;
      &lt;li&gt;Loss was crossentropy loss.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;play-algorithm&quot;&gt;Play algorithm&lt;/h1&gt;

&lt;p&gt;When playing, the algorithm is simply:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Sample a desired reward and horizon based on the top episodes in the priority queue:
    &lt;ul&gt;
      &lt;li&gt;desired horizon is the average of the lengths of top episodes&lt;/li&gt;
      &lt;li&gt;desired reward is between the average and the average plus one standard deviation&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;While the game is not finished:
    &lt;ul&gt;
      &lt;li&gt;Get an action from the model, by feeding in game state, desired horizon and desired reward&lt;/li&gt;
      &lt;li&gt;Enact the action and receive reward&lt;/li&gt;
      &lt;li&gt;Decrement desired horizon by one and reward by the reward from the last step. Clip at zero.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Save each full episode into the FIFO and priority queue.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;hyperparameters&quot;&gt;Hyperparameters&lt;/h1&gt;

&lt;p&gt;For cartpole, my hyperparameters were as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Both the FIFO and queue had max size 200&lt;/li&gt;
  &lt;li&gt;Each run is 100 episodes&lt;/li&gt;
  &lt;li&gt;I gather 500 random plays at the beginning, before the first training&lt;/li&gt;
  &lt;li&gt;To calculate my desired reward and horizon, I use the top 100 plays in the queue&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;code-tour&quot;&gt;Code tour&lt;/h2&gt;

&lt;h1 id=&quot;trying-new-environments&quot;&gt;Trying new environments&lt;/h1&gt;

&lt;p&gt;To try a new environment, I suggest the following steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Clone the code&lt;/li&gt;
  &lt;li&gt;Get &lt;a href=&quot;https://python-poetry.org/&quot;&gt;poetry&lt;/a&gt; and install, then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;poetry install&lt;/code&gt; in the codebase directory.&lt;/li&gt;
  &lt;li&gt;copy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cartpole.py&lt;/code&gt; to a new file&lt;/li&gt;
  &lt;li&gt;Modify your new file in two ways:&lt;/li&gt;
  &lt;li&gt;Return a different environment in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cartpole&lt;/code&gt; function. Consider renaming this function.&lt;/li&gt;
  &lt;li&gt;Modify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CartPoleTorchModel&lt;/code&gt; to match your chosen environment&lt;/li&gt;
  &lt;li&gt;Run the code, and see how it all goes&lt;/li&gt;
  &lt;li&gt;Tweak the hyperparameters to improve performance.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;implementation-details-in-udrlpy&quot;&gt;Implementation details in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udrl.py&lt;/code&gt;&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReplayBuffer&lt;/code&gt; class tracks episodes&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TrainItem&lt;/code&gt; class represents an item passed to the model to train on&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModelInterface&lt;/code&gt; is the interface a model must support for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;udrl&lt;/code&gt; code to use it:
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; takes a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TrainItems&lt;/code&gt; and trains the model, and returns nothing&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run&lt;/code&gt; runs the model, this should return something that can be fed into the environment&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_action&lt;/code&gt; should sample a random action&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;URLDConfig&lt;/code&gt; class configures the tool&lt;/li&gt;
  &lt;li&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Episode&lt;/code&gt; represents a full play of the game in question, including
    &lt;ul&gt;
      &lt;li&gt;Actions&lt;/li&gt;
      &lt;li&gt;Observations&lt;/li&gt;
      &lt;li&gt;Rewards&lt;/li&gt;
      &lt;li&gt;The total reward for the episode&lt;/li&gt;
      &lt;li&gt;Ident: a random integer between 0 and 999, useful for doing train-test splits&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_run_episode&lt;/code&gt; will run a single episode&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_run_multi_episodes&lt;/code&gt; will run many episodes&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_par_run_episodes&lt;/code&gt; will run many episodes in parallel&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_random_fill&lt;/code&gt; will do step 1 of the overall algorithm, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random_action&lt;/code&gt; function&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_run_model&lt;/code&gt; will do step 4 of the overall algorithm (running the model and tracking episodes)
    &lt;ul&gt;
      &lt;li&gt;If configured, this function will write out to tensorboard. Simply pass a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tensorboardX.SummaryWriter&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDRLConfig.writer&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;If configured, this function will render the environment. Simply set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt; to true in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDRLConfig.render&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_train_model&lt;/code&gt; will build a training dataset and pass it to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; function of the model&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_save_model&lt;/code&gt; will call the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;save_model&lt;/code&gt; if provided in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;URLDConfig&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train_udrl&lt;/code&gt; the main loop. Simply provide:
    &lt;ul&gt;
      &lt;li&gt;A function that returns an OpenAI gym compatible environment. This needs to be a factory, rather than an instance, so that multiple instances can be spawned for parallel operation&lt;/li&gt;
      &lt;li&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDRLConfig&lt;/code&gt; instance&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;discussion&quot;&gt;Discussion&lt;/h2&gt;

&lt;p&gt;I found this approach very easy to implement, which was quite satisfying. The main advantages I see are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Simplicity: this is a reasonably simple approach to reinforcement learning&lt;/li&gt;
  &lt;li&gt;Directness: this seems like a very straightforward algorithm, rather than having multiple interacting systems as in other approaches&lt;/li&gt;
  &lt;li&gt;The command is quite interesting: the idea of commands seems like it would provide a lot of ways to tune and interact with the system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Downsides:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hyperparameters: there are quite a few hyperparameters, and they significantly affect the results.&lt;/li&gt;
  &lt;li&gt;I wasn’t able to get the lunar lander gym experiment working. I’m going to look at the hyperparams noted in the paper and see how well they work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regardless, it was neat to set the whole thing up, and I plan on playing around more with the command encoding and looking for alternative algorithms that require intrinsically fewer hyper parameters.&lt;/p&gt;

</description>
        <pubDate>Sat, 11 Jan 2020 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2020/01/11/upside-down-reinforcement-learning.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2020/01/11/upside-down-reinforcement-learning.html</guid>
        
        
      </item>
    
      <item>
        <title>Truncated Poisson Distributions in PyMC3</title>
        <description>&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;In this post, I’ll be describing how I implemented a zero-truncated poisson distribution in PyMC3, as well as why I did so.&lt;/p&gt;

&lt;h1 id=&quot;what-is-truncation&quot;&gt;What is truncation?&lt;/h1&gt;

&lt;p&gt;Truncated distributions arise when some parts of a distribution are impossible to observe. 
For example, consider how often someone visits a store in a week.
It’s natural to count people from one, and it’s hard to imagine the people who didn’t visit.
For example, do we assume that every single person in the world didn’t visit? All 7.6 billion of them?&lt;/p&gt;

&lt;p&gt;It’s natural to use a Poisson distribution for counts like these, but unfortunately the poisson distribution can produce zero counts.
Because there are no zero-counts, the poisson’s estimate of the average visiting rate is far higher than expected.
This also causes something called overdispersion, where the estimated variance of the lambda value is higher than it needs to be, making it hard to compare different rates.&lt;/p&gt;

&lt;h1 id=&quot;example-problem&quot;&gt;Example problem&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;See the &lt;a href=&quot;https://github.com/ririw/ririw.github.io/blob/master/assets/zero-truncated-poisson/Zero truncated poisson.ipynb&quot;&gt;accompanying notebook for the code&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this problem, we’ll be working with a really simple dataset. 
In it, we have two groups of people, one with an average of 0.79 visits per week, the other with 0.95 visits per week:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;lams&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.79&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.95&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;# The two lambdas
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# Pick 4000 people, and give them groups
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poisson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lams&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Count their visits
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# Remove any counts that are zero
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_choices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;choices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# And also find the groups for those non-zero visitors
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;truncated_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_palette&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;non-truncated-solution&quot;&gt;Non-truncated solution&lt;/h2&gt;

&lt;p&gt;Just to start off, we make sure that we an fit the non-truncated version:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HalfNormal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'lam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Poisson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;# We only take trunc_size values here,
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;observed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# because we want to make sure the tests are
&lt;/span&gt;                                           &lt;span class=&quot;c1&quot;&gt;# given the same amount of data.
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ppc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_ppc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This works perfectly (that’s good), and here is the distribution of the two lambda values, as well as the true values (in blue and orange), 
and histograms for post-predictive checks:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/zero-truncated-poisson/truncated-zpt-easy.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Generally, the 95% confidence intervals will not include each other (depending on the particulars of the random dataset).
And also, the 95% CI contains the two lambda values we expect!&lt;/p&gt;

&lt;h2 id=&quot;naive-solution&quot;&gt;Naive solution&lt;/h2&gt;

&lt;p&gt;Here, we just use the poisson distribution and ignore any issues of truncation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HalfNormal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'lam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Poisson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; 
        &lt;span class=&quot;n&quot;&gt;observed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;full_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trunc_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This distribution is less promising:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/zero-truncated-poisson/truncated-zpt-dumb.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The lambdas recovered are far from where we’d expect them, and the HPD intervals sit over each other, suggesting that the two groups do not have different visit rates (even though we know they do). Lastly, the PPC checks are just way off - the two distributions are wildly different.&lt;/p&gt;

&lt;p&gt;I also had some warnings while fitting this - PyMC3 is trying to tell me I messed up 😊.&lt;/p&gt;

&lt;h2 id=&quot;solution-using-a-truncated-distribution&quot;&gt;Solution using a truncated distribution&lt;/h2&gt;

&lt;p&gt;I’ll go into the implementation of the zero-truncated poisson in the next section, but here’s an example of it in action;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HalfNormal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'lam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ZTP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_choices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;observed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;truncated_counts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;/assets/zero-truncated-poisson/truncated-zpt-clever.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Notice here that we’ve successfully used the truncated data to retrieve the correct lambda values!
Additionally, the two distribution’s HPD do not overlap, meaning that we’ve (correctly) inferred that there are different values for the two group’s visitation rates.&lt;/p&gt;

&lt;h2 id=&quot;things-weve-done&quot;&gt;Things we’ve done:&lt;/h2&gt;
&lt;p&gt;With our zero-truncated poisson we’ve:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;✔ Accounted for the truncated zero data&lt;/li&gt;
  &lt;li&gt;✔ correctly recovered underlying lambda values even when large parts of the dataset are unobserved&lt;/li&gt;
  &lt;li&gt;✔ managed to prove that there’s a significant difference between the two groups - impossible without accounting for the truncation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;implementation&quot;&gt;Implementation&lt;/h1&gt;
&lt;p&gt;How’s it done? Two steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the &lt;a href=&quot;https://en.wikipedia.org/wiki/Truncated_distribution&quot;&gt;truncated distribution&lt;/a&gt; formula to work out the log-pdf of the distribution.&lt;/li&gt;
  &lt;li&gt;Use the same formulas to work out the inverse-CDF of the new distribution&lt;/li&gt;
  &lt;li&gt;Implement the distribution in pymc3&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step-1&quot;&gt;Step 1&lt;/h2&gt;

&lt;p&gt;The poisson distribution’s PDF is \(\frac{\lambda^k e^{-\lambda}}{k!}\).&lt;/p&gt;

&lt;p&gt;The new PDF is given by \(\frac{g(x)}{F(\infty) - F(0)}\) where \(F\) is the poisson CDF and \(g(x)\) is the modified PDF which is zero at \(x=0\).&lt;/p&gt;

&lt;p&gt;What we’re doing here is cutting out part of the PDF (to make \(g(x)\)), and scaling the PDF to account for the missing probability.
The poisson CDF is 1 at inifinity (remember that the CDF here represents the probability of seeing a count less than infinity - so its probability is one), so \(F(\infty) = 1\).
\(F(0)\) is simply found by substituting it into the formula on wikipedia for the CDF of the poisson distribution. I used the summation approach - because it’s very simple:&lt;/p&gt;

\[e^{-\lambda}\sum_{i=0}^{0}\frac{\lambda^i}{i!}\]

&lt;p&gt;Which of course simplifies to \(e^{-\lambda}\). The final formula is (&lt;a href=&quot;https://en.wikipedia.org/wiki/Zero-truncated_Poisson_distribution&quot;&gt;click here for a derivation&lt;/a&gt;):&lt;/p&gt;

\[\frac{\lambda^k}{(e^\lambda-1)k!}\]

&lt;p&gt;So now we simply take the log of this, and we get:&lt;/p&gt;

\[\log \lambda^k - \log(k!) - \log(e^\lambda - 1)\]

&lt;p&gt;PyMC3 has a few useful functions here to help us out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;logpow: for taking the log of powers, with special cases for zeros&lt;/li&gt;
  &lt;li&gt;factln: a differential log-factorial&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the final expression is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p = logpow(mu, value) - (factln(value) + pm.math.log(pm.math.exp(mu)-1))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;step-2&quot;&gt;Step 2&lt;/h2&gt;
&lt;p&gt;For this, we need a few ingredients:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a way to map random numbers in interval [0, 1) to the truncated interval&lt;/li&gt;
  &lt;li&gt;The ppf function, which is &lt;a href=&quot;https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.stats.rv_continuous.ppf.html&quot;&gt;what scipy calls the inverse CDF&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then it’s simply a matter of taking the mapped numbers and then passing them through the ppf function of the existing poisson distribution.&lt;/p&gt;

&lt;p&gt;I implemented this as a “scipy-like” function that I could pass in to PyMC3’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generate_samples&lt;/code&gt; method:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# This is part of the ZPT class
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;zpt_cdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# get the lam value and produce the scipy frozen
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# poisson distribution using it
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scipy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distributions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poisson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# find the lower CDF value, the probability of a 
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# zero (or lower) occuring. 
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The upper CDF is 1, as we are not placing an upper
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# bound on this distribution
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;upper_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Move our random sample into the truncated area of
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# the distribution
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;shrink_factor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shrink_factor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# and find the value of the poisson distribution
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# at those sampled points. In this case, this will
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# mean no zeros.
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ppf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;step-3&quot;&gt;Step 3&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pymc3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pymc3.distributions.dist_math&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logpow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factln&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pymc3.distributions&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;draw_values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_samples&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;theano.tensor&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;scipy.stats.distributions&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ZTP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Discrete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# The mode of the poisson is given by floor(mu), but if this is 
&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# zero we use 1 instead as it's the next most-frequent value.
&lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;maximum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'int32'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_tensor_variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;zpt_cdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scipy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distributions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poisson&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
        &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;upper_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;nrm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;upper_cdf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nrm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lower_cdf&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ppf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;draw_values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generate_samples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zpt_cdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;dist_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;logp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;log_prob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Return zero when mu and value are both zero
&lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                         &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Hopefully at this point you’ve seen the importance of accoutning for truncated data - if you want to the the right results, you should be checking for truncation effects. And hopefully I’ve given you enough of a starting point to write your own truncated distributions, should the need arise.&lt;/p&gt;

&lt;p&gt;Happy sampling!&lt;/p&gt;
</description>
        <pubDate>Wed, 18 Oct 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/10/18/pymc3-truncated-poisson.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/10/18/pymc3-truncated-poisson.html</guid>
        
        
      </item>
    
      <item>
        <title>Using optaplanner to plan water supplies</title>
        <description>&lt;h1 id=&quot;code&quot;&gt;Code&lt;/h1&gt;
&lt;p&gt;The corresponding code is on &lt;a href=&quot;https://github.com/ririw/burning-water/tree/post&quot;&gt;github&lt;/a&gt;, just run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sbt run&lt;/code&gt; to run it. This blog post is based on the codebase tagged “post”.&lt;/p&gt;

&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;

&lt;p&gt;I’m heading to the burn this year. 
My job in the camp is to handle water logistics, both fresh water and grey water.
For those who don’t know about the burning man, it’s a party in the desert, with zero infrastructure.
That means you need to plan everything yourself, including water, both brining fresh water in, and moving dirty water out.
And of course, water is probably the most important element of our planning. 
Without it, we’ll die.&lt;/p&gt;

&lt;p&gt;So, I was able to produce a basic water plan in excel, but it was built on multiple assumptions (consumption rates, grey water production rates, etc).
I wanted to be able to quickly challeng my assumptions, and to do so, I want to be able to find good solutions quickly and easily. 
Excel wouldn’t do.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.optaplanner.org/&quot;&gt;Optaplanner&lt;/a&gt; is a constraint satisfaction solver, meaning I enter my constraints, and have it produce a working solution to them.
By encoding my water problem in it, I’m able to explore the effects of many different situations and contingencies.&lt;/p&gt;

&lt;p&gt;I also wanted to provide a really simple introuction to Optaplanner. 
Their documentation assumes a high level of familiarly with their ecosystem, as well as assuming that you want to use DROOLs, their business rule language.
For this project however, I wanted something simpler and easier.&lt;/p&gt;

&lt;h2 id=&quot;background-on-constraint-solving&quot;&gt;Background on constraint solving&lt;/h2&gt;

&lt;p&gt;I’ve fallen in love with optaplanner. 
I thinks it’s because it’s something I can see directly improving my ability to do things: a real jump forward.
Where previously I’d solve complex planning problems with a spreadsheet, I feel like now I’ve got a new ability to make a computer do the work for me.
I also see it being an under appreciated area of business planning.
For the most part, complex planning solvers are embedded in specific applications, due to the difficulty of developing them.
With tools like optaplanner, I can see potential for a lot of new applications within businesses and organizations to improve things, without having to develop (or buy) a specialized tool.&lt;/p&gt;

&lt;p&gt;Anyway, constraint solving is basically:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;given a set of constraints, find a way to satisfy them.
Constraint solving is NP-hard in general.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;my-problem&quot;&gt;My problem&lt;/h2&gt;

&lt;p&gt;To model my water problem, I first thought about the constraints:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Water is stored in 2 water barrels, and in an RV’s tank, and in several smaller bottles (for redundancy and convenience)&lt;/li&gt;
  &lt;li&gt;Grey water is to be stored in the RV, and in one of the water tanks once it is empty.&lt;/li&gt;
  &lt;li&gt;Once grey water enters a container, it may not be used for fresh water&lt;/li&gt;
  &lt;li&gt;We may not use more water than exists in a container&lt;/li&gt;
  &lt;li&gt;We may not fill a container with grey water above its capacity&lt;/li&gt;
  &lt;li&gt;Neat solutions are preferred, so we don’t need to change barrels too often.&lt;/li&gt;
  &lt;li&gt;We’d prefer not to use the fresh water barrels for grey water, because that means we can’t use it for fresh water next year.&lt;/li&gt;
  &lt;li&gt;We want to allow showers in the RV.&lt;/li&gt;
  &lt;li&gt;RV showers must draw from the RV water supply, and feed to the RV grey water tank&lt;/li&gt;
  &lt;li&gt;Showers are best if they’re around the middle of the burn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There’s a lot here, but of course I’ve built them up incrementally, which is how I suggest you do it as well.&lt;/p&gt;

&lt;p&gt;You’ll also notice some of these are hard constraints (“may not”, “must”, etc), that must be satisfied, and soft constraints (“like”, “best”, etc), which do not. 
Optaplanner recommends using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HardSoftScore&lt;/code&gt; for this, and it will aim to optimize the hard score over the soft.&lt;/p&gt;

&lt;p&gt;Optaplanner also has several important concepts that I’ll weave together in my solution:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningEntity&lt;/code&gt; represents something optaplanner will modify to find a solution.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningVariable&lt;/code&gt;, a variable optaplanner modifies to find a solution.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ValueRangeProvider&lt;/code&gt;, that gives a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningVariable&lt;/code&gt; a set of possible values.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningEntityCollectionProperty&lt;/code&gt;, that gives optaplanner a set of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningEntity&lt;/code&gt;s to work on.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningScore&lt;/code&gt;, that tells it where to put the score for a particular solution.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningSolution&lt;/code&gt;, representing a solution to a set of constraints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I was developing it, Optaplanner was pretty good about telling me what I did wrong.
If you’re confused about how a problem fits together, I recommend trying something then working through the error message Optaplanner throws up.&lt;/p&gt;

&lt;h1 id=&quot;the-water-containers&quot;&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L180-L192&quot;&gt;The water containers&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;The water containers are quite straightforward.
I use a class structure to keep track of the different types of container.
Each container has:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A name, for printing stuff later&lt;/li&gt;
  &lt;li&gt;A capacity, meaning it either &lt;em&gt;contains&lt;/em&gt; that much water at the start of the burn, or is empty and &lt;em&gt;can contain&lt;/em&gt; that much grey water at the end.&lt;/li&gt;
  &lt;li&gt;A flag denoting whether it is for grey water.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the constraints above talks about transforming water barrels into grey water barrels. 
I found it easiest to design this as a constraint, rather than modeling transformable water barrels.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;capacity&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isGrey&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// Optaplanner often requires a no-args constructor so it can clone and modify things.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterBarrel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;barrel $id&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GreywaterBarrel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;grey barrel $id&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Boxes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;boxwater&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVWater&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rv water&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVGreyWater&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTanks&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rv grey&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nTanks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVBlackWater&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RVContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rv black&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;21&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h1 id=&quot;the-water-use-day&quot;&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L194-L201&quot;&gt;The water use day&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;This object represents a day of water use.
I frequently refer to this as a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grain&lt;/code&gt;, which I think is a word I took from optaplanner’s docs.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Water consumed (given)&lt;/li&gt;
  &lt;li&gt;Grey water produced (given)&lt;/li&gt;
  &lt;li&gt;Where the water came from (set by optaplanner)&lt;/li&gt;
  &lt;li&gt;Where the grey water goes (set by optaplanner)&lt;/li&gt;
  &lt;li&gt;The number of showers (set by optaplannner)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, you can see a number of the annotations needed by optaplanner&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningEntity&lt;/code&gt; one means that this is an object that optaplanner can manipulate&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningVariable&lt;/code&gt; ones show which variables optaplanner can alter. 
They also tell optaplanner where to find the values that it can use for those variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@PlanningEntity&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterUseDay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;day&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterUse&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;greyWater&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                  &lt;span class=&quot;nc&quot;&gt;_source&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_dest&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@PlanningVariable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valueRangeProviderRefs&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;nshowers&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;showers&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@PlanningVariable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valueRangeProviderRefs&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;containers&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_dest&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@PlanningVariable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valueRangeProviderRefs&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;containers&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;_source&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// All the things optaplanner manipulates need no-args constructors.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h1 id=&quot;the-solution-object&quot;&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L207-L224&quot;&gt;The solution object&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;A solution object represents an entire problem, both the containers and the days of use.
There’s a lot of sillyness here due to scala/java compatibility issues. 
The scala/java boundary was a bit tricky, but I much prefer the scala solution over the java one I started on.&lt;/p&gt;

&lt;p&gt;Here you see:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Range providers, so optaplanner knows where to find the containers and shower values. 
notice the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id&lt;/code&gt; field matches the ones above in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PlanningVariable&lt;/code&gt; annotations.&lt;/li&gt;
  &lt;li&gt;A slot for the score.&lt;/li&gt;
  &lt;li&gt;And that’s pretty much it.&lt;/li&gt;
&lt;/ul&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@PlanningSolution&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WaterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;showerTarget&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_containers&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;],&lt;/span&gt;
                   &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_grains&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;WaterUseDay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@ValueRangeProvider&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;containers&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@ProblemFactCollectionProperty&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;java.util.List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;WaterContainer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asJava&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@ValueRangeProvider&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;nshowers&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;showerCapacity&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;CountableValueRange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ValueRangeFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;createIntValueRange&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)}&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@PlanningEntityCollectionProperty&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;usageGrains&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;java.util.List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;WaterUseDay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;_grains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asJava&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@PlanningScore&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;HardSoftScore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// To keep the solution neat, I've put the messy print code into a separate function.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Utils&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;problemToString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;scoring&quot;&gt;Scoring&lt;/h2&gt;

&lt;p&gt;The scoring is where all the magic happens.
This was also the most confusing part of the project. 
The optaplanner docs assume that you’re going all in, and you’ll be using their DROOLs planning system. 
I’m planning on learning how to write the DROOLs rules, but for a simple problem like this, I found them to be more effort than they’re probably worth.
Particularly if you’re a new user, it’s off putting to have to understand a new language, just to write some simple rules.&lt;/p&gt;

&lt;p&gt;I’ll only show off two of my scoring functions, there were quite a few.&lt;/p&gt;

&lt;h3 id=&quot;check-barrel-capacity&quot;&gt;Check barrel capacity&lt;/h3&gt;
&lt;p&gt;Basically this checks the containers have space! Super duper important!
It does so by:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Making a mapping, container → capacity&lt;/li&gt;
  &lt;li&gt;Over each grain, subtract water and grey water use from the corresponding container.&lt;/li&gt;
  &lt;li&gt;Find the RV fresh water and the RV grey water containers, because I’ll need references to them to do the shower accounting in step 4.&lt;/li&gt;
  &lt;li&gt;Over each grain, find showers, and appropriately handle their 3GA of water use and grey water production.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can see it returning a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HardSoftScore&lt;/code&gt;, with soft score zero and hard score set to the problem count, ie, the number of overcapacity containers.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;verifyCapacity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;HardSoftScore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;containerCapacities&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mutable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;capacity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grain&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;usageGrains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// Handle direct source/dest&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;waterUse&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;greyWater&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rvFresh&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;isInstanceOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RVWater&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;get&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;rvGrey&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;isInstanceOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;RVGreyWater&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;get&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grain&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;usageGrains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rvFresh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rvFresh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;showers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rvGrey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rvGrey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;showers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;problems&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;containerCapacities&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;HardSoftScore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;problems&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;encourage-neat-solutions&quot;&gt;Encourage neat solutions&lt;/h3&gt;
&lt;p&gt;This encourages the planner to find “neat” solutions, where we don’t change barrel too often. 
Whenever the water source or destination changes, it counts towards the soft score as a negative.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;neatness&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;WaterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;HardSoftScore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;softScore&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;usageGrains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;waterProblem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;usageGrains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;asScala&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;tail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;softScore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;softScore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;HardSoftScore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softScore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;a-tour-of-the-rest-of-the-code&quot;&gt;A tour of the rest of the code&lt;/h2&gt;

&lt;p&gt;I don’t really like code heavy posts - github is a better place for that sort of thing. 
I will add a breakdown of things to look at though.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/789520795d0f2f301fd37e5a230d5377207d0aa6/src/main/resources/waterplanner/solverconfig.xml&quot;&gt;solverconfig.xml&lt;/a&gt; - this sets up the optaplanner config. 
It’s very simple, all it does is tell optplanner what classes to scan, the name of the scoring class, and the timout on the problem&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/Main.scala&quot;&gt;Main&lt;/a&gt; - the program entry point.
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/Main.scala#L9-L36&quot;&gt;Line 9-34&lt;/a&gt;: problem setup, with various containers and day-grains, and camp sizes.&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/Main.scala#L37-L42&quot;&gt;Line 36-42&lt;/a&gt;: instantiating the solver, from the XML config.&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/Main.scala#L43-L45&quot;&gt;Line 43-45&lt;/a&gt;: running the solver&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/Utils.scala&quot;&gt;Utils&lt;/a&gt; - a really really confusing printer for the solution. Probably best ignored.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala&quot;&gt;WaterProblem.scala&lt;/a&gt; - the problem itself, the interesting part.
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/789520795d0f2f301fd37e5a230d5377207d0aa6/src/main/scala/waterplanner/WaterProblem.scala#L15&quot;&gt;WaterSoutionScore&lt;/a&gt; - this class is where the scoring happens, probably the most important part of the problem.
        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L16-L29&quot;&gt;forceRV&lt;/a&gt; - ensure that we don’t carry water barrels in in the RV with EA.&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L31-L51&quot;&gt;checkGrainExists&lt;/a&gt; - make sure every day of water goes somewhere&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L53-L84&quot;&gt;verifyCapacity&lt;/a&gt; - make sure we don’t go over the barrel’s capacity&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L82-L94&quot;&gt;neatness&lt;/a&gt; - encourage neat solutions&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L96-L138&quot;&gt;ensureNoverlap&lt;/a&gt; - make sure that the barrels aren’t used for fresh water if they’re used for grey water.&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L140-L144&quot;&gt;discourageBarrelGreyWater&lt;/a&gt; - while we could use both fresh water barrels for grey water, I’d prefer to keep one clean for next year. This codifies that objective.&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L146-L155&quot;&gt;encourageShowers&lt;/a&gt; - this is our only “positive constraint”. 
All the rest deal with things we &lt;em&gt;don’t&lt;/em&gt; want to happen, but this one basically says that showers are good and to try to fit lots in, by associating showers with positive returns.&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L157-L166&quot;&gt;calulateScore&lt;/a&gt; - find the overall score by adding up all the rules.&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L180-L192&quot;&gt;Various containers&lt;/a&gt;, as discussed above&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L194-L201&quot;&gt;The water use day&lt;/a&gt;, discussed above&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/ririw/burning-water/blob/post/src/main/scala/waterplanner/WaterProblem.scala#L207-L224&quot;&gt;The water problem&lt;/a&gt;, discussed above&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;an-example-solution&quot;&gt;An example solution&lt;/h2&gt;

&lt;p&gt;The solver, after solving for a while, spits out the following result:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Greybarrels:0hard/-100soft
Graincheck: 0hard/0soft
Capacity:   0hard/0soft
neatness:   0hard/-5soft
Noverlap:   0hard/0soft
Showers:    0hard/48soft
forceRV:    0hard/0soft
total:      0hard/-57soft
        showers: |   █▄ ▄ █  |
       barrel 1: |  sss      |	54.0/55.0
       barrel 2: |     sss   |	54.0/55.0
       boxwater: |ss      s  |	33.0/39.0
  grey barrel 1: |     dddddd|	54.0/55.0
  grey barrel 2: |           |	0.0/55.0
       rv black: |           |	0.0/21.0
        rv grey: |ddddd      |	52.5/56.0
       rv water: |         ss|	54.0/55.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; means “source” of water&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt; means “destination” for grey water&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;█&lt;/code&gt; denotes two showers scheduled for that day&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;▄&lt;/code&gt; denotes one&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see we’ve satisfied all hard objectives - that’s great news!
In terms of the soft objectives, I’m also happy with how they’ve all been satisfied.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;There’s good balancing over the tanks,&lt;/li&gt;
  &lt;li&gt;The solution is as neat as I think it could be. 
Even the split boxwater makes sense: we need to crack the fresh water barrel right away so we can use it all and turn it into a grey water barrel.
Note that the fresh water barrel arrives with the main group on the first day, rather than going in with EA.&lt;/li&gt;
  &lt;li&gt;And we managed to fit in 6 showers! Great!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This actually found a better solution than my manual attempt, by moving the box-water use to the start of the period, and thus leaving more water for showers.
I didn’t notice that option when I did it in excel.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Optaplanner provides an easy way to solve a large set of interesting problems.
I’ve aimed to show off these capabilities in a really simple example, to make it easily approachable.
Even though this is a simple example, it has real-world applicability for me, making it doubly satisfying.
I encourage you to take time to learn the sorts of things optaplanner can do, there’s a good chance you’ll find something cool to do with it, even if it’s something as simple as planning camp water supplies.&lt;/p&gt;
</description>
        <pubDate>Sun, 06 Aug 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/08/06/optaplanning-water.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/08/06/optaplanning-water.html</guid>
        
        
      </item>
    
      <item>
        <title>Cargo cult data science</title>
        <description>&lt;h1 id=&quot;data-science-as-a-cargo-cult&quot;&gt;Data science as a cargo cult&lt;/h1&gt;

&lt;p&gt;Data science is becoming an important competitive advantage for companies of all kinds.
With the introduction of a new set practices, there’s an increased risk of “&lt;a href=&quot;https://en.wikipedia.org/wiki/Cargo_cult_programming&quot;&gt;cargo cults&lt;/a&gt;” emerging within organizations. 
Cargo cults describe situations where people emulate a set of behaviours, without understanding their underlying motivations. 
When this happens with data-science, organizations will emulate the &lt;em&gt;technology&lt;/em&gt; behind data science, without creating a data-driven organizational culture.
I see this sort of issue pop up from time to time, and I’d like to share my ideas on how to &lt;em&gt;avoid&lt;/em&gt; its emergence.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/JohnFrumCrossTanna1967.jpg/320px-JohnFrumCrossTanna1967.jpg&quot; alt=&quot;Cargo cult symbol, credit to Tim Ross, https://commons.wikimedia.org/wiki/File:JohnFrumCrossTanna1967.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Data science can be an immensely powerful tool, and provides two things to organizations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Automated decision making&lt;/li&gt;
  &lt;li&gt;Improved manual decision making.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While many organizations &lt;em&gt;recognize&lt;/em&gt; the importance of data science, many of them fail to build effective data science capabilities.
I believe that this is because of a fundamental misunderstanding of data science, which causes them to implement the &lt;em&gt;technical&lt;/em&gt; parts of data science, without considering its human side.&lt;/p&gt;

&lt;p&gt;This is the same cargo-cult problem that appears in other fields.
In this post, I’ll talk about what I see as the best way to &lt;em&gt;avoid&lt;/em&gt; cargo-cult data science, and on how to deliver more value from a data science investment.&lt;/p&gt;

&lt;h1 id=&quot;a-typical-data-science-project&quot;&gt;A typical data-science project&lt;/h1&gt;
&lt;p&gt;Too many data science projects follow this trajectory, common to other IT projects:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;They start out exciting, and full of promise, with strong buy-in from upper management.&lt;/li&gt;
  &lt;li&gt;Initial prototypes look promising, and the project appears to address an important organizational issue.&lt;/li&gt;
  &lt;li&gt;The project starts to slip, failing to deliver on promises.&lt;/li&gt;
  &lt;li&gt;At the same time, the organization starts to lose interest in the project, and it begins to falter.&lt;/li&gt;
  &lt;li&gt;The project doesn’t fail, but doesn’t deliver the organizational change it promised.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is particularly problematic for data-projects, because they’re typically meant to introduce new forms of management or organizational behavior.
In contrast to many traditional IT projects, which represent improvements in existing processes, data science projects often aim to &lt;em&gt;change&lt;/em&gt; the way an organization behaves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why did this project fail?&lt;/strong&gt; Most people, especially data scientists, would focus on either the technical execution, or blame management for abandoning it.
However, I believe the failure occurred earlier, when the initial design failed to account for how it would fit into the organization.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://blog.richardweiss.org/assets/data-science-orgs/jetty-landing-stage-sea-fog.smol.jpg&quot; alt=&quot;A failed data lake&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;the-human-side-of-data-science&quot;&gt;The human side of data science&lt;/h1&gt;
&lt;p&gt;In my experience, a “data driven organization” is about far more than the existence of analytics and measurement.
Fundamentally, to be a data driven company, data needs to be part of the internal dialogue spoken by all members.&lt;/p&gt;

&lt;p&gt;This is in direct contrast to the project described above, which focused on technology over objectives, a classic sign of a cargo cult.
For example, take the all to common “enterprise data lake project”. 
Many organizations now see some sort of data warehouse as a necessity, but the project itself can never deliver a culture of analytics within the company.
The problem here is that the project is viewed in &lt;em&gt;technical&lt;/em&gt; rather than &lt;em&gt;human&lt;/em&gt; terms.&lt;/p&gt;

&lt;p&gt;Notice that we have a very clear idea of the technical side of this project. 
There’s a good chance it involves:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A large SQL database&lt;/li&gt;
  &lt;li&gt;An ETL tool&lt;/li&gt;
  &lt;li&gt;A frontend, perhaps tableau or similar&lt;/li&gt;
  &lt;li&gt;And dozens of reports that managers don’t read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In contrast, a good data project would be described as “the tool we use to pick phone plans to sell”.
Here we know far more about where it fits within the organization:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We know the organization is probably a telco&lt;/li&gt;
  &lt;li&gt;And that the tool is specifically for their retail operations&lt;/li&gt;
  &lt;li&gt;We know that the tool puts forward recommendations for things to sell&lt;/li&gt;
  &lt;li&gt;It presumably monitors the outcomes of sales and optimizes itself. After all, it’s a data project.
It probably uses a data-warehouse to do so, and maybe it reports with tableau.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, to avoid a cargo cult of data, organizations should stop chasing &lt;em&gt;technology&lt;/em&gt; and start working with experienced technologists who can apply technology to solve organizational problems.&lt;/p&gt;

&lt;h1 id=&quot;a-successful-data-project&quot;&gt;A successful data project&lt;/h1&gt;

&lt;p&gt;How might the phone plan tool above come about?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The telco engaged an experienced strategist with data experience&lt;/li&gt;
  &lt;li&gt;They identified above average churn in phone plans, and that it was a problem that could easily be tackled with data based tools.&lt;/li&gt;
  &lt;li&gt;They worked closely with the following stakeholders:
    &lt;ul&gt;
      &lt;li&gt;Executives, to understand how their project relates to company goals, and how success would be reported.&lt;/li&gt;
      &lt;li&gt;Retailers, to understand their needs&lt;/li&gt;
      &lt;li&gt;Retail managers, to understand why retailers are being pushed to oversell plans.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;With these in mind, they design a small technical MVP that recommends plans and monitors for feedback. Nothing too complicated&lt;/li&gt;
  &lt;li&gt;And finally, they ensure that the retailers work towards the “churn optimal” goal, rather than the “sell the biggest plan” goal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This project’s best feature is that it identified a specific sub-part of the organization, with a clear problem, and then built a solution for it.
It’s important to try to find an organizational capability that can be easily partitioned off and then viewed through the data science lens, as I’ll explain later.&lt;/p&gt;

&lt;h1 id=&quot;spreading-data-science&quot;&gt;Spreading data science&lt;/h1&gt;

&lt;p&gt;I used to think data science would take over every organization, once it was introduced. I was very naive.
My reasoning was simple: anyone with data science on their side would be able to &lt;em&gt;prove&lt;/em&gt; that their efforts worked better than their peers.
That would be rewarded, and data-science would quickly become an essential internal requirement for everyone, due to competitive pressure.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I used to think data science would take over every organization, once it was introduced. I was very naive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, that assumes that someone presenting an analytical presentation will be viewed more favourably than someone presenting something softer.
Basically, I had assumed a data-driven culture exists, when in reality businesses are struggling to create that culture in the first place.&lt;/p&gt;

&lt;p&gt;That’s why the recommendations above talk about picking a small, somewhat isolated part or aspect of the organization.
Doing this makes it easier to move that sub-group to being data driven and analytical.
It also minimizes the problematic boundary between the data-driven subset and the rest of the business, making it easier to view that subgroup according to their metrics.&lt;/p&gt;

&lt;p&gt;The other advantage of the “minimal boundary” is that the organizational hierarchy will tend to push the data science down into the subgroup.
With their bosses demanding analytical results, managers will demand analytical results from their peers, and so on, down throughout the subgroup.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Data science is best viewed as a form of company culture, rather than a set of technologies.
However, many firms will try to create that company culture by acquiring data-science technology, rather than working on their culture.&lt;/p&gt;

&lt;p&gt;This is understandable: company culture is notoriously difficult to change, but data science is also a little bit different from other cultural matters.
I argue that it’s best to spread a data-driven culture from the top of an organization down, by requiring that reports be analytical.
Doing so then means that organizational subunits must become data driven, which causes their sub-units to become data-driven, and so on.
For companies trying to increase their analytical strength, this suggests that solutions that &lt;em&gt;start&lt;/em&gt; with technology (“we’ll build a data warehouse”, or “let’s get AI”) are likely to fail.&lt;/p&gt;

&lt;p&gt;Solutions that help measure and improve the performance of a part of the company (“we’ll help you measure marketing ROI”, or “we will introduce predictive maintenance), will spread and become enduring organizational strengths.&lt;/p&gt;

&lt;p&gt;So in summary:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Look for solutions that focus on the organization, not the technology&lt;/li&gt;
  &lt;li&gt;In fact, I’d be a little skeptical of tech-first solutions - are they really solving your &lt;em&gt;real&lt;/em&gt; problem?&lt;/li&gt;
  &lt;li&gt;And then apply them to problems that can be “boxed up” so that the data-driven solution is actually used to evaluate that problem.&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Tue, 25 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/07/25/data-science-in-organizations.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/07/25/data-science-in-organizations.html</guid>
        
        
      </item>
    
      <item>
        <title>Experiment: simple conditioning of WGANs</title>
        <description>&lt;p&gt;I’m trying a new blog format. 
I’m going to lay these out light science experiments in high school: hypothesis/experiment/results/discussion.
We’ll see how it goes.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ririw/ririw.github.io/blob/master/assets/conditional-wasserstein-gans/Improved.ipynb&quot;&gt;The notebook for this project can be found here&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;hypothesis&quot;&gt;Hypothesis&lt;/h2&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;
&lt;p&gt;Wassenstein generative adversarial networks minimize the EM-distance between a &lt;em&gt;generated&lt;/em&gt; distribution, and a &lt;em&gt;true&lt;/em&gt; distribution:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; EM(P(generated data), P(actual data))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A conditional GAN allows one to include a condition (as an input to the generator), that effects the generated data:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; EM(P(generated data|y), P(real data|y))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;hypothesis-1&quot;&gt;Hypothesis&lt;/h3&gt;

&lt;p&gt;I want to see if I can include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; condition by simply forcing it to be a particular value. 
The WGAN critic simply the EM distance between the generated data and the real distribution, so by including y in the data, I convert the minimization objective to&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; EM(P(generated data, y), P(real data, y))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In effect, I’ve taken away the generator’s ability to control the condition.
At the same time, I pass the condition in to the generator, so that it is forced to adapt to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; passed to it.
The critic sees the joint distribution of data and condition, so it learns the full joint distribution.&lt;/p&gt;

&lt;h2 id=&quot;method&quot;&gt;Method&lt;/h2&gt;

&lt;p&gt;This is quite easy, and for my experiment I’ve used data from one of 10 2d Gaussians. 
Here’s the real data-distribution, colored by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;, the conditional:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/conditional-wasserstein-gans/real.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The generator’s structure is simply:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Gen (
   (layers): ModuleList (
     (0): Linear (32 -&amp;gt; 128)
     (1): ReLU ()
     (2): Linear (128 -&amp;gt; 256)
     (3): ReLU ()
     (4): Linear (256 -&amp;gt; 256)
     (5): ReLU ()
     (6): Linear (256 -&amp;gt; 2)
   )
 )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The critic is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Critic (
  (layers): ModuleList (
    (0): Linear (12 -&amp;gt; 256) # 2 inputs from the generator, and 10 one-hot classes.
    (1): ReLU ()
    (2): Linear (256 -&amp;gt; 256)
    (3): ReLU ()
    (4): Dropout (p = 0.5)
    (5): Linear (256 -&amp;gt; 256)
    (6): ReLU ()
    (7): Dropout (p = 0.5)
    (8): Linear (256 -&amp;gt; 1)
  )
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I use the &lt;em&gt;improved&lt;/em&gt; Wasserstien training regularization method, rather than the clipping approach.&lt;/p&gt;

&lt;p&gt;During each training step I do the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Train the critic (x5)
    &lt;ol&gt;
      &lt;li&gt;Sample a batch from the dataset, of both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and matching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;s.&lt;/li&gt;
      &lt;li&gt;Sample from the generator, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Review the sample with the critic, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; and the sample.&lt;/li&gt;
      &lt;li&gt;Review the real data with the critic, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;.&lt;/li&gt;
      &lt;li&gt;Compute the loss, as mean(sample_review) - mean(real_review) + improved wasserstein regularization term.&lt;/li&gt;
      &lt;li&gt;Optimize the critic&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Sample a batch from the dataset, of both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and matching &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;s.&lt;/li&gt;
  &lt;li&gt;Sample from the generator, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Review the sample with the critic, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; and the sample.&lt;/li&gt;
  &lt;li&gt;Review the real data with the critic, passing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Compute the loss, as mean(sample_review)&lt;/li&gt;
  &lt;li&gt;Optimize the generator&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ririw/ririw.github.io/blob/master/assets/conditional-wasserstein-gans/Improved.ipynb&quot;&gt;The notebook for this project can be found here&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;results&quot;&gt;Results&lt;/h2&gt;

&lt;p&gt;This worked great! By the 30-thousandth iteration, the generator was closely matching both the clusters:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/conditional-wasserstein-gans/points_actual_30000.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And the classes of the data:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/conditional-wasserstein-gans/points_classes_30000.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This means I now have a way to feed in conditions to a WGAN, and really easily generate data conditioned on it.&lt;/p&gt;

&lt;p&gt;It’s also interesting to look at the scores of the generated points, so these are the “reviews” of the data:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/conditional-wasserstein-gans/points_scores_30000.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;discussion&quot;&gt;Discussion&lt;/h2&gt;

&lt;p&gt;This leaves a few open questions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Is it possible to double-down and make a generative process for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;s.
In this case we drew samples of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; from the dataset, but perhaps it would be better to generate both data and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; values,
but generate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; values in a separate module so that later I can set conditions.&lt;/li&gt;
  &lt;li&gt;Does it work for MNIST? And CIFAR?&lt;/li&gt;
  &lt;li&gt;Is it possible to condition on continuous values? This is easy to test as a complex multiple regression problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;It’s quite easy to make a conditional-WGAN, by simply:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Passing the conditions to the data-generator.&lt;/li&gt;
  &lt;li&gt;Also passing the conditions (and the generated data) to the critic when scoring fake data.&lt;/li&gt;
  &lt;li&gt;Passing the real-data and matching conditions to the critic when scoring real data.&lt;/li&gt;
&lt;/ol&gt;

</description>
        <pubDate>Fri, 21 Jul 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/07/21/conditional-wasserstein-gan.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/07/21/conditional-wasserstein-gan.html</guid>
        
        
      </item>
    
      <item>
        <title>Fixing wandering intercepts in PYMC3</title>
        <description>&lt;p&gt;For a while now, I’ve had a problem in PYMC3, where intercepts would “wander”. In this post, I explain,&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;How to fix this problem&lt;/li&gt;
  &lt;li&gt;An explanation of different ways to encode categorical values in linear models.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The notebook for this project can be found &lt;a href=&quot;https://github.com/ririw/ririw.github.io/blob/master/assets/wandering-coefficients.ipynb&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;motivating-example&quot;&gt;Motivating example&lt;/h1&gt;

&lt;p&gt;I built a simple model, of the form &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y ~ C(a) + b + 1&lt;/code&gt;, and put it through pymc3. In the trace, you can see the “wandering” behaviour, which arises because pymc3 has a choice of where to put the constant value. It can either put the constant into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; values, or into the intercept, and either way is pretty much fine.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/wandering.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This leads to a number of problems: the model is very slow to run, I think because the NUTS sampler rejects many values before working out how it needs to share the wandering value. It also makes the results of the sampler very hard to use. As you can see, the different &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; values don’t settle down, meaning I can’t really infer things about them.&lt;/p&gt;

&lt;h2 id=&quot;dataset&quot;&gt;Dataset&lt;/h2&gt;
&lt;p&gt;The dataset is easy to build, simply:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;804188826&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;y_underlying&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_underlying&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'y'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;model&quot;&gt;Model&lt;/h2&gt;
&lt;p&gt;The model follows the basic structure of a lot of PYMC3 models. The problematic line is commented.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Wandering example
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'intercept'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HalfNormal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'std'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_coeff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Right here was our mistake.
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;observed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ppc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_ppc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;treatment-coding&quot;&gt;Treatment coding&lt;/h2&gt;
&lt;p&gt;The problem here is that pymc3 even has a choice of where to put the constant term. In this model, it would be possible to remove the intercept term, but that wouldn’t really solve the problem when there are multiple constant terms. In order to solve this, it’s necessary to use better contrast encoding schemes. Contrast coding provides a better way to encode these constant parameters. The simplest way is “treatment” coding, which is basically one-hot encoding, &lt;em&gt;but one of the options has no effect&lt;/em&gt;. Doing this enforces a constraint that the missing option has effect zero.&lt;/p&gt;

&lt;p&gt;To do this, I recommend the patsy contrasts module.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;cs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contrasts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Treatment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code_without_intercept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;a_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'b'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'intercept'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HalfNormal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'std'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theano&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_coeff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# No longer a problem
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;b_coeff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;intercept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'obs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;observed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_mat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ppc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_ppc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;One other advantage of this is that the sampling is &lt;em&gt;much&lt;/em&gt; faster in some cases.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/treatment_trace.png&quot; alt=&quot;&quot; /&gt;
You can see that the parameters no longer wander! Here’s the contents of the contrast matrix:
&lt;img src=&quot;/assets/wandering-coefficients/treatment_coding.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;sum-coding&quot;&gt;Sum coding&lt;/h2&gt;
&lt;p&gt;In treatment coding, one of the coefficients of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; had zero effect, and all the others are expressed as deviations from that value. That’s neat, but another option might be to make all the treatments sum to zero:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;cs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contrasts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Treatment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code_without_intercept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And proceeed as before.  The coding has a final row of -1, rather than zero as before:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/sum_coding.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;diff-coding&quot;&gt;Diff coding&lt;/h2&gt;
&lt;p&gt;This is the last coding schema I’ll cover, it’s pretty weird. In diff coding, the coefficients are the differences between neighbouring coefficients:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; cs = contrasts.Diff().code_without_intercept(list(range(10)))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s contrast matrix looks interesting to say the least:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/diff_coding.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;different-coding-outputs&quot;&gt;Different coding outputs&lt;/h1&gt;
&lt;p&gt;The best way to understand these differences is to look at the coefficients, and what we expect them to be.&lt;/p&gt;

&lt;p&gt;To start with, here are all the different coefficients, side by side:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/coeffs.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;treatment-coding-1&quot;&gt;Treatment coding&lt;/h2&gt;
&lt;p&gt;In sum coding, the coefficients are the same as the original coefficients, minus the first coefficient:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'treatment'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace_trt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'actual'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/treatment_coeffs.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;sum-coding-1&quot;&gt;Sum coding&lt;/h2&gt;
&lt;p&gt;In sum coding, the coefficients are the same as the original coefficients, minus the mean of all coefficients, and ignoring the first coefficient.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'sum'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'actual'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/sum_coeffs.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;diff-coding-1&quot;&gt;Diff coding&lt;/h2&gt;
&lt;p&gt;Diff coding is neat! Each coefficient is the difference with the coefficient preceding it! Now, I’m not sure why you’d need this, but you know, you do you.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;pandas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'diff'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trace_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'a'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;'actual'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;/assets/wandering-coefficients/diff_coeffs.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;conclusion--discussion&quot;&gt;Conclusion &amp;amp; Discussion&lt;/h1&gt;
&lt;p&gt;One thing I now realize is that some of my decision heuristics have been broken for a long time. I’ve always assumed that having a positive coefficient in a category meant that that category had an effect. From these categorical codings, it’s clear that simply looking at each category isn’t enough. You need to look at all categories, together, and then link them back to the original question to properly answer it.&lt;/p&gt;

&lt;p&gt;However, overall this approach solves the wandering problem, and adds in a bunch of new regression tricks for me to start considering.&lt;/p&gt;
</description>
        <pubDate>Tue, 27 Jun 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/06/27/wandering-pymc3.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/06/27/wandering-pymc3.html</guid>
        
        
      </item>
    
      <item>
        <title>Relevancy scoring reddit to find related subs</title>
        <description>&lt;p&gt;I spend so much free time on reddit. So much, that I decided what I really needed was a way to spend &lt;em&gt;even more time&lt;/em&gt; browsing it.&lt;/p&gt;

&lt;p&gt;So, naturally, I decided to download some reddit data and try to find some new subs. In doing so, I encountered a common problem with these sorts of recommendation situations. Often, recommendations are for what everybody does, rather than being personalized for me and my situation. Relevancy scoring is a simple way to avoid this problem.&lt;/p&gt;

&lt;h2 id=&quot;code&quot;&gt;Code&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ririw/subreddit-scoring&quot;&gt;You can find the code on github, of course.&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;demo-type-a-subreddit&quot;&gt;Demo: type a subreddit&lt;/h2&gt;
&lt;p&gt;This takes a few seconds to load, it needs to pull in the subreddit data.&lt;/p&gt;

&lt;iframe src=&quot;/assets/demo.html&quot; style=&quot;width:100%; height:512px; border: 0&quot;&gt;&lt;/iframe&gt;

&lt;h2 id=&quot;relevancy-scoring&quot;&gt;Relevancy scoring&lt;/h2&gt;
&lt;p&gt;Let’s start with a really simple approach to the problem of finding new subreddits:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;What other subreddits do people who comment on /r/programming most often comment on?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is disappointing: they comment on the subs that &lt;em&gt;everyone&lt;/em&gt; comments on. The top 5 subreddits most commented on are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AskReddit&lt;/li&gt;
  &lt;li&gt;funny&lt;/li&gt;
  &lt;li&gt;pics&lt;/li&gt;
  &lt;li&gt;worldnews&lt;/li&gt;
  &lt;li&gt;todayilearned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lame.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;scoring-with-probability&quot;&gt;Scoring with probability&lt;/h2&gt;

&lt;p&gt;How about something a bit more refined?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For those who comment on /r/programming, which subreddits do they comment on &lt;em&gt;unusually&lt;/em&gt; often?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is called the relevancy score, and is simply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P(comments on sub|comments on programming) / P(comments on sub)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This doesn’t quite work either. The top 5 are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;learnbasketball&lt;/li&gt;
  &lt;li&gt;Hahahahaha&lt;/li&gt;
  &lt;li&gt;MozillaTech&lt;/li&gt;
  &lt;li&gt;VeganFood&lt;/li&gt;
  &lt;li&gt;Clear_Void&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, I’ve never heard of any of these subs. The problem here is that we’re looking at cases with one or two comments, which means their scores are massive. The commonplace solution would be to add some “smoothing” to the denominator, making less-likely subs appear less often, but it’s not quite right.&lt;/p&gt;

&lt;h2 id=&quot;thinking-about-probability&quot;&gt;Thinking about probability&lt;/h2&gt;
&lt;p&gt;I find that in my work, many, many questions in machine learning or data analysis can be solved by thinking of the problem probabalistically. In this case, we asked for some measure of how more relevant a subreddit is to a set of users, compared to the general baseline.&lt;/p&gt;

&lt;p&gt;The mistake here is that we don’t have accurate probabilities. For these smaller subreddits, we estimate the probability of a visit to them by looking at the number of visits to them, but even small amounts of noise in visitation will change the results massively.&lt;/p&gt;

&lt;p&gt;Instead, we need to ask:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;What is the probaility of observing this level of commenting, by /r/programmer commenters, compared to baseline.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By re-phrasing as a probability, and including the possiblity of randomness in our measurements (comments), we’re able to solve the problem nicely!&lt;/p&gt;

&lt;p&gt;I calculate &lt;em&gt;how many standard deviations from the expected commenting level is the observed commenting level&lt;/em&gt;. In code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;score_sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;given_sums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;means&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        sub_id: the ID of the subreddit of interest
        given_sums: the number of visits to different subs by people who visit /r/programming
        means: the global expected visit rate for different subs
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# prepare a binomial distribution, assuming the &quot;global&quot; case
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;given_sums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;means&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# Find its average and std
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# and calculate how many std-devs away our observed level of visitation is.
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;given_sums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dist&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This works perfectly. The top 5 results are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;linux&lt;/li&gt;
  &lt;li&gt;ProgrammerHumor&lt;/li&gt;
  &lt;li&gt;compsci&lt;/li&gt;
  &lt;li&gt;Python&lt;/li&gt;
  &lt;li&gt;cpp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other examples:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;politics: technology, worldnews, news, science, atheism&lt;/li&gt;
  &lt;li&gt;funny: pics, AdviceAnimals, gifs, todayilearned, WTF&lt;/li&gt;
  &lt;li&gt;books: television, explainlikeimfive, booksuggestions, news, literature&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This sort of relevancy scoring can be great for when you want simple recommendations, based on 
single items. They help work around the problem of popularity, where a recommendation tends to simply be what’s popular with everyone, and zoom in on the things that are particular to a group. At the same time, they aren’t overwhelmed by noise.&lt;/p&gt;

</description>
        <pubDate>Sun, 23 Apr 2017 00:00:00 +0000</pubDate>
        <link>http://blog.richardweiss.org/2017/04/23/relevancy-scoring-reddit.html</link>
        <guid isPermaLink="true">http://blog.richardweiss.org/2017/04/23/relevancy-scoring-reddit.html</guid>
        
        
      </item>
    
  </channel>
</rss>
