<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Christian Aichinger's posts</title><link href="https://www.caichinger.com/" rel="alternate"></link><link href="https://www.caichinger.com/feeds/all.atom.xml" rel="self"></link><id>https://www.caichinger.com/</id><updated>2022-10-24T00:00:00+02:00</updated><subtitle></subtitle><entry><title>Separate development feedback and performance evaluation</title><link href="https://www.caichinger.com/blog/2022/10/24/development_feedback_vs_performance_evaluation" rel="alternate"></link><published>2022-10-24T00:00:00+02:00</published><updated>2022-10-24T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2022-10-24:/blog/2022/10/24/development_feedback_vs_performance_evaluation</id><summary type="html">&lt;p&gt;Development feedback, given when reviewing an reports's recent work, and performance evaluation should be separated. The main goal of development feedback is to have the employee learn, which is all but impossible with the high stress level associated with receiving performance feedback.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Development feedback—intended to help team members learn and improve—needs an environment where the recipient can absorb information.
This requires psychological safety: no blame, no pressure, no personal attacks, no consequences, no reason to get defensive.&lt;/p&gt;
&lt;p&gt;Performance evaluation—informing employees how happy the company is with their output—is a high-stakes and high-stress situation that precludes learning.&lt;/p&gt;
&lt;p&gt;While both are important, they're distinct types of communication:&lt;/p&gt;
&lt;table class='table table-sm table-striped table-bordered table-condensed'&gt;
&lt;tr&gt;
  &lt;th&gt;
  &lt;th&gt;Development Feedback
  &lt;th&gt;Performance Evaluation
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Goal
  &lt;td&gt;Learning and information gain
  &lt;td&gt;Assessment and rating
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Scope
  &lt;td&gt;Feedback on a specific action, close in time to the actual performance
  &lt;td&gt;Feedback on performance over a longer duration
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Frequency
  &lt;td&gt;Daily-weekly: whenever reviewing a report's work output
  &lt;td&gt;Quarterly/yearly or on-demand when performance problems arise
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Level
  &lt;td&gt;Often about details, such as: missing error handling code, a better way to do X, better wording for customer communication
  &lt;td&gt;Higher-level, such as: progress toward yearly goals, or work output over the last weeks
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Stakes
  &lt;td&gt;Low stakes: "&lt;a href="https://www.hanselman.com/blog/you-are-not-your-code"&gt;you are not your code&lt;/a&gt;"
  &lt;td&gt;High stakes: career progress or negative consequences
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Formality
  &lt;td&gt;Informal and usually verbal
  &lt;td&gt;More formal and usually documented in writing
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Consistency
  &lt;td&gt;Individual feedback for each team member
  &lt;td&gt;Fairness requires consistent success criteria for all team members
&lt;/tr&gt;&lt;tr&gt;
  &lt;th&gt;Preparation
  &lt;td&gt;Little or no preparation necessary: a casual discussion
  &lt;td&gt;Fair evaluation requires research and preparation
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;It's easy to slip a performance remark into a feedback discussion, but it's a mistake.
The employee will focus on the performance aspect and not on learning.&lt;/p&gt;
&lt;p&gt;Off-hand, negative performance evaluations are especially damaging.
They encourage a defensive, argumentative mindset which precludes learning.
They also risk escalating into a larger performance discussion you're not prepared for.&lt;/p&gt;
&lt;p&gt;Here are some examples of what to avoid:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="color: red; font-weight: bold;"&gt;BAD:&lt;/span&gt; Your code is missing error handling. You seem distracted recently.&lt;/li&gt;
&lt;li&gt;&lt;span style="color: red; font-weight: bold;"&gt;BAD:&lt;/span&gt; Your presentation was hard to follow. This way you'll never reach your sales quota.&lt;/li&gt;
&lt;li&gt;&lt;span style="color: red; font-weight: bold;"&gt;BAD:&lt;/span&gt; Your customer email sounded rude. I'm fairly unhappy with your work at the moment.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If general performance concerns arise during a feedback session, wrap up as usual, schedule a separate performance talk, and prepare accordingly.&lt;/p&gt;
&lt;p&gt;An example of a better approach:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="color: green; font-weight: bold;"&gt;GOOD:&lt;/span&gt;&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Feedback session&lt;/em&gt;: Here are some points you could improve in your project proposal: &amp;laquo;&lt;em&gt;detailed explanation&lt;/em&gt;&amp;raquo;.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Performance discussion a day later in private&lt;/em&gt;: I had the impression that your recent work is less polished then it used to be. Is everything alright?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Decide up-front which discussion you will have: feedback to help your reports grow or performance evaluation. Avoid mixing the two and pick the right approach and setting for each.&lt;/p&gt;</content><category term="blog"></category><category term="leadership"></category></entry><entry><title>Git Best Practices</title><link href="https://www.caichinger.com/blog/2022/10/04/git_best_practices" rel="alternate"></link><published>2022-10-04T00:00:00+02:00</published><updated>2022-10-04T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2022-10-04:/blog/2022/10/04/git_best_practices</id><summary type="html">&lt;p&gt;Advanced tips and tricks for using git effectively.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Here's a list of Git version control best practices I recommend.&lt;/p&gt;
&lt;h4&gt;Staging changes: &lt;a href="https://git-scm.com/docs/git-add#Documentation/git-add.txt--p"&gt;&lt;code&gt;git add -p&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://git-scm.com/docs/git-diff#_examples"&gt;&lt;code&gt;git diff --cached&lt;/code&gt;&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Use &lt;code&gt;git add -p&lt;/code&gt; to stage changes to commit.
It displays each change individually and asks whether to include this part in the commit or not.
It thus gives control of the contents of the commit.&lt;/p&gt;
&lt;p&gt;Similarly, use &lt;code&gt;git diff --cached&lt;/code&gt; to review the staged changes before committing.&lt;/p&gt;
&lt;p&gt;I'm a big fan of both methods.
They force me to code review my own commits and make sure there is no commented out code and no spurious whitespace or formatting change.
More importantly, this approach finds countless bugs!
Re-reading the diff is a great opportunity to ask questions like "is this really correct," or "does it cover all corner cases."&lt;/p&gt;
&lt;h4&gt;Rewriting history: &lt;a href="https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---amend"&gt;&lt;code&gt;git commit --amend&lt;/code&gt;&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;When discovering a problem with the most recent commit, make your changes, stage them, and use &lt;code&gt;git commit --amend&lt;/code&gt; to fix the problem directly in the previous commit.
This helps to keep a clean history.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A note on changing history in git:&lt;/strong&gt;
Altering commits that were already pushed to others is considered a bad practice, because it breaks work that others might have based on top of the now-obsolete commit.
In most situations, I agree with this assessment.
However, the risk is manageable when you're working with a small team.
Especially for feature branches that are unlikely to be the basis for further work, I tend to advocate rebasing / amending pushed commits to keep a clean history.
Just make sure that it's OK with the team.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;Rewriting history: &lt;a href="https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt--i"&gt;&lt;code&gt;git rebase -i HEAD~10&lt;/code&gt;&lt;/a&gt;&lt;/h4&gt;
&lt;p&gt;Interactive rebasing allows reordering or fixing past commits and generally reworking recent commit history.
See the &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History"&gt;Git documentation on rewriting history&lt;/a&gt; for in-depth explanation.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;HEAD~10&lt;/code&gt; part tells git to &lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_ancestry_references"&gt;look at the 10 previous commits&lt;/a&gt;, which often is a sensible default.&lt;/p&gt;
&lt;p&gt;Also note the "changing history" section above.&lt;/p&gt;
&lt;h4&gt;Atomic commits&lt;/h4&gt;
&lt;p&gt;Commits should contain one logical change.
Avoid having multiple, independent changes in a single commit.
Why?
It's easier to review and understand multiple smaller commits than one big commit.
It's easier to merge commits together than to split one big commit.
If &lt;a href="https://git-scm.com/docs/git-bisect"&gt;bisecting&lt;/a&gt; found that a bug originated from a commit, it's easier to identify and fix the issue in a small commit than in a big commit.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;git add -p&lt;/code&gt; is great for creating atomic commits from a source tree that already has several changes.&lt;/p&gt;
&lt;h4&gt;Commit messages&lt;/h4&gt;
&lt;p&gt;Three key points:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Form: Write a proper subject line, separated by a blank line from the rest of your commit message.&lt;/li&gt;
&lt;li&gt;References: Include a link/ID of the relevant task in your ticket system.&lt;/li&gt;
&lt;li&gt;Content: Write for your future self.
  One year down the line questions like "why did we want this / why did we choose this particular approach / what alternatives did we consider" are going to come up.
  Between your commit message and your ticket system task description, you should be able to answer those questions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Git branching patterns&lt;/h4&gt;
&lt;p&gt;The branch structure of a git repository should adapt to the development activity:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If several people work on the same repository at the same time, you'll need feature branches.&lt;/li&gt;
&lt;li&gt;If you have to support several releases in parallel, you'll need release branches.&lt;/li&gt;
&lt;li&gt;If you need frequent updates of your releases, you might need hotfix branches as well.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The large number of branches created with these schemes is unavoidable in heavily used repositories.
However, the added complexity makes the commit history hard to understand.&lt;/p&gt;
&lt;p&gt;For less active repositories, such as small tools or legacy software, simpler approaches are better suited.
Often a single &lt;code&gt;main&lt;/code&gt; or &lt;code&gt;master&lt;/code&gt; branch together with occasional feature branches is enough.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you have other Git best practices to include, please comment below.&lt;/p&gt;</content><category term="blog"></category><category term="git"></category></entry><entry><title>Covid variant competition and a longer-term strategy</title><link href="https://www.caichinger.com/blog/2022/01/16/covid-variant-competiton" rel="alternate"></link><published>2022-01-16T00:00:00+01:00</published><updated>2022-01-16T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2022-01-16:/blog/2022/01/16/covid-variant-competiton</id><summary type="html">&lt;p&gt;Older Covid variants die out due to the measures needed to contain newer, more infectious strains. As the virus adapts better to humans, we need to find a way to live with it.&lt;/p&gt;</summary><content type="html">&lt;p&gt;How do new Covid strains replace older ones? A &lt;a href="https://gist.github.com/ChrisAichinger/b92e3f7388bedf15acb8cf696934eb7b"&gt;simple model&lt;/a&gt; provides some answers:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Infection numbers timeline: one strain out-competes another" src="https://www.caichinger.com/files/covid-variant-model.png" title=""&gt;&lt;/p&gt;
&lt;p&gt;The model consists of three rules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At the beginning only the base variant exists, until on day 200 the Omicron variant appears.&lt;/li&gt;
&lt;li&gt;Each base variant infection causes 1.25 follow-up infections. Each Omicron infection causes 1.40 follow-up infections.&lt;/li&gt;
&lt;li&gt;A lockdown starts when the number of sick people reaches 20000. This results in a reduction of new infections by 36%. The lockdown ends when the number of sick people falls below 6000 again.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This might be the world's worst Covid model, but it gets one thing right: the competition between variants. It shows the dynamic:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Normal life: numbers rise, Omicron grows faster, total numbers mandate a lockdown; Lockdown: numbers fall, base variant shrinks faster, the lockdown ends" src="https://www.caichinger.com/files/covid-variant-dynamic.png" title=""&gt;&lt;/p&gt;
&lt;p&gt;Earlier variants vanish due to the stronger human interventions required to keep the newer variants in check.
We decide on preventive measures based on total numbers, not per-variant details.
More infectious strains require longer, more frequent, or more stringent lockdowns.
These measures hit older variants harder, which makes them recede.&lt;sup&gt;1&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;At some point we might not be able to enact strict enough countermeasures to prevent newly evolved strains from spreading.
Arguably, this already applies to Omicron.
If not, new variants will continue to emerge.
Our lukewarm vaccination efforts exacerbate this issue by applying &lt;a href="https://en.wikipedia.org/wiki/Evolutionary_pressure"&gt;evolutionary pressure&lt;/a&gt; on the virus.
Vaccination rates are high enough to stop earlier strains, but low enough to allow the spread of more infectious, more vaccination-resistant variants.
Thus, we should expect to see more problematic strains in the future.&lt;/p&gt;
&lt;p&gt;Where does this leave us?
Covid is on track to become endemic, so everyone will get infected eventually.
The big question is how to manage this process without overwhelmed hospitals, and ideally without endless lockdowns.
Vaccination is a key ingredient, since it &lt;a href="https://www.cdc.gov/mmwr/volumes/70/wr/mm7037e1.htm"&gt;reduces hospitalization and death rates tenfold&lt;/a&gt;.
A 100%-vaccinated society could support ten times more infections at the same time.&lt;sup&gt;2&lt;/sup&gt;
During Corona peaks, masks, social distancing, and lockdowns help to remove pressure from hospitals.
Outside of peak seasons, a relaxed approach is probably best.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; Does this argument still hold despite the lower hospitalization rates of Omicron patients? It does: avoiding overwhelmed hospitals still requires strict Omicron countermeasures, just at a higher number of infected people.&lt;br&gt;
&lt;sup&gt;2&lt;/sup&gt; Compared to an unvaccinated society. Countries with a 65&amp;ndash;75% vaccination rate can afford 3&amp;ndash;4 times the infections as before vaccination campaigns. The comparison of &lt;a href="https://ourworldindata.org/explorers/coronavirus-data-explorer?facet=none&amp;amp;Metric=Confirmed+cases&amp;amp;Interval=Biweekly&amp;amp;Relative+to+Population=true&amp;amp;Color+by+test+positivity=false&amp;amp;country=USA~ITA~DEU~GBR~FRA~JPN~AUT"&gt;Covid numbers of winter 2020/2021 and winter 2021/2022&lt;/a&gt; supports this.&lt;/p&gt;</content><category term="blog"></category><category term="health"></category></entry><entry><title>How to create pixel-perfect PDF reports in 2021</title><link href="https://www.caichinger.com/blog/2021/09/07/pdf-reports" rel="alternate"></link><published>2021-09-07T00:00:00+02:00</published><updated>2021-09-07T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2021-09-07:/blog/2021/09/07/pdf-reports</id><summary type="html">&lt;p&gt;Generate PDF reports and invoices from HTML, with support for modern CSS features and pixel-perfect layouts.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Web development projects often require printable reports or invoices, but relying on the browser's built-in print function is a frustrating experience.
Results vary with operating systems and render engines, leading to frequent bugs and broken layouts.
Worse, fixing them risks degrading other platforms.&lt;/p&gt;
&lt;p&gt;PDF reports avoid these pitfalls by rendering in a controlled server environment.
However, most &lt;a href="https://stackoverflow.com/questions/176476/how-can-i-automate-html-to-pdf-conversions"&gt;tools to convert HTML to PDF&lt;/a&gt; have serious limitations:
&lt;a href="https://wkhtmltopdf.org/"&gt;wkhtmltopdf&lt;/a&gt;, &lt;a href="https://weasyprint.org/"&gt;WeasyPrint&lt;/a&gt;, and &lt;a href="https://phantomjs.org/"&gt;PhantomJS&lt;/a&gt; lack support for modern CSS features and often misrender pages, whereas
&lt;a href="https://stackoverflow.com/a/45671273"&gt;Google Chrome PDF conversion&lt;/a&gt; adds ugly headers and footers.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/puppeteer/puppeteer/"&gt;Puppetteer&lt;/a&gt;, the successor to PhantomJS, solves these issues by bundling its own Chromium browser.
It supports pixel-perfect PDF generation and respects CSS page margins, allowing full control over the PDF page layout.&lt;/p&gt;
&lt;p&gt;My &lt;a href="https://gist.github.com/ChrisAichinger/151050a7c16070cdbd84e2697f7332d1"&gt;htmlpdf.js&lt;/a&gt; command-line tool automates HTML to PDF conversion:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;npm install puppetteer&lt;/span&gt;
&lt;span class="go"&gt;./htmlpdf.js input.html output.pdf&lt;/span&gt;
&lt;span class="go"&gt;./htmlpdf.js https://google.com output.pdf&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Give it a try and let me know what you think.&lt;/p&gt;</content><category term="blog"></category><category term="web"></category></entry><entry><title>Capture Bluetooth Traffic from Android</title><link href="https://www.caichinger.com/blog/2020/11/08/capture_bluetooth_traffic_from_android" rel="alternate"></link><published>2020-11-08T00:00:00+01:00</published><updated>2020-11-08T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2020-11-08:/blog/2020/11/08/capture_bluetooth_traffic_from_android</id><summary type="html">&lt;p&gt;Bluetooth devices are often controlled by opaque mobile applications. Here are two ways to capture Bluetooth traffic on Android, which enable us to create custom tools to take back our freedom.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Bluetooth devices have become ubiquitous, whether it's headphones, heart rate monitors, smart watches, or light bulbs. While many of them come with mobile apps for control and configuration, sometimes it's convenient to access them from a computer instead. If this is not supported out of the box, recording the Bluetooth traffic is useful in order to create a custom tool.&lt;/p&gt;
&lt;p&gt;Helpfully, newer versions of &lt;a href="https://www.wireshark.org/"&gt;Wireshark&lt;/a&gt; include an &lt;a href="https://www.wireshark.org/docs/man-pages/androiddump.html"&gt;androiddump&lt;/a&gt; utility to capture Bluetooth traffic directly from Android phones. The setup couldn't be simpler: connect the phone to the computer via USB, enable USB debugging, select the "Android Bluetooth" capture source in Wireshark, and start recording!&lt;/p&gt;
&lt;p&gt;Android also has a function to log all traffic into a file, which can later be copied to a computer for analysis in Wireshark. This is similar to creating a &lt;a href="https://www.tcpdump.org/"&gt;tcpdump&lt;/a&gt; of TCP/IP traffic, just for Bluetooth. Unfortunately, the location of this log file changes between Android versions, and access seems to require &lt;a href="https://en.wikipedia.org/wiki/Rooting_(Android)"&gt;root&lt;/a&gt; in recent versions. One reliable method I found was:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enable "Bluetooth HCI snoop log" in the Android &lt;a href="https://stackoverflow.com/a/19888449/25097"&gt;developer options&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;Connect to the phone via USB debugging with &lt;code&gt;adb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;adb root&lt;/code&gt; to gain root-level access to the phone&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;adb bugreport bugreport.out&lt;/code&gt; to create a bug report ZIP file. Either this contains the Bluetooth log file directly (search for a &lt;code&gt;btsnoop_hci.log&lt;/code&gt;), or the full path of the log file (grep the bug report for &lt;code&gt;btsnoop_hci.log&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;If the log was not contained in the bug report, get it via &lt;code&gt;adb pull &amp;lt;full_path_to_logfile&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;</content><category term="blog"></category><category term="android"></category></entry><entry><title>Force enable dark mode in Chrome, Safari, Firefox</title><link href="https://www.caichinger.com/blog/2020/05/16/force_enable_dark_mode" rel="alternate"></link><published>2020-05-16T00:00:00+02:00</published><updated>2020-05-16T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2020-05-16:/blog/2020/05/16/force_enable_dark_mode</id><summary type="html">&lt;p&gt;Force the CSS media selector prefers-color-scheme to "dark" on Chrome, Safari, and Firefox. This enables the dark mode on all websites that support it.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The last years have seen a rapid rise in the number of web sites that support a
"dark mode". Some pages offer an explicit light/dark switch. But typically the
selection is based on the browsers "prefers-color-scheme" CSS selector. It is
surprisingly difficult to change this browser default without switching the whole
operating system.&lt;/p&gt;
&lt;p&gt;Follow the instructions below to switch to dark mode.&lt;/p&gt;
&lt;style&gt;
.cs-nopref { display: inline; }
.cs-light {
  display: none;
  color: #555;
  background-color: #fff;
  border: 1px solid #555;
}
.cs-dark {
  display: none;
  color: #ddd;
  background-color: #444;
  border: 1px solid black;
}

@media (prefers-color-scheme: dark) {
  .cs-dark { display: inline; }
  .cs-light, .cs-nopref { display: none; }
}
@media (prefers-color-scheme: light) {
  .cs-light { display: inline; }
  .cs-nopref, .cs-dark { display: none; }
}
&lt;/style&gt;

&lt;div class="well well-fitcontent"&gt;The color scheme preference of your browser is &lt;span class="cs-nopref badge"&gt;not set&lt;/span&gt;&lt;span class="cs-light badge"&gt;light&lt;/span&gt;&lt;span class="cs-dark badge"&gt;dark&lt;/span&gt;.&lt;/div&gt;

&lt;h2&gt;Chrome&lt;/h2&gt;
&lt;p&gt;For Chrome, the instructions depend on the system it is running on.&lt;/p&gt;
&lt;h3&gt;On Android&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In Chrome, open the top-right "..." menu and go to "Settings"&lt;/li&gt;
&lt;li&gt;Open "Themes"&lt;/li&gt;
&lt;li&gt;Select "Dark"&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;On iOS&lt;/h3&gt;
&lt;p&gt;There is no direct way to enable dark mode only for Chrome on iOS. You have
to change the whole device to iOS via "Settings" &amp;rarr; "Display &amp;amp;
Brightness".&lt;/p&gt;
&lt;h3&gt;On Windows&lt;/h3&gt;
&lt;p&gt;Chrome switches into dark mode when it is started with the &lt;code&gt;--force-dark-mode&lt;/code&gt; command line flag.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Close all Chrome instances&lt;/li&gt;
&lt;li&gt;Shift-Right click the Chrome shortcut in the taskbar or on the desktop&lt;/li&gt;
&lt;li&gt;Select "Properties"&lt;br&gt;
  &lt;img src="https://www.caichinger.com/files/dark-mode-chrome1.png" alt="Chrome instructions: open shortcut properties" class="well well-sm" style="margin-bottom: 0px;"&gt;&lt;/li&gt;
&lt;li&gt;In the "Shortcut" tab, append &lt;code&gt;--force-dark-mode&lt;/code&gt; to the "Target" field&lt;/li&gt;
&lt;li&gt;Close the dialog with "OK"&lt;br&gt;
  &lt;img src="https://www.caichinger.com/files/dark-mode-chrome2.png" alt="Chrome instructions: add the --force-dark-mode flag" class="well well-sm" style="margin-bottom: 0px;"&gt;&lt;/li&gt;
&lt;li&gt;Restart Chrome with that shortcut&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Safari&lt;/h2&gt;
&lt;p&gt;Safari doesn't have a separate setting for dark mode. It always follows the operating system setting.&lt;/p&gt;
&lt;h3&gt;Changing the system setting on iOS&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Open Settings&lt;/li&gt;
&lt;li&gt;Open "Display &amp;amp; Brightness"&lt;/li&gt;
&lt;li&gt;Select "Dark"&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Changing the system setting on MacOS&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Open the system settings in the Apple menu&lt;/li&gt;
&lt;li&gt;Open the "General" dialog&lt;/li&gt;
&lt;li&gt;Select the "Dark" appearance&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Firefox&lt;/h2&gt;
&lt;p&gt;Firefox has hidden configuration option that enables dark mode:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Type &lt;code&gt;about:config&lt;/code&gt; into the address bar and press Enter&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;ui.systemUsesDarkTheme&lt;/code&gt; into the search bar&lt;/li&gt;
&lt;li&gt;The search will not find anything, but allow you to add a new preference with that name&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set the property type to "Number" and click the "+" button to create:&lt;br&gt;
  &lt;img src="https://www.caichinger.com/files/dark-mode-firefox1.png" alt="Firefox instructions: creating the property" class="well well-sm" style="margin-bottom: 0px;"&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enter the value "1" to enable dark mode and click the check mark to save:&lt;br&gt;
  &lt;img src="https://www.caichinger.com/files/dark-mode-firefox2.png" alt="Firefox instructions: setting the property to 'dark mode'" class="well well-sm" style="margin-bottom: 0px;"&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</content><category term="blog"></category><category term="web"></category></entry><entry><title>LDAP basics for users</title><link href="https://www.caichinger.com/blog/2019/02/06/ldap_basics" rel="alternate"></link><published>2019-02-06T00:00:00+01:00</published><updated>2019-02-06T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2019-02-06:/blog/2019/02/06/ldap_basics</id><summary type="html">&lt;p&gt;An introduction to LDAP concepts. Reach a rough understanding of what LDAP is good for, and get comfortable with the terminology surrounding it.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Administrating an &lt;a href="https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol"&gt;LDAP&lt;/a&gt; server may be hard — using it doesn't have to be.&lt;/p&gt;
&lt;p&gt;LDAP servers commonly provide auth services to web servers, mail servers, web apps, and so on.
To do this, the LDAP database stores user and group membership information. The combination of these two datasets
allows both authentication (is the user who they claim to be?) and
authorization (is the user in a group that has permission to perform a specific action?).
Thus, LDAP enables central management of user, group, and permission information for any number of services.&lt;/p&gt;
&lt;h3&gt;Concepts&lt;/h3&gt;
&lt;p&gt;So what does an LDAP database consist of?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An LDAP database contains a hierarchical data structure, similar to a directory tree.&lt;/li&gt;
&lt;li&gt;Each tree node is called an &lt;strong&gt;entity&lt;/strong&gt;.&lt;ul&gt;
&lt;li&gt;LDAP doesn't distinguish between files and directories.
  Entities often contain both child entities — like a directory — as well as attributes, which are similar to a file's content.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Each entity has a &lt;strong&gt;distinguished name (DN)&lt;/strong&gt;, which is the entity's absolute
  pathname in LDAP tree. The elements of the pathname are called
  &lt;strong&gt;relative distinguished names (RDNs)&lt;/strong&gt;.&lt;br&gt;
  These concepts are pretty similar to filesystem directory trees. The key differences are:&lt;ul&gt;
&lt;li&gt;Directory separators: LDAP uses &lt;code&gt;,&lt;/code&gt; instead of &lt;code&gt;/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;RDN format: RDNs are typically key-value-pairs, instead of simple
  strings: &lt;code&gt;uid=ca&lt;/code&gt; instead of &lt;code&gt;Desktop&lt;/code&gt;. Commonly used keys
  are &lt;a href="https://stackoverflow.com/questions/18756688/what-are-cn-ou-dc-in-an-ldap-search"&gt;&lt;code&gt;dc&lt;/code&gt;, &lt;code&gt;o&lt;/code&gt;, &lt;code&gt;ou&lt;/code&gt;, &lt;code&gt;uid&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Parent nodes are on the right: so it's &lt;code&gt;dc=child,dc=parent&lt;/code&gt; instead of
  &lt;code&gt;/parent/child&lt;/code&gt;&lt;br&gt;
  Consequently, DNs usually look like this: &lt;code&gt;uid=ca,ou=people,dc=caichinger,dc=com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Entities have &lt;strong&gt;attributes&lt;/strong&gt;, which store the entity's data, similar to a file's contents. Each attribute has a &lt;strong&gt;type&lt;/strong&gt; that
  describes the attribute's data structure, as well as one or more &lt;strong&gt;values&lt;/strong&gt;
  containing the attribute's information. Additionally, each attribute can have
  &lt;strong&gt;options&lt;/strong&gt; — a rarely used feature for distinguishing different versions (e.g. English, German) of the same attribute.&lt;/li&gt;
&lt;li&gt;Entities also have associated &lt;strong&gt;object classes&lt;/strong&gt;, which are conceptually
  similar to attribute types. But whereas types describe attributes, object
  classes describe which attributes must be found within the entity.&lt;/li&gt;
&lt;li&gt;Both attribute types and entity object classes are metadata: they describe                                                                                                                                                                                                     the database's schema. Each of these metadata objects has an &lt;strong&gt;OID&lt;/strong&gt;.
  Aside from the schema definition, OIDs are also used for other
  database-specific metadata, such as identifying extended requests and responses.
  OIDs are denoted as dot-separated numbers, e.g. &lt;code&gt;1.2.840.1234567890&lt;/code&gt;, but often
  have human-readable names assigned as well.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What actions can be performed over the LDAP protocol?&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Binding&lt;/strong&gt;: &lt;a href="https://superuser.com/questions/592650/what-does-binding-to-a-ldap-server-mean"&gt;authenticating to the LDAP server&lt;/a&gt; — essentially "logging in". Since most servers don't allow un-authenticated querying, this is required before performing any other actions.
Many servers also support re-authentication as a different user over existing
connections: this is known as &lt;strong&gt;re-binding&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Searching&lt;/strong&gt;: querying the existing LDAP directory tree, and listing its information.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Add&lt;/strong&gt;, &lt;strong&gt;modify&lt;/strong&gt;, and &lt;strong&gt;delete&lt;/strong&gt;: altering the LDAP directory tree.&lt;/li&gt;
&lt;li&gt;Many others, often including custom commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Querying&lt;/h3&gt;
&lt;p&gt;Querying an LDAP server is straight-forward with the &lt;code&gt;ldapsearch&lt;/code&gt; tool:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;ldapsearch
  -h ldap.caichinger.com                       # LDAP server name
  -D &amp;quot;uid=ca,ou=people,dc=caichinger,dc=com&amp;quot;   # Authenticate as uid=caichinger
  -W                                           # Ask for password for uid=caichinger
  -b &amp;quot;ou=people,dc=caichinger,dc=com&amp;quot;          # Base search path
  (uid=caichinger)                             # Search expression
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;-D&lt;/code&gt; and &lt;code&gt;-W&lt;/code&gt; switches tell &lt;code&gt;ldapsearch&lt;/code&gt; as which user to authenticate as.
The &lt;code&gt;-b&lt;/code&gt; switch defines the "base directory" where the search should start. The
search expression is then applied to all entities under this directory tree.&lt;/p&gt;
&lt;p&gt;The server response will then contain all matching users, as well as their
associated attributes.&lt;/p&gt;
&lt;p&gt;If you have any questions after this whirlwind-tour of LDAP, please leave a comment!&lt;/p&gt;</content><category term="blog"></category><category term="databases"></category><category term="security"></category></entry><entry><title>Converting Unix timestamps</title><link href="https://www.caichinger.com/blog/2018/10/15/vim_strftime" rel="alternate"></link><published>2018-10-15T00:00:00+02:00</published><updated>2018-10-15T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-10-15:/blog/2018/10/15/vim_strftime</id><summary type="html">&lt;p&gt;Efficient conversion to and from Unix timestamps in vim. This is often helpful when editing text files containing Unix timestamps (e.g. &lt;code&gt;.bash_history&lt;/code&gt;).&lt;/p&gt;</summary><content type="html">&lt;p&gt;Sometimes, we need to convert &lt;a href="https://en.wikipedia.org/wiki/Unix_time"&gt;Unix timestamps&lt;/a&gt; (seconds since
January 1st, 1970) to human-readable dates. For example, we might transform
1539561600 to 2018-10-15 00:00 UTC.&lt;/p&gt;
&lt;p&gt;There are multiple online services that do this, I like &lt;a href="https://www.unixtimestamp.com"&gt;unixtimestamp.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Every now and then we need to batch-convert timestamps. The &lt;code&gt;date&lt;/code&gt; command
shipped on Linux distributions does this nicely:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;date &amp;quot;+%c&amp;quot; --date=@1539561600&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I recently ran into a similar problem when logfiles  contained Unix
timestamps instead of human-readable dates. Using &lt;code&gt;date&lt;/code&gt; seemed a bit clumsy
here. Fortunately, &lt;a href="https://superuser.com/a/590196/305200"&gt;Superuser.com had a nice solution&lt;/a&gt; involving
&lt;a href="https://www.vim.org/"&gt;Vim&lt;/a&gt;. The following sequence converts the timestamp under the cursor and
records a macro &lt;code&gt;q&lt;/code&gt; to facilitate future conversions:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;qq                             &amp;quot; start recording
&amp;quot;mciw                          &amp;quot; put time in register m and replace it…
&amp;lt;C-r&amp;gt;=strftime(&amp;quot;%c&amp;quot;, @m)&amp;lt;CR&amp;gt;   &amp;quot; …with localized datetime
&amp;lt;Esc&amp;gt;                          &amp;quot; exit insert mode
q                              &amp;quot; stop recording
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Quick and convenient &amp;mdash; and easily incorporated into a macro to convert timestamps across the entire file.&lt;/p&gt;</content><category term="blog"></category><category term="vim"></category><category term="linux"></category></entry><entry><title>The Kelly Criterion: Comparison with Expected Values</title><link href="https://www.caichinger.com/blog/2018/04/19/kelly_criterion4" rel="alternate"></link><published>2018-04-19T00:00:00+02:00</published><updated>2018-04-19T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-04-19:/blog/2018/04/19/kelly_criterion4</id><summary type="html">&lt;p&gt;Why do you need the Kelly criterion at all? Why not invest as much as you can in the opportunity with the highest expected return?&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;This is the final post in a four-part series exploring the Kelly criterion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;The Kelly Criterion: Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;The Kelly Criterion: Multiple Investment Opportunities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;The Kelly Criterion: Where does the logarithm come from?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The Kelly Criterion: Comparison with Expected Values (this post)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;In a &lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;previous post&lt;/a&gt;, we looked at the Kelly formula, which maximizes
earnings in a series of gambles where winnings are constantly re-invested. Is
this equivalent to maximizing the &lt;a href="https://en.wikipedia.org/wiki/Expected_value"&gt;expected return&lt;/a&gt; in each
game? It turns out that the answer is "no". In this post we'll look into the
reasons for this and discover the pitfalls of expected values.&lt;/p&gt;
&lt;p&gt;We will look at the same game as in the previous post:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msup&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\frac{V_1}{V_0} = (1+lr_W)^W(1+lr_L)^{(1-W)}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8913309999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.188em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.938em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;with the variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_0, V_1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8777699999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: the available money before and after the first round&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: fraction of available money to bet in each round (the variable to optimize)&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_W, r_L&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: return on win and loss, 0.4 and -1 in our example (i.e. 40% of
  wager awarded on win, otherwise 100% of wager lost)&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;: Random variable describing our chances to win; valued &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p=0.8&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.2&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p=0.2&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Kelly formula obtained from maximizing &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\log V_1/V_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; tells us to invest
30% of our capital in such a gamble. Let's see what the result is if we
maximize the expected value &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;E[V_1/V_0]&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; instead.&lt;/p&gt;
&lt;p&gt;This is trivial by hand, but we'll use &lt;a href="http://www.sympy.org/en/index.html"&gt;SymPy&lt;/a&gt;, because we can:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sympy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;sp&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sympy.stats&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;ss&lt;/span&gt;
&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;init_printing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;symbols&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;             &lt;span class="c1"&gt;# Define the symbol/variable l&lt;/span&gt;
&lt;span class="n"&gt;W&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bernoulli&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;W&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# Random variable, 1 with p=0.8, else 0&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;                      &lt;span class="c1"&gt;# Define f1 = V_1/V_0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;W&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;W&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;                     &lt;span class="c1"&gt;# Calculate the expected value&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Evaluating this gives:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;0.12&lt;/mn&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;E[\frac{V_1}{V_0}] = 1 + 0.12l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Uh-hum... so the expected return has no maximum, but grows linearly with
increasing &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Essentially, this approach advises that you should bet all
your money, and more if you can borrow it for negligible interest rates.&lt;/p&gt;
&lt;p&gt;Could the problem be that we only look at a single round? Let's examine the
expected return after playing 10 rounds:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# Note: We can not do f10 = f1(W)**10, since we need independent samples&lt;/span&gt;
&lt;span class="n"&gt;W_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bernoulli&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;W_&lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="n"&gt;f10&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;f1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Wi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Wi&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;W_list&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;E[\frac{V_{10}}{V_0}] = 6 \cdot 10^{-10} l^{10} + 5 \cdot 10^{-8} l^9 + ...&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.947438em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.864108em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;5&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.947438em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.864108em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.10556em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;All the coefficients of the polynomial are positive, there are no maxima for
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;&amp;gt;&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l &amp;gt;= 0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.73354em;vertical-align:-0.0391em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.36687em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. What's going on?&lt;/p&gt;
&lt;p&gt;Time to dig deeper. Let's say we bet all of our money each round. If we lose
just once, all of our money is gone. After 10 rounds playing this strategy, the
probability of total loss is:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mtext&gt;one loss in 10 games&lt;/mtext&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.89&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p({\text{one loss in 10 games}}) = 1 - 0.8^{10} = 0.89&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;one loss in 10 games&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8641079999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="mord"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;So 89% of the time we would lose all our money. However, the expected return
after 10 rounds at &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l=1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;msub&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;3.1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;E[\frac{V_{10}}{V_0}]_{l=1} = 3.1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.33610799999999996em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;So on average we'd have $3.10 after 10 rounds for every Dollar initially bet,
but 90% of the time we'd lose everything. Strange.&lt;/p&gt;
&lt;h2&gt;The big reveal&lt;/h2&gt;
&lt;p&gt;Things become clearer when we look at in more detail at the calculation of
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;E[V_{10}/V_0]&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable rowspacing="0.24999999999999992em" columnalign="right left right left right left right left right left right left" columnspacing="0em 1em 0em 1em 0em 1em 0em 1em 0em 1em 0em"&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo stretchy="false"&gt;[&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;msub&gt;&lt;mo stretchy="false"&gt;]&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mo lspace="0em" rspace="0em"&gt;=&lt;/mo&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msup&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msup&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;mi mathvariant="normal"&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/msup&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;mn&gt;0.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;\begin{aligned}
  E[\frac{V_{10}}{V_0}]_{l=1} &amp;amp;&amp;amp; = &amp;amp;&amp;amp;&amp;amp; 1.4^0    0^{10} &amp;amp;&amp;amp; \times &amp;amp;&amp;amp; 0.8^0 0.2^{10} &amp;amp;&amp;amp; + \\
                              &amp;amp;&amp;amp;   &amp;amp;&amp;amp;&amp;amp; 1.4^1    0^9    &amp;amp;&amp;amp; \times &amp;amp;&amp;amp; 0.8^1 0.2^{9}  &amp;amp;&amp;amp; + \\
                              &amp;amp;&amp;amp;   &amp;amp;&amp;amp;&amp;amp; 1.4^2    0^8    &amp;amp;&amp;amp; \times &amp;amp;&amp;amp; 0.8^2 0.2^{8}  &amp;amp;&amp;amp; + \\
                              &amp;amp;&amp;amp;   &amp;amp;&amp;amp;&amp;amp; ...             &amp;amp;&amp;amp;        &amp;amp;&amp;amp;                     \\
                              &amp;amp;&amp;amp;   &amp;amp;&amp;amp;&amp;amp; 1.4^9    0^1    &amp;amp;&amp;amp; \times &amp;amp;&amp;amp; 0.8^9 0.2^{1}  &amp;amp;&amp;amp; + \\
                              &amp;amp;&amp;amp;   &amp;amp;&amp;amp;&amp;amp; 1.4^{10} 0^0    &amp;amp;&amp;amp; \times &amp;amp;&amp;amp; 0.8^{10} 0.2^{0}    \\
\end{aligned}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:10.092762em;vertical-align:-4.796381em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;]&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.33610799999999996em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mrel mtight"&gt;=&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep" style="width:1em;"&gt;&lt;/span&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep" style="width:1em;"&gt;&lt;/span&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep" style="width:1em;"&gt;&lt;/span&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;×&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;×&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;×&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;×&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;×&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep" style="width:1em;"&gt;&lt;/span&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.272165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:0.7760509999999998em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8641079999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.796381em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="arraycolsep" style="width:1em;"&gt;&lt;/span&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.272273em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:5.296381em;"&gt;&lt;span style="top:-7.296381em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-5.296273em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.772165em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.7480570000000002em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mord"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.272273em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;The expected value is the sum of probability-weighted outcomes(&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1.4&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1.4&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are the per-round outcomes for win and loss). Since a single loss results
in loss of &lt;em&gt;all&lt;/em&gt; money, the only non-zero term in the sum is the starred one,
that occurs with about 11% probability, at a value gain of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1.&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;28&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1.4^{10} = 28&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8141079999999999em;"&gt;&lt;span style="top:-3.063em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.
This high gain is enough to drag the expected return up to 3.1. When more then
10 rounds are played, these numbers become more extreme: the winning
probability plummets, but the winning payoff skyrockets, dragging the expected
return further upwards.&lt;/p&gt;
&lt;p&gt;This is a bit reminiscent of the &lt;a href="https://en.wikipedia.org/wiki/St._Petersburg_paradox"&gt;St. Petersburg paradox&lt;/a&gt;, in that
arbitrarily small probabilities can drag the expected return to completely
different (read "unrealistic") values.&lt;/p&gt;
&lt;h2&gt;Different kinds of playing&lt;/h2&gt;
&lt;p&gt;The Kelly approach builds on the assumption that you play with all your
available wealth as base capital, and tells you what fraction of that amount to
invest. It requires reinvestment of your winnings. Obviously, investing
everything in one game (&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l=1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) is insane, since a single loss would brankrupt
you. However, following Kelly's strategy is the fastest way to grow total
wealth.&lt;/p&gt;
&lt;p&gt;The expected-value approach of "invest everything you have" is applicable in a
different kind of situation. Let's say you can play only one game per day, have
a fixed gambling budget each day, and thus are barred from reinvesting your
wins. If you invest your full daily gambling budet, you may win or lose, but
over the long run you will average a daily return of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1.4&lt;/mn&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1.12&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1.4 \cdot 0.8 = 1.12&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
for every dollar invested. The more you can invest per day, the higher your
wins, thus the pressure towards large &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; values.&lt;/p&gt;
&lt;p&gt;In a way, Kelly optimizes for the highest probability of large returns when
re-investing winnings, while the expected value strategy optimizes for large
returns, even if the probability is very low.&lt;/p&gt;
&lt;h2&gt;A dubious game&lt;/h2&gt;
&lt;p&gt;Should you play a game where the winning probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;10^{-6}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8141079999999999em;"&gt;&lt;span style="top:-3.063em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;6&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, but
the winning return &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msup&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;2\cdot 10^6&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8141079999999999em;"&gt;&lt;span style="top:-3.063em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;6&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;? Mathematically it seems like a
solid bet with a 100% return on investment in the long run. The question is
whether you can reach "the long run". Can you afford to play the game a million
times? If not, you'll most likely lose money. If you can afford to play a few
million times, it becomes a nice investment indeed.&lt;/p&gt;
&lt;p&gt;Kelly would tell you to invest only a very small fraction of your total wealth
into such a game. The expected-value formalism advises to invest as much as
possible, which for most people is bad advice even when playing with a fixed
daily budget and no reinvestment (i.e. the expected-value play style).&lt;/p&gt;
&lt;p&gt;This is an interesting example for two reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It demonstrates one of the ways how "rich become richer" - the game has high
  returns, but also a significant barrier to entry.&lt;/li&gt;
&lt;li&gt;It demonstrates a downside of both the Kelly- and the expected-value
  approach. The two strategies are optimal in their use cases in the limit of
  infinitely many games, however for finitely many games they may give bad
  advice, especially regarding to very low-probability winning scenarios.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So, a brief discussion of the relationship between the Kelly strategy
and the expected return. For me, it was striking how two seemingly
similar approaches ("maximize the moneys") lead to so different results and how
unintuitively the expectation value can be in the face of outliers.&lt;/p&gt;
&lt;p&gt;If you're interested in interactive plots that really helped me understand this
material, you can find them in &lt;a href="https://www.caichinger.com/files/Kelly.ipynb"&gt;this Jupyter Notebook&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;The Kelly criterion&lt;/h2&gt;
&lt;p&gt;Over the course of this &lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;blog post series&lt;/a&gt;, we looked at the
classical Kelly criterion in the &lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;first post&lt;/a&gt;, and how it can be extended to situations such as
stock buying, with multiple parallel investment opportunities, in the &lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;second
post&lt;/a&gt;. Next, we investigated the origin of the logarithm in the
Kelly formula in the &lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;third post&lt;/a&gt;, before finishing
up with the current discussion about expected values.&lt;/p&gt;
&lt;p&gt;Surely, there's more to say about the Kelly criterion. If you want to leave
your thoughts, please do so in the comments below!&lt;/p&gt;
&lt;link crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" rel="stylesheet"&gt;</content><category term="blog"></category><category term="math"></category><category term="investment"></category></entry><entry><title>The Kelly Criterion: Where does the logarithm come from?</title><link href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3" rel="alternate"></link><published>2018-04-18T00:00:00+02:00</published><updated>2018-04-18T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-04-18:/blog/2018/04/18/kelly_criterion3</id><summary type="html">&lt;p&gt;The Kelly criterion optimizes a logarithmic equation. We look at the origin of this logarithm, which lies in the nature of compound investment.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;This is the third post in a four-part series exploring the Kelly criterion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;The Kelly Criterion: Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;The Kelly Criterion: Multiple Investment Opportunities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The Kelly Criterion: Where does the logarithm come from? (this post)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/19/kelly_criterion4"&gt;The Kelly Criterion: Comparison with Expected Values&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;The neat thing about the derivations in the &lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;last&lt;/a&gt; two
&lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;posts&lt;/a&gt; is that they give a motivation for
"optimizing the logarithm of wealth". The logarithm is not put in by decree,
but is a mathematical technicality that arises from the repeated betting process!
Kelly mentions this in &lt;a href="http://www.herrold.com/brokerage/kelly.pdf"&gt;his original paper&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;At  every  bet  he  maximizes  the  expected  value  of  the
logarithm of his capital.  The reason has nothing to do with the value
function which he attached to his money,  but merely with the fact
that  it  is  the  logarithm  which  is  additive  in  repeated  bets
and  to  which  the law of large numbers applies.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This argument is very general.
Let's say we model the wealth &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; after &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; rounds of betting based on
the initial wealth &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in terms of a function &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;f(R, l) = V_{n+1} / V_n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.00773em;"&gt;R&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.301108em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;span class="mbin mtight"&gt;+&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.208331em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; as&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable width="100%"&gt;&lt;mtr&gt;&lt;mtd width="50%"&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd width="50%"&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mtext&gt;(1)&lt;/mtext&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;V_n = V_0 f(R, l)^n \tag 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.00773em;"&gt;R&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.7143919999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="tag"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;where &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;R&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.00773em;"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is a random variable describing the possible outcomes of the game,
and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the fraction of available money to invest in each round.
Then we can derive the following formula for the best &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; value:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable width="100%"&gt;&lt;mtr&gt;&lt;mtd width="50%"&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mtable rowspacing="0.24999999999999992em" columnalign="right left" columnspacing="0em"&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;munder&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/munder&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence="true"&gt;[&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo fence="true"&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/mtd&gt;&lt;mtd width="50%"&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mtext&gt;(2)&lt;/mtext&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;\begin{aligned}
l_{opt} &amp;amp; =  \operatorname*{argmax}_l \prod_j f(r_j, l)^{p_j n} \\
        &amp;amp; =  \operatorname*{argmax}_l \sum_j p_j \log f(r_j, l) \\
        &amp;amp; =  \operatorname*{argmax}_l E \left[ \log \frac{V_{n+1}}{V_n} \right] \tag 2
\end{aligned}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:8.227594em;vertical-align:-3.863797em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.363797em;"&gt;&lt;span style="top:-6.7637920000000005em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-4.0000100000000005em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.8362330000000004em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.863797em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:4.363797em;"&gt;&lt;span style="top:-6.7637920000000005em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.050005em;"&gt;&lt;span style="top:-1.8723309999999997em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.0500049999999996em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.413777em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.7143919999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.3280857142857143em;"&gt;&lt;span style="top:-2.357em;margin-left:0em;margin-right:0.07142857142857144em;"&gt;&lt;span class="pstrut" style="height:2.5em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.2818857142857143em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-4.0000100000000005em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.050005em;"&gt;&lt;span style="top:-1.8723309999999997em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.0500049999999996em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.413777em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-0.8362330000000004em;"&gt;&lt;span class="pstrut" style="height:3.45em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05764em;"&gt;E&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size3"&gt;[&lt;/span&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.301108em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;span class="mbin mtight"&gt;+&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.208331em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size3"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.863797em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="tag"&gt;&lt;span class="strut" style="height:8.227594em;vertical-align:-3.863797em;"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;where &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are the possible investment outcomes and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are the
associated probabilities. The crucial change from random variable &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;R&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.00773em;"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to
outcomes and probabilities &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is justified by the &lt;a href="https://en.wikipedia.org/wiki/Law_of_large_numbers"&gt;law of
large numbers&lt;/a&gt;. Based on the exponential nature of the formula, switching
to a logarithmic view feels very natural.&lt;/p&gt;
&lt;p&gt;Consequently, neither the details of the game — represented by the
random variable &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;R&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.00773em;"&gt;R&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; — nor the exact form of the per-round return &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;f&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; matter. Any
iterative scheme with reinvestment of profits should be representable in the
form of equation (1), leading to the logarithm in solution (2).
Beyond the origin of the logarithm, this analysis also shows the universality
of the Kelly derivation.&lt;/p&gt;
&lt;p&gt;Unfortunately, this argument is mostly skipped in online discussions.
Often the logarithm is not justified at all, or it is treated
as "genius from the 1950s says: use &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\log&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;". Sometimes the result is also
linked to &lt;a href="https://en.wikipedia.org/wiki/Expected_utility_hypothesis"&gt;utility theory&lt;/a&gt;, which posits that having twice the money is not
twice as useful. While utility theory may be true, reasonable people can
disagree on their utility function — exactly how useful more or less money is
to them. However, Kelly's result is not grounded in utility, and the &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\log&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
does not represent logarithmic utility of money. Consequently, even people who
disagree on their utility function should agree that the Kelly criterion is the
fastest way to gain wealth.&lt;/p&gt;
&lt;p&gt;I hope this post shed some light on the reasoning behind the Kelly decision
scheme.
If you're interested in interactive plots that really helped me understand this
material, you can find them in &lt;a href="https://www.caichinger.com/files/Kelly.ipynb"&gt;this Jupyter Notebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the &lt;a href="https://www.caichinger.com/blog/2018/04/19/kelly_criterion4"&gt;next post&lt;/a&gt;, we'll take a closer look at the relation
of the Kelly criterion to expected values.&lt;/p&gt;
&lt;link crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" rel="stylesheet"&gt;</content><category term="blog"></category><category term="math"></category><category term="investment"></category></entry><entry><title>The Kelly Criterion: Multiple Investment Opportunities</title><link href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2" rel="alternate"></link><published>2018-04-17T00:00:00+02:00</published><updated>2018-04-17T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-04-17:/blog/2018/04/17/kelly_criterion2</id><summary type="html">&lt;p&gt;Extending the Kelly criterion from a single investment to multiple investments at the same time.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;This is the second post in a four-part series exploring the Kelly criterion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;The Kelly Criterion: Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The Kelly Criterion: Multiple Investment Opportunities (this post)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;The Kelly Criterion: Where does the logarithm come from?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/19/kelly_criterion4"&gt;The Kelly Criterion: Comparison with Expected Values&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;After the &lt;a href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1"&gt;last post&lt;/a&gt; introduced the Kelly criterion and its application in
deciding how much to invest in a single gamble, we'll investigate whether Kelly
can help us choosing between multiple investment opportunities.&lt;/p&gt;
&lt;p&gt;We'll start with a mathematical model:
&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence="true"&gt;(&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo fence="true"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_1 = V_0 \left( (1+r_0)(1 - \sum_i l_i) + \sum_i l_i(1 + r_i) \right)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:3.027669em;vertical-align:-1.277669em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size4"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mclose delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size4"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Simple, right? :-)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_0, V_1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8777699999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; are the available money before and after the first
  round, as before.&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the risk-free return rate. This allows us to model opportunity costs
  (could put the money into a bank account instead of investing, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.004&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_0 = 0.004&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;), or inflation
  (non-invested money loses value over time &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;0.02&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_0 = -0.02&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;).&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.84444em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the fraction of the available money invested in stock &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.65952em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is a &lt;a href="https://en.wikipedia.org/wiki/Random_variable"&gt;random variable&lt;/a&gt; describing returns for stock &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.65952em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first term &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;(1+r_0) (1 - \sum_i l_i)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.0497100000000001em;vertical-align:-0.29971000000000003em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op" style="position:relative;top:-0.0000050000000000050004em;"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.16195399999999993em;"&gt;&lt;span style="top:-2.40029em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.29971000000000003em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents all the money not
invested in any stock, being invested at the risk-free return rate &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. The
second term &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\sum_i l_i(1 + r_i)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.0497100000000001em;vertical-align:-0.29971000000000003em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op" style="position:relative;top:-0.0000050000000000050004em;"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.16195399999999993em;"&gt;&lt;span style="top:-2.40029em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.29971000000000003em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; represents the outcomes of the investments
in individual stocks &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.65952em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We will re-formulate a bit to simplify the expression:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable rowspacing="0.24999999999999992em" columnalign="right left" columnspacing="0em"&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;\begin{aligned}
\frac{V_1}{V_0} &amp;amp; = (1+r_0) -  \sum_i l_i (1+r_0) + \sum_i l_i(1 + r_i) \\
                &amp;amp; = (1+r_0) + \sum_i l_i(r_i - r_0)
\end{aligned}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:5.565673em;vertical-align:-2.5328365em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.0328365em;"&gt;&lt;span style="top:-5.0328365em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.4051625000000003em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:2.5328365em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.0328365em;"&gt;&lt;span style="top:-5.0328365em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.4051625000000003em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:2.5328365em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;We can further simplify by merging the &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.84444em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; into the vectors
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.9774399999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.714em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mover accent="true"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.92744em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.92744em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.21344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.2355em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; being a vector where all elements
are &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mover accent="true"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\frac{V_1}{V_0} = (1+r_0) + \vec l \cdot (\vec r - r_0 \vec 1)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.9774399999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.17744em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.92744em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.21344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.2355em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Let's move from a single game round to &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; rounds:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence="true"&gt;(&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mover accent="true"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo fence="true"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\frac{V_n}{V_0} = \left( (1+r_0) + \vec l \cdot (\vec r - r_0 \vec 1) \right)^n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.854312em;vertical-align:-0.65002em;"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.92744em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.21344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.2355em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mclose delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.204292em;"&gt;&lt;span style="top:-3.6029em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Now we are again in a position where we can follow Kelly's prescription:
invoking the &lt;a href="https://en.wikipedia.org/wiki/Law_of_large_numbers"&gt;law of large numbers&lt;/a&gt; and finding the &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; which maximizes
the gain &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_n/V_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;For large &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, we can approximate:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;∏&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/munder&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence="true"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mover accent="true"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo fence="true"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\frac{V_n}{V_0} \approx \prod_j \left( 1+r_0 + \vec l \cdot (\vec r_j - r_0 \vec 1) \right)^{p_j n}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.19633em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≈&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.618069em;vertical-align:-1.413777em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.050005em;"&gt;&lt;span style="top:-1.8723309999999997em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.0500049999999996em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.413777em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.92744em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.21344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.2355em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mclose delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.204292em;"&gt;&lt;span style="top:-3.6029em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.3280857142857143em;"&gt;&lt;span style="top:-2.357em;margin-left:0em;margin-right:0.07142857142857144em;"&gt;&lt;span class="pstrut" style="height:2.5em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.2818857142857143em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;In this step we switched from the random variable &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.714em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to its outcomes
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.000108em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Each possible outcome &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.000108em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; occurs with probability
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. We justify this switch with the law of large numbers: the outcome
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.000108em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; will be observed proportionally to its probability, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
times (for large &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;). Consequently, we will have &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; factors involving
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.000108em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; in the overall product, with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.85396em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; iterating over all potential
outcomes of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.714em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Note that since &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec r_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.000108em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is vector-valued, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_j&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.716668em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is a &lt;a href="https://en.wikipedia.org/wiki/Joint_probability_distribution"&gt;joint probability
distribution&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;At this point we are nearly finished. We are looking for the vector &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\vec
l_{opt}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.263548em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; that maximizes &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;{V_n}/{V_0}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. In analogy to last post, we arrive at:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable rowspacing="0.24999999999999992em" columnalign="right left" columnspacing="0em"&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/munder&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/munder&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;/munder&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence="true"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mover accent="true"&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mover accent="true"&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mover accent="true"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;⃗&lt;/mo&gt;&lt;/mover&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo fence="true"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;\begin{aligned}
\vec l_{opt} &amp;amp; = \operatorname*{argmax}_{\vec l} \frac{V_n}{V_0}
               = \operatorname*{argmax}_{\vec l} \log \frac{V_n}{V_0} \\
             &amp;amp; = \operatorname*{argmax}_{\vec l} \sum_j p_j \log \left( 1+r_0 + \vec l \cdot (\vec r_j - r_0 \vec 1) \right)
\end{aligned}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:5.668755000000001em;vertical-align:-2.5843775000000004em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.0843775000000004em;"&gt;&lt;span style="top:-5.0843775em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.4897295em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:2.5843775000000004em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.0843775000000004em;"&gt;&lt;span style="top:-5.0843775em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43056000000000005em;"&gt;&lt;span style="top:-1.9553520000000002em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord accent mtight"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-2.714em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.97744em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay mtight" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1446479999999999em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43056000000000005em;"&gt;&lt;span style="top:-1.9553520000000002em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord accent mtight"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-2.714em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.97744em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay mtight" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1446479999999999em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.36033em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.4897295em;"&gt;&lt;span class="pstrut" style="height:3.3603300000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43056000000000005em;"&gt;&lt;span style="top:-1.9553520000000002em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord accent mtight"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-2.714em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-2.97744em;"&gt;&lt;span class="pstrut" style="height:2.714em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay mtight" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1446479999999999em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.050005em;"&gt;&lt;span style="top:-1.8723309999999997em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.0500049999999996em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.413777em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="minner"&gt;&lt;span class="mopen delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.9774399999999999em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.26344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.15216em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.17994em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.05724em;"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord accent"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.92744em;"&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.21344em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="accent-body" style="left:-0.2355em;"&gt;&lt;span class="overlay" style="height:0.714em;width:0.471em;"&gt;&lt;svg width='0.471em' height='0.714em' style='width:0.471em' viewBox='0 0 471 714' preserveAspectRatio='xMinYMin'&gt;&lt;path d='M377 20c0-5.333 1.833-10 5.5-14S391 0 397 0c4.667 0 8.667 1.667 12 5
3.333 2.667 6.667 9 10 19 6.667 24.667 20.333 43.667 41 57 7.333 4.667 11
10.667 11 18 0 6-1 10-3 12s-6.667 5-14 9c-28.667 14.667-53.667 35.667-75 63
-1.333 1.333-3.167 3.5-5.5 6.5s-4 4.833-5 5.5c-1 .667-2.5 1.333-4.5 2s-4.333 1
-7 1c-4.667 0-9.167-1.833-13.5-5.5S337 184 337 178c0-12.667 15.667-32.333 47-59
H213l-171-1c-8.667-6-13-12.333-13-19 0-4.667 4.333-11.333 13-20h359
c-16-25.333-24-45-24-59z'/&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mclose delimcenter" style="top:0em;"&gt;&lt;span class="delimsizing size2"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:2.5843775000000004em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;This equation is not analytically solvable, but may be approximated as a
quadratic programming problem as described in a &lt;a href="https://dx.doi.org/10.2139/ssrn.2259133"&gt;paper by Vasily
Nekrasov&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;It should be obvious that the Kelly criterion is applicable in a wide range of
scenarios, from gambling over investment decisions to &lt;a href="https://money.stackexchange.com/a/77622"&gt;whether to buy
insurance&lt;/a&gt;.
If you're interested in interactive plots that really helped me understand this
material, you can find them in &lt;a href="https://www.caichinger.com/files/Kelly.ipynb"&gt;this Jupyter Notebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the &lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;next post&lt;/a&gt; we'll discuss the origins of the logarithm
in the Kelly formula.&lt;/p&gt;
&lt;link crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" rel="stylesheet"&gt;</content><category term="blog"></category><category term="math"></category><category term="investment"></category></entry><entry><title>The Kelly Criterion: Introduction</title><link href="https://www.caichinger.com/blog/2018/04/16/kelly_criterion1" rel="alternate"></link><published>2018-04-16T00:00:00+02:00</published><updated>2018-04-16T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-04-16:/blog/2018/04/16/kelly_criterion1</id><summary type="html">&lt;p&gt;How much money should you invest in any opportunity? The Kelly criterion is a mathematical way to answer this question.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;This is the first post in a four-part series exploring the Kelly criterion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The Kelly Criterion: Introduction (this post)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;The Kelly Criterion: Multiple Investment Opportunities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/18/kelly_criterion3"&gt;The Kelly Criterion: Where does the logarithm come from?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.caichinger.com/blog/2018/04/19/kelly_criterion4"&gt;The Kelly Criterion: Comparison with Expected Values&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Kelly_criterion"&gt;Kelly criterion&lt;/a&gt; is a formula used to determine the optimal size of a
series of bets in order to maximize wealth. It is often described as optimizing
the logarithm of wealth, and will do better than any other strategy in the long
run.&lt;/p&gt;
&lt;p&gt;Let's look at a simple gamble: when you play, you win with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and
lose with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;q&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;q = 1-p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;q&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. If you win, you get back your initial bet plus
a fraction &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; of that bet. If you lose, your bet is forfeited. The Kelly
formula calculating the optimal fraction of your available wealth to bet is:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;q&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_{opt} = \frac{pr - q}{r} = \frac{pr - (1-p)}{r} = p - \frac{1-p}{r}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.980548em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.9463300000000001em;vertical-align:-0.686em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.2603300000000002em;"&gt;&lt;span style="top:-2.314em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;q&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.686em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.113em;vertical-align:-0.686em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.427em;"&gt;&lt;span style="top:-2.314em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.686em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.7777700000000001em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.00744em;vertical-align:-0.686em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.32144em;"&gt;&lt;span style="top:-2.314em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.686em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Here is a small demo to explore the formula:&lt;/p&gt;
&lt;script&gt;
function computeKelly(p, r) {
  return p - (1-p)/r;
}
function updateKelly() {
  const p = document.getElementById('pslider').value;
  const r = document.getElementById('rslider').value;
  document.getElementById('pdisp').textContent = (p/100).toFixed(2);
  document.getElementById('rdisp').textContent = (r/100).toFixed(2);
  document.getElementById('pdisp2').textContent = p;
  document.getElementById('rdisp2').textContent = r;
  document.getElementById('kellycoeff').textContent = (100*computeKelly(p/100, r/100)).toFixed(1);
}
&lt;/script&gt;
&lt;div id="app-6" class="well well-sm"&gt;
  &lt;p&gt;&lt;b&gt;Kelly calculation demo&lt;/b&gt;&lt;/p&gt;
  &lt;table style="width: 100%; text-align: center;"&gt;
    &lt;tr&gt;&lt;td style="width: 4em"&gt;p&lt;td&gt;&lt;input id="pslider" type="range" min="0" max="100" value="80" class="slider" oninput="updateKelly()"&gt;&lt;td style="width: 4em" id="pdisp"&gt;0.80&lt;/tr&gt;
    &lt;tr&gt;&lt;td style="width: 4em"&gt;r&lt;td&gt;&lt;input id="rslider" type="range" min="0" max="400" value="40" class="slider" oninput="updateKelly()"&gt;&lt;td style="width: 4em" id="rdisp"&gt;0.40&lt;/tr&gt;
  &lt;/table&gt;
    &lt;p&gt;
    Probability of winning: &lt;span id="pdisp2"&gt;80&lt;/span&gt; %&lt;br&gt;
    Profit on win, as percentage of placed bet: &lt;span id="rdisp2"&gt;40&lt;/span&gt; %&lt;br&gt;
    Optimal fraction of wealth bet in this game: &lt;span id="kellycoeff"&gt;30&lt;/span&gt; %
    &lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;The main takeaways are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In each game you invest a fraction &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; of the total amount of money you
  have. If you play the game from the demo (&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.4&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p=0.8, r=0.4&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8388800000000001em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) and you have
  $100 available, you should invest $30 (since &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.3&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l=0.3&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;). If you win,
  you will have $112 (&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;100&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.4&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;100 + 30 \times 0.4&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;×&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) and invest $33.60 (&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;112&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;0.3&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;112 \times
  0.3&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;×&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;) in the second round. If you lost the first round you'd have $70,
  and invest $21 in the second round. If you can not reinvest your
  earnings, Kelly does not apply.&lt;/li&gt;
&lt;li&gt;This formula holds when a large number of games are played. If you play only
  few rounds of a game this may not apply.&lt;/li&gt;
&lt;li&gt;When it applies, the Kelly strategy will generate the most wealth out of your
  initial starting capital.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Derivation of the Kelly criterion&lt;/h2&gt;
&lt;p&gt;We will take a short dive into mathematics to understand how Kelly came up with
this formula. Let's compute the wealth &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; after the first round of gaming,
when starting from an initial wealth &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_{1} = V_{0} (1 + Wlr_W + (1-W)lr_L)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.84444em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the fraction of money bet, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is the profit on win, &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_L&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
is the fraction of the bet forfeited on loss (in the above scenario,
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_L=-1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;). &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; is a random variable that's &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1-p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. So with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; we'll have &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W=1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, giving
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_{1,win} = V_0 (1+lr_W)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.969438em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.02691em;"&gt;w&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1-p&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; we'll get &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_{1,loss} =
V_0 (1+lr_L)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.969438em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.3361079999999999em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;s&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;We can reformulate the expression in a way that will show an important generalization:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msup&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_1 = V_0 (1+lr_W)^W(1+lr_L)^{(1-W)}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8913309999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.188em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.938em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;The two expressions look very different, but they are equivalent since &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
can only take on the values &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;0&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. If &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W=1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; the second term will
vanish since &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;x^0 = 1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8141079999999999em;"&gt;&lt;span style="top:-3.063em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, and we will get the same result for &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_{1,win}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.969438em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.311664em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.02691em;"&gt;w&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.
The same consideration holds for &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_{1,loss}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.969438em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.3361079999999999em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mpunct mtight"&gt;,&lt;/span&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;s&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;s&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If we repeat this game &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; times, we arrive at&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_n = V_0 (1+lr_W)^{W_n}(1+lr_L)^{(n-W_n)}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.1413309999999999em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8913309999999999em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.16454285714285719em;"&gt;&lt;span style="top:-2.357em;margin-left:-0.13889em;margin-right:0.07142857142857144em;"&gt;&lt;span class="pstrut" style="height:2.5em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.143em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.188em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.938em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.16454285714285719em;"&gt;&lt;span style="top:-2.357em;margin-left:-0.13889em;margin-right:0.07142857142857144em;"&gt;&lt;span class="pstrut" style="height:2.5em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.143em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;with &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W_n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.13889em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; being the number of wins in those &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; games. Essentially, each
won round adds a factor &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;(1+lr_W)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; to the expression, each lost round
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;(1+lr_L)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Now comes a crucial step: how many wins do we expect after playing many games?
Asked mathematically, what value will &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W_n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.13889em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; have for large &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;? Because of
the &lt;a href="https://en.wikipedia.org/wiki/Law_of_large_numbers"&gt;law of large numbers&lt;/a&gt; we expect &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W = pn&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. So for &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.8&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p=0.8&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;10000&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n=10000&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.64444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; we would expect to win 8000 out of the 10000 games.&lt;/p&gt;
&lt;p&gt;Let's plug this into our formula:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mrow&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;V_n = V_0 (1+lr_W)^{pn}(1+lr_L)^{(1-p)n}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.714392em;"&gt;&lt;span style="top:-3.1130000000000004em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.188em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.938em;"&gt;&lt;span style="top:-3.113em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mopen mtight"&gt;(&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mclose mtight"&gt;)&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;The law of large numbers allowed us to eliminate the random variables &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.68333em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;,
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;W_n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.83333em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.13889em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; and instead obtain an explicit formula in terms of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator="true"&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p, n, r_W, r_L&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.
Since we are interested in maximizing our profit, we are looking for &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_opt&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
— the &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; value that maximizes &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mi mathvariant="normal"&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;{V_n}/{V_0}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.22222em;"&gt;V&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.22222em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mtable rowspacing="0.24999999999999992em" columnalign="right left" columnspacing="0em"&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel="0" displaystyle="true"&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding="application/x-tex"&gt;\begin{aligned}
l_{opt} &amp;amp; = \operatorname*{argmax}_l \frac{v_n}{v_0} \\
        &amp;amp; = \operatorname*{argmax}_l \log \frac{v_n}{v_0} \\
        &amp;amp; = \operatorname*{argmax}_l p \log(1 + l r_W) + (1-p) \log(1 + l r_L)
\end{aligned}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:6.794764000000001em;vertical-align:-3.1473820000000003em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mtable"&gt;&lt;span class="col-align-r"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.6473820000000003em;"&gt;&lt;span style="top:-5.6473819999999995em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.2932740000000003em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-1.2067259999999995em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.1473820000000003em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="col-align-l"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.6473820000000003em;"&gt;&lt;span style="top:-5.6473819999999995em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1075599999999999em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.03588em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.03588em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.2932740000000003em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1075599999999999em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.30110799999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.03588em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.03588em;"&gt;v&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.151392em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.03588em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-1.2067259999999995em;"&gt;&lt;span class="pstrut" style="height:3.10756em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:3.1473820000000003em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;We are free to introduce the logarithm and drop the &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;n&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.43056em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; factor because this
does not change the value of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_{opt}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.980548em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;sup&gt;1&lt;/sup&gt; Differentiating the
logarithmic expression and solving for &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.69444em;vertical-align:0em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; gives:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mfrac&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_{opt} = -\frac{p}{r_L} - \frac{1-p}{r_W}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.980548em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1.94356em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.1075599999999999em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.1574400000000002em;vertical-align:-0.8360000000000001em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.32144em;"&gt;&lt;span style="top:-2.3139999999999996em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.13889em;"&gt;W&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.23em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="frac-line" style="border-bottom-width:0.04em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.677em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.8360000000000001em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;This is identical with the formula given in the introduction after substituting
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_L = -1&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.32833099999999993em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;L&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.72777em;vertical-align:-0.08333em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;−&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;h2&gt;Extension to games with more than two possible outcomes&lt;/h2&gt;
&lt;p&gt;We can extend the Kelly argument to games that have a greater number of
possible outcomes. Such games could be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A game with 6 different rewards selected by roll of a 6-sided die, where
  rewards may be negative (money lost)&lt;/li&gt;
&lt;li&gt;An investment in a stock, here we have a continuous range of outcomes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If we have multiple outcomes &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;r_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.58056em;vertical-align:-0.15em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;, each happening with probability &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;p_i&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;,
we obtain:&lt;/p&gt;
&lt;div class="math"&gt;&lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;munder&gt;&lt;mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/munder&gt;&lt;munder&gt;&lt;mo&gt;∑&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/munder&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;l_{opt}
  = \operatorname*{argmax}_l \sum_i p_i \log(1 + l r_i)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.980548em;vertical-align:-0.286108em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.28055599999999997em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.01968em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault mtight"&gt;t&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.286108em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:2.327674em;vertical-align:-1.277669em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.43055999999999994em;"&gt;&lt;span style="top:-2.153452em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3em;"&gt;&lt;span class="pstrut" style="height:3em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.946548em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.0500050000000003em;"&gt;&lt;span style="top:-1.872331em;margin-left:0em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="top:-3.050005em;"&gt;&lt;span class="pstrut" style="height:3.05em;"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:1.277669em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:0em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2222222222222222em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.01968em;"&gt;l&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathdefault" style="margin-right:0.02778em;"&gt;r&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.31166399999999994em;"&gt;&lt;span style="top:-2.5500000000000003em;margin-left:-0.02778em;margin-right:0.05em;"&gt;&lt;span class="pstrut" style="height:2.7em;"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathdefault mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist" style="height:0.15em;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;While this equation is not easily solvable analytically, it is trivial to solve
numerically. This allows us to extend the Kelly approach to a new kind of game
with some interesting practical applications.&lt;/p&gt;
&lt;p&gt;If you're interested in interactive plots that really helped me understand this
material, you can find them in &lt;a href="https://www.caichinger.com/files/Kelly.ipynb"&gt;this Jupyter Notebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The Kelly formalism can be generalized further still, taking into account
multiple parallel investment opportunities. We'll look at this in the
&lt;a href="https://www.caichinger.com/blog/2018/04/17/kelly_criterion2"&gt;next post&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; The logarithm is a strictly monotonically increasing function, so
&lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mo&gt;&amp;gt;&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mo&gt;⇔&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mo&gt;&amp;gt;&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\alpha \gt \beta \Leftrightarrow \log \alpha \gt \log \beta&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.0037em;"&gt;α&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05278em;"&gt;β&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⇔&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.0037em;"&gt;α&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.05278em;"&gt;β&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. Thus it
doesn't change the location of any maxima: &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo stretchy="false"&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy="false"&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\operatorname*{argmax} f(x) =
\operatorname*{argmax} \log f(x)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault"&gt;x&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.2777777777777778em;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:1em;vertical-align:-0.25em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace" style="margin-right:0.16666666666666666em;"&gt;&lt;/span&gt;&lt;span class="mord mathdefault" style="margin-right:0.10764em;"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault"&gt;x&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;. See &lt;a href="https://www.cs.ubc.ca/~schmidtm/Documents/2016_540_Argmax.pdf"&gt;Mark Schmidt's Argmax and Max Calculus paper&lt;/a&gt; for more information
about properties of &lt;span class="math"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi mathvariant="normal"&gt;argmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding="application/x-tex"&gt;\operatorname*{argmax}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class="katex-html" aria-hidden="true"&gt;&lt;span class="base"&gt;&lt;span class="strut" style="height:0.625em;vertical-align:-0.19444em;"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;r&lt;/span&gt;&lt;span class="mord mathrm" style="margin-right:0.01389em;"&gt;g&lt;/span&gt;&lt;span class="mord mathrm"&gt;m&lt;/span&gt;&lt;span class="mord mathrm"&gt;a&lt;/span&gt;&lt;span class="mord mathrm"&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;link crossorigin="anonymous" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" rel="stylesheet"&gt;</content><category term="blog"></category><category term="math"></category><category term="investment"></category></entry><entry><title>Exploring GDB's Python API with Jupyter</title><link href="https://www.caichinger.com/blog/2018/03/30/explore_gdb_python_api_with_jupyter" rel="alternate"></link><published>2018-03-30T00:00:00+02:00</published><updated>2018-03-30T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2018-03-30:/blog/2018/03/30/explore_gdb_python_api_with_jupyter</id><summary type="html">&lt;p&gt;The GDB debugger is arcane but powerful. It's Python API allows flexible analysis of running programs or crashdumps. This post describes how to use Jupyter to explore this API.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="http://matplotlib.org/"&gt;GDB&lt;/a&gt; — the most common console debugger on Linux systems — has
a &lt;a href="https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html"&gt;Python API&lt;/a&gt; for adding new debugger commands and
pretty-printers for complex data structures, or to automate debugging
tasks.&lt;/p&gt;
&lt;p&gt;While Python scripts can be loaded from files, it is nice to interactively
explore the API or the debugged program. &lt;a href="https://ipython.org/"&gt;IPython&lt;/a&gt; would be
perfect for the job, but starting it directly inside GDB doesn't work well.
Fortunately, it's easy to launch an IPython kernel and connect with an external
Jupyter console.&lt;/p&gt;
&lt;p&gt;Launching the kernel from the gdb prompt:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gdb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;IPython&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;IPython&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;embed_kernel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Gives the following message:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;To connect another client to this kernel, use:&lt;/span&gt;
&lt;span class="go"&gt;    --existing kernel-12688.json&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can start Jupyter on a separate terminal and connect to this kernel:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt; jupyter console --existing kernel-12688.json
&lt;span class="go"&gt;In [1]: gdb.newest_frame().name()&lt;/span&gt;
&lt;span class="go"&gt;Out[1]: &amp;#39;main&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The GDB Python API is then available from the &lt;code&gt;gdb&lt;/code&gt; module within this
Python session. To get started, I'd suggest the &lt;a href="https://sourceware.org/gdb/onlinedocs/gdb/Python-API.html"&gt;API documentation&lt;/a&gt;
or &lt;a href="https://sourceware.org/gdb/wiki/PythonGdbTutorial"&gt;this series of tutorials&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Currently, only the console client can connect to existing kernels.
Support in the Notebook or in Jupyter Lab is tackled in &lt;a href="https://github.com/jupyterlab/jupyterlab/issues/2044"&gt;this Github
issue&lt;/a&gt;. Even with the limited capabilities of the console client, it's
a great way to explore the API and to tackle more complicated debugging
problems that require automation to solve.&lt;/p&gt;</content><category term="blog"></category><category term="python"></category><category term="debugging"></category></entry><entry><title>PyDays 2017</title><link href="https://www.caichinger.com/blog/2017/06/20/pydays_2017" rel="alternate"></link><published>2017-06-20T00:00:00+02:00</published><updated>2017-06-20T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2017-06-20:/blog/2017/06/20/pydays_2017</id><summary type="html">&lt;p&gt;Thoughts about PyDays 2017, Austria's first Python conference&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="https://pydays.at/"&gt;PyDays 2017&lt;/a&gt; was Austria's first conference dedicated to the Python programming language. It took place on May 5 and May 6 and was graciously hosted by the &lt;a href="https://www.linuxwochen.at/wien/"&gt;Linuxwochen Wien&lt;/a&gt; at &lt;a href="https://www.technikum-wien.at/en/"&gt;FH Technikum&lt;/a&gt; in Vienna. It was great on many levels: meeting new people excited about Python and where it's headed, over 20 talks, interesting hallway conversations, etc.&lt;/p&gt;
&lt;p&gt;I helped out with the organization of the conference, mostly taking care of catering. We (the organization team) are very happy with how everything went. The atmosphere was welcoming and open, the quality of the talks and workshops was very good, attendance was great, and people seemed to have a really good time. From an organizational perspective, the talks mostly stayed on-time, the audio/video hardware worked fine, and the PyDays booth was well-received. Giving out snacks, coffee, soda, cake during breaks worked out nicely and was received very well. The feedback, both at and after the conference, was very positive.&lt;/p&gt;
&lt;p&gt;In addition to the co-organization of the Conference, I also held a talk about &lt;a href="http://dask.pydata.org/en/latest/"&gt;Dask&lt;/a&gt;, a Python library for parallel computing. It provides data structures similar to NumPy Arrays or Python Dataframes that process operations in parallel and scale to out-of-memory datasets. Dask is exciting since it provides the analytical power and familiar mental model of Dataframes, but handles hundreds of gigabytes of data and allows seamless scaling from a single laptop to a computing cluster. The talk was well-received, and there was a lot of interest at the end and in the hallway afterwards. &lt;a href="https://www.slideshare.net/ChristianAichinger/paralalel-computing-with-dask-pydays-2017"&gt;The slides are online&lt;/a&gt;, if you're interested as well.&lt;/p&gt;
&lt;p&gt;Finally, I'd like to thank my co-organizers, &lt;a href="https://www.linkedin.com/in/claus-aichinger-data-scientist/"&gt;Claus&lt;/a&gt;, &lt;a href="http://kerstinkollmann.com/"&gt;Kay&lt;/a&gt;, Helmut and &lt;a href="https://sebix.at/"&gt;Sebastian&lt;/a&gt;, as well as the people who helped out during the conference, for all their great work to make the PyDays a success. I'd also like to thank our sponsors: the &lt;a href="https://www.python.org/psf/"&gt;Python Software Foundation&lt;/a&gt;, the &lt;a href="http://python-verband.org/"&gt;Python Software Verband e.V.&lt;/a&gt;, &lt;a href="http://t-mobile.at/"&gt;T-Mobile&lt;/a&gt;, &lt;a href="http://www.ubimet.com/en"&gt;UBIMET&lt;/a&gt;, and &lt;a href="https://www.jetbrains.com/"&gt;Jetbrains&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Overall it was great to see so much community interest in Python. If you're passionate about Python as well, join us on the &lt;a href="https://pythonaustria.github.io/"&gt;Python Austria Slack&lt;/a&gt; and &lt;a href="http://pydays.at/_static/about.html#about-meetups"&gt;our meetups&lt;/a&gt;!&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>GCC compatibility: inline namespaces and ABI tags</title><link href="https://www.caichinger.com/blog/2016/10/29/gcc_compatibility" rel="alternate"></link><published>2016-10-29T00:00:00+02:00</published><updated>2016-10-29T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-10-29:/blog/2016/10/29/gcc_compatibility</id><summary type="html">&lt;p&gt;A look at the two techniques GCC used to mitigate its C++11 ABI break: inline namespaces and ABI tags.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Keeping libraries binary-compatible with old versions is hard. Recently, &lt;a href="https://gcc.gnu.org/"&gt;GCC&lt;/a&gt;
was in the unenviable situation of having to switch its
&lt;a href="http://www.cplusplus.com/reference/string/string/"&gt;&lt;code&gt;std::string&lt;/code&gt;&lt;/a&gt; implementation.&lt;sup&gt;1&lt;/sup&gt; GCC used &lt;a href="http://stackoverflow.com/questions/11016220/what-are-inline-namespaces-for"&gt;inline
namespaces&lt;/a&gt; and &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html"&gt;ABI tags&lt;/a&gt; to minimize the extent of breakage and to ensure
that old and new versions could only be combined in a safe way. Here, we'll have a
look at those mitigation techniques: what they are and how they work.&lt;/p&gt;
&lt;p&gt;First, some background: GCC used to have a copy-on-write implementation for
&lt;code&gt;std::string&lt;/code&gt;. However, C++11 does &lt;a href="http://stackoverflow.com/questions/12199710/legality-of-cow-stdstring-implementation-in-c11"&gt;not allow this anymore&lt;/a&gt;
because of new iterator and reference invalidation rules. So, GCC 5.1
introduced a new implementation of &lt;code&gt;std::string&lt;/code&gt;. The new version was not
binary-compatible with the old one: exchanging strings between old (pre-5.1)
and new code would crash. We say the &lt;a href="https://en.wikipedia.org/wiki/Application_binary_interface"&gt;application binary interface (ABI)&lt;/a&gt;
changed.&lt;/p&gt;
&lt;p&gt;To understand the consequences of this, let's look at several scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A program uses only code compiled with GCC &amp;lt; 5.1: only old strings, works&lt;/li&gt;
&lt;li&gt;A program uses only code compiled with GCC &amp;gt;= 5.1: only new strings, works&lt;/li&gt;
&lt;li&gt;A program mixes code compiled with different GCC versions: both types of
  strings exist in the program, it could crash.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let's dig into the last bullet point. When would it crash? Whenever
"new" code accesses an "old" string or vice versa. Here are some examples
where &lt;code&gt;f()&lt;/code&gt; is compiled with an older GCC version and called from "new"
code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// (a) safe, no std::string involved&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// (b) will crash when f accesses s&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;               &lt;span class="c1"&gt;// (c) will crash when the returned string is used&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Given how common &lt;code&gt;std::string&lt;/code&gt; is, such crashes would happen frequently if
"old" and "new" code were combined. Unfortunately, it's surprisingly easy to
end up with a program with some pre-5.1 parts and some newer ones. It's
sufficient to link a "new" executable to an "old" library. Given the bad
consequences, the GCC developers needed to solve this.&lt;/p&gt;
&lt;p&gt;The solution is to change the symbol names of the GCC 5.1 &lt;code&gt;std::string&lt;/code&gt; and
all functions using it. Because the linker uses symbol names to resolve
function calls into libraries, this would cause link-time errors for cases (b)
and (c) while (a) would still work. Exactly the intended behavior.&lt;/p&gt;
&lt;p&gt;How could this be done? The mechanism that converts C++ names into symbol names
is called &lt;a href="http://stackoverflow.com/a/2937401/25097"&gt;name mangling&lt;/a&gt;. The generated symbol names contain information
about namespaces, function names, argument types, etc. Putting the new
&lt;code&gt;std::string&lt;/code&gt; into a separate namespace would change the
symbol names of all functions accepting &lt;code&gt;std::string&lt;/code&gt; as argument. But that's
crazy—&lt;code&gt;std::string&lt;/code&gt; needs to be in namespace &lt;code&gt;std&lt;/code&gt;, right?&lt;/p&gt;
&lt;p&gt;The solution for this is &lt;a href="http://stackoverflow.com/questions/11016220/what-are-inline-namespaces-for"&gt;inline namespaces&lt;/a&gt;. All classes, functions, and
templates declared in an inline namespace are automatically imported into the
parent namespace. Their mangled name still references the original location,
though.&lt;/p&gt;
&lt;p&gt;Here's what GCC 5.1 does:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;__cxx11&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;_CharT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  &lt;span class="n"&gt;basic_string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;      &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looking at &lt;code&gt;f(const std::string&amp;amp; s)&lt;/code&gt;, what would the symbol names be
when compiled with older and newer GCC versions?&lt;/p&gt;
&lt;table class='table table-sm table-striped table-bordered table-condensed'&gt;
&lt;tr&gt;&lt;td&gt;&lt;th&gt;Older GCC&lt;th&gt;GCC 5.1
&lt;tr&gt;&lt;th&gt;Symbol name&lt;td&gt;_Z1fRKSs&lt;td&gt;_Z1fRKNSt7&lt;wbr&gt;__&lt;wbr&gt;cxx1112&lt;wbr&gt;basic&lt;wbr&gt;_string&lt;wbr&gt;IcSt11char&lt;wbr&gt;_traits&lt;wbr&gt;IcESaIcEEE
&lt;tr&gt;&lt;th&gt;Decoded symbol name&lt;sup&gt;2&lt;/sup&gt;&lt;td&gt;&lt;code&gt;f(std::&lt;wbr&gt;basic_string&lt;wbr&gt;&amp;lt;char, ...&amp;gt; const&amp;)&lt;/code&gt;&lt;td&gt;&lt;code&gt;f(std::&lt;wbr&gt;&amp;#95;&amp;#95;cxx11::&lt;wbr&gt;basic_string&lt;wbr&gt;&amp;lt;char, ...&amp;gt; const&amp;)&lt;/code&gt;
&lt;/table&gt;

&lt;p&gt;Indeed, the symbol names are different and the linker would give an error if
GCC 5.1 code called &lt;code&gt;f(const std::string &amp;amp;s)&lt;/code&gt; from a library compiled
with an older GCC version. This solves the compatibility problem for all
functions taking &lt;code&gt;std::string&lt;/code&gt; as argument.&lt;/p&gt;
&lt;p&gt;One problem remains: case (c) from above, &lt;code&gt;std::string f()&lt;/code&gt;. The return value
type of a function is not part of its mangled name.&lt;sup&gt;3&lt;/sup&gt; Thus, the symbol
name wouldn't change for functions that return &lt;code&gt;std::string&lt;/code&gt;, which could
still lead to runtime crashes.&lt;/p&gt;
&lt;p&gt;GCC 5.1 solves this with the use of ABI tags. From the &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html"&gt;documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;abi_tag&lt;/code&gt; attribute can be applied to a function, variable, or class
declaration. It modifies the mangled name of the entity to incorporate the
tag name, in order to distinguish the function or class from an earlier
version with a different ABI&lt;/p&gt;
&lt;p&gt;[...]&lt;/p&gt;
&lt;p&gt;When a type involving an ABI tag is used as the type of a variable or return
type of a function where that tag is not already present in the signature of
the function, the tag is automatically applied to the variable or function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;GCC applies the attribute &lt;code&gt;__abi_tag__ ("cxx11"))&lt;/code&gt; to the &lt;code&gt;std::__cxx11&lt;/code&gt;
namespace. This affects all classes therein, including the new version of
&lt;code&gt;std::string&lt;/code&gt;. The ABI tag propagates to all functions that
return a string and changes their symbol name.&lt;/p&gt;
&lt;p&gt;Let's look at the symbol names for &lt;code&gt;std::string f()&lt;/code&gt; for different compiler versions:&lt;/p&gt;
&lt;table class='table table-sm table-striped table-bordered table-condensed'&gt;
&lt;tr&gt;&lt;td&gt;&lt;th&gt;Older GCC&lt;th&gt;GCC 5.1
&lt;tr&gt;&lt;th&gt;Symbol name&lt;td&gt;_Z1fv&lt;td&gt;_Z1fB5cxx11v
&lt;tr&gt;&lt;th&gt;Decoded symbol name&lt;sup&gt;2&lt;/sup&gt;&lt;td&gt;&lt;code&gt;f()&lt;/code&gt;&lt;td&gt;&lt;code&gt;f[abi:cxx11]()&lt;/code&gt;
&lt;/table&gt;

&lt;p&gt;Again, the symbol name is different, and the "cxx11" ABI tag is applied to
&lt;code&gt;std::string f()&lt;/code&gt; with GCC 5.1. This completes the second part of GCC's solution
for the &lt;code&gt;std::string&lt;/code&gt; ABI change.&lt;/p&gt;
&lt;p&gt;These two methods to induce symbol name changes for anything using
&lt;code&gt;std::string&lt;/code&gt; make the migration path to GCC 5.1 much easier.
If the program links correctly it should work, and be safe from difficult-to-debug
runtime errors.
There would be more to discuss about the &lt;a href="https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html"&gt;GCC 5.1 changes&lt;/a&gt;, such as how
libstdc++.so still exports the old &lt;code&gt;std::string&lt;/code&gt; implementation for
compatibility. But this post has gone on too long already, so let's leave it at
that :-)&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; GCC 5.1 also introduced a new version of &lt;code&gt;std::list&lt;/code&gt;. It is handled analogously to &lt;code&gt;std::string&lt;/code&gt;.&lt;br&gt;
&lt;sup&gt;2&lt;/sup&gt; Decoded using &lt;a href="https://sourceware.org/binutils/docs/binutils/c_002b_002bfilt.html"&gt;c++filt&lt;/a&gt; from the binutils package.&lt;br&gt;
&lt;sup&gt;3&lt;/sup&gt; The return value type doesn't need to be part of the symbol name because it doesn't get used for &lt;a href="http://en.cppreference.com/w/cpp/language/overload_resolution"&gt;overload resolution&lt;/a&gt;.&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category></entry><entry><title>New C++ documentation section in the sidebar</title><link href="https://www.caichinger.com/blog/2016/06/26/cpp_variable_initialization" rel="alternate"></link><published>2016-06-26T00:00:00+02:00</published><updated>2016-06-26T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-06-26:/blog/2016/06/26/cpp_variable_initialization</id><summary type="html">&lt;p&gt;A new "C++ for Humans" section on this website contains documentation about the C++ language.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I started a new section on this website that attempts to make C++ language
constructs more accessible, it's called &lt;a href="https://www.caichinger.com/cpp/index.html"&gt;C++ for Humans&lt;/a&gt;.
For now, there are two articles: one about the &lt;a href="https://www.caichinger.com/cpp/initialization.html"&gt;details of variable
initialization&lt;/a&gt; and one about the &lt;a href="https://www.caichinger.com/cpp/storage-duration.html"&gt;types of storage
duration&lt;/a&gt; C++ supports.&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category></entry><entry><title>Building External Libraries with Boost Build</title><link href="https://www.caichinger.com/blog/2016/06/18/building_external_libraries_with_boost_build" rel="alternate"></link><published>2016-06-18T00:00:00+02:00</published><updated>2016-06-18T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-06-18:/blog/2016/06/18/building_external_libraries_with_boost_build</id><summary type="html">&lt;p&gt;Integrate external libraries into Boost Build projects by leveraging their own make, cmake, autotools, ... build system.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I recently ran into the problem of having to use an external library
not available for our distribution. Of course, it would be possible
to build Debian or RPM packages, deploy them, and then link against those.
However, building sanely-behaving library packages is
&lt;a href="https://www.netfort.gr.jp/~dancer/column/libpkg-guide/libpkg-guide.html"&gt;tricky&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The easier way is to include the source distribution in a sub-folder
of the project and integrate it into our build system, &lt;a href="http://www.boost.org/build/"&gt;Boost
Build&lt;/a&gt;. It seemed prohibitive to
create Boost Build rules to build the library from scratch (from &lt;code&gt;*.cpp&lt;/code&gt; to &lt;code&gt;*.a&lt;/code&gt;/&lt;code&gt;*.so&lt;/code&gt;)
because the library had fairly complex build requirements.
The way to go was to leverage the library's own build
system.&lt;/p&gt;
&lt;p&gt;Easier said than done. I only arrived at a fully integrated
solution after &lt;a href="http://lists.boost.org/boost-build/2016/06/28712.php"&gt;seeking help&lt;/a&gt; on the Boost Build Mailing
List (thanks Steven, Vladimir). This is the core of it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;path-constant LIBSQUARE_DIR : . &lt;span class="p"&gt;;&lt;/span&gt;

make libsquare.a
    : &lt;span class="o"&gt;[&lt;/span&gt; glob-tree *.c *.cpp *.h : generated-file.c gen-*.cpp &lt;span class="o"&gt;]&lt;/span&gt;
    : @build-libsquare
    &lt;span class="p"&gt;;&lt;/span&gt;
actions build-libsquare
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;(&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="k"&gt;$(&lt;/span&gt;LIBSQUARE_DIR&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; make &lt;span class="o"&gt;)&lt;/span&gt;
    cp &lt;span class="k"&gt;$(&lt;/span&gt;LIBSQUARE_DIR&lt;span class="k"&gt;)&lt;/span&gt;/libsquare.a &lt;span class="k"&gt;$(&lt;/span&gt;&amp;lt;&lt;span class="k"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;alias&lt;/span&gt; libsquare
    : libsquare.a                                        &lt;span class="c1"&gt;# sources&lt;/span&gt;
    :                                                    &lt;span class="c1"&gt;# requirements&lt;/span&gt;
    :                                                    &lt;span class="c1"&gt;# default-build&lt;/span&gt;
    : &amp;lt;include&amp;gt;&lt;span class="k"&gt;$(&lt;/span&gt;LIBSQUARE_DIR&lt;span class="k"&gt;)&lt;/span&gt; &amp;lt;dependency&amp;gt;libsquare.a  &lt;span class="c1"&gt;# usage-requirements&lt;/span&gt;
    &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first line defines a filesystem constant for the library directory, so
the build process becomes independent of the working directory.&lt;/p&gt;
&lt;p&gt;The next few lines tell Boost Build about a library called &lt;code&gt;libsquare.a&lt;/code&gt;, which
is built by calling &lt;code&gt;make&lt;/code&gt;. The &lt;code&gt;cp&lt;/code&gt; command copies the library to the target
location expected by Boost Build (&lt;code&gt;$(&amp;lt;)&lt;/code&gt;). &lt;code&gt;glob-tree&lt;/code&gt; defines source files
that should trigger library rebuilds when changed, taking care to exclude files
generated by the build process itself.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;alias&lt;/code&gt; command defines a Boost Build target to link against
the library. The real magic is the
&lt;code&gt;&amp;lt;dependency&amp;gt;libsquare.a&lt;/code&gt; directive: it is required because the library
build may produce header files used by client code. Thus,
&lt;code&gt;build-libsquare&lt;/code&gt; must run before compiling any dependent
C++ files. Adding &lt;code&gt;libsquare.a&lt;/code&gt; to an executable's dependency list
won't quite enforce this: it only builds &lt;code&gt;libsquare.a&lt;/code&gt; in time
for the linking step (&lt;code&gt;*.o&lt;/code&gt; → &lt;code&gt;executable&lt;/code&gt;), but not for the compilation
(&lt;code&gt;*.cpp&lt;/code&gt; → &lt;code&gt;*.o&lt;/code&gt;). In contrast, the &lt;code&gt;&amp;lt;dependency&amp;gt;libsquare.a&lt;/code&gt; directive
propagates to all dependent build steps, including the compilation, and induces
the required dependencies.&lt;/p&gt;
&lt;p&gt;I created an &lt;a href="https://github.com/ChrisAichinger/external-libraries-with-boost-build"&gt;example project&lt;/a&gt; on GitHub that
demonstrates this. It builds a simple executable relying on a library
built via &lt;code&gt;make&lt;/code&gt;. The process is fully integrated in the Boost Build framework
and triggered by a single call to &lt;code&gt;bjam&lt;/code&gt; (or &lt;code&gt;bb2&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;If you need something along these lines give this solution a try!&lt;/p&gt;</content><category term="blog"></category><category term="boost"></category><category term="c++"></category></entry><entry><title>Working with angles (is surprisingly hard)</title><link href="https://www.caichinger.com/blog/2016/06/14/working_with_angles" rel="alternate"></link><published>2016-06-14T00:00:00+02:00</published><updated>2016-06-14T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-06-14:/blog/2016/06/14/working_with_angles</id><summary type="html">&lt;p&gt;Two well-tested functions to average and subtract angles in a sane way.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Due to the periodic nature of angles, especially the discontinuity at 2π /
360°, working with them presents some subtle issues. For example, the angles 5°
and 355° are "close" to each other, but are numerically quite different. In a
geographic (GIS) context, this often surfaces when working with geometries
spanning Earth's date-line at -180°/+180° longitude, which often requires
multiple code paths to obtain the desired result.&lt;/p&gt;
&lt;p&gt;I've recently run into the problem of computing averages and
differences of angles. The difference should increase linearly, it should be 0
if they are equal, negative if the second angle lies to one side of the first,
positive if it's on the other side (whether that's left or right depends on
whether the angles increase in the clockwise direction or in counter-clockwise
direction). Getting that right and coming up with test cases to prove that it's
correct was quite interesting. As Bishop notes in &lt;a href="http://research.microsoft.com/en-us/um/people/cmbishop/prml/"&gt;Pattern Recognition and
Machine Learning&lt;/a&gt;, it's often simpler to perform operations on angles in
a 2D (x, y) space and then back-transform to angles using the
&lt;a href="https://en.wikipedia.org/wiki/Atan2"&gt;&lt;code&gt;atan2()&lt;/code&gt;&lt;/a&gt; function. I've used that for the averaging function; the
difference is calculated using modulo arithmetic.&lt;/p&gt;
&lt;p&gt;Here's the Python version of the two functions:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;average_angles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;angles&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Average (mean) of angles&lt;/span&gt;

&lt;span class="sd"&gt;    Return the average of an input sequence of angles. The result is between&lt;/span&gt;
&lt;span class="sd"&gt;    ``0`` and ``2 * math.pi``.&lt;/span&gt;
&lt;span class="sd"&gt;    If the average is not defined (e.g. ``average_angles([0, math.pi]))``,&lt;/span&gt;
&lt;span class="sd"&gt;    a ``ValueError`` is raised.&lt;/span&gt;
&lt;span class="sd"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cos&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;angles&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;angles&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="ne"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s2"&gt;&amp;quot;The angle average of the inputs is undefined: &lt;/span&gt;&lt;span class="si"&gt;%r&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;angles&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# To get outputs from -pi to +pi, delete everything but math.atan2() here.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;atan2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;subtract_angles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Return the signed difference between angles lhs and rhs&lt;/span&gt;

&lt;span class="sd"&gt;    Return ``(lhs - rhs)``, the value will be within ``[-math.pi, math.pi)``.&lt;/span&gt;
&lt;span class="sd"&gt;    Both ``lhs`` and ``rhs`` may either be zero-based (within&lt;/span&gt;
&lt;span class="sd"&gt;    ``[0, 2*math.pi]``), or ``-pi``-based (within ``[-math.pi, math.pi]``).&lt;/span&gt;
&lt;span class="sd"&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fmod&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;lhs&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code, along with test cases can also be found in &lt;a href="https://gist.github.com/ChrisAichinger/91037856ce025540b672eaf541b6c1d7"&gt;this GitHub Gist&lt;/a&gt;.
Translation of these functions to other languages should be straight-forward,
&lt;code&gt;sin()&lt;/code&gt;/&lt;code&gt;cos()&lt;/code&gt;/&lt;code&gt;fmod()&lt;/code&gt;/&lt;code&gt;atan2()&lt;/code&gt; are pretty ubiquitous.&lt;/p&gt;</content><category term="blog"></category><category term="python"></category><category term="algorithms"></category></entry><entry><title>Resource management with Python</title><link href="https://www.caichinger.com/blog/2016/05/28/resource_management_with_python" rel="alternate"></link><published>2016-05-28T00:00:00+02:00</published><updated>2016-05-28T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-05-28:/blog/2016/05/28/resource_management_with_python</id><summary type="html">&lt;p&gt;The correct way to manage resources in Python is the "with" statement. A look at potential alternatives, why "with" is better, and how to use it with custom resources.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;There should be one – and preferably only one – obvious way to do it.&lt;/p&gt;
&lt;p&gt;&lt;footer&gt;Tim Peters in &lt;a href="https://www.python.org/dev/peps/pep-0020/"&gt;The Zen of Python&lt;/a&gt;&lt;/footer&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There are multiple ways to manage resources with Python, but only one of them
is save, reliable and Pythonic.&lt;/p&gt;
&lt;p&gt;Before we dive in, let's examine what &lt;em&gt;resources&lt;/em&gt; can mean in this context. The
most obvious examples are open files, but the concept is broader: it includes
locked mutexes, started client processes, or a temporary directory change
using &lt;code&gt;os.chdir()&lt;/code&gt;. The common theme is that all of these require some sort
of cleanup that must reliably be executed in the future.
The file must be closed, the mutex unlocked, the process terminated, and the
current directory must be changed back.&lt;/p&gt;
&lt;p&gt;So the core question is: how to ensure that this cleanup really happens?&lt;/p&gt;
&lt;h3&gt;Failed solutions&lt;/h3&gt;
&lt;p&gt;Manually calling the cleanup function at the end of a code block is the most obvious solution:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem with this is that &lt;code&gt;f.close()&lt;/code&gt; will never be executed if
&lt;code&gt;do_something(f)&lt;/code&gt; throws an exception. So we'll need a better solution.&lt;/p&gt;
&lt;p&gt;C++ programmers see this and try to apply the C++ solution: &lt;a href="https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization"&gt;RAII&lt;/a&gt;,
where resources are acquired in an object's constructor and released in the
destructor:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fname&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="fm"&gt;__del__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;my_f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# my_f.__del__() automatically called once my_f goes out of scope&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Apart from being verbose and a bit un-Pythonic, it's also not necessarily
correct. &lt;code&gt;__del__()&lt;/code&gt; is only called once the object's refcount reaches zero,
which can be prevented by &lt;a href="https://docs.python.org/3/reference/datamodel.html#object.__del__"&gt;reference cycles or leaked references&lt;/a&gt;.
Additionally, until Python 3.4 some &lt;code&gt;__del__()&lt;/code&gt; methods were &lt;a href="https://www.python.org/dev/peps/pep-0442/"&gt;not called during
interpreter shutdown&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;A workable solution&lt;/h3&gt;
&lt;p&gt;The way to ensure that cleanup code is called in the face of exceptions is the
&lt;code&gt;try ... finally&lt;/code&gt; construct:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In contrast to the previous two solutions, this ensures that the file is closed
no matter what (short of an interpreter crash). It's a bit unwieldy, especially
when you think about &lt;code&gt;try ... finally&lt;/code&gt; statements sprinkled all over a large
code base. Fortunately, Python provides a better way.&lt;/p&gt;
&lt;h3&gt;The correct solution™&lt;/h3&gt;
&lt;p&gt;The Pythonic solution is to use the &lt;a href="https://www.python.org/dev/peps/pep-0343/"&gt;&lt;code&gt;with&lt;/code&gt;&lt;/a&gt; statement:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It is concise and correct even if &lt;code&gt;do_something(f)&lt;/code&gt; raises an exception. Nearly
all built-in classes that manage resources can be used in this way.&lt;/p&gt;
&lt;p&gt;Under the covers, this functionality is implemented using objects known as
&lt;a href="https://www.python.org/dev/peps/pep-0343/"&gt;context managers&lt;/a&gt;, which provide &lt;code&gt;__enter__()&lt;/code&gt; and &lt;code&gt;__exit__()&lt;/code&gt;
methods that are called at the beginning and end of the &lt;code&gt;with&lt;/code&gt; block. While
it's possible to write such classes manually, an easier way is to use the
&lt;a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager"&gt;contextlib.contextmanager&lt;/a&gt; decorator.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;contextlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;contextmanager&lt;/span&gt;

&lt;span class="nd"&gt;@contextmanager&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;managed_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acquire_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;release_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;managed_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;contextmanager&lt;/code&gt; decorator turns a generator function (a function with a
&lt;code&gt;yield&lt;/code&gt; statement) into a context manager. This way it is possible to make
arbitrary code compatible with the &lt;code&gt;with&lt;/code&gt; statement in just a few lines of
Python.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;try ... finally&lt;/code&gt; is used as a building block here. In contrast
to the previous solution, it is hidden away in a utility resource manager
function, and doesn't clutter the main program flow, which is nice.&lt;/p&gt;
&lt;p&gt;If the client code doesn't need to obtain an explicit reference to the
resource, things are even simpler:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nd"&gt;@contextmanager&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;managed_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acquire_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt;
    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;release_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;managed_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Sometimes the argument comes up that this makes it harder to use those
resources in interactive Python sessions – you can't wrap your whole session in
a gigantic &lt;code&gt;with&lt;/code&gt; block, after all. The solution is simple: just call
&lt;code&gt;__enter__()&lt;/code&gt; on the context manager manually to obtain the resource:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;cm_r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;managed_resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;file.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cm_r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="fm"&gt;__enter__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# Work with r...&lt;/span&gt;
&lt;span class="n"&gt;cm_r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="fm"&gt;__exit__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;__exit__()&lt;/code&gt; method takes three arguments, passing &lt;code&gt;None&lt;/code&gt; here is fine
(these are used to pass exception information, where applicable). Another option
in interactive sessions is to not call &lt;code&gt;__exit__()&lt;/code&gt; at all, if you can live
with the consequences.&lt;/p&gt;
&lt;h3&gt;Wrap Up&lt;/h3&gt;
&lt;p&gt;Concise, correct, Pythonic. There is no reason to ever manage resources in any
other way in Python. If you aren't using it yet - start now!&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>Remarks on enable_shared_from_this</title><link href="https://www.caichinger.com/blog/2016/05/21/enable_shared_from_this" rel="alternate"></link><published>2016-05-21T00:00:00+02:00</published><updated>2016-05-21T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-05-21:/blog/2016/05/21/enable_shared_from_this</id><summary type="html">&lt;p&gt;An in-depth look at C++'s std::enable_shared_from_this: utility, pitfalls and patterns to use it safely.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="http://en.cppreference.com/w/cpp/memory/enable_shared_from_this"&gt;&lt;code&gt;std::enable_shared_from_this&lt;/code&gt;&lt;/a&gt; is a template base class that allows
derived classes to get a &lt;a href="http://en.cppreference.com/w/cpp/memory/shared_ptr"&gt;&lt;code&gt;std::shared_ptr&lt;/code&gt;&lt;/a&gt; to themselves. This can be
handy, and it's not something that C++ classes can normally do. Calling
&lt;code&gt;std::shared_ptr&amp;lt;T&amp;gt;(this)&lt;/code&gt; is not an option as it creates a new shared pointer
independent of the existing one, which leads to double destruction.&lt;/p&gt;
&lt;p&gt;The caveat is that before calling the &lt;a href="http://en.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this"&gt;&lt;code&gt;shared_from_this()&lt;/code&gt;&lt;/a&gt; member function, a
&lt;code&gt;shared_ptr&lt;/code&gt; to the object must already exist, otherwise undefined behavior results.
In other words, the object must already be managed by a shared pointer.&lt;/p&gt;
&lt;p&gt;This presents an interesting issue. When using this technique, there are member
functions (those that rely on &lt;code&gt;shared_from_this()&lt;/code&gt;) that can only be called if
the object is managed via a &lt;code&gt;shared_ptr&lt;/code&gt;. This is a rather subtle requirement:
the compiler won't enforce it. If violated, the object may even work at runtime
until a problematic code path is executed, which may happen rarely – a nice
little trap. At the very least, this should be prominently mentioned in the
class documentation. But frankly, relying on the documentation to
communicate such a subtle issue sounds wrong.&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html"&gt;correct solution&lt;/a&gt; is to let the compiler enforce it. Make the
constructors private and provide a static factory method that returns a
&lt;code&gt;shared_ptr&lt;/code&gt; to a new instance. Take care to delete the copy constructor
and the assignment operator to prevent anyone from obtaining
non-shared-pointer-managed instances this way.&lt;/p&gt;
&lt;p&gt;Another point worth mentioning about &lt;code&gt;enable_shared_from_this&lt;/code&gt; is that the
member functions it provides, &lt;code&gt;shared_from_this()&lt;/code&gt; and &lt;code&gt;weak_from_this()&lt;/code&gt;, are
public. Not only the object itself can retrieve it's owning &lt;code&gt;shared_ptr&lt;/code&gt;,
everyone else can too. Whether this is desirable is essentially an API design
question and depends on the context. To restrict access to these functions,
use &lt;a href="https://isocpp.org/wiki/faq/private-inheritance"&gt;private inheritance&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Overall, &lt;code&gt;enable_shared_from_this&lt;/code&gt; is an interesting tool, if a bit
situational. However, it requires care to use safely, in a way that prevents
derived classes from being used incorrectly.&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category></entry><entry><title>Building a computer from logic gates</title><link href="https://www.caichinger.com/blog/2016/05/14/building_a_computer_from_logic_gates" rel="alternate"></link><published>2016-05-14T00:00:00+02:00</published><updated>2016-05-14T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-05-14:/blog/2016/05/14/building_a_computer_from_logic_gates</id><summary type="html">&lt;p&gt;Exploring how it would be possible to build a general-purpose CPU starting from logic gates that are easily mass-produced using modern semiconductor technology.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Ever wondered how computers actually work on a low level?&lt;/p&gt;
&lt;p&gt;After Jeff Atwood's &lt;a href="http://blog.codinghorror.com/heres-the-programming-game-you-never-asked-for/"&gt;recent post about Robot Odyssey&lt;/a&gt;, I did.&lt;/p&gt;
&lt;p&gt;The following is a sketch of what could work, not necessarily what modern hardware actually does. The aim is to explore how a &lt;a href="https://en.wikipedia.org/wiki/Turing_completeness"&gt;Turing-complete&lt;/a&gt;, multi-purpose computation engine could in principle be built from simple logic elements.&lt;/p&gt;
&lt;h3&gt;From silicon to computation&lt;/h3&gt;
&lt;p&gt;Nearly all chips are &lt;a href="https://en.wikipedia.org/wiki/Semiconductor_device_fabrication"&gt;manufactured&lt;/a&gt; on silicon plates called &lt;em&gt;wafers&lt;/em&gt;. These plates are modified in a complex process to create semiconductor-based diodes and transistors. Most general-purpose processors use a technology called &lt;a href="https://en.wikipedia.org/wiki/CMOS"&gt;CMOS&lt;/a&gt; that arranges the transistors into logic gates – devices that carry out &lt;a href="https://en.wikipedia.org/wiki/Boolean_algebra"&gt;operations on zeros and ones&lt;/a&gt;. The most common gate to implement is the &lt;a href="https://en.wikipedia.org/wiki/NAND_gate"&gt;NAND&lt;/a&gt;. All other common logic gates (AND, OR, NOT, XOR, ...) can be &lt;a href="https://en.wikipedia.org/wiki/NAND_logic"&gt;constructed from NAND building blocks&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Flip-flop_%28electronics%29#SR_NOR_latch"&gt;Memory cells&lt;/a&gt; can be constructed by composing multiple logic gates. Each cell stores a single bit of information. Conceptually, it has one output (&lt;code&gt;VALUE&lt;/code&gt;) where the current value can be read. Additionally, there are two input pins: &lt;code&gt;SET&lt;/code&gt; and &lt;code&gt;SET_VALUE&lt;/code&gt;. For reading, &lt;code&gt;SET&lt;/code&gt; is zero. For writing, &lt;code&gt;SET&lt;/code&gt; is one and the &lt;code&gt;SET_VALUE&lt;/code&gt; becomes the new value stored in the cell. It's not hard to imagine how to build a memory controller on top of an array of memory cells that allows addressing of individual cells for getting and setting their value.&lt;/p&gt;
&lt;p&gt;How can memory be modified in practice? For example, how is it possible to invert (change 0 to 1 and vice versa) the value of a memory cell? Reading the memory, inverting it and writing it back into the memory cell leads to oscillation: when the cell value is changed it is immediately read back, and inverted and written again. This cycle repeats as quickly as the electronics can handle.&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="Memory cell feeding back to itself via an inverter!" src="https://www.caichinger.com/files/memory-inverter.png" title="Inverting a bit this way doesn't work - uncontrolled oscillation is observed."&gt;&lt;div class="img-caption"&gt;Inverting a bit this way doesn't work - uncontrolled oscillation is observed.&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;The solutions to this conundrum are clocks and &lt;a href="https://en.wikipedia.org/wiki/Flip-flop_%28electronics%29#D_flip-flop"&gt;edge-triggered flip-flops&lt;/a&gt;. Clocks are signals switching between 0 and 1 at a defined frequency. Edge-triggered flip-flops read their input at the rising edge of the clock (when it switches from 0 to 1) and output that value until the next rising edge. In other words, they sample their input once per clock cycle and hold that value until the next cycle. When such an element is inserted into the inversion loop, the memory value is inverted exactly once per clock cycle.&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="Memory cell feeding back to itself via an edge-triggered flip-flop and an inverter!" src="https://www.caichinger.com/files/memory-inverter-buffered.png" title="With additional edge-triggered flip-flop, the bit is inverted exactly once per clock tick."&gt;&lt;div class="img-caption"&gt;With additional edge-triggered flip-flop, the bit is inverted exactly once per clock tick.&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;Based on this technique other operations can be implemented as well, such as &lt;a href="https://en.wikipedia.org/wiki/Adder_%28electronics%29"&gt;adding&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Binary_multiplier"&gt;multiplying&lt;/a&gt; memory cells, copying memory contents to other locations, performing bitwise operations, and so on.&lt;/p&gt;
&lt;h3&gt;General-purpose processors&lt;/h3&gt;
&lt;p&gt;For each of those operations the logic gates would have to be arranged differently, though. In contrast, real general-purpose CPU's have fixed logic circuits, their gate configuration doesn't change during runtime. Instead, the operations to execute are read from memory and interpreted according to the chip's &lt;a href="https://en.wikipedia.org/wiki/Instruction_set"&gt;instruction set&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For our analysis, let's assume the command is read from separate input lines instead. We'll return to reading commands from memory later on.&lt;/p&gt;
&lt;p&gt;How could one design and implement an instruction set? Let's say we have a machine with 8 lines (bits) of input and four 8-bit registers A, B, C, D. External memory is addressed in chunks of 8 bits and is attached via 8 address lines that select the location, 8 lines for reading/writing the 8-bit value, and one line to switch between reading and writing. What operations could we have?&lt;/p&gt;
&lt;style&gt;
#opcode-table code { white-space: nowrap; }
&lt;/style&gt;

&lt;table class='table table-sm table-striped table-bordered table-condensed' id='opcode-table'&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opcode&lt;/th&gt;
&lt;th&gt;Mnemonic&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;00RRVVVV&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SetHi VVVV, RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Set the 4 highest bits of register RR to VVVV.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;01RRVVVV&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SetLo VVVV, RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Set the 4 lowest bits of register RR to VVVV.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1000RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Mov RR, SS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Copy the value of register RR into register SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;100100RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Read [RR]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Read from memory address stored in RR, store the result in register RR.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;100110RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Not RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Logically invert the value of register RR.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;100111RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Inv RR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Negate (one's complement) the value of register RR.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1010RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Add RR, SS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add registers RR and SS, store the result in SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1011RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Mul RR, SS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Multiply registers RR and SS, store the result in SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1100RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;And RR, SS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Logical AND of registers RR and SS, store the result in SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1101RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Or RR, SS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Logical OR of registers RR and SS, store the result in SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1111RRSS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Write RR, [SS]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Write the value of register RR to the memory address stored in register SS.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;It's not very efficient, but it enables a good amount of
computation. How could it be implemented? All the separate opcodes could be
realized as separate logic blocks on a chip. Each of them individually
should be relatively easy to implement. Selecting which block to run (depending
on the opcode) is a bit tricky. The easiest way to handle this is to run them
all, but only enable output to the registers and memory for the single command
that is desired by the input. On every cycle, all possible commands would be
computed simultaneously, but only the desired one would be allowed to write to
registers and memory. Is it efficient? No. Would it work? Yes.&lt;/p&gt;
&lt;p&gt;Finally, we can address the problem of reading instructions from memory. Given
the system described in the previous paragraphs, it shouldn't be too hard to
add a separate component that reads instructions from memory and feeds it to
this computation engine. The two components would communicate via an
instruction-pointer register. The instruction set could be expanded to
include (conditional) jumps, making the overall system Turing complete.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;There are several small problems with what I've described, e.g. how to
deal with instructions that consume multiple clock cycles, but all of them
are solvable without too much trouble.&lt;/p&gt;
&lt;p&gt;Thinking this topic is an interesting exercise. On the transistor level, it's
hard to see how a real processor could ever be constructed from these
primitives. Possible in principle – but hard to see how to do in practice.
Three levels of abstractions above, after gates and memory cells there are
suddenly memory blocks that are addressable via a parallel protocol. Every
abstraction step is comprehensible, yet complexity is built up quickly. Two
levels of abstraction further we suddenly have an 8-bit microprocessor.&lt;/p&gt;
&lt;p&gt;It must have been an exciting opportunity to figure all of this out in the
middle of the last century.&lt;/p&gt;</content><category term="blog"></category><category term="hardware"></category></entry><entry><title>Boost Range Highlights</title><link href="https://www.caichinger.com/blog/2016/05/07/boost_range_highlights" rel="alternate"></link><published>2016-05-07T00:00:00+02:00</published><updated>2016-05-07T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-05-07:/blog/2016/05/07/boost_range_highlights</id><summary type="html">&lt;p&gt;Exploring the most interesting and useful parts of Boost Range, a C++ library that wraps begin/end iterators in convenient "range" objects.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Last week, I presented &lt;a href="https://caichinger.com/boost-range"&gt;Boost Range for Humans&lt;/a&gt;: documentation for the
&lt;a href="http://www.boost.org/doc/libs/1_60_0/libs/range/doc/html/index.html"&gt;Boost Range&lt;/a&gt; library with concrete code examples. This week we'll
talk about some of the cool features in Boost Range.&lt;/p&gt;
&lt;h3&gt;irange&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://caichinger.com/boost-range/boost-irange.html"&gt;&lt;code&gt;boost::irange()&lt;/code&gt;&lt;/a&gt; is the C++ equivalent to Python's
&lt;a href="https://docs.python.org/3/library/functions.html#func-range"&gt;&lt;code&gt;range()&lt;/code&gt;&lt;/a&gt; function. It returns a range object containing an
arithmetic series of numbers:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;irange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;irange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Together with &lt;a href="https://caichinger.com/boost-range/boost-adaptors-indexed.html"&gt;&lt;code&gt;indexed()&lt;/code&gt;&lt;/a&gt; (see below), it serves as a range-based
alternative to the classic C &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;
&lt;h3&gt;combine&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://caichinger.com/boost-range/boost-combine.html"&gt;&lt;code&gt;boost::combine()&lt;/code&gt;&lt;/a&gt; takes two or more input ranges and creates a
zipped range – a range of tuples where each tuple contains corresponding
elements from each input range.&lt;/p&gt;
&lt;p&gt;The input ranges must have equal size.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;abcde&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nl"&gt;zipped&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;tie&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zipped&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Iterates over the pairs (&amp;#39;a&amp;#39;, 1), (&amp;#39;b&amp;#39;, 2), ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;Algorithms&lt;/h3&gt;
&lt;p&gt;Most if not all algorithms from the C++ standard library that apply to
containers (via begin/end iterator pairs) have been wrapped in Boost Range.
Examples include &lt;a href="https://caichinger.com/boost-range/boost-range-copy.html"&gt;&lt;code&gt;copy()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caichinger.com/boost-range/boost-range-remove.html"&gt;&lt;code&gt;remove()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caichinger.com/boost-range/boost-range-sort.html"&gt;&lt;code&gt;sort()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caichinger.com/boost-range/boost-range-count.html"&gt;&lt;code&gt;count()&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://caichinger.com/boost-range/boost-range-find_if.html"&gt;&lt;code&gt;find_if()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Adaptors&lt;/h3&gt;
&lt;p&gt;Adaptors are among the most interesting concepts Boost Range has to offer.&lt;/p&gt;
&lt;p&gt;There are generally two ways to use adaptors, either via function syntax or via
a pipe syntax. While the former is handy for simple cases, while the latter
allows chaining data transformations into an easy-to-read pipeline.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Function-style call:&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nl"&gt;i&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;adaptors&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Pipe-style call:&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nl"&gt;i&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;adaptors&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;filtered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To see the power of the latter syntax, consider a transformation pipeline:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;input_map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;adaptors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_map&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;map_values&lt;/span&gt;
                        &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;filtered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_even&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;transformed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;indexed&lt;/h3&gt;
&lt;p&gt;The &lt;a href="https://caichinger.com/boost-range/boost-adaptors-indexed.html"&gt;&lt;code&gt;boost::adaptors::indexed()&lt;/code&gt;&lt;/a&gt; adapter warrants special mention: it is analogous to
Python's &lt;a href="https://docs.python.org/3/library/functions.html#enumerate"&gt;&lt;code&gt;enumerate()&lt;/code&gt;&lt;/a&gt;. Given a Range, it gives access to the elements as well
as their index within the range. Boost 1.56 or higher is required for this to
work properly.&lt;/p&gt;
&lt;h3&gt;accumulate&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://caichinger.com/boost-range/boost-accumulate.html"&gt;&lt;code&gt;boost::accumulate()&lt;/code&gt;&lt;/a&gt; by default sums all the items in an input
range, but other reduction functions can be supplied as well. Together with
range adapters, this makes map-reduce pipelines easy to write.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;accumulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;multiplies&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;as_literal&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://caichinger.com/boost-range/boost-as_literal.html"&gt;&lt;code&gt;boost::as_literal()&lt;/code&gt;&lt;/a&gt; may be less a highlight and more of a crutch,
but it bears mentioning still. Boost Range functions accept a wide variety of
types, among them strings. C++ style strings (&lt;code&gt;std::string&lt;/code&gt;) always work as
expected, but with character arrays (&lt;code&gt;char[]&lt;/code&gt;), there is an ambiguity as to
whether the argument should be interpreted as array (including the terminal
'\0' character) or as string (excluding the terminator).&lt;/p&gt;
&lt;p&gt;By default, Boost Range treats them as arrays, which they are, after all.
In practice, this is often a pitfall for newcomers. If any string-related range
operations don't work as expected, this is a common reason.&lt;/p&gt;
&lt;p&gt;To force the library to treat character arrays as strings, they can be wrapped
in an &lt;code&gt;as_literal()&lt;/code&gt; call. Alternatively, the C strings can be cast to
&lt;code&gt;std::string&lt;/code&gt; as well.&lt;/p&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;There are several interesting aspects about Boost Range. It plays very well
with C++11's range-based for loops and makes code operating on containers much
easier to write and (most importantly) read. In addition, it makes it possible
to lay out data processing pipelines a lot more clearly.&lt;/p&gt;
&lt;p&gt;Container iteration and modification becomes as easy as it is in modern
scripting languages, which is a huge, huge step for the C++ language.&lt;/p&gt;
&lt;p&gt;Let's hope that C++17 brings similar capabilities in the standard library.
Until then, Boost Range is the way to go, so check out the &lt;a href="https://caichinger.com/boost-range"&gt;docs&lt;/a&gt; and try
it yourself.&lt;/p&gt;</content><category term="blog"></category><category term="boost"></category><category term="c++"></category></entry><entry><title>Boost Range for Humans</title><link href="https://www.caichinger.com/blog/2016/04/30/boost_range_for_humans" rel="alternate"></link><published>2016-04-30T00:00:00+02:00</published><updated>2016-04-30T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-04-30:/blog/2016/04/30/boost_range_for_humans</id><summary type="html">&lt;p&gt;Boost Range is an excellent library that preceded the C++20 std::ranges library. I created documentation and working examples for each Boost Range function.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="http://www.boost.org/doc/libs/1_60_0/libs/range/doc/html/index.html"&gt;Boost Range&lt;/a&gt; encapsulates the common C++ pattern of passing
begin/end iterators around by combining them into into a single &lt;em&gt;range&lt;/em&gt; object.
It makes code that operates on containers much more readable. One wonders why
such functionality was not included in the C++ standard library in the first
place, and indeed, similar ideas could be added to C++17, see &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4128.html"&gt;N4128&lt;/a&gt; and
&lt;a href="https://meetingcpp.com/index.php/tv15/items/14.html"&gt;Ivan Cukic's Meeting C++ presentation&lt;/a&gt;. In my opinion,
Boost Range is something that every C++ programmer should know about.&lt;/p&gt;
&lt;p&gt;The library is reasonably well documented, but I was often missing concrete
code examples and an explicit mention what headers are required for which
function. Since this presumably happens to other people as well, I invested the
time to change that situation.&lt;/p&gt;
&lt;p&gt;Thus, I present &lt;a href="https://caichinger.com/boost-range"&gt;Boost Range for Humans&lt;/a&gt;. It contains working example
code for every function in Boost Range, along with required headers and links
to the official documentation and the latest source code. I hope it will make
Boost Range more accessible and furthers its adoption.&lt;/p&gt;
&lt;p&gt;Next week, we'll look into some of the highlights of what Boost Range can offer.&lt;/p&gt;</content><category term="blog"></category><category term="boost"></category><category term="c++"></category></entry><entry><title>Debugging riddle of the day</title><link href="https://www.caichinger.com/blog/2016/04/23/debugging_riddle_of_the_day" rel="alternate"></link><published>2016-04-23T00:00:00+02:00</published><updated>2016-04-23T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-04-23:/blog/2016/04/23/debugging_riddle_of_the_day</id><summary type="html">&lt;p&gt;A devious debugging puzzle posed by the &lt;code&gt;shm_open()&lt;/code&gt; library call.&lt;/p&gt;</summary><content type="html">&lt;p&gt;One of our services failed to start on a test system (Ubuntu 12.04 on amd64).
The stdout/stderr log streams contained only the string “Permission denied” –
less than helpful. &lt;a href="http://linux.die.net/man/1/strace"&gt;strace&lt;/a&gt; showed that the service tried to create a
file under &lt;code&gt;/run&lt;/code&gt;, which it doesn't have write permissions to. This caused the
it to bail out:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/run/some_service&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;O_RDWR&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;O_CREAT&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;O_NOFOLLOW&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;O_CLOEXEC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mo"&gt;0644&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;-1&lt;/span&gt;
    &lt;span class="n"&gt;EACCES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Permission&lt;/span&gt; &lt;span class="n"&gt;denied&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Grepping the source code and configuration files for &lt;code&gt;/run&lt;/code&gt; didn't turn up
anything that could explain this &lt;code&gt;open()&lt;/code&gt; call. Debugging with &lt;a href="https://www.gnu.org/software/gdb/"&gt;gdb&lt;/a&gt;
gave further hints:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Breakpoint 2, 0x00007ffff73e3ea0 in open64 () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0  0x00007ffff73e3ea0 in open64 () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff7bd69bf in shm_open () from /lib/x86_64-linux-gnu/librt.so.1
#2  0x0000000000400948 in daemonize () at service.cpp:93
#3  0x00000000004009ac in main () at main.cpp:24
(gdb) p (char*)$rdi
$1 = 0x7fffffffe550 &amp;quot;/run/some_service&amp;quot;
(gdb) frame 2
#2  0x0000000000400948 in daemonize () at service.cpp:93
9           int fd = shm_open(fname.c_str(), O_RDWR | O_CREAT, 0644);
(gdb) p fname
$2 = {...., _M_p = 0x602028 &amp;quot;/some_service&amp;quot;}}
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;open("/run/some_service", ...)&lt;/code&gt; was caused by an &lt;code&gt;shm_open("/some_service", ...)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This code is working on other machines, why does it fail on this particular
one? Can you figure it out? Bonus points if you can explain why it is trying to
access &lt;code&gt;/run&lt;/code&gt; and not some other directory. You might find the &lt;code&gt;shm_open()&lt;/code&gt;
&lt;a href="http://linux.die.net/man/3/shm_open"&gt;man page&lt;/a&gt; and &lt;a href="http://code.metager.de/source/xref/lib/eglibc/libc/sysdeps/unix/sysv/linux/shm_open.c"&gt;source code&lt;/a&gt;
helpful.&lt;/p&gt;
&lt;p&gt;I'll be waiting for you.&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="Caaaaaaaaat!" src="https://www.caichinger.com/files/waiting-cat.jpg" title="&amp;lt;a href='https://www.flickr.com/photos/stefantell/354864232'&amp;gt;Photo by Stefan Tell&amp;lt;/a&amp;gt;, &amp;lt;a href='https://creativecommons.org/licenses/by/2.0/'&amp;gt;CC-BY-2.0&amp;lt;/a&amp;gt;"&gt;&lt;div class="img-caption"&gt;&lt;a href="https://www.flickr.com/photos/stefantell/354864232"&gt;Photo by Stefan Tell&lt;/a&gt;, &lt;a href="https://creativecommons.org/licenses/by/2.0/"&gt;CC-BY-2.0&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;The solution is pretty evident after examining the Linux version of
&lt;code&gt;shm_open()&lt;/code&gt;. By default, it tries to create shared memory files under
&lt;code&gt;/dev/shm&lt;/code&gt;. If that doesn't exist, it will pick the first tmpfs mount point
from &lt;code&gt;/proc/mounts&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Ubuntu 12.04, &lt;code&gt;/dev/shm&lt;/code&gt; is a symlink to &lt;code&gt;/run/shm&lt;/code&gt;.
On this machine the symlink was missing, which caused &lt;code&gt;shm_open()&lt;/code&gt; to go
hunting for a tmpfs filesystem, and &lt;code&gt;/run&lt;/code&gt; happened to be the first one in
&lt;code&gt;/proc/mounts&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Re-creating the symlink solved the problem. Why it was missing in the first
place is still unclear. In the aftermath, we're also improving the error
messages in this part of the code to make such issues easier to diagnose.&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category><category term="debugging"></category><category term="linux"></category><category term="ubuntu"></category></entry><entry><title>libconf - a Python reader for libconfig files</title><link href="https://www.caichinger.com/blog/2016/04/16/python_libconf" rel="alternate"></link><published>2016-04-16T00:00:00+02:00</published><updated>2016-04-16T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-04-16:/blog/2016/04/16/python_libconf</id><summary type="html">&lt;p&gt;I wrote a Python library for reading/writing files in &lt;code&gt;libconfig&lt;/code&gt; format. It's available via PyPI and GitHub.&lt;/p&gt;</summary><content type="html">&lt;p&gt;This weekend, I uploaded my first package to PyPI: &lt;a href="https://pypi.python.org/pypi/libconf"&gt;libconf&lt;/a&gt;, a
pure-Python reader for files in &lt;em&gt;libconfig format&lt;/em&gt;. This configuration file
format is reminiscent of JSON and is mostly used in C/C++ projects through the
&lt;a href="http://www.hyperrealm.com/libconfig/"&gt;libconfig&lt;/a&gt; library. It looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;libconfig example&amp;quot;&lt;/span&gt;
   &lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;375&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;210&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;800&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;600&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;capabilities&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;can&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x3A20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;sublist&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;subgroup&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;ok&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
   &lt;span class="nx"&gt;can&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;arrays&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;yes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;True&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are already two Python implementations: &lt;a href="https://github.com/heinzK1X/pylibconfig2"&gt;pylibconfig2&lt;/a&gt; is a pure-Python
reader licensed under &lt;a href="https://tldrlegal.com/license/gnu-general-public-license-v3-%28gpl-3%29"&gt;GPLv3&lt;/a&gt; and &lt;a href="https://github.com/cnangel/python-libconfig"&gt;python-libconfig&lt;/a&gt; provides bindings for the
libconfig C++ library. The first one I didn't like because of it's licensing,
the second one I didn't like because of the more involved installation
procedure. Also, I kind of enjoy writing parsers.&lt;/p&gt;
&lt;p&gt;So, I set down during the easter weekend and wrote libconf. It's a pure-Python
reader for libconfig files with an interface similar to the Python
&lt;a href="https://docs.python.org/3/library/json.html"&gt;&lt;code&gt;json&lt;/code&gt;&lt;/a&gt; module. There are two main methods: &lt;code&gt;load(f)&lt;/code&gt; and
&lt;code&gt;loads(string)&lt;/code&gt;. Both return a &lt;code&gt;dict&lt;/code&gt;-like data-structure that can be indexed
(&lt;code&gt;config['version']&lt;/code&gt;), but supports attribute access as well
(&lt;code&gt;config.version&lt;/code&gt;):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;libconf&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;example.cfg&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;libconf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;window&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;title&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;libconfig example&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;
&lt;span class="s1"&gt;&amp;#39;libconfig example&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It was a fun little project. Creating a recursive descent parser is pretty
straightforward, especially for such a simple file format. Writing
documentation, packaging and uploading to &lt;a href="https://github.com/ChrisAichinger/python-libconf"&gt;GitHub&lt;/a&gt; and
&lt;a href="https://pypi.python.org/pypi/libconf"&gt;PyPI&lt;/a&gt; took longer than coding up the implementation itself.&lt;/p&gt;</content><category term="blog"></category><category term="Python"></category></entry><entry><title>Data access times</title><link href="https://www.caichinger.com/blog/2016/04/06/data_access_times" rel="alternate"></link><published>2016-04-06T00:00:00+02:00</published><updated>2016-04-06T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-04-06:/blog/2016/04/06/data_access_times</id><summary type="html">&lt;p&gt;A graphical overview of data access latencies on modern computers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Recently, I looked up how fast data can be &lt;a href="http://stackoverflow.com/questions/4087280/approximate-cost-to-access-various-caches-and-main-memory"&gt;accessed on modern
computers&lt;/a&gt;. To remember this a bit better, I made a graphic that gives a
nicer (to me) overview than the more common block diagrams:&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="Approximate data access times on 2010-2020 class hardware" src="https://www.caichinger.com/files/data-access-times.svg" title="Approximate data access times on 2010-2020 class hardware"&gt;&lt;div class="img-caption"&gt;Approximate data access times on 2010-2020 class hardware&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;I hope other people find it useful as well.&lt;/p&gt;</content><category term="blog"></category><category term="optimization"></category><category term="hardware"></category></entry><entry><title>Requesting certificates the whirlwind way</title><link href="https://www.caichinger.com/blog/2016/03/30/practical_certificates" rel="alternate"></link><published>2016-03-30T00:00:00+02:00</published><updated>2016-03-30T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-03-30:/blog/2016/03/30/practical_certificates</id><summary type="html">&lt;p&gt;A hands-on tutorial to getting SSL certificates for a web page and installing them.&lt;/p&gt;</summary><content type="html">&lt;p&gt;As a follow-up to &lt;a href="https://www.caichinger.com/blog/2016/03/23/secure_web_overview"&gt;last post&lt;/a&gt;, let's take a closer look at certificates.
Specifically, when you'd want one and how you'd go about obtaining it.&lt;/p&gt;
&lt;h3&gt;When would you need a certificate?&lt;/h3&gt;
&lt;p&gt;The most common reason is to provide access to a service over a secure
connection, e.g. HTTPS for web traffic or IMAPS for emails. This requires a
certificate signed by a CA that's recognized by browsers and operating systems.
Signatures by these CAs indicate control over the internet domain for which the
certificate was issued (e.g. &lt;em&gt;caichinger.com&lt;/em&gt;). They are used to prove to users
that they are talking to the real thing, not to some scammer who hijacked
their internet connection. This kind of certificate is often called "server
certificate".&lt;/p&gt;
&lt;p&gt;Instead of proving the identity of a server, certificates can also be used to
authenticate a client. Unsurprisingly, this is called "client certificate" and
can serve as a more secure alternative to password-based authentication.
For example, &lt;a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html"&gt;Apple Push Notifications&lt;/a&gt; (for sending notifications to
apps on an iPhone) require a certificate signed by Apple to prove that someone
is allowed to send push notifications. Several &lt;a href="https://en.wikipedia.org/wiki/Virtual_private_network"&gt;VPN&lt;/a&gt; technologies also
rely on client certificates for authentication.&lt;/p&gt;
&lt;h3&gt;How do you go about obtaining a certificate?&lt;/h3&gt;
&lt;p&gt;You need to create a &lt;a href="https://en.wikipedia.org/wiki/Certificate_signing_request"&gt;certificate signing request (CSR)&lt;/a&gt; and submit it to
the CA. &lt;a href="https://www.caichinger.com/blog/2016/03/23/secure_web_overview"&gt;Last time&lt;/a&gt; we saw that a certificate consists of a
keypair identifier (hash), metadata, and signatures. A CSR contains only the
first two of these: a hash that uniquely identifies a keypair and metadata
relating to the intended use of the certificate.&lt;/p&gt;
&lt;p&gt;Obviously, this means there can be no CSR without a keypair!&lt;sup&gt;1&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;For most purposes, it's best to start with a new keypair that hasn't been used
anywhere else. So we'll first generate that and then create a CSR for it.&lt;/p&gt;
&lt;p&gt;Those are two pretty simple operations, they just get complicated by the
horrific user interface of OpenSSL. For most people, these commands equate to
mystical incantations that must be recited to work their magic. That's
unfortunate: it just deepens the confusion about how this certificate shebang
is supposed to work. OpenSSL is the most widely available and popular tool for
the job, though, so for better or worse we'll have to put up with it.&lt;/p&gt;
&lt;p&gt;On Linux, OpenSSL should be available from the package manager, if it isn't
installed already. On MacOS, OpenSSL is probably pre-installed, if not, it can
be obtained from &lt;a href="http://brew.sh/"&gt;Homebrew&lt;/a&gt;. Windows binaries can be found
&lt;a href="https://wiki.openssl.org/index.php/Binaries"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Creating a CSR&lt;/h3&gt;
&lt;p&gt;First, generate a keypair:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;openssl genrsa -out certtest.key 2048&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will write a new keypair to the file "certtest.key". The number 2048 is
the desired key size, with larger keys being more secure but also slower. As of
2016, 2048 bits is a reasonable default.&lt;/p&gt;
&lt;p&gt;Next create a CSR for that keypair:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;openssl req -new -sha256 -key certtest.key -out certtest.csr&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;OpenSSL will ask some questions to set the CSR metadata. For server
certificates, the crucial field is "Common Name", which has to be the internet
address of the server the certificate is intended for. The other fields are for
informational purposes only. You may set them, or you may write a single dot
(".") to leave the field blank. Not typing anything at all at the question
prompts will give you OpenSSL's default values, don't do that.&lt;/p&gt;
&lt;p&gt;Here is a screen capture of the process:&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="csr-generation-graphic" src="https://www.caichinger.com/files/csr-creation.gif" title="Screen capture of creating a CSR - it's super simple!"&gt;&lt;div class="img-caption"&gt;Screen capture of creating a CSR - it's super simple!&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;Before submitting the CSR for signing, it's a good practice to re-check the
metadata fields:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;openssl req -noout -text -in certtest.csr&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This outputs several lines of data, the important part is the "Subject" line at
the very top. Check that all the fields are what you expect them to be.&lt;/p&gt;
&lt;h3&gt;Submitting the CSR for signing&lt;/h3&gt;
&lt;p&gt;The details of submitting a CSR for signing vary from CA to CA, typically it
involves an upload on a web page. Depending on the intended use of the
certificate, additional steps may be required, such as proving your real-life
identity and/or proving that you have control over some domain (for server
certificates). Fortunately, these steps are typically quite thoroughly
described in the CA's documentation or on the internet.&lt;/p&gt;
&lt;p&gt;After the submission, it may take some time for the certificate to be signed,
then you can download it from the CA's web page.&lt;/p&gt;
&lt;p&gt;Pretty simple, right?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; For users of the Apple Keychain application this can be a bit
confusing, because there is an assistant to generate CSR's, but it doesn't
mention keypairs anywhere. Under the hood, it does create a new keypair and add
it to the keychain, it just doesn't tell you.&lt;/p&gt;</content><category term="blog"></category><category term="cryptography"></category></entry><entry><title>A whirlwind introduction to the secure web</title><link href="https://www.caichinger.com/blog/2016/03/23/secure_web_overview" rel="alternate"></link><published>2016-03-23T00:00:00+01:00</published><updated>2016-03-23T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-03-23:/blog/2016/03/23/secure_web_overview</id><summary type="html">&lt;p&gt;A conceptual overview of how HTTPS ensures the security of the connection.&lt;/p&gt;</summary><content type="html">&lt;p&gt;How secure internet connections work is often a mystery, even for fairly
technical people &amp;ndash; let's rectify this!&lt;/p&gt;
&lt;p&gt;Although some fairly complicated mathematics lays the foundation, it's not
necessary to grasp all of it to get a good high-level understanding of the
secure web. In this post, I'll give an overview of the major components of
the secure web and of how they interact. I want to shed some light on the
wizardry that both your browser and webservers do to make secure communication
over the internet possible. Specifically, the focus is on &lt;em&gt;authentication&lt;/em&gt;: how
the &lt;a href="https://en.wikipedia.org/wiki/Public_key_infrastructure"&gt;public key infrastructure (PKI)&lt;/a&gt; guarantees you that you are really
talking to your bank not to some scammer.&lt;/p&gt;
&lt;p&gt;This is going to be a high-level overview with a lot of handwaving. We'll gloss
over some of the nitty gritty details, and focus on the big picture.&lt;/p&gt;
&lt;h2&gt;Keypairs&lt;/h2&gt;
&lt;p&gt;Let's start off with the basic building block of &lt;a href="https://en.wikipedia.org/wiki/Public-key_cryptography"&gt;public-key
cryptography&lt;/a&gt;, the public/private keypair. It consists of
two halves: the public part can be shared with the world, the private half must
stay hidden, for your eyes only. With such a keypair, you can do three pretty
nifty things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;People can &lt;em&gt;encrypt&lt;/em&gt; data with your public key, and only you can decrypt it.&lt;/li&gt;
&lt;li&gt;You can prove to other people (who have your public key) that you know the
  private key. In some sense, they can &lt;em&gt;verify&lt;/em&gt; your identity.&lt;/li&gt;
&lt;li&gt;You can &lt;em&gt;sign&lt;/em&gt; data with your private key, and everyone with the public key
  can verify that this data came from you and was not tempered with.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let's look at an example to see how cool this is: let's say your bank has a
keypair and you know their public key (it could be printed in large letters on
the walls of their building, it's public after all). You can then securely
communicate with your bank, without anyone being able to listen in, even if
they can intercept or modify the messages between you and the bank
(encryption). You can be sure you really are talking to the bank, and not to a
fraudster (verification). And the bank can make statements that anyone who has
their public key can ascertain is genuine, i.e. it really came from the bank
(signing).&lt;/p&gt;
&lt;p&gt;The last part is nice because the bank can sign a statement about your balance,
give it to you, and you can forward it to your sleazy landlord who wants proof
of your financial situation. The bank and the landlord never directly talk
with each other, nevertheless the latter has full certainty that the statement
was made by the bank, and that you didn't tamper with it.&lt;/p&gt;
&lt;p&gt;So it's cool that we can securely communicate with our banks. We can do the
same with websites: once we have the public key of e.g. Google, it's easy to
setup an encrypted communication channel. Via the verification function of
keypairs, it's also easy to prove we really are talking to that Google, and
not to some kid who's trying to steal our password to post dog pictures in our
cat groups.&lt;/p&gt;
&lt;p&gt;How do we get Google's public key? &amp;mdash; This is where things start going
downhill.&lt;/p&gt;
&lt;p&gt;In the bank example, we'd gotten the public key personally from the bank
(written on its front wall). With Google, it'd be kind of difficult to
travel to Mountain View just to get get their public key. And we can't just go and
download the key from &lt;em&gt;google.com&lt;/em&gt;, the whole point is that we're not sure
that the &lt;em&gt;google.com&lt;/em&gt; we're talking to is the real Google.&lt;/p&gt;
&lt;p&gt;Are we completely out of luck? Can we communicate securely over the internet
only if we manually exchange keys before, which we usually can't? It turns out
we are only sort-of out of luck: we are stuck with certificates and the
halfway-broken system of certificate authorities.&lt;/p&gt;
&lt;h2&gt;Certificates&lt;/h2&gt;
&lt;p&gt;A certificate contains several parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an identifier (a hash) that uniquely identifies a keypair&lt;/li&gt;
&lt;li&gt;metadata that says who this keypair belongs to&lt;/li&gt;
&lt;li&gt;signatures: statements signed by other keys that vouch that the keypair
  referenced here really belongs to the entity described in the metadata section&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It's important to note that certificates don't need to be kept secret. The
keypair identifier doesn't reveal the private key, so certificates can be
shared freely. The corollary of this is that a certificate alone can't be used
to verify you're talking to anyone in particular. To be used for
authentification, it needs to be paired with the associated private key.&lt;/p&gt;
&lt;p&gt;With that out of the way, let's look at why certificates are useful. Say
someone gives you a certificate and proves they have the associated private
key. You've never met this person. However, the certificate carries signatures
from several keys that you know belong to close friends of yours. All of those
signatures attest that this person is called "Hari Seldon". If you trust your
friends, you can be pretty certain that the person is really called that way.&lt;/p&gt;
&lt;p&gt;When you think about this, it's kind of neat. A stranger can authenticate to
you (prove that they say who they are) just because someone you trust made a
statement confirming the stranger's identity. That this statement is really
coming from your trusted friend is ensured, because it's signed with their
private key.&lt;/p&gt;
&lt;p&gt;The same concept can be applied websites. As long as there's someone you trust
and you have their public key, they can sign other people's certificates to
affirm that identity to you. For  example, they can sign a certificate for
Google that says "This really is the real &lt;em&gt;google.com&lt;/em&gt;". When you see that
certificate and verify that the other party has the associated private key,
you'll have good reason to believe that you really are talking to Google's
&lt;em&gt;google.com&lt;/em&gt; server, not some scam version by a North Korean hacker.&lt;/p&gt;
&lt;h2&gt;Certificate Authorities&lt;/h2&gt;
&lt;p&gt;So how do you find someone you can trust? And how does that person make sure
that the certificate they are signing really belongs to Google? They face the
same problems confirming that fact as you did! Does this even improve the
situation in any way?&lt;/p&gt;
&lt;p&gt;It does &amp;ndash; let's take the questions in order. The reality on the internet is:
it's not &lt;em&gt;you&lt;/em&gt; trusting someone, it's your &lt;em&gt;browser&lt;/em&gt; that does the trusting.
Your browser includes public keys from so-called "&lt;a href="https://en.wikipedia.org/wiki/Certificate_authority"&gt;certificate
authorities&lt;/a&gt;" (CA's).  You can find the list of CA's trusted by your own
browser in its options, under Advanced / Security / Certificates / Authorities.
If the browser sees certificates signed by any one of these keys, it believes
them to be true. It trusts CA's not to sign any bogus certificates.&lt;/p&gt;
&lt;p&gt;Why are these keys trustworthy? Because CA's are mostly operated by large
companies that have strict policies in place to make sure they only sign stuff
that's legit. How do they do that? After all, as an individual you'd have a
pretty tough time verifying that the public key offered by &lt;em&gt;google.com&lt;/em&gt;
really belongs to Google. Don't CA's face the same problem?&lt;/p&gt;
&lt;p&gt;Not really. There are billions of people accessing &lt;em&gt;google.com&lt;/em&gt;. There are
only about 200 CA's that are trusted by the common browsers. And Google needs
a signed certificate by only one of them (one signature is enough to earn the
browser's trust). So Google can afford to prove it's identity to a CA: by
sending written letters, a team of lawyers, or whatever. Once Google gets a
certificate for &lt;em&gt;google.com&lt;/em&gt; signed by any reputable CA, it is recognized by
pretty much every device in the world.&lt;/p&gt;
&lt;p&gt;Similarly, I, as a private person, can get a certificate for &lt;em&gt;caichinger.com&lt;/em&gt; by
proving my identity and my ownership of this domain to the CA. The identity
part is usually done by submitting a scan of a driver's license or passport.
Ownership of the domain can be shown by uploading a file supplied by the CA to
the webserver. Once the CA confirms that the file is there, it knows I have
control of that domain.&lt;/p&gt;
&lt;p&gt;So instead of me having to prove my identity to every single user visiting this
website, I can prove it once to a CA, and all browsers coming here will
recognize this as good enough. This way, CA's make the problem of
authentication of servers ("I'm the real &lt;em&gt;google.com&lt;/em&gt;, not a cheap fake")
tractable. It's a system that has made the large-scale deployment of secure
internet traffic via HTTPS possible.&lt;/p&gt;
&lt;h2&gt;The half-broken part&lt;/h2&gt;
&lt;p&gt;Let's get back to the analogy of a stranger authenticating to you via a
certificate signed by someone you know. What if the signature wasn't from a
close friend of yours, but from a seedy guy you meet occasionally when going
out? Would you still have full confidence in the certificate? Hopefully not.&lt;/p&gt;
&lt;p&gt;What does this mean for the web?&lt;/p&gt;
&lt;p&gt;Not all of the CA's included in the common web browsers are the equivalent of a
trusted friend:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They may be in control of some government who wants a certificate for &lt;em&gt;gmail.com&lt;/em&gt;, so it can read dissident's emails&lt;/li&gt;
&lt;li&gt;An employee with access to the certificate authority key may create certificates for bank websites and sell them on the black market&lt;/li&gt;
&lt;li&gt;The computer network where the CA keys are stored could have been hacked&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I'm pretty sure all three of those have actually happened in the past. Given
that a single forged certificate can be used to attack millions of users, CA's
are juicy targets. As soon as forged certificates are detected in the wild,
they tend to be blacklisted (blocked by browsers) very quickly, but there is
still a window of vulnerability.&lt;/p&gt;
&lt;p&gt;For this reason, the whole CA system has been questioned over the last few
years, but replacing it does not seem feasible at the moment. There are
techniques (such as &lt;a href="https://developer.mozilla.org/en/docs/Web/Security/Public_Key_Pinning"&gt;public key pinning&lt;/a&gt;) to augment the CA-based
authentication, but it takes time for them to be picked up by website owners.&lt;/p&gt;
&lt;p&gt;While this is a problem, it mostly affects the largest websites (obtaining a
forged certificate is difficult and costly). Together with browser vendors,
they are developing new mitigation techniques against forged certificates. In
the meantime, the rest of us is still pretty well served by the current CA
system, even though it is not perfect.&lt;/p&gt;
&lt;h2&gt;Wrapup&lt;/h2&gt;
&lt;p&gt;So, this is it for an overview of the public key infrastructure that enables
secure connections to internet sites, from the basics of public key
cryptography to certificate authorities. If you want to dig deeper, I recommend
starting with the Wikipedia articles I linked throughout the article. If you
are interested in cryptography in general, I highly recommend Bruce Schneier's
book &lt;a href="https://www.schneier.com/books/applied_cryptography/"&gt;Applied Cryptography&lt;/a&gt;. It's 20 years old now, and still
enormously relevant today.&lt;/p&gt;
&lt;p&gt;I hope this text helps a bit to clear up the confusion associated with public
key cryptography and the secure web. If you liked it, or if you have any
suggestions for improvement, please let me know in the comments!&lt;/p&gt;</content><category term="blog"></category><category term="cryptography"></category></entry><entry><title>dh_virtualenv and long package names (FileNotFound error)</title><link href="https://www.caichinger.com/blog/2016/03/01/dh_virtualenv_and_long_package_names" rel="alternate"></link><published>2016-03-01T00:00:00+01:00</published><updated>2016-03-01T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-03-01:/blog/2016/03/01/dh_virtualenv_and_long_package_names</id><summary type="html">&lt;p&gt;&lt;code&gt;dh_virtualenv&lt;/code&gt; has problems with long install paths. This easily happens if projects live in a deeper directory hierarchy, for example on CI systems.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Interesting tidbit about Linux:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A maximum line length of 127 characters is allowed for the first line in a #! executable shell script.&lt;/p&gt;
&lt;p&gt;&lt;footer&gt; from &lt;a href="http://linux.die.net/man/2/execve"&gt;&lt;code&gt;man execve&lt;/code&gt;&lt;/a&gt;&lt;/footer&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Should be enough, right? Wrong.&lt;/p&gt;
&lt;p&gt;Well, not if you are using &lt;a href="https://github.com/spotify/dh-virtualenv"&gt;&lt;code&gt;dh_virtualenv&lt;/code&gt;&lt;/a&gt; with long package
names, anyway:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;Installing pip...&lt;/span&gt;
&lt;span class="go"&gt;  Error [Errno 2] No such file or directory while executing command /tmp/lo...me/bin/easy_install /usr/share/python-virtualenv/pip-1.1.tar.gz&lt;/span&gt;
&lt;span class="go"&gt;...Installing pip...done.&lt;/span&gt;
&lt;span class="go"&gt;Traceback (most recent call last):&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/bin/virtualenv&amp;quot;, line 3, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class="go"&gt;virtualenv.main()&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/dist-packages/virtualenv.py&amp;quot;, line 938, in main&lt;/span&gt;
&lt;span class="go"&gt;never_download=options.never_download)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/dist-packages/virtualenv.py&amp;quot;, line 1054, in create_environment&lt;/span&gt;
&lt;span class="go"&gt;install_pip(py_executable, search_dirs=search_dirs, never_download=never_download)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/dist-packages/virtualenv.py&amp;quot;, line 643, in install_pip&lt;/span&gt;
&lt;span class="go"&gt;filter_stdout=_filter_setup)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/dist-packages/virtualenv.py&amp;quot;, line 976, in call_subprocess&lt;/span&gt;
&lt;span class="go"&gt;cwd=cwd, env=env)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/subprocess.py&amp;quot;, line 679, in __init__&lt;/span&gt;
&lt;span class="go"&gt;errread, errwrite)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/subprocess.py&amp;quot;, line 1249, in _execute_child&lt;/span&gt;
&lt;span class="go"&gt;raise child_exception&lt;/span&gt;
&lt;span class="go"&gt;OSError: [Errno 2] No such file or directory&lt;/span&gt;
&lt;span class="go"&gt;Traceback (most recent call last):&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/bin/dh_virtualenv&amp;quot;, line 106, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class="go"&gt;sys.exit(main() or 0)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/bin/dh_virtualenv&amp;quot;, line 83, in main&lt;/span&gt;
&lt;span class="go"&gt;deploy.create_virtualenv()&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/dist-packages/dh_virtualenv/deployment.py&amp;quot;, line 112, in create_virtualenv&lt;/span&gt;
&lt;span class="go"&gt;subprocess.check_call(virtualenv)&lt;/span&gt;
&lt;span class="go"&gt;  File &amp;quot;/usr/lib/python2.7/subprocess.py&amp;quot;, line 511, in check_call&lt;/span&gt;
&lt;span class="go"&gt;raise CalledProcessError(retcode, cmd)&lt;/span&gt;
&lt;span class="go"&gt;subprocess.CalledProcessError: Command &amp;#39;[&amp;#39;virtualenv&amp;#39;, &amp;#39;--system-site-packages&amp;#39;, &amp;#39;--setuptools&amp;#39;, &amp;#39;debian/long-package-name/usr/share/python/long-package-name&amp;#39;]&amp;#39; returned non-zero exit status 1&lt;/span&gt;
&lt;span class="go"&gt;make: *** [binary-arch] Error 1&lt;/span&gt;
&lt;span class="go"&gt;dpkg-buildpackage: error: fakeroot debian/rules binary gave error exit status 2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;dh_virtualenv&lt;/code&gt; is used to create Debian packages that include Python
&lt;a href="http://docs.python-guide.org/en/latest/dev/virtualenvs/"&gt;virtualenvs&lt;/a&gt;. It is one of the better ways of packaging
Python software, especially if there are Python dependencies that are not
available in Debian or Ubuntu. When building a &lt;code&gt;.deb&lt;/code&gt; package, it
creates a virtualenv in a location such as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;/&amp;lt;build-directory&amp;gt;/debian/&amp;lt;packagename&amp;gt;/usr/share/python/&amp;lt;packagename&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This virtualenv has several tools under its &lt;code&gt;bin/&lt;/code&gt; directory, and they all have
the absolute path of the virtualenv's Python interpreter hard-coded in their
&lt;code&gt;#!&lt;/code&gt; shebang line:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;#!/&amp;lt;build-directory&amp;gt;/debian/&amp;lt;packagename&amp;gt;/usr/share/python/&amp;lt;packagename&amp;gt;/bin/python
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Given that &lt;code&gt;&amp;lt;build-directory&amp;gt;&lt;/code&gt; often contains the package name as well, it's
easy to overflow the 128 byte limit of the &lt;code&gt;#!&lt;/code&gt; shebang line. In my case, with
a ~30 character package name, the path length grew to 160 characters!&lt;/p&gt;
&lt;p&gt;Consequently, the kernel couldn't find the Python executable anymore, and
running any of the tools from the &lt;code&gt;bin/&lt;/code&gt; directory gave an &lt;code&gt;ENOENT&lt;/code&gt; (file not
found) error. This is what happened when virtualenv tried to install &lt;code&gt;pip&lt;/code&gt;
during the initial setup. The root cause of this error is not immediately obvious,
to say the least.&lt;/p&gt;
&lt;p&gt;To check whether this affects you, check the line length of any script with &lt;code&gt;wc&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;head -n 1 /path/to/virtualenv/bin/easy_install | wc -c&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If that's larger than 128, it's probably the cause of the problem.&lt;/p&gt;
&lt;p&gt;The fix is to change the package name and/or the build location
to something shorter. The alternative would be to patch the Linux kernel,
which &amp;ndash; depending on your preferences &amp;ndash; sounds either fun
or really unpleasant. Suit yourself!&lt;/p&gt;</content><category term="blog"></category><category term="python"></category><category term="linux"></category></entry><entry><title>Behold the power of perf-tools</title><link href="https://www.caichinger.com/blog/2016/02/10/perf-tools" rel="alternate"></link><published>2016-02-10T00:00:00+01:00</published><updated>2016-02-10T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-02-10:/blog/2016/02/10/perf-tools</id><summary type="html">&lt;p&gt;Quick introduction to opensnoop and execsnoop.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="https://github.com/brendangregg/perf-tools"&gt;perf-tools&lt;/a&gt; is a collection of scripts for system-wide tracing on Linux.
It's really, really cool. It's what the &lt;a href="https://perf.wiki.kernel.org/index.php/Tutorial"&gt;perf&lt;/a&gt; command should have included
from day one, but didn't.&lt;/p&gt;
&lt;p&gt;It is packaged in Debian and Ubuntu, but those versions miss some key features.
As &lt;a href="https://github.com/brendangregg/perf-tools"&gt;perf-tools&lt;/a&gt; consists of shell scripts (no compilation necessary), I
recommend using the GitHub version directly:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;git clone https://github.com/brendangregg/perf-tools.git&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Two tools that are included are &lt;code&gt;execsnoop&lt;/code&gt; and &lt;code&gt;opensnoop&lt;/code&gt;, which trace new
program executions and &lt;code&gt;open()&lt;/code&gt; calls across the whole system.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt; sudo ./execsnoop
&lt;span class="go"&gt;TIME        PID   PPID ARGS&lt;/span&gt;
&lt;span class="go"&gt;21:12:56  22898  15674 ls --color=auto -la&lt;/span&gt;
&lt;span class="go"&gt;21:12:56  22899  15674 git rev-parse --is-inside-work-tree&lt;/span&gt;
&lt;span class="go"&gt;21:12:56  22900  15674 git rev-parse --git-dir&lt;/span&gt;
&lt;span class="go"&gt;...&lt;/span&gt;

&lt;span class="gp"&gt;$&lt;/span&gt; sudo ./opensnoop
&lt;span class="go"&gt;Tracing open()s. Ctrl-C to end.&lt;/span&gt;
&lt;span class="go"&gt;COMM             PID      FD FILE&lt;/span&gt;
&lt;span class="go"&gt;opensnoop        22924   0x3 /etc/ld.so.cache&lt;/span&gt;
&lt;span class="go"&gt;gawk             22924   0x3 /usr/lib/locale/locale-archive&lt;/span&gt;
&lt;span class="go"&gt;top              15555   0x8 /proc/1/stat&lt;/span&gt;
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Maybe the most interesting tool is &lt;code&gt;uprobe&lt;/code&gt;. It's magic: it traces function
calls in arbitrary user-space programs. With debugging symbols available,
it can trace practically every function in a program. Without them, it can trace
exported functions or arbitrary code locations (specified by raw address).
It can also trace library code, e.g. libc). Having these possibilities on
a production system without any prior setup is staggering.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt; sudo user/uprobe -F -l /tmp/a.out &lt;span class="p"&gt;|&lt;/span&gt; grep quicksort
&lt;span class="go"&gt;_Z9quicksortN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES5_&lt;/span&gt;
&lt;span class="gp"&gt;$&lt;/span&gt; sudo user/uprobe -F p:/tmp/a.out:_Z9quicksortN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES5_
&lt;span class="go"&gt;Tracing uprobe _Z9quicksort[snip] (p:_Z9quicksort[snip] /tmp/a.out:0x8ba). Ctrl-C to end.&lt;/span&gt;
&lt;span class="go"&gt;   a.out-23171 [000] d... 1860355.891238: _Z9quicksort[snip]: (0x80488ba)&lt;/span&gt;
&lt;span class="go"&gt;   a.out-23171 [000] d... 1860355.891353: _Z9quicksort[snip]: (0x80488ba)&lt;/span&gt;
&lt;span class="go"&gt;   ...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;(To demangle the C++ function names, use the &lt;a href="https://sourceware.org/binutils/docs/binutils/c_002b_002bfilt.html"&gt;&lt;code&gt;c++filt&lt;/code&gt;&lt;/a&gt; tool.)&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/brendangregg/perf-tools"&gt;perf-tools&lt;/a&gt; really shows the power of the Linux perf/ftrace infrastructure, and
make it usable for the broad masses. There are several other tools that analyze
latency and cache hit rates, trace kernel functions, and much more. To finally
have such functionality in Linux is fabulous!&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category></entry><entry><title>Running strace for multiple processes</title><link href="https://www.caichinger.com/blog/2016/02/09/strace_multiple_processes" rel="alternate"></link><published>2016-02-09T00:00:00+01:00</published><updated>2016-02-09T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-02-09:/blog/2016/02/09/strace_multiple_processes</id><summary type="html">&lt;p&gt;Strace can be attached to multiple processes at once. This is handy when investigating multi-process web servers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Just a quick note about &lt;a href="http://linux.die.net/man/1/strace"&gt;strace&lt;/a&gt;, the ancient Linux system-call tracing tool.&lt;/p&gt;
&lt;p&gt;It can trace multiple processes at once: simply start it with multiple &lt;code&gt;-p&lt;/code&gt;
arguments (the numbers give the processes' PIDs):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;sudo strace -p 2916 -p 2929 -p 2930 -p 2931 -p 2932 -o /tmp/strace.log&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is great for tracing daemons which use separate worker processes, for
example Apache with &lt;code&gt;mpm-prefork&lt;/code&gt;.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category></entry><entry><title>Plotting maps with Folium</title><link href="https://www.caichinger.com/blog/2016/01/27/plotting_maps_with_folium" rel="alternate"></link><published>2016-01-27T00:00:00+01:00</published><updated>2016-01-27T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-01-27:/blog/2016/01/27/plotting_maps_with_folium</id><summary type="html">&lt;p&gt;Folium is a nice Python library for generating maps on web pages. It's pretty and easy to get into.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Data visualization in Python is a well solved problem by now. &lt;a href="http://matplotlib.org/"&gt;Matplotlib&lt;/a&gt; and
it's prettier cousin &lt;a href="https://stanford.edu/~mwaskom/software/seaborn/"&gt;Seaborn&lt;/a&gt; are widely used to generate static graphs.
&lt;a href="http://bokeh.pydata.org/en/latest/"&gt;Bokeh&lt;/a&gt; generates HTML files with interactive, JavaScript-based graphs. It's
a great way of sharing data with other people who don't have a Python
development environment ready. &lt;a href="https://github.com/wrobstory/vincent"&gt;Several&lt;/a&gt; &lt;a href="https://github.com/wrobstory/bearcart"&gt;other&lt;/a&gt;
&lt;a href="https://networkx.github.io/documentation/latest/reference/drawing.html"&gt;libraries&lt;/a&gt; &lt;a href="https://ioam.github.io/holoviews/"&gt;exist&lt;/a&gt; for more specialized purposes.&lt;/p&gt;
&lt;p&gt;What has been missing for a long time was good map libraries. Plotting
capabilities were fine, but basemap support of the existing libraries was very
limited. For example, the popular &lt;a href="http://matplotlib.org/basemap/users/examples.html"&gt;Matplotlib-basemap&lt;/a&gt; has great plot
types (contour maps, heatmaps, ...) but can't show any high-resolution
maps: it only has country/state shapes or whole-world images. Consequently,
it's useless for drawing city or street level maps, unless you want to set up
your own tile server (you don't).&lt;/p&gt;
&lt;p&gt;Along comes &lt;a href="https://github.com/python-visualization/folium"&gt;Folium&lt;/a&gt;, a library that generates interactive maps in HTML format
based on &lt;a href="http://leafletjs.com/"&gt;Leaflet.js&lt;/a&gt;. It supports, among others, &lt;a href="https://www.openstreetmap.org/"&gt;OpenStreetMap&lt;/a&gt; and
&lt;a href="https://www.mapbox.com/maps/"&gt;MapBox&lt;/a&gt; base layers which look great and provide enough details for
large-scale maps.&lt;/p&gt;
&lt;p&gt;Here is an example that shows some GPS data I cleaned up with a &lt;a href="http://www.bzarg.com/p/how-a-kalman-filter-works-in-pictures/"&gt;Kalman filter&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;map_osm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;folium&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zoom_start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_zoom&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;map_osm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locations&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;map_osm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;folium-example.html&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's what it looks like. I find it pretty neat, especially given that
it took only 3 lines of code to create:&lt;/p&gt;
&lt;iframe style="width:100%; height: 350px; padding: 0.8em;"
        src="/files/folium-example.html" frameborder="0"&gt;&lt;/iframe&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;</content><category term="blog"></category><category term="python"></category><category term="plotting"></category></entry><entry><title>Mixing C runtime library versions</title><link href="https://www.caichinger.com/blog/2016/01/01/mixing_crt_versions" rel="alternate"></link><published>2016-01-01T00:00:00+01:00</published><updated>2016-01-01T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2016-01-01:/blog/2016/01/01/mixing_crt_versions</id><summary type="html">&lt;p&gt;When compiling code in Visual Studio, it's easy to accidentally link against multiple versions of the C Runtime Library. This is hard to spot, and can create issues that are challenging to debug..&lt;/p&gt;</summary><content type="html">&lt;p&gt;When compiling code with Microsoft's Visual C/C++ compiler, a
dependency on Microsoft's C runtime library (CRT) is introduced. The CRT can be
linked either statically or dynamically, and comes in several versions
(different version numbers as well as in debug and release variants).&lt;/p&gt;
&lt;p&gt;Complications arise when libraries linked into the same program use
different CRTs. This happens if they were compiled with different compiler
versions, or with different compiler flags (static/dynamic CRT linkage or
release/debug switches). In theory, this could be made to work, but in practice
it is asking for trouble:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the CRT versions differ (either version number or debug/release flag), you
  can't reliably share objects generated by CRT A with any code that uses CRT
  B. The reason is that the two CRTs may have a different memory
  layout (structure layout) for the that object. The memory location that CRT A
  wrote the object size to might be interpreted by CRT B as a pointer, leading
  to a crash when CRT B tries to access that memory.&lt;/li&gt;
&lt;li&gt;Even if the same CRT version is included twice (once statically, once
  dynamically linked), they won't share a heap. Both CRTs track the memory they
  allocate individually, but they don't know anything about objects allocated
  by the other CRT. If CRT A tries to free memory allocated by CRT B, that
  causes heap corruption as the two CRTs trample on each others feet. While you
  can freely share objects between CRTs, you have to be careful whenever memory
  is allocated or freed. This can sometimes be managed when writing C code, but
  is very hard to do correctly in C++ (where e.g. pushing to a vector can cause
  a reallocation of its internal buffer).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Accordingly, having multiple CRTs in the same process is fragile at best.
When mixing CRTs, there are no tools to check whether objects are shared in a
way that's problematic, and manual tracking is subtle, easy to get wrong, and
unreliable. Mistakes will lead to difficult-to-diagnose bugs and intermittent
crashes, generally at the most inconvenient times.&lt;/p&gt;
&lt;p&gt;To keep your sanity, ensure that all code going into your program
uses the same CRT.&lt;sup&gt;1&lt;/sup&gt; Consequently, all program code, &lt;em&gt;as well as all
libraries&lt;/em&gt;, need to be compiled from scratch using the same &lt;a href="https://msdn.microsoft.com/en-us/library/19z1t1wy.aspx"&gt;runtime library
options&lt;/a&gt; (&lt;code&gt;/MD&lt;/code&gt; or &lt;code&gt;/MT&lt;/code&gt;). Pre-compiled libraries are a major
headache, because they force the use of a specific compiler version to get
matching CRT version requirements. If multiple pre-compiled libraries use
different CRT versions, there may not be any viable solution at all.&lt;/p&gt;
&lt;p&gt;This situation will improve with the &lt;a href="http://blogs.msdn.com/b/vcblog/archive/2014/06/10/the-great-crt-refactoring.aspx"&gt;runtime library refactoring&lt;/a&gt; in
VS2015, which promises CRT compatibility to subsequent compiler versions. Thus,
this inconvenience should mostly be solved in the future.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; &lt;a href="http://www.dependencywalker.com/"&gt;Dependency Walker&lt;/a&gt; can be used to list all dynamically linked
CRT versions. I'm not sure whether that can be done for statically linked CRTs,
I generally avoid those.&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category><category term="windows"></category></entry><entry><title>Debian's most annoying warning message: "Setting locale failed"</title><link href="https://www.caichinger.com/blog/2015/12/03/debian_locales" rel="alternate"></link><published>2015-12-03T00:00:00+01:00</published><updated>2015-12-03T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-12-03:/blog/2015/12/03/debian_locales</id><summary type="html">&lt;p&gt;The Perl "Setting locale failed." comes up frequently when working with non-standard locales, especially when creating chroots/Docker containers/virtual machines. Fortunately, the warning message is easy to fix with &lt;code&gt;locale-gen&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">&lt;p&gt;You ssh into a server or you enter a chroot, and the console overflows with
these messages:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;perl: warning: Setting locale failed.&lt;/span&gt;
&lt;span class="go"&gt;perl: warning: Please check that your locale settings:&lt;/span&gt;
&lt;span class="go"&gt;        LANGUAGE = &amp;quot;en_US:en&amp;quot;,&lt;/span&gt;
&lt;span class="go"&gt;        LC_ALL = (unset),&lt;/span&gt;
&lt;span class="go"&gt;        LC_TIME = &amp;quot;en_US.utf8&amp;quot;,&lt;/span&gt;
&lt;span class="go"&gt;        LC_CTYPE = &amp;quot;de_AT.UTF-8&amp;quot;,&lt;/span&gt;
&lt;span class="go"&gt;        LC_COLLATE = &amp;quot;C&amp;quot;,&lt;/span&gt;
&lt;span class="go"&gt;        LC_MESSAGES = &amp;quot;en_US.utf8&amp;quot;,&lt;/span&gt;
&lt;span class="go"&gt;        LANG = &amp;quot;de_AT.UTF-8&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;    are supported and installed on your system.&lt;/span&gt;
&lt;span class="go"&gt;perl: warning: Falling back to the standard locale (&amp;quot;C&amp;quot;).&lt;/span&gt;
&lt;span class="go"&gt;locale: Cannot set LC_CTYPE to default locale: No such file or directory&lt;/span&gt;
&lt;span class="go"&gt;locale: Cannot set LC_MESSAGES to default locale: No such file or directory&lt;/span&gt;
&lt;span class="go"&gt;locale: Cannot set LC_ALL to default locale: No such file or directory&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Gaaaaarrrrh!&lt;/p&gt;
&lt;p&gt;Fortunately the fix is easy (adjust the locale names for your situation):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;locale-gen en_US.UTF-8 de_AT.UTF-8
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, peace on the console.&lt;/p&gt;</content><category term="blog"></category><category term="debian"></category><category term="ubuntu"></category></entry><entry><title>Profiling</title><link href="https://www.caichinger.com/blog/2015/11/28/profiling" rel="alternate"></link><published>2015-11-28T00:00:00+01:00</published><updated>2015-11-28T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-11-28:/blog/2015/11/28/profiling</id><summary type="html">&lt;p&gt;A deeper look into application profiling on Linux. There are many options, but the tools are often difficult to use, and the results need careful interpretation. Often the "poor man's profiler" is a good short-hand.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Profiling is hard. Measuring the right metric and correctly interpreting the
obtained data can be difficult even for relatively simple programs.&lt;/p&gt;
&lt;p&gt;For performance optimization, I'm a big fan of the &lt;a href="http://stackoverflow.com/a/378024/25097"&gt;poor man's
profiler&lt;/a&gt;: run the binary to analyze under a debugger,
periodically stop the execution, get a backtrace and continue. After doing this
a few times, the hotspots will become apparent. This works amazingly well in
practice and gives a reliable picture of where time is spent, without the
danger of skewed results from instrumentation overhead.&lt;/p&gt;
&lt;p&gt;Sometimes it's nice to get a more fine-grained view.
That is, not only find the hotspot, but get an overview how much time
is spent where. That's where 'real' profilers come in handy.&lt;/p&gt;
&lt;p&gt;Under Windows, I like the built-in "Event Tracing for Windows" (ETW),
which produces files that can be analyzed with Xperf/Windows Performance
Analyzer. It is a really well thought out system, and the Xperf UI is amazing
in the analyzing abilities that it offers. Probably the best place to start
reading up on this is &lt;a href="https://randomascii.wordpress.com/2015/09/24/etw-central/"&gt;ETW Central&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Under Linux, I haven't found a profiler I can really recommend, yet. &lt;a href="https://sourceware.org/binutils/docs/gprof/"&gt;gprof&lt;/a&gt;
and &lt;a href="http://man7.org/linux/man-pages/man1/sprof.1.html"&gt;sprof&lt;/a&gt; are both ancient and have severe limitations. &lt;a href="http://oprofile.sourceforge.net/examples/"&gt;OProfile&lt;/a&gt;
may be nice, but I haven't had a chance to use it yet, as it wasn't available
for my Ubuntu LTS release.&lt;/p&gt;
&lt;p&gt;I have used &lt;a href="http://valgrind.org/docs/manual/cl-manual.html"&gt;Callgrind&lt;/a&gt; from the Valgrind toolkit in combination with the
&lt;a href="http://kcachegrind.sourceforge.net/html/Home.html"&gt;KCachegrind&lt;/a&gt; GUI analyzer. I typically invoke it like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;valgrind --tool&lt;span class="o"&gt;=&lt;/span&gt;callgrind --callgrind-out-file&lt;span class="o"&gt;=&lt;/span&gt;callgrind-cpu.out ./program-to-profile
kcachegrind callgrind-cpu.out
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Callgrind works by instrumenting the binary under test. It slows down
program execution, often by a factor of 10. Further, it only measures CPU time,
so sleeping times are not included. This makes it unsuitable for programs that wait
a significant amount of time for network or disk operations to complete.
Despite these drawbacks, it's pretty handy if CPU time is all that you're
interested in.&lt;/p&gt;
&lt;p&gt;If blocking times are important (as they are for so many modern applications - we
generally spend less time computing and more time communicating),
&lt;a href="https://github.com/gperftools/gperftools"&gt;gperftools&lt;/a&gt; is a decent choice. It includes a CPU profiler that can be run in
real-time sampling mode, and the results can viewed in KCachegrind. It is
recommended to compile &lt;code&gt;libprofiler.so&lt;/code&gt; into the binary to analyze, but using
&lt;code&gt;LD_PRELOAD&lt;/code&gt; works decently well:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;CPUPROFILE_REALTIME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="nv"&gt;CPUPROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;prof.out &lt;span class="nv"&gt;LD_PRELOAD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/lib/libprofiler.so ./program-to-profile
google-pprof --callgrind ./program_to_profile prof.out &amp;gt; callgrind-wallclock.out
kcachegrind callgrind-wallclock.out
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If it works, this gives a good overall profile of the application.
Unfortunately, it sometimes fails: on amd64, there are sporadic crashes
from within libunwind. It's possible to just ignore those and rerun the profile, at
least interesting data is obtained 50% of the time.&lt;/p&gt;
&lt;p&gt;The more serious problem is that &lt;code&gt;CPUPROFILE_REALTIME=1&lt;/code&gt; causes gperftools
to use &lt;code&gt;SIGALARM&lt;/code&gt; internally, conflicting with any applications that want to
use that signal for themselves. Looking at the profiler source code, it should
be possible to work around this limitation with the undocumented
&lt;code&gt;CPUPROFILE_PER_THREAD_TIMERS&lt;/code&gt; and &lt;code&gt;CPUPROFILE_TIMER_SIGNAL&lt;/code&gt; environment
variables, but I couldn't get that to work yet.&lt;/p&gt;
&lt;p&gt;You'd think that &lt;a href="https://perf.wiki.kernel.org/index.php/Tutorial"&gt;perf&lt;/a&gt; has something to offer in this area as well.
Indeed, it has a CPU profiling mode (with nice &lt;a href="http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html"&gt;flamegraph&lt;/a&gt; visualizations)
and a sleeping time profiling mode, but I couldn't find a way to combine the
two to get a real-time profile.&lt;/p&gt;
&lt;p&gt;Overall, there still seems to be room for a good, reliable real-time sampling
profiler under Linux. If I'm missing something, please let me know!&lt;/p&gt;</content><category term="blog"></category><category term="c++"></category><category term="optimization"></category></entry><entry><title>Returning generators from with statements</title><link href="https://www.caichinger.com/blog/2015/11/12/returning_generators_from_with_statements" rel="alternate"></link><published>2015-11-12T00:00:00+01:00</published><updated>2015-11-12T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-11-12:/blog/2015/11/12/returning_generators_from_with_statements</id><summary type="html">&lt;p&gt;Returning a Python generator from a &lt;code&gt;with&lt;/code&gt; statement produces unexpected behavior: the generator executes after the end of the &lt;code&gt;with&lt;/code&gt; block. Look out!&lt;/p&gt;</summary><content type="html">&lt;p&gt;Recently, an interesting issue came up at work that involved a subtle
interaction between context managers and generator functions. Here is some
example code demonstrating the problem:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nd"&gt;@contextlib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contextmanager&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Context manager for some resource&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Resource setup&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
    &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Resource teardown&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Load a list of values (requires resource to be held)&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Generating value &lt;/span&gt;&lt;span class="si"&gt;%d&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Load values while holding the required resource&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_load_values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the output when run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="n"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;
&lt;span class="n"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;teardown&lt;/span&gt;
&lt;span class="n"&gt;Generating&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;Generating&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;Generating&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Whoops. The resource is destroyed before the values are actually generated.
This is obviously a problem if the generator depends on the existence of the
resource.&lt;/p&gt;
&lt;p&gt;When you think about it, it's pretty clear what's going on. Calling
&lt;code&gt;_load_values()&lt;/code&gt; produces a generator object, whose code is only executed when
values are requested. &lt;code&gt;load_values()&lt;/code&gt; returns that generator, exiting the
&lt;code&gt;with&lt;/code&gt; statement and leading to the destruction of the resource. When the outer
&lt;code&gt;for&lt;/code&gt; loop (&lt;code&gt;for val&lt;/code&gt;) comes around to iterating over the generator, the
resource is long gone.&lt;/p&gt;
&lt;p&gt;How do you solve this problem? In Python 3.3 and newer, you can use the &lt;a href="https://docs.python.org/3/whatsnew/3.3.html#pep-380"&gt;&lt;code&gt;yield
from&lt;/code&gt;&lt;/a&gt; syntax to turn &lt;code&gt;load_values()&lt;/code&gt; into a generator as well. The
execution of &lt;code&gt;load_values()&lt;/code&gt; is halted at the &lt;code&gt;yield from&lt;/code&gt; point until the
child generator is exhausted, at which point it is safe to dispose of the
resource:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Load values while holding the required resource&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;yield from&lt;/span&gt; &lt;span class="n"&gt;_load_values&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In older Python versions, an explicit for loop over the child generator is
required:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sd"&gt;&amp;quot;&amp;quot;&amp;quot;Load values while holding the required resource&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_load_values&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Still another method would be to turn the result of &lt;code&gt;_load_values()&lt;/code&gt; into a
&lt;code&gt;list&lt;/code&gt; and returning that instead. This incurs higher memory overhead since all
values have to be held in memory at the same time, so it's only
appropriate for relatively short lists.&lt;/p&gt;
&lt;p&gt;To sum up, it's a bad idea to return generators from under &lt;code&gt;with&lt;/code&gt; statements.
While it's not terribly confusing what's going on, it's a whee bit subtle and
not many people think about this until they ran into the issue. Hope this
heads-up helps.&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>A better way for deleting Docker images and containers</title><link href="https://www.caichinger.com/blog/2015/06/11/a_better_way_to_delete_docker_objects" rel="alternate"></link><published>2015-06-11T00:00:00+02:00</published><updated>2015-06-11T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-06-11:/blog/2015/06/11/a_better_way_to_delete_docker_objects</id><summary type="html">&lt;p&gt;A script to automatically delete obsolete docker images and containers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In one of my &lt;a href="https://www.caichinger.com/blog/2015/05/06/cleaning_up_docker"&gt;last posts&lt;/a&gt;, I described the current (sad) state of
managing &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; container and image expiration. Briefly, Docker creates
new containers and images for many tasks, but there is no good way to
automatically remove them. The best practice seems to be a rather hack-ish
bash one-liner.&lt;/p&gt;
&lt;p&gt;Since this wasn't particularly satisfying, I decided to do something
about it. Here, I present &lt;a href="https://github.com/ChrisAichinger/docker-cleanup"&gt;docker-cleanup&lt;/a&gt;, a Python application
for removing containers and images based on a configurable set of rules.&lt;/p&gt;
&lt;p&gt;This is a rules file example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c"&gt;# Keep currently running containers, delete others if they last finished&lt;/span&gt;
&lt;span class="c"&gt;# more than a week ago.&lt;/span&gt;
KEEP CONTAINER IF Container.State.Running&lt;span class="p"&gt;;&lt;/span&gt;
DELETE CONTAINER IF Container.State.FinishedAt.before&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;1 week ago&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;# Delete dangling (unnamed and not used by containers) images.&lt;/span&gt;
DELETE IMAGE IF Image.Dangling&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Clear, expressive, straight-forward. The rule language can do a whole lot more
and provides a readable and intuitive way to define removal policies for images
and containers.&lt;/p&gt;
&lt;p&gt;Head over to &lt;a href="https://github.com/ChrisAichinger/docker-cleanup"&gt;GitHub&lt;/a&gt;, give it a try, and let me know what you
think!&lt;/p&gt;</content><category term="blog"></category><category term="docker"></category><category term="virtualization"></category><category term="python"></category></entry><entry><title>Using Python slice objects for fun and profit</title><link href="https://www.caichinger.com/blog/2015/06/04/using_slice_objects_for_fun_and_profit" rel="alternate"></link><published>2015-06-04T00:00:00+02:00</published><updated>2015-06-04T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-06-04:/blog/2015/06/04/using_slice_objects_for_fun_and_profit</id><summary type="html">&lt;p&gt;Getting familiar with Python's slice notation.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Just a quick tip about the hardly known &lt;a href="https://docs.python.org/3/reference/datamodel.html#frame-objects"&gt;&lt;code&gt;slice&lt;/code&gt;&lt;/a&gt; objects in Python.
They are used to implement the slicing syntax for sequence types (lists,
strings):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The quick brown fox jumps over the lazy dog&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;# s[4:9] is internally converted (and equivalent) to s[slice(4, 9)].&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;#39;Not present&amp;#39; is encoded as &amp;#39;None&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;slice&lt;/code&gt; object can be used in normal code too, for example for tracking
regions in strings: instead of having separate &lt;code&gt;start_idx&lt;/code&gt; and &lt;code&gt;end_idx&lt;/code&gt;
variables (or writing a custom class/&lt;a href="https://docs.python.org/3/library/collections.html#collections.namedtuple"&gt;&lt;code&gt;namedtuple&lt;/code&gt;&lt;/a&gt;) simply roll
the indices into a &lt;code&gt;slice&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# A column-aligned table:&lt;/span&gt;
&lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;REPOSITORY   TAG      IMAGE ID       CREATED       VIRTUAL SIZE&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="s1"&gt;&amp;#39;&amp;lt;none&amp;gt;       &amp;lt;none&amp;gt;   0987654321AB   2 hours ago   385.8 MB&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="s1"&gt;&amp;#39;chris/web    latest   0123456789AB   2 hours ago   385.8 MB&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;entries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;

&lt;span class="c1"&gt;# Compute the column slices by parsing the header. Gives a list of slices.&lt;/span&gt;
&lt;span class="n"&gt;slices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;find_column_slices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;repo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;sl&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sl&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;slices&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is mostly useful when the indices are computed at runtime and applied to
more than one string.&lt;/p&gt;
&lt;p&gt;More generally, &lt;code&gt;slice&lt;/code&gt; objects encapsulate regions of strings/lists/tuples,
and are an appropriate tool for simplifying code that operates on start/end
indices. They provide a clean abstraction, make the code more straight-forward
and save a bit of typing.&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>A neat Python debugger command</title><link href="https://www.caichinger.com/blog/2015/05/28/neat_python_debugger_command" rel="alternate"></link><published>2015-05-28T00:00:00+02:00</published><updated>2015-05-28T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-05-28:/blog/2015/05/28/neat_python_debugger_command</id><summary type="html">&lt;p&gt;Tips to make the &lt;code&gt;pdb&lt;/code&gt; Python debugger more useful.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="https://docs.python.org/3/library/pdb.html"&gt;&lt;code&gt;pdb&lt;/code&gt;&lt;/a&gt; is a console-mode debugger built into Python. Out of the box,
it has basic features like variable inspection, breakpoints, and stack
frame walking, but it lacks more advanced capabilities.&lt;/p&gt;
&lt;p&gt;Fortunately, it can be customized with a &lt;code&gt;.pdbrc&lt;/code&gt; file in the user's home
directory. Ned Batchelder has several helpful commands in &lt;a href="http://nedbatchelder.com/blog/200704/my_pdbrc.html"&gt;his &lt;code&gt;.pdbrc&lt;/code&gt;
file&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pl&lt;/code&gt;: print local variables&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pi obj&lt;/code&gt;: print the instance variables of &lt;code&gt;obj&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ps&lt;/code&gt;: print the instance variables of &lt;code&gt;self&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Printing instance variables is great for quickly inspecting objects, but it
shows only one half of the picture. What about the class-side of objects?
&lt;a href="https://docs.python.org/3/library/functions.html#property"&gt;Properties&lt;/a&gt; and methods are crucial for understanding what can actually be
done with an object, in contrast to what data it encapsulates.&lt;/p&gt;
&lt;p&gt;Since I couldn't find a readily available pdb command for listing class
contents, I wrote my own:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# Print contents of an object&amp;#39;s class (including bases).&lt;/span&gt;
&lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="bp"&gt;cls&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;reversed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mf"&gt;1.&lt;/span&gt;&lt;span class="vm"&gt;__class__&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="vm"&gt;__mro__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="bp"&gt;cls&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="vm"&gt;__dict__&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;cls&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;%s%-20s&lt;/span&gt;&lt;span class="s2"&gt;= &lt;/span&gt;&lt;span class="si"&gt;%-80.80s&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;%1.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;repr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;pc&lt;/code&gt; lists the contents of an object's class and its base classes. Typically,
these are the properties and methods supported by the object.
It is used like
this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# &amp;#39;proc&amp;#39; is a multiprocessing.Process() instance.&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pdb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;daemon&lt;/span&gt;              &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x036B9A20&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exitcode&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x036B99C0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ident&lt;/span&gt;               &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x036B9A50&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_alive&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;BaseProcess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_alive&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x033E4618&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;                &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;BaseProcess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x033E45D0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;                &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x036B99F0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;                 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x036B9A50&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;                 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;BaseProcess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x033E4A98&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;               &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;BaseProcess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x033E4DB0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminate&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;BaseProcess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminate&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x033E4DF8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note the difference to &lt;code&gt;pi&lt;/code&gt;, which lists the contents of the &lt;code&gt;proc&lt;/code&gt; instance:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pdb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="n"&gt;proc&lt;/span&gt;       &lt;span class="c1"&gt;# In contrast, here is the image dictionary.&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_args&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_config&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;authkey&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="se"&gt;\xd0\xc8\xbd\xd6\xcf\x7f&lt;/span&gt;&lt;span class="s1"&gt;o&lt;/span&gt;&lt;span class="se"&gt;\xab\x19&lt;/span&gt;&lt;span class="s1"&gt;_A6&lt;/span&gt;&lt;span class="se"&gt;\xf8&lt;/span&gt;&lt;span class="s1"&gt;M&lt;/span&gt;&lt;span class="se"&gt;\xd4\xef\x88\xa9&lt;/span&gt;&lt;span class="s1"&gt;;&lt;/span&gt;&lt;span class="se"&gt;\x99&lt;/span&gt;&lt;span class="s1"&gt;c\x9&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_identity&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_kwargs&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_name&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Process-2&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_parent_pid&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1308&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_popen&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_target&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In general, &lt;code&gt;pc&lt;/code&gt; focuses on the &lt;em&gt;interface&lt;/em&gt; while &lt;code&gt;pi&lt;/code&gt; examines the &lt;em&gt;state&lt;/em&gt;
of the object. The two complement each other nicely. Especially when working
with an unfamiliar codebase, &lt;code&gt;pc&lt;/code&gt; is helpful for quickly figuring out how to
use a specific class.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pc&lt;/code&gt; works with both Python 2 and Python 3 (on Python 2 it only shows
&lt;a href="https://www.python.org/doc/newstyle/"&gt;new-style classes&lt;/a&gt;). Add it to your &lt;code&gt;.pdbrc&lt;/code&gt; and give it a try. Let me
know what you think!&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>Python's GIL and atomic reference counting</title><link href="https://www.caichinger.com/blog/2015/05/23/python_atomic_refcounting_slowdown" rel="alternate"></link><published>2015-05-23T00:00:00+02:00</published><updated>2015-05-23T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-05-23:/blog/2015/05/23/python_atomic_refcounting_slowdown</id><summary type="html">&lt;p&gt;A deep dive into Python reference counting and the GIL.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I love Python because it's an incredibly fun, expressive and productive
language. However, it's often criticised for being slow. I think the correct
answer to that is two-fold:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use an alternative Python implementation for impressive
  speed-ups. For example, &lt;a href="http://pypy.org/"&gt;PyPy&lt;/a&gt; is on average &lt;a href="http://speed.pypy.org/"&gt;7 times
  faster&lt;/a&gt; than the standard CPython.&lt;/li&gt;
&lt;li&gt;Not all parts of a program have to be blazingly fast. Use Python for all
  non-performance-critical areas, such as the UI and database access, and
  drop to C or C++ only when it's required for for CPU intensive tasks.
  This is easy to achieve with language binding generators such as &lt;a href="https://cffi.readthedocs.org/en/latest/"&gt;CFFI&lt;/a&gt;,
  &lt;a href="http://www.swig.org/"&gt;SWIG&lt;/a&gt; or &lt;a href="http://pyqt.sourceforge.net/Docs/sip4/introduction.html"&gt;SIP&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A further performance-related issue is Python's &lt;a href="https://wiki.python.org/moin/GlobalInterpreterLock"&gt;Global Interpreter Lock&lt;/a&gt;
(GIL), which ensures that only one Python thread can run at a single time. This
is a bit problematic, because it affects PyPy as well, unless you want to use
its experimental &lt;a href="https://pypy.readthedocs.org/en/latest/stm.html"&gt;software transactional memory&lt;/a&gt; support.&lt;/p&gt;
&lt;p&gt;Why is this such a big deal? With the rise of multi-core processors,
multithreading is becoming more important as well. This not only affects
performance on large servers, it impacts desktop programs and is
crucial for battery life on mobile phones (&lt;a href="https://en.wikipedia.org/wiki/Dynamic_frequency_scaling"&gt;race to idle&lt;/a&gt;). Further, other
programming languages make multi-threaded programming easier and easier. C,
C++, and Java have all moved to a common memory model for multithreading. C++
has gained &lt;a href="http://www.cplusplus.com/reference/atomic/atomic/"&gt;&lt;code&gt;std::atomic&lt;/code&gt;&lt;/a&gt;, &lt;a href="http://www.cplusplus.com/reference/future/"&gt;futures&lt;/a&gt;, and &lt;a href="http://www.cplusplus.com/reference/thread/thread/"&gt;first-class thread
support&lt;/a&gt;.
C# has &lt;a href="https://msdn.microsoft.com/en-us/library/hh191443.aspx"&gt;&lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt;&lt;/a&gt;, which is &lt;a href="http://blogs.msdn.com/b/vcblog/archive/2014/11/12/resumable-functions-in-c.aspx"&gt;proposed&lt;/a&gt; for
inclusion in C++ as well. This trend will only accelerate in the future.&lt;/p&gt;
&lt;p&gt;With this in mind, I decided to investigate the CPython GIL. Previous
proposals for its removal have &lt;a href="https://wiki.python.org/moin/GlobalInterpreterLock"&gt;failed&lt;/a&gt;, but I thought it's worth a look —
especially since I couldn't find any recent attempts.&lt;/p&gt;
&lt;p&gt;The results were not encouraging. Changing the reference count used by
Python objects from a normal &lt;code&gt;int&lt;/code&gt; to an atomic type resulted in a
~23% &lt;abbr title="Measured rather unscientifically using the pystone benchmark."&gt;slowdown
on my machine&lt;/abbr&gt;. This is without actually changing any of the
locking. This penalty could be moderated for single-threaded programs by only
using atomic instructions once a second thread is started. This requires a
function call and an &lt;code&gt;if&lt;/code&gt; statement to check whether to use atomics or not in
the refcount hot path. Doing this still results in an 11% slowdown in the
single-threaded case. If hot-patching was used instead of the &lt;code&gt;if&lt;/code&gt;, a 6%
slowdown remained.&lt;/p&gt;
&lt;p&gt;
&lt;div class="img-container"&gt;&lt;img alt="refcount slowdown" src="https://www.caichinger.com/files/cpython-atomic-refcount-slowdown.png" title="Atomic refcounts slow down the overall Python execution speed by &amp;gt;20%."&gt;&lt;div class="img-caption"&gt;Atomic refcounts slow down the overall Python execution speed by &amp;gt;20%.&lt;/div&gt;
&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;The last result looks promising, but is deceiving. Hot-patching would rely
on having a single function to patch. Alas, the compiler decided to mostly
inline the &lt;code&gt;Py_INCREF&lt;/code&gt;/&lt;code&gt;Py_DECREF&lt;/code&gt; function calls. Disabling inlining of these
functions gives a 16% slowdown, which is worse than the "call + &lt;code&gt;if&lt;/code&gt;" method.
Furthermore, hot-patching is probably not something that could be merged in
CPython anyway.&lt;/p&gt;
&lt;p&gt;So what's the conclusion? Maybe turning &lt;code&gt;Py_INCREF&lt;/code&gt; and &lt;code&gt;Py_DECREF&lt;/code&gt; into
functions and living with the 11% slowdown of the single-threaded case
would be sell-able, if compelling speed-ups of multithreaded workloads could
be shown. It should be possible to convert one module at a time from the GIL to
fine-grained locking, but performance increases would only be expected once at
least a couple of core modules are converted. That would take a substantial
amount of work, especially given the high risk that the resulting patches
wouldn't be accepted upstream.&lt;/p&gt;
&lt;p&gt;Where does this leave Python as a language in the multi-threaded world?
I'm not sure. Since PyPy is already the solution to Python's performance issue,
perhaps it can solve the concurrency problem as well with its &lt;a href="https://pypy.readthedocs.org/en/latest/stm.html"&gt;software
transactional memory&lt;/a&gt; mode.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; My profiling showed that the reference counting in
Python (&lt;code&gt;Py_INCREF()&lt;/code&gt; and &lt;code&gt;Py_DECREF()&lt;/code&gt;) takes up to about 5–10% of the
execution time of benchmarks (not including actual object destruction), crazy!&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>Subclassing C++ in Python with SIP</title><link href="https://www.caichinger.com/blog/2015/05/17/subclassing_cpp_in_python_with_sip" rel="alternate"></link><published>2015-05-17T00:00:00+02:00</published><updated>2015-05-17T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-05-17:/blog/2015/05/17/subclassing_cpp_in_python_with_sip</id><summary type="html">&lt;p&gt;Exploring how to subclass C++ classes in Python using the SIP binding generator.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I use &lt;a href="http://pyqt.sourceforge.net/Docs/sip4/introduction.html"&gt;SIP&lt;/a&gt; in &lt;a href="https://github.com/ChrisAichinger/MapsEvolved"&gt;MapsEvolved&lt;/a&gt; to generate bindings for interfacing Python
with C++. I really like SIP due to its straight-forward syntax that mostly
allows just copying class definitions over from C++. Further, it's really
well thought out and contains support for a number of advanced use cases.&lt;/p&gt;
&lt;p&gt;One such feature is implementing a C++ interface in Python. The resulting class
can then even be passed back to C++, and any methods called on it will be
forwarded to the Python implementation. Sweet!&lt;/p&gt;
&lt;p&gt;Here is an example I originally wrote for &lt;a href="http://stackoverflow.com/a/29935113/25097"&gt;this Stack Overflow
question&lt;/a&gt;. It illustrates how
ridiculously easy it is to get this working:&lt;/p&gt;
&lt;p&gt;visitor.h:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EXPORT&lt;/span&gt; &lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;getN&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;EXPORT&lt;/span&gt; &lt;span class="n"&gt;NodeVisitor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;OnNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;visit_graph_nodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NodeVisitor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;visitor.sip:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;Module&lt;/span&gt; &lt;span class="n"&gt;pyvisit&lt;/span&gt;

&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;ModuleHeaderCode&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;quot;visitor.h&amp;quot;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;End&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;getN&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;NodeVisitor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;OnNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;visit_graph_nodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NodeVisitor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using it from Python:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pyvisit&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PyNodeVisitor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pyvisit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NodeVisitor&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;OnNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;         &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getN&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pnv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PyNodeVisitor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;visit_graph_nodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pnv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the C++ function &lt;code&gt;visit_graph_nodes()&lt;/code&gt; calls the Python method
&lt;code&gt;pnv.OnNode()&lt;/code&gt; for every node in its (internal) graph. A zip file with the full
working source code of this example can be downloaded
&lt;a href="https://caichinger.com/div/sip_visitor_test.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The subclassing capabilities of SIP don't stop at interfaces, either. It's
possible to derive from any C++ class, abstract or not, inheriting (or
overriding) existing method implementations as needed. This gives a lot of
flexibility and makes it easy to have classes with some parts implemented
in C++, and others being in Python.&lt;/p&gt;</content><category term="blog"></category><category term="python"></category></entry><entry><title>Configuring SSL on Apache 2.4</title><link href="https://www.caichinger.com/blog/2015/05/11/configuring_ssl_on_apache24" rel="alternate"></link><published>2015-05-11T00:00:00+02:00</published><updated>2015-05-11T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-05-11:/blog/2015/05/11/configuring_ssl_on_apache24</id><summary type="html">&lt;p&gt;Hands-on tutorial for enabling SSL encryption on Apache.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Configuring a modern web server to employ strong encryption and forward secrecy
doesn't have to be hard. There is excellent documentation from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Security"&gt;Mozilla&lt;/a&gt; and
from the &lt;a href="https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet"&gt;OWASP&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Obtaining an SSL certificate&lt;/h2&gt;
&lt;p&gt;One major stumbling block is where to obtain an SSL certificate. In the
future, this should hopefully be easy with &lt;a href="https://letsencrypt.org/"&gt;Let's Encrypt&lt;/a&gt;. Until that is
actually functional, &lt;a href="https://www.startssl.com/"&gt;StartSSL&lt;/a&gt; offers free SSL certificates. The process
takes a bit of patience, but it's &lt;em&gt;not difficult&lt;/em&gt;. There's also a &lt;a href="http://www.h-online.com/security/features/SSL-for-free-step-by-step-906862.html"&gt;StartSSL
HOWTO from h-online.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;While I've used StartSSL in the past, I had some trouble with them because 10
years after I registered &lt;code&gt;greek0.net&lt;/code&gt;, someone grabbed &lt;code&gt;greekO.net&lt;/code&gt; and
StartSSL was alleging I was trying to mislead users?! So that was the end of
my business with them...&lt;/p&gt;
&lt;p&gt;I've now switched to &lt;a href="https://comodosslstore.com/positivessl.aspx"&gt;Comodo's Positive SSL Certificate&lt;/a&gt;, which I like for a
couple of reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it lasts for 3 years,&lt;/li&gt;
&lt;li&gt;it's really uncomplicated, and&lt;/li&gt;
&lt;li&gt;it's crazy cheap: 7.45$ per year.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The process of getting the cert from them was super easy, simpler than
StartSSL. About 3 hours from going to their website to having the certificate
installed on my server, with most of it waiting email verifications. Credit
card payment was quick and easy. 10/10, would buy again :-)&lt;/p&gt;
&lt;h2&gt;Apache configuration&lt;/h2&gt;
&lt;p&gt;With the certificate acquisition out of the way, here are the juicy bits from
my Apache config.&lt;/p&gt;
&lt;p&gt;mod_ssl config:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c"&gt;# Enable only cyphers that support forward secrecy.&lt;/span&gt;
&lt;span class="c"&gt;# See these two links for reference:&lt;/span&gt;
&lt;span class="c"&gt;# https://stackoverflow.com/questions/17308690&lt;/span&gt;
&lt;span class="c"&gt;# https://wiki.mozilla.org/Security/Server_Side_TLS#Non-Backward_Compatible_Ciphersuite&lt;/span&gt;
&lt;span class="nb"&gt;SSLCipherSuite&lt;/span&gt; ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK

&lt;span class="c"&gt;# Use server priorities for cipher algorithm choice.&lt;/span&gt;
&lt;span class="nb"&gt;SSLHonorCipherOrder&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt;

&lt;span class="c"&gt;# With Apache 2.4, SSLv2 is gone and only SSLv3 and TLSv* are supported.&lt;/span&gt;
&lt;span class="c"&gt;# Disable SSLv3, all TLS protocols are OK.&lt;/span&gt;
&lt;span class="nb"&gt;SSLProtocol&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; -SSLv3

&lt;span class="c"&gt;# Enable OCSP stapling&lt;/span&gt;
&lt;span class="c"&gt;# With this, the client can verify that our certificate isn&amp;#39;t revoked&lt;/span&gt;
&lt;span class="c"&gt;# without having to query an external OCSP service.&lt;/span&gt;
&lt;span class="nb"&gt;SSLUseStapling&lt;/span&gt; &lt;span class="k"&gt;On&lt;/span&gt;
&lt;span class="nb"&gt;SSLStaplingCache&lt;/span&gt; shmcb:${APACHE_RUN_DIR}/ssl_stapling(32768)
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The per-site configuration:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nb"&gt;SSLEngine&lt;/span&gt; &lt;span class="k"&gt;On&lt;/span&gt;
&lt;span class="nb"&gt;SSLCertificateKeyFile&lt;/span&gt;   &lt;span class="sx"&gt;/path/to/serverkey.key&lt;/span&gt;    # The private server key.
&lt;span class="nb"&gt;SSLCertificateFile&lt;/span&gt;      &lt;span class="sx"&gt;/path/to/certificate.crt&lt;/span&gt;  # The certificate provided by CA.
&lt;span class="nb"&gt;SSLCertificateChainFile&lt;/span&gt; &lt;span class="sx"&gt;/path/to/cert-bundle&lt;/span&gt;      # A separate download from your CA.


&lt;span class="c"&gt;# Use a customized prime group for DH key exchange (vs Logjam attack).&lt;/span&gt;
&lt;span class="c"&gt;# Generate a DH group file with:&lt;/span&gt;
&lt;span class="c"&gt;#    openssl dhparam -out dhparams.pem 2048&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# Newer Apache versions support the following command to set the dhparams:&lt;/span&gt;
&lt;span class="nb"&gt;SSLOpenSSLConfCmd&lt;/span&gt; DHParameters &lt;span class="s2"&gt;&amp;quot;/path/to/dhparams.pem&amp;quot;&lt;/span&gt;

&lt;span class="c"&gt;# If Apache reports an error for the above line, remove it and include&lt;/span&gt;
&lt;span class="c"&gt;# the dhparams in the certificate:&lt;/span&gt;
&lt;span class="c"&gt;#   cat &amp;lt;CERT&amp;gt;.crt dhparams.pem &amp;gt; cert-with-dhparams.crt&lt;/span&gt;
&lt;span class="c"&gt;#   SSLCertificateFile cert-with-dhparams.crt&lt;/span&gt;


&lt;span class="c"&gt;# HSTS: Force browsers to require SSL for this domain for the next year.&lt;/span&gt;
&lt;span class="c"&gt;# Down-grade to HTTP will cause browsers to abort with a security error.&lt;/span&gt;
&lt;span class="nb"&gt;Header&lt;/span&gt; always set Strict-Transport-Security &lt;span class="s2"&gt;&amp;quot;max-age=31536000; includeSubDomains; preload&amp;quot;&lt;/span&gt;


&lt;span class="c"&gt;# HPKP: Pin the current key for the next two months.&lt;/span&gt;
&lt;span class="c"&gt;# Generate hash using:&lt;/span&gt;
&lt;span class="c"&gt;#   openssl rsa -in &amp;lt;serverkey&amp;gt;.key -outform der -pubout | \&lt;/span&gt;
&lt;span class="c"&gt;#   openssl dgst -sha256 -binary | openssl enc -base64&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# You ideally want to generate a backup key and include that here as well,&lt;/span&gt;
&lt;span class="c"&gt;# in case the primary key is lost or compromised.&lt;/span&gt;
&lt;span class="c"&gt;# Also note the implications for key rollover.&lt;/span&gt;
&lt;span class="c"&gt;# See: https://developer.mozilla.org/en-US/docs/Web/Security/Public_Key_Pinning&lt;/span&gt;
&lt;span class="nb"&gt;Header&lt;/span&gt; always set Public-Key-Pins &lt;span class="s2"&gt;&amp;quot;pin-sha256=\&amp;quot;&amp;lt;HASH&amp;gt;\&amp;quot;; max-age=5184000; includeSubDomains&amp;quot;&lt;/span&gt;


&lt;span class="c"&gt;# Disable compression to avoid BREACH HTTPS/SSL attack.&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;Location&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nb"&gt;SetEnv&lt;/span&gt; no-gzip
&lt;span class="nt"&gt;&amp;lt;/Location&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That should cover the basics.&lt;/p&gt;
&lt;h2&gt;Testing&lt;/h2&gt;
&lt;p&gt;As for SSL connection testing, I found the &lt;a href="https://www.ssllabs.com/ssltest/"&gt;Qualys SSL Labs Test&lt;/a&gt; helpful.
It shows what browsers (browser versions) will get which
encryption quality (forward secrecy or not) and highlights common problems such
as certificate chain issues.&lt;/p&gt;
&lt;p&gt;Hope this helps someone out there!&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="apache"></category></entry><entry><title>Cleaning up unused Docker images and containers</title><link href="https://www.caichinger.com/blog/2015/05/06/cleaning_up_docker" rel="alternate"></link><published>2015-05-06T00:00:00+02:00</published><updated>2015-05-06T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-05-06:/blog/2015/05/06/cleaning_up_docker</id><summary type="html">&lt;p&gt;Docker images tend to accumulate and eventually eat up all available disk space. Two lines of bash code can remove unused images.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Docker doesn't delete old/unused images or containers by itself, even if they
weren't used for a long time or were only intermediary steps on the way to
another image. This leads to an image sprawl that eats up a lot of disk space
if not kept in check.&lt;/p&gt;
&lt;p&gt;The right way to solve this would be to parse the output of &lt;code&gt;docker inspect&lt;/code&gt; and
remove containers and images based on certain policies. Unfortunately, a quick
internet search did not turn up a script that does this.&lt;/p&gt;
&lt;p&gt;Since I didn't want to spend the time to write such a thing myself, I resorted
to what – sadly – seems to be state-of-the-art docker image management: a
cronjob running those two lines:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;docker ps -a &lt;span class="p"&gt;|&lt;/span&gt; grep &lt;span class="s1"&gt;&amp;#39;weeks ago&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; awk &lt;span class="s1"&gt;&amp;#39;{print $1}&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; xargs --no-run-if-empty docker rm
docker images -f &lt;span class="s2"&gt;&amp;quot;dangling=true&amp;quot;&lt;/span&gt; -q &lt;span class="p"&gt;|&lt;/span&gt; xargs --no-run-if-empty docker rmi &amp;gt;/dev/null &lt;span class="m"&gt;2&lt;/span&gt;&amp;gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first line removes containers that are older than two weeks and are not
currently running (&lt;code&gt;docker rm&lt;/code&gt; simply will not remove running containers).
The second line removes images that are not used by any container and are not
tagged (i.e. don't have proper repository name).&lt;/p&gt;
&lt;p&gt;These two invocations are based on &lt;a href="https://stackoverflow.com/questions/17236796"&gt;this Stack Overflow
question&lt;/a&gt; and on this &lt;a href="http://jimhoskins.com/2013/07/27/remove-untagged-docker-images.html"&gt;blog
post&lt;/a&gt;
by Jim Hoskins.&lt;/p&gt;
&lt;p&gt;This solution works well enough, you probably shouldn't use it on production
servers, though. :-)&lt;/p&gt;</content><category term="blog"></category><category term="docker"></category><category term="virtualization"></category></entry><entry><title>Manually creating Docker images</title><link href="https://www.caichinger.com/blog/2015/04/30/homegrown_docker_images" rel="alternate"></link><published>2015-04-30T00:00:00+02:00</published><updated>2015-04-30T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-04-30:/blog/2015/04/30/homegrown_docker_images</id><summary type="html">&lt;p&gt;Creating Docker images from scratch, without relying on externally built base images. This is a good option for those who don't necessarily trust DockerHub.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; is a virtualization solution that's been gaining a lot of momentum
over the last few years. It focuses on light-weight, ephemeral containers that
can be created based on simple config files.&lt;/p&gt;
&lt;p&gt;Docker's main target platform is amd64, but it also works on x86. However,
practically all official container images in the Docker registry are amd64
based, which means they can't be used on an x86 machine. So, it's necessary to
manually create the required base images. As you might have guessed, my server
runs Docker on x86, so I've had to find a solution for that problem.&lt;/p&gt;
&lt;p&gt;Fortunately, creating images from scratch is really easy with the &lt;code&gt;mkimage.sh&lt;/code&gt;
script that comes bundled with Docker. On Debian systems, its installed in
&lt;code&gt;/usr/share/docker.io/contrib/mkimage.sh&lt;/code&gt;, on Fedora it has to be
obtained from the Docker git repository:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt; git clone https://github.com/docker/docker.git
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The script can then be found under &lt;code&gt;docker/contrib/mkimage.sh&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Creating a Debian Jessie image is straight-forward:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;#&lt;/span&gt; mkimage.sh -t debootstrap/minbase debootstrap --variant&lt;span class="o"&gt;=&lt;/span&gt;minbase jessie
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This command will create a minimal Debian Jessie image using &lt;a href="https://wiki.debian.org/Debootstrap"&gt;Debootstrap&lt;/a&gt;,
and import it into Docker with the name &lt;code&gt;debootstrap/minbase&lt;/code&gt;. Further options
can set a specific Debian mirror server and a list of additional packages to
install:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="gp"&gt;#&lt;/span&gt; mkimage.sh -t debootstrap/minbase debootstrap &lt;span class="se"&gt;\&lt;/span&gt;
             --include&lt;span class="o"&gt;=&lt;/span&gt;locales --variant&lt;span class="o"&gt;=&lt;/span&gt;minbase &lt;span class="se"&gt;\&lt;/span&gt;
             jessie http://httpredir.debian.org/debian
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will use
&lt;a href="https://lists.debian.org/debian-devel-announce/2015/05/msg00003.html"&gt;&lt;code&gt;httpredir.debian.org&lt;/code&gt;&lt;/a&gt;
as mirror and install the &lt;code&gt;locales&lt;/code&gt; package in the image.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;mkimage.sh&lt;/code&gt; has backends to bootstrap Arch Linux, Busybox, Centos, Mageia, and
Ubuntu. Fedora images doesn't seem to be supported directly, but they can be
generated by following
&lt;a href="http://allthingsopen.com/2013/12/19/building-docker-images-on-fedora/"&gt;instructions&lt;/a&gt;
compiled by James Labocki.&lt;/p&gt;
&lt;p&gt;Finally, it's worth mentioning that this should only be used to generate
base images. You'd then use Docker itself (cf. &lt;a href="https://docs.docker.com/reference/builder/"&gt;Dockerfile&lt;/a&gt;) to create images
that actually do something interesting, based on these base images. This will
save both time and memory, due to Docker's caching and copy-on-write mechanisms.&lt;/p&gt;</content><category term="blog"></category><category term="docker"></category><category term="virtualization"></category></entry><entry><title>Installing CyanogenMod 11 on a Samsung Galaxy S2</title><link href="https://www.caichinger.com/blog/2015/04/15/installing_cyanogenmod_on_sgs2" rel="alternate"></link><published>2015-04-15T00:00:00+02:00</published><updated>2015-04-15T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2015-04-15:/blog/2015/04/15/installing_cyanogenmod_on_sgs2</id><summary type="html">&lt;p&gt;Setup instructions for the CyanogenMod image on a Samsung Galaxy S2 (SGS2)&lt;/p&gt;</summary><content type="html">&lt;p&gt;I bought my Samsung Galaxy S2 in 2011, and it's still going strong. It really
was a great phone for the time and held up incredibly well. Unfortunately,
Samsung's support has ended long ago, and users are stranded with an obsolete
(and insecure) firmware.&lt;/p&gt;
&lt;p&gt;Fortunately, &lt;a href="http://www.cyanogenmod.org/"&gt;CyanogenMod&lt;/a&gt; still provides relatively recent images for the
device. As of this writing, snapshots of CM11 (based on Android 4.4) are
available, but there are no images of CM12.&lt;/p&gt;
&lt;p&gt;Here is how I flashed CM11 to my phone. This is based on the &lt;a href="http://wiki.cyanogenmod.org/w/Install_CM_for_i9100"&gt;official
CyanogenMod wiki page for the SGS2&lt;/a&gt; and on &lt;a href="http://forum.xda-developers.com/showpost.php?p=53998181&amp;amp;postcount=4"&gt;this
xda-developers post&lt;/a&gt;. Since you can brick your phone if you don't
know what you are doing, I suggest reading both of these pages.
Note that you will need to factory-reset your phone, so backup all
your data (files, apps, SMS, contacts, ...).&lt;/p&gt;
&lt;p&gt;All the following steps have to be performed on a root shell on Linux.&lt;/p&gt;
&lt;p&gt;To start from a clean slate, create a new Debian Jessie chroot (you may need to
install &lt;code&gt;debootstrap&lt;/code&gt; first). Don't use LXC/Docker/VMWare here, you need raw
hardware access:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;host#  mkdir sgs2-flash-chroot&lt;/span&gt;
&lt;span class="go"&gt;host#  cd sgs2-flash-chroot&lt;/span&gt;
&lt;span class="go"&gt;host#  debootstrap jessie .&lt;/span&gt;
&lt;span class="go"&gt;host#  mount --bind /dev/ dev&lt;/span&gt;
&lt;span class="go"&gt;host#  mount --bind /sys sys&lt;/span&gt;
&lt;span class="go"&gt;host#  mount --bind /proc proc&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Copy the following files to &lt;code&gt;sgs2-flash-chroot/tmp&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://download.cyanogenmod.org/?device=i9100"&gt;cm-11-20141115-SNAPSHOT-M12-i9100.zip&lt;/a&gt;: CyanogenMod image&lt;/li&gt;
&lt;li&gt;&lt;a href="http://wiki.cyanogenmod.org/w/Google_Apps"&gt;gapps-kk-20140105-signed.zip&lt;/a&gt;: Google Apps package; note that version &amp;gt;= 20140606 may not work: check the WARNING section on &lt;a href="http://wiki.cyanogenmod.org/w/Install_CM_for_i9100"&gt;the CM wiki&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;zImage (from &lt;a href="http://forum.xda-developers.com/galaxy-s2/orig-development/kernel-clockworkmod-recovery-6-0-2-9-t1118693"&gt;GT-I9100_JB_ClockworkMod-Recovery_6.0.2.9.tar&lt;/a&gt;): Recovery kernel&lt;/li&gt;
&lt;li&gt;&lt;a href="http://forum.xda-developers.com/galaxy-s2/development-derivatives/cwm-clockworkmod-recovery-kit-kat-4-4-t2628412"&gt;Recovery_CWM_6.0.4.7_I9100.zip&lt;/a&gt;: Newer recovery image required to install CM &amp;gt;= 11.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Boot the phone into download-mode (shutdown, then VOLDOWN + HOME + POWER) and
connect to the Linux computer.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;host#  chroot .&lt;/span&gt;
&lt;span class="go"&gt;chroot#  apt-get install heimdall-flash android-tools-adb&lt;/span&gt;
&lt;span class="go"&gt;chroot#  heimdall print-pit&lt;/span&gt;
&lt;span class="go"&gt;chroot#  cd /tmp&lt;/span&gt;
&lt;span class="go"&gt;chroot#  heimdall flash --KERNEL zImage --no-reboot&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Disconnect the USB cable and hold POWER until the phone shuts down. Reboot into
recovery (VOLUP + HOME + POWER, let go of POWER after 5 seconds or
you'll trigger a reboot). Then reconnect the USB cable.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;chroot#  adb devices    # Check if device recognized.&lt;/span&gt;
&lt;span class="go"&gt;chroot#  adb push Recovery_CWM_6.0.4.7_I9100.zip /emmc&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In recovery, select "install from zip file" to flash the new recovery image.
Then go into advanced -&amp;gt; "reboot recovery". Mount &lt;code&gt;/storage/sdcard0&lt;/code&gt; in the
recovery menu, then reconnect the USB cable.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="go"&gt;chroot#  adb devices    # Check if device recognized.&lt;/span&gt;
&lt;span class="go"&gt;chroot#  adb push cm-11-20141115-SNAPSHOT-M12-i9100.zip /storage/sdcard0&lt;/span&gt;
&lt;span class="go"&gt;chroot#  adb push gapps-kk-20140105-signed.zip /storage/sdcard0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Again, in recovery, select "install from zip files", first install the CM
image, then the GApps package. Select "reboot" to boot into CyanogenMod. Shut
down again, reboot into recovery, wipe cache and perform factory reset, reboot
into CM (avoid factory reset with stock kernel due to the "super brick"
problem).&lt;/p&gt;
&lt;p&gt;Done. You should now have a not-so-shiny-anymore Galaxy S2 running a
new-and-shiny CyanogenMod 11. Enjoy :-)&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="android"></category></entry><entry><title>Checklib finally announced</title><link href="https://www.caichinger.com/blog/2006/09/27/checklib" rel="alternate"></link><published>2006-09-27T00:00:00+02:00</published><updated>2006-09-27T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-09-27:/blog/2006/09/27/checklib</id><summary type="html">&lt;p&gt;Checklib is a tool to detect unnecessary dependencies between C/C++ libraries and Debian packages.&lt;/p&gt;</summary><content type="html">&lt;p&gt;On Monday, &lt;a href="http://rerun.lefant.net/checklib"&gt;checklib&lt;/a&gt; was finally &lt;a href="http://lists.debian.org/debian-devel-announce/2006/09/msg00018.html"&gt;announced&lt;/a&gt; on debian-devel-announce,
thanks to Andreas Barth for sponsoring the mail.&lt;/p&gt;
&lt;p&gt;I got very positive reactions from a number of people, which is great. I got
less friendly comments prior to the announcement (by one person) , and I'm
happy this reaction wasn't representative for the rest of Debian.&lt;/p&gt;
&lt;p&gt;It's nice that people show interest in the problem, there's currently a
discussion on debian-devel if and how automatic checking (and fixing) could be
added to debhelper. That would seriously rock, as it would be one of the
faster ways to get the number of affected packages down.&lt;/p&gt;
&lt;p&gt;It's also cool to hear that the GNOME people are fixing their &lt;code&gt;.la&lt;/code&gt; files with
2.16 in order to cut down dependencies introduced by broken libtool files.&lt;/p&gt;
&lt;p&gt;There are some other interesting things on the horizon on the technical side of
the project, as automatically built dbgsym packages (containing debug symbols,
Ubuntu does that already), and the &lt;a href="http://www.hogyros.de/?q=node/176"&gt;idea Simon Richter already talked
about&lt;/a&gt;, which could really cut down the work the release team has
with library transitions.&lt;/p&gt;</content><category term="blog"></category><category term="debian"></category></entry><entry><title>Installing Debian on an oldworld PPC</title><link href="https://www.caichinger.com/blog/2006/09/20/ppc_install" rel="alternate"></link><published>2006-09-20T00:00:00+02:00</published><updated>2006-09-20T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-09-20:/blog/2006/09/20/ppc_install</id><summary type="html">&lt;p&gt;My journey to getting Debian to run on an ancient PowerPC.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The victim was a PowerMac 9500 with a 300MHz G3 CPU, 200Mb RAM, a 9G HDD (with
OS9 installed), and a 2G HDD (blank).&lt;/p&gt;
&lt;p&gt;I hooked up a PowerBook to see the serial console output, since OpenFirmware
only talks over the serial line. Then I finally found &lt;a href="http://ftp.at.debian.org/debian/dists/woody/main/disks-powerpc/current/powermac/images-1.44/"&gt;floppy images&lt;/a&gt;
that would boot actually boot. They were from Woody, so I did a netinst using
boot-floppies.&lt;/p&gt;
&lt;p&gt;Unfortunately the Linux didn't come up after reboot. After resetting the nvram
I could at least boot MacOS again and then start Linux via BootX. It took quite
some fiddling with quik and the nvram till I had the direct boot working.&lt;/p&gt;
&lt;p&gt;Subsequently I upgraded to etch, but I couldn't get a 2.6 kernel
to boot. First I got a bus error from the IMS TwinTurbo graphics card driver,
then the kernel "forgot" where the initramfs was loaded, which turned out
to be a grave bug in the Debian kernel images (&lt;a href="http://bugs.debian.org/366620"&gt;#366620&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;I wrote patches for both bugs and now I finally have Etch with Linux 2.6
working. Whee!&lt;/p&gt;
&lt;p&gt;The only sad thing is that all this took the better part of last week :-/&lt;/p&gt;</content><category term="blog"></category><category term="debian"></category></entry><entry><title>Correct use of hyphens in man-pages</title><link href="https://www.caichinger.com/blog/2006/09/06/man_hyphens" rel="alternate"></link><published>2006-09-06T00:00:00+02:00</published><updated>2006-09-06T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-09-06:/blog/2006/09/06/man_hyphens</id><summary type="html">&lt;p&gt;The Groff file format used for manpages supports several different hyphens. It's important to pick the correct one.&lt;/p&gt;</summary><content type="html">&lt;p&gt;When writing manual pages the question comes up when to use "&lt;code&gt;-&lt;/code&gt;" and when to
use "&lt;code&gt;\-&lt;/code&gt;".  The answer is actually quite simple. Use "&lt;code&gt;-&lt;/code&gt;" whenever you want a
hyphen and "&lt;code&gt;\-&lt;/code&gt;" when you want a minus sign.&lt;/p&gt;
&lt;p&gt;There are two exceptions though: In the name section, "&lt;code&gt;\-&lt;/code&gt;" is used to
separate program name from short description, as in "&lt;code&gt;man \- an interface to
on-line manuals&lt;/code&gt;".&lt;/p&gt;
&lt;p&gt;The other exception is that you have to use "&lt;code&gt;\-&lt;/code&gt;" for options/switches (&lt;code&gt;-h&lt;/code&gt;,
&lt;code&gt;--foo&lt;/code&gt;, etc.). "&lt;code&gt;\-&lt;/code&gt;" causes &lt;code&gt;man&lt;/code&gt; to emit an U+002d Hyphen-Minus character,
whereas "&lt;code&gt;-&lt;/code&gt;" results in U+2010 Hyphen (in a unicode locale).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;U+2d&lt;/code&gt; is the normal ASCII hyphen char, the one programs use to test for
switches. So "&lt;code&gt;\-&lt;/code&gt;" allows copy&amp;amp;paste from the manpage, while "&lt;code&gt;-&lt;/code&gt;"
doesn't.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="documentation"></category></entry><entry><title>ELF talk</title><link href="https://www.caichinger.com/blog/2006/08/19/elf_talk" rel="alternate"></link><published>2006-08-19T00:00:00+02:00</published><updated>2006-08-19T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-19:/blog/2006/08/19/elf_talk</id><summary type="html">&lt;p&gt;Notes on a talk about the ELF executable format I gave.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Last monday I held a short talk about ELF objects and dynamic linking for the
&lt;a href="http://debienna.at/"&gt;Debienna&lt;/a&gt; crowd. It went semi-well; people were quite interested but somtimes
didn't seem to grasp what I was talking about. Which was probably my
fault because I didn't spend enough time preparing the talk, being on a
difficult subject to begin with.&lt;/p&gt;
&lt;p&gt;Perhaps I'll talk about the subject again for maks, Rhonda and baumgartner (if
they are still interested), since they weren't able to attend.&lt;/p&gt;
&lt;p&gt;In case anyone cares, I've written up some &lt;a href="https://www.caichinger.com/elf.html"&gt;notes&lt;/a&gt; about ELF, dynamic
linking, symbol lookup and related stuff, covering most the thinks I talked
about.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category></entry><entry><title>Cross-compiler fun</title><link href="https://www.caichinger.com/blog/2006/08/12/cross-compiler_fun" rel="alternate"></link><published>2006-08-12T00:00:00+02:00</published><updated>2006-08-12T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-12:/blog/2006/08/12/cross-compiler_fun</id><summary type="html">&lt;p&gt;Setting up a cross-compiling toolchain in Debian&lt;/p&gt;</summary><content type="html">&lt;p&gt;I needed to fix the &lt;a href="http://packages.debian.org/unstable/source/elfutils"&gt;elfutils&lt;/a&gt; build failure on ia64, but I didn't have
access to such a machine. Fortunately Herbert Pötzl pointed out &lt;a href="http://www.hpl.hp.com/research/linux/ski/index.php"&gt;ski&lt;/a&gt;,
an ia64 emulator for Linux.&lt;/p&gt;
&lt;p&gt;Ski needs a custom guest kernel however, so I had to cross-compile that for
ia64.&lt;/p&gt;
&lt;p&gt;Setting up a cross-compiling toolchain on Debian is really easy nowadays;
there's even a nice &lt;a href="http://psas.pdx.edu/DebianCrossCompilerHowto"&gt;HOWTO&lt;/a&gt; describing the needed steps. For lazy
people &lt;a href="http://debian.speedblue.org/"&gt;pre-built packages&lt;/a&gt; are available.&lt;/p&gt;
&lt;p&gt;When compiling the toolchain yourself, note that you may need more/other
library packages then listed in the HOWTO. This depends on the target
architecture, e.g. for ia64 you will need libunwind7-dev, libatomic-ops-dev,
and further libc6.1 instead of libc6. Otherwise gcc will complain about missing
build-dependencies.&lt;/p&gt;
&lt;p&gt;For ia64 I ran into a linker error when building gcc, however a &lt;a href="./div/cross/gcc-cross-fix-ia64.diff"&gt;patch&lt;/a&gt; from
&lt;a href="http://vserver.13thfloor.at/Stuff/Cross/"&gt;Bertl's cross-compiling corner&lt;/a&gt; solved that.&lt;/p&gt;
&lt;p&gt;While doing all this I wrote some &lt;a href="./div/cross/cross.tar.gz"&gt;scripts&lt;/a&gt; to automate the process, so
compiling a cross-toolchain (for any architecture) is now a matter of 5 minutes
configuration and one &lt;code&gt;./driver&lt;/code&gt; run. Whee!&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category></entry><entry><title>Using mutt's header_cache feature</title><link href="https://www.caichinger.com/blog/2006/08/08/mutt_header_cache" rel="alternate"></link><published>2006-08-08T00:00:00+02:00</published><updated>2006-08-08T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-08:/blog/2006/08/08/mutt_header_cache</id><summary type="html">&lt;p&gt;Improving the performance of the mutt email client on large mailboxes&lt;/p&gt;</summary><content type="html">&lt;p&gt;In the past I needed to move old mails out to a backup
folder occasionally, since it took too long for &lt;a href="http://www.mutt.org"&gt;mutt&lt;/a&gt; to load all the
headers when opening the maildir (often some 10 seconds for about 6000 mails).&lt;/p&gt;
&lt;p&gt;Today I remembered that mutt should actually do header caching, so I &lt;a href="http://mutt.org/doc/devel/manual.html#caching"&gt;looked it
up in the docs&lt;/a&gt;, and saw that the config option was missing in my
&lt;code&gt;.muttrc&lt;/code&gt;. I put it in, restarted mutt, and after the cache was initialized the
performance was noticeably better:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;set header_cache=&amp;quot;~/.mutt/headercache&amp;quot;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Mutt can actually use 3 kinds of database backends for the cache, bdb, gdbm and
qdbm. The default in Debian is bdb, to use one of the others you have to flip a
switch in &lt;code&gt;debian/rules&lt;/code&gt; and recompile the package.&lt;/p&gt;
&lt;p&gt;On my machine qdbm was fastest (gdbm slightly slower, dbm far behind), so I
stuck with that. Since I use a self-compiled mutt package anyway (because I
want my &lt;a href="mutt/"&gt;index_color patch&lt;/a&gt; included), it's not much of a problem.&lt;/p&gt;</content><category term="blog"></category><category term="mutt"></category></entry><entry><title>Automatically syncing files between hosts without compromising security</title><link href="https://www.caichinger.com/blog/2006/08/05/secure_unison" rel="alternate"></link><published>2006-08-05T00:00:00+02:00</published><updated>2006-08-05T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-05:/blog/2006/08/05/secure_unison</id><summary type="html">&lt;p&gt;Jailing the file-synchronization tool unison like it's 2006&lt;/p&gt;</summary><content type="html">&lt;h2&gt;The problem&lt;/h2&gt;
&lt;p&gt;The goal is to automatically synchronize files between several hosts without
compromising the integrity of the separate machines. A nice tool for 2-way sync
is &lt;a href="http://www.cis.upenn.edu/~bcpierce/unison/"&gt;unison&lt;/a&gt;. To
sync files between different machines The Right Way (TM) is to tunnel the
unison protocol over ssh. This is well supported by unison.&lt;/p&gt;
&lt;p&gt;To run sync automatically (e.g. via cron), you need to
&lt;a href="http://www.hackinglinuxexposed.com/articles/20030109.html"&gt;create an SSH keypair&lt;/a&gt;
without passphrase, so unison can log into the other machine without human
interaction. This is where the problems start, since anyone who got access to
the private key (e.g. by compromising or stealing the machine the private key
was on) can log into the other host.&lt;/p&gt;
&lt;p&gt;Now ssh has a nice way to restrict what you can do with a specific key, so you
can e.g. use the following in the remote hosts
&lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;command=&amp;quot;/usr/bin/unison -server&amp;quot; ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8K2cd0yemw...
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That way someone who has the private key can't execute arbitrary commands, but
just unison in server mode. However it's still possible to tell the unison
server to overwrite arbitrary files (which the user has write access to). This
is a major problem, since also files like &lt;code&gt;~/.bashrc&lt;/code&gt; can be
overwritten, so the next time the user logs in, arbitrary commands will be
executed.&lt;/p&gt;
&lt;h2&gt;A possible solution&lt;/h2&gt;
&lt;p&gt;One solution is to simply create a new user on the remote host with a disabled
password, and let unison run as that user (via adding the appropriate line to
&lt;code&gt;$HOME/.ssh/authorized_keys&lt;/code&gt;, and telling the local unison to use
that username).&lt;/p&gt;
&lt;p&gt;That's possible, but the &lt;code&gt;.bashrc&lt;/code&gt; trick still works, it's just less
likely that the code there is ever executed (root would have to use
&lt;code&gt;su&lt;/code&gt; to become that user).&lt;/p&gt;
&lt;p&gt;For me this solution didn't work out since I wanted to sync my maildir, and it
was hard to ensure that file permissions were set in a way that both allowed me
to read my mail and allowed unison (running under user unison-sync) to sync the
files.&lt;/p&gt;
&lt;h2&gt;The Right Solution (TM)&lt;/h2&gt;
&lt;p&gt;All the problems vanish as soon as you run unison under the user you'd normally
use, but in a chroot. Now a full-blown chroot takes up a lot of space, and
there's once again the danger that someone might enter the chroot and run some
kind of shell (though the risk is even lower).&lt;/p&gt;
&lt;p&gt;It's best to use a chroot which only contains the bare minimum of files
necessary to run &lt;code&gt;unison -server&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You get numerous advantages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No problems with file permissions&lt;/li&gt;
&lt;li&gt;No shell inside the chroot that would read startup files from &lt;code&gt;$HOME&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Hardly any space wasted. The whole chroot is about 4Mb in size&lt;/li&gt;
&lt;li&gt;Since the chroot is pretty much empty, many common exploits (well, shell codes) won't work&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;How to do it&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;greek0@orest:/home/chroots/unichroot$ cat ~/.ssh/authorized_keys
command=&amp;quot;/usr/bin/dchroot -q -c unison -- /usr/bin/unison -server&amp;quot; ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8K2cd0.....

greek0@orest:/home/chroots/unichroot$ grep unison /etc/dchroot.conf
unison /home/chroots/unichroot

greek0@orest:/home/chroots/unichroot$ find . -maxdepth 3 | xargs ls -ld
drwxr-xr-x   2 root   root      4096 2006-08-04 16:39 ./bin
-rwxr-xr-x   1 root   root    576100 2006-08-04 15:06 ./bin/sash
lrwxrwxrwx   1 root   root         4 2006-08-04 16:39 ./bin/zsh -&amp;amp;gt; sash
drwxr-xr-x   3 root   root      4096 2006-08-04 15:10 ./home
drwx------   4 greek0 greek0    4096 2006-08-04 15:18 ./home/greek0
drwx------  31 greek0 greek0    4096 2006-08-04 13:47 ./home/greek0/Maildir
drwx------   2 greek0 greek0    4096 2006-08-04 15:47 ./home/greek0/.unison
drwxr-xr-x   2 root   root      4096 2006-08-04 15:07 ./lib
-rwxr-xr-x   1 root   root     88164 2006-08-04 14:58 ./lib/ld-linux.so.2
-rwxr-xr-x   1 root   root   1151644 2006-08-04 14:56 ./lib/libc.so.6
-rw-r--r--   1 root   root      9592 2006-08-04 14:56 ./lib/libdl.so.2
-rw-r--r--   1 root   root    141040 2006-08-04 14:55 ./lib/libm.so.6
-rw-r--r--   1 root   root      9656 2006-08-04 14:55 ./lib/libutil.so.1
drwxr-xr-x   3 root   root      4096 2006-08-04 14:53 ./usr
drwxr-xr-x   2 root   root      4096 2006-08-04 14:55 ./usr/bin
lrwxrwxrwx   1 root   root        14 2006-08-04 15:12 ./usr/bin/unison -&amp;amp;gt; unison-2.13.16
-rwxr-xr-x   1 root   root    955784 2006-08-04 14:54 ./usr/bin/unison-2.13.16
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;zsh&lt;/code&gt; symlink is there because I have &lt;code&gt;/bin/zsh&lt;/code&gt; as
my shell in &lt;code&gt;/etc/passwd&lt;/code&gt;, and dchroot also wants to use it in the
chroot (for launching unison).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/home/greek0/Maildir&lt;/code&gt; is bind-mounted from outside the chroot,
bind-mounting is done at boot-time via &lt;code&gt;/etc/fstab&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The chroot was created manually, simply by copying the files from the host. You
obviously need &lt;code&gt;/usr/bin/unison&lt;/code&gt; plus all the libraries it depends
on. You can find those via &lt;code&gt;readelf -d /usr/bin/unison | grep NEEDED&lt;/code&gt;.
Additionally you need the dynamic linker &lt;code&gt;/lib/ld-linux.so.2&lt;/code&gt; (seen
from &lt;code&gt;readelf -l /usr/bin/unison | grep INTERP -A 1&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;One thing to pay attention to is that most of the files copied from
&lt;code&gt;/lib&lt;/code&gt; are symlinks. Be sure to either use &lt;code&gt;cp&lt;/code&gt; without
arguments, or use &lt;code&gt;cp -a&lt;/code&gt; and copy the link targets too.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category></entry><entry><title>Installing Windows 95 inside QEMU on Linux</title><link href="https://www.caichinger.com/blog/2006/08/05/win95_qemu" rel="alternate"></link><published>2006-08-05T00:00:00+02:00</published><updated>2006-08-05T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-05:/blog/2006/08/05/win95_qemu</id><summary type="html">&lt;p&gt;Tutorial on how to get Windows 95 to run inside QEMU&lt;/p&gt;</summary><content type="html">&lt;p&gt;If you can just install Windows 95 inside QEMU and it magically works consider
yourself lucky. I tried this and got an error message that I need to create a
FAT partition first where the installer can place some files. That's where it
all began.&lt;/p&gt;
&lt;h2&gt;Installing&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Create the disk image. &lt;code&gt;dd if=/dev/zero of=hda bs=$(( 1024*1024 ))
        seek=1000 count=0&lt;/code&gt;. That way you create a sparse 1GB file.&lt;/li&gt;
&lt;li&gt;Get a FreeDOS Floppy and CD image and install FreeDOS inside QEMU,
        there you can partition your disk. &lt;code&gt;qemu -hda hda -fda fd0 -cdrom
        cdrom-img -boot a&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Insert the windows CD, run qemu with &lt;code&gt;-cdrom /dev/cdrom -boot
        c&lt;/code&gt; so you get to the FreeDOS prompt, then go to the cdrom drive and run
        setup. The setup should work then&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Getting networking up inside Windows&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;You need the Realtek rtl8139 driver, other network cards won't work
        (ne2000 at least doesn't). So run qemu with &lt;code&gt;-net user -net
        nic,model=rtl8139&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Windows 95 unfortunately doesn't have the driver for the rtl8139, so
        you need to get it onto the system. Download it from well, the &lt;a href="http://www.realtek.com.tw/downloads/downloads.aspx"&gt;Realtek
        driver download
        page&lt;/a&gt;.  Unzip it,
        put it onto a floppy, create an image of that floppy and run qemu with
        &lt;code&gt;-fda image&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then setup the networking under windows, reboot and you should be able to
        access the internet.&lt;/li&gt;
&lt;/ul&gt;</content><category term="blog"></category><category term="qemu"></category><category term="windows"></category></entry><entry><title>Tools for mutt</title><link href="https://www.caichinger.com/blog/2006/08/02/mutt-tools" rel="alternate"></link><published>2006-08-02T00:00:00+02:00</published><updated>2006-08-02T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-02:/blog/2006/08/02/mutt-tools</id><summary type="html">&lt;p&gt;Tools I use around my mutt email workflow&lt;/p&gt;</summary><content type="html">&lt;h2&gt;Tools&lt;/h2&gt;
&lt;h3&gt;mutt-bug&lt;/h3&gt;
&lt;p&gt;This is a tool that displays Debian bug reports in mutt. You can then
directly read all messages sent to the bug and reply. The messages are fetched
directly from the web interface, so there is no delay between requesting bug
and getting it per email.&lt;/p&gt;
&lt;p&gt;This tool was originally written by Christoph Berg, I've made some
modifications to make it work in arbitrary directories.&lt;/p&gt;
&lt;p&gt;Useage:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;$ mutt-bug bugnumber
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Download: &lt;a href="mutt/mutt-bug"&gt;mutt-bug&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;gpgverify&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;gpg --verify&lt;/code&gt; is quite slow when you have large keyrings included (like the
debian keyring). This is nasty, since mutt has to wait until gpg is finished
when displaying a gpg signed message (with signature verification on). So I've
written a tool that splits a huge keyring into a lot of smaller keyrings (one
key per keyring) and a shell script to verify signatures, to be used
from within mutt. The former tool is called &lt;code&gt;splitkeyring.sh&lt;/code&gt;. The
latter one is &lt;code&gt;gpgverify.sh&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;gpgverify.sh&lt;/code&gt; first invokes &lt;code&gt;gpg --verify&lt;/code&gt; as normal and
captures its output. If gpg failed because the key was not found in any
keyring, the script looks if the key is in one of the splitted keyrings, and if
so, reruns gpg with that keyring included. Otherwise the gpg error is returned.&lt;/p&gt;
&lt;p&gt;These scripts are still hacky, if you want to use them you'll probably have
to modify them a bit. They aren't too big, so this shouldn't be too much of a
problem.&lt;/p&gt;
&lt;p&gt;Download: &lt;a href="mutt/gpgverify"&gt;gpgverify&lt;/a&gt;&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="mutt"></category></entry><entry><title>A mutt patch for a more colorful index</title><link href="https://www.caichinger.com/blog/2006/08/01/mutt-indexcolor-patch" rel="alternate"></link><published>2006-08-01T00:00:00+02:00</published><updated>2006-08-01T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-08-01:/blog/2006/08/01/mutt-indexcolor-patch</id><summary type="html">&lt;p&gt;A patch to provide nicer index view for the mutt email client&lt;/p&gt;</summary><content type="html">&lt;p&gt;This patch enables different colorings for different parts of the index
display. For example you can choose one color for the subject, another one for
the author, and a third one for the flags.&lt;/p&gt;
&lt;p&gt;Screenshots:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="/mutt/mutt-indexcolor-1.png"&gt;Screenshot #1&lt;/a&gt;: Just a random screenshot...&lt;/li&gt;
&lt;li&gt;&lt;a href="/mutt/mutt-indexcolor-2.png"&gt;Screenshot #2&lt;/a&gt;: Notice how messages
    written by me are differently colored then those from other authors. This
    is done using indexcolor's author pattern matching.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Downloads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="/patches/mutt-1.5.12-indexcolor-3+cb.diff"&gt;mutt-1.5.12-indexcolor-3+cb.diff&lt;/a&gt;:
    indexcolor-3 forward-ported to mutt 1.5.12 by &lt;a href="http://www.df7cb.de/"&gt;Christoph
    Berg&lt;/a&gt;. Thanks a lot!&lt;/li&gt;
&lt;li&gt;&lt;a href="/patches/mutt-1.5.8-indexcolor-3.diff"&gt;mutt-1.5.8-indexcolor-3.diff&lt;/a&gt;:
    Added pattern to index_author and index_color command. Documentation
    updates and some other fixes. This one is stable and has the biggest
    feature-set.&lt;/li&gt;
&lt;li&gt;&lt;a href="/patches/mutt-1.5.8-indexcolor-2+cb.diff"&gt;mutt-1.5.8-indexcolor-2+cb.diff&lt;/a&gt;:
    -2 with additional fixes by Christoph Berg (thanks!). This one works really
    well.&lt;/li&gt;
&lt;li&gt;&lt;a href="/patches/mutt-1.5.8-indexcolor-2.diff"&gt;mutt-1.5.8-indexcolor-2.diff&lt;/a&gt;: Bugfix release&lt;/li&gt;
&lt;li&gt;&lt;a href="/patches/mutt-1.5.8-indexcolor-1.diff"&gt;mutt-1.5.8-indexcolor-1.diff&lt;/a&gt;: First release of
    the indexcolor patch. It's undocumented and buggy.&lt;/li&gt;
&lt;/ul&gt;</content><category term="blog"></category><category term="linux"></category><category term="mutt"></category></entry><entry><title>Vim notes</title><link href="https://www.caichinger.com/blog/2006/07/13/vim" rel="alternate"></link><published>2006-07-13T00:00:00+02:00</published><updated>2006-07-13T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2006-07-13:/blog/2006/07/13/vim</id><summary type="html">&lt;p&gt;A small vim script to update timestamps in files&lt;/p&gt;</summary><content type="html">&lt;h2&gt;Scripts&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="vim/UpdateModDate.vim"&gt;UpdateModDate.vim&lt;/a&gt; is a
  vim script I have written for updating the date-stamp of these pages
  automatically every time I save the files from within vim.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It searches for lines marked with &lt;code&gt;%DATE_TAG%&lt;/code&gt; and updates the date on
  these lines every time the file is saved. Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;Last Modified: Tue Jun  2 04:44:58 UTC 2004     // %DATE_TAG%
&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;Bugs&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;I've hit a bug in vim related to html highlighting that would cause get
  really really slow. This has already been fixed though. The interested might
  look at the &lt;a href="vim/bug1.html"&gt;detailed description&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content><category term="blog"></category><category term="vim"></category></entry><entry><title>Debian on Linksys WRT54GS (German)</title><link href="https://www.caichinger.com/blog/2005/08/28/wrt" rel="alternate"></link><published>2005-08-28T00:00:00+02:00</published><updated>2005-08-28T00:00:00+02:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2005-08-28:/blog/2005/08/28/wrt</id><summary type="html">&lt;p&gt;German notes on a talk I held at the Debienna meetup in August 2005.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Notizen zu einem kurzen Vortrag, den ich am 28.8.2005 beim &lt;a href="http://debienna.at"&gt;Debienna
Treffen&lt;/a&gt; gehalten habe.&lt;/p&gt;
&lt;h2&gt;Vortrag&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Was ist der WRT54GS?&lt;/li&gt;
&lt;li&gt;Was sind die Vorteile einer eigenen Firmware?&lt;/li&gt;
&lt;li&gt;Spielerei&lt;/li&gt;
&lt;li&gt;Shell auf dem WRT&lt;/li&gt;
&lt;li&gt;Mehr Möglichkeiten als die offizielle Linksys Firmware
    z.B: OpenVPN&lt;/li&gt;
&lt;li&gt;Meine Wahl: OpenWRT&lt;/li&gt;
&lt;li&gt;Paketsystem, ähnlich Debian&lt;/li&gt;
&lt;li&gt;Wieso zusätzlich noch Debian installieren?&lt;/li&gt;
&lt;li&gt;Spielerei&lt;/li&gt;
&lt;li&gt;Mipsel Architektur&lt;/li&gt;
&lt;li&gt;Gcc/Binutils -&amp;gt; Code schreiben&lt;/li&gt;
&lt;li&gt;Debugging&lt;/li&gt;
&lt;li&gt;Wie macht man's?&lt;/li&gt;
&lt;li&gt;Debootstrap will nicht (Bashisms)&lt;/li&gt;
&lt;li&gt;CDebootstrap muss her&lt;/li&gt;
&lt;li&gt;Debian Binary funktioniert auf OpenWRT nicht (glibc &amp;lt;-&amp;gt; uclibc)&lt;/li&gt;
&lt;li&gt;Man muss cdebootstrap (und libdebian-installer) .ipkg Pakete
    mit der OpenWRT Toolchain bauen&lt;/li&gt;
&lt;li&gt;Nicht genug Platz am WRT (nur 8Mb Flash) -&amp;gt; NFS share&lt;/li&gt;
&lt;li&gt;Bootstrap am WRT läuft nicht glatt durch weil dpkg fehlt:
    Das lässt sich zwar in Busybox einkompilieren, das erfordert aber etwas
    mehr Änderungen, und man muss den ganzen OpenWRT Tree bauen.&lt;/li&gt;
&lt;li&gt;Einfacherer Weg (etwas hacky):
    &lt;code&gt;cdebootstrap --arch&lt;/code&gt; am NFS server im Share, das scheitert irgendwann, weil
    Arch-spezifische Sachen fehlschlagen.
    Dpkg ist dann aber schon entpackt.
    Danach lässt man cdebootstrap am WRT laufen (selbe Optionen), das merkt,
    dass schon einiges da ist, und macht dort weiter wo cdebootstrap am Host
    aufgehört hat&lt;/li&gt;
&lt;li&gt;Danach: Chroot &amp;amp; Freude an Debian haben!&lt;/li&gt;
&lt;li&gt;Fragen?&lt;/li&gt;
&lt;li&gt;Danke für's Zuhören&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Bild vom WRT: &lt;a href="http://tinyurl.com/dqsrs"&gt;http://tinyurl.com/dqsrs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Infos über die verschiedenen Hardware-Versionen
  &lt;a href="http://www.linksysinfo.org/modules.php?name=Content&amp;amp;pa=showpage&amp;amp;pid=6"&gt;http://www.linksysinfo.org/modules.php?name=Content&amp;amp;pa=showpage&amp;amp;pid=6&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenWRT: &lt;a href="http://openwrt.org/"&gt;http://openwrt.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;OpenWRT SDK:
  &lt;a href="http://downloads.openwrt.org/whiterussian/rc2/bin/OpenWrt-SDK-Linux-i686-1.tar.bz2"&gt;http://downloads.openwrt.org/whiterussian/rc2/bin/OpenWrt-SDK-Linux-i686-1.tar.bz2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Source &amp;amp; Binaries&lt;/h2&gt;
&lt;p&gt;Liegen in &lt;a href="/div/wrt/"&gt;/div/wrt/&lt;/a&gt; herum.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="debian"></category></entry><entry><title>wmbutton: dockapp displaying configureable buttons</title><link href="https://www.caichinger.com/blog/2005/02/20/wmbutton" rel="alternate"></link><published>2005-02-20T00:00:00+01:00</published><updated>2005-02-20T00:00:00+01:00</updated><author><name>Christian Aichinger</name></author><id>tag:www.caichinger.com,2005-02-20:/blog/2005/02/20/wmbutton</id><summary type="html">&lt;p&gt;WMButton is a small utility to display command buttons in X sessions&lt;/p&gt;</summary><content type="html">&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I have been using wmbutton for quite some time, and I think it's
really nice. One thing that has always sucked was the need to recompile
just to change the images. So I got the source code of the wmbutton
Debian package and added the capability to change the images without a
recompile. One day later I discovered that someone (ehflora) had
already done this. I got his version and it looked a bit better then
mine. While playing around I noticed that wmbutton was leaking memory,
so I ran &lt;a href="http://packages.debian.org/valgrind"&gt;valgrind&lt;/a&gt; on
it, which confirmed my suspicions. I fixed the Leaks and did some more
code cleanup.&lt;/p&gt;
&lt;p&gt;I've increased the version number to 0.6. The code is still quite a
mess, but at least it's better then before. Feel free to further
improve it if you like, there's plenty room for it ;-).&lt;/p&gt;
&lt;h2&gt;Details&lt;/h2&gt;
&lt;p&gt;This program is released under the GPL version 2. A copy of the license
may be obtained at &lt;a href="http://www.gnu.org/licenses/gpl.txt"&gt;http://www.gnu.org/licenses/gpl.txt&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Screenshots and more can be found at:
&lt;a href="http://www.dockapps.org/file.php/id/241"&gt;http://www.dockapps.org/file.php/id/241&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Sourcecode Download&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="wmbutton/wmbutton-0.6.1.tar.gz"&gt;wmbutton-0.6.1.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="wmbutton/wmbutton-0.6.tar.gz"&gt;wmbutton-0.6.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Debian packages&lt;/h2&gt;
&lt;p&gt;wmbutton is available directly from Debian. Check out the &lt;a href="http://packages.debian.org/wmbutton"&gt;package
page&lt;/a&gt;.&lt;/p&gt;</content><category term="blog"></category><category term="linux"></category><category term="debian"></category></entry></feed>