<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>101</title>
<link>https://manurathi1.github.io/</link>
<atom:link href="https://manurathi1.github.io/index.xml" rel="self" type="application/rss+xml"/>
<description>Quant &amp; AI, from first principles</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Thu, 01 Dec 2022 00:00:00 GMT</lastBuildDate>
<item>
  <title>Drawdown and Its Linearity</title>
  <dc:creator>Manu Rathi</dc:creator>
  <link>https://manurathi1.github.io/posts/2022-drawdown-linearity/</link>
  <description><![CDATA[ 





<p>In this article, we first discuss the conditional drawdown at risk (CDaR), its special cases — maximum drawdown and average drawdown — and then move to the optimization problem for the return–CDaR efficient frontier as discussed by Uryasev et al. <sup>1</sup></p>
<section id="assumption" class="level2">
<h2 class="anchored" data-anchor-id="assumption">Assumption</h2>
<p>This approach concentrates on the portfolio equity curve over a particular sample path (historical, or the most probable future sample path) — i.e.&nbsp;we define a sample-path risk function rather than a risk measure over a <em>set</em> of sample paths. It makes no assumption about the underlying probability distribution, which unlocks various practical applications.</p>
</section>
<section id="cdar" class="level2">
<h2 class="anchored" data-anchor-id="cdar">CDaR</h2>
<p>For some value of the confidence parameter <img src="https://latex.codecogs.com/png.latex?%5Calpha">, the <img src="https://latex.codecogs.com/png.latex?%5Calpha">-CDaR is defined as the mean of the worst <img src="https://latex.codecogs.com/png.latex?(1-%5Calpha)%20%5Ctimes%20100%5C%25"> drawdowns experienced over some period of time on a sample path.</p>
<p>The CDaR risk function is based on the CVaR concept and can be viewed as a modification of CVaR to the case where the loss function is defined as a drawdown. Similarly to CVaR — the average of all losses greater than or equal to <img src="https://latex.codecogs.com/png.latex?%5Calpha">-VaR — CDaR is the average of all drawdowns greater than or equal to the <img src="https://latex.codecogs.com/png.latex?%5Calpha"> drawdown-at-risk (<img src="https://latex.codecogs.com/png.latex?%5Czeta_%5Calpha">), such that a low CVaR ensures a low VaR.</p>
<blockquote class="blockquote">
<p>Risk functions are parameterized by the portfolio weight matrix <img src="https://latex.codecogs.com/png.latex?W">; <img src="https://latex.codecogs.com/png.latex?T"> is the total time period considered for the calculation — the sample-path time horizon.</p>
</blockquote>
<p><span id="eq-cdar"><img src="https://latex.codecogs.com/png.latex?%0ACDaR_%5Calpha(W)%20=%20%5Cfrac%7B1%7D%7B(1-%20%5Calpha)T%7D%20%5Cint_%7B%5COmega_%5Calpha%7D%20D(W,t)%20%5C,dt%0A%5Ctag%7B1%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5COmega(W)%20=%20%5C%7Bt%20%5Cin%20%5B0,T%5D%20:%20D(W,t)%20%5Cgeq%20%5Czeta_%5Calpha(W)%5C%7D">.</p>
<blockquote class="blockquote">
<p>By definition, with a specified probability <img src="https://latex.codecogs.com/png.latex?%5Calpha">, the <img src="https://latex.codecogs.com/png.latex?%5Calpha">-DaR (<img src="https://latex.codecogs.com/png.latex?%5Czeta_%5Calpha">) of a portfolio is the lowest amount such that the probability the drawdown will not exceed <img src="https://latex.codecogs.com/png.latex?%5Czeta_%5Calpha"> is greater than or equal to <img src="https://latex.codecogs.com/png.latex?%5Calpha">.</p>
</blockquote>
<p>We consider two risk functions that are special cases of CDaR — maximum drawdown (MaxDD) and average drawdown (AvDD) — which are limiting cases as <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Cto%201"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Cto%200"> respectively:</p>
<p><span id="eq-maxdd"><img src="https://latex.codecogs.com/png.latex?%0AMaxDD(W)%20=%20%5Cmax_%7Bt%20%5Cin%20%5B0,T%5D%7D%20D(W,t)%0A%5Ctag%7B2%7D"></span></p>
<p><span id="eq-avdd"><img src="https://latex.codecogs.com/png.latex?%0AAvDD(W)%20=%20%5Cfrac%7B1%7D%7BT%7D%20%5Cint_%7B0%7D%5E%7BT%7D%20D(W,t)%20%5C,dt%0A%5Ctag%7B3%7D"></span></p>
</section>
<section id="linear-optimization" class="level2">
<h2 class="anchored" data-anchor-id="linear-optimization">Linear optimization</h2>
<section id="maxdd" class="level3">
<h3 class="anchored" data-anchor-id="maxdd">MaxDD</h3>
<p>We consider a modification of the classic return–risk efficient-frontier optimization: maximize return while constraining maximum drawdown to a pre-defined level.</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Cmax_%7BW%7D%7BW%5ETR%7D%20"></p>
<p>constrained on</p>
<p><img src="https://latex.codecogs.com/png.latex?%20MaxDD(W)%20%5Cleq%20v_1%20C%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?R"> is the return matrix <img src="https://latex.codecogs.com/png.latex?R%20=%20%5Br_1,%20r_2,%20r_3,%20%5Cdots,%20r_%5Cgamma%5D">, <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> is all constituents, and <img src="https://latex.codecogs.com/png.latex?v_1"> is the percentage of deployed capital <img src="https://latex.codecogs.com/png.latex?C"> allowed to be in drawdown.</p>
<p>Maximum drawdown is <strong>not linear</strong> in the parameter <img src="https://latex.codecogs.com/png.latex?W">. It can be made linear using an auxiliary variable, as follows:</p>
<p><span id="eq-maxdd-k"><img src="https://latex.codecogs.com/png.latex?%0AMaxDD(W)%20:%20%5Cmax_%7Bk%20%5Cin%20(0,T)%7D%20DD_k%20%5Cleq%20v_1%20C%0A%5Ctag%7B4%7D"></span></p>
<p><span id="eq-maxdd-aux"><img src="https://latex.codecogs.com/png.latex?%0AMaxDD(W)%20:%20%5Cmax_%7Bk%20%5Cin%20(0,T)%7D%20%5CBig(%20%5Cbig(%5Cmax_%7Bj%20%5Cin%20(0,k)%7D%20W%5ETR_j%5Cbig)%20-%20W%5ETR_k%20%5CBig)%20%5Cleq%20v_1%20C%0A%5Ctag%7B5%7D"></span></p>
<p>Converting the first maximum function in Equation&nbsp;5, using the property of maximum, applies a constraint on each day to be less than the desired drawdown:</p>
<p><span id="eq-maxdd-linear1"><img src="https://latex.codecogs.com/png.latex?%0AMaxDD(W):%20%5Cbig(%5Cmax_%7Bj%20%5Cin%20(0,k)%7D%20W%5ETR_j%5Cbig)%20-%20W%5ETR_k%20%5Cleq%20v_1%20C%20%5Cquad%20%5Cforall%5C%20k%20%5Cin%20%5B0,T%5D%0A%5Ctag%7B6%7D"></span></p>
<p>Converting the second max function:</p>
<p><span id="eq-maxdd-linear2"><img src="https://latex.codecogs.com/png.latex?%0AMaxDD(W)%20:%20u_k%20-%20W%5ETR_k%20%5Cleq%20v_1%20C%20%5Cquad%20%5Cforall%5C%20k%20%5Cin%20%5B0,T%5D%0A%5Ctag%7B7%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?u_k%20=%20%5Cmax_%7Bj%20%5Cin%20(0,k)%7D%20(W%5ETR_j)"> is an auxiliary variable.</p>
<p>Defining <img src="https://latex.codecogs.com/png.latex?u_k"> in Equation&nbsp;7: <img src="https://latex.codecogs.com/png.latex?u_k"> is equal to a maximum function, hence it’s an increasing function.</p>
<p><span id="eq-uk-increasing"><img src="https://latex.codecogs.com/png.latex?%0Au_k%20%5Cgeq%20u_%7Bk-1%7D%20%5Cquad%20%5Cforall%5C%20k%20%5Cin%20%5B0,T%5D,%5C%20%5Ctext%7Bwith%20%7D%20u_0%20=%200%0A%5Ctag%7B8%7D"></span></p>
<p>Also, <img src="https://latex.codecogs.com/png.latex?u_k"> should be equal to or greater than <img src="https://latex.codecogs.com/png.latex?W%5ETR_j">:</p>
<p><span id="eq-uk-geq"><img src="https://latex.codecogs.com/png.latex?%0Au_k%20%5Cgeq%20W%5ETR_j%20%5Cquad%20%5Cforall%5C%20j%20%5Cin%20%5B0,k%5D,%5C%20k%20%5Cin%20%5B0,T%5D%0A%5Ctag%7B9%7D"></span></p>
<p><span id="eq-w-bounds"><img src="https://latex.codecogs.com/png.latex?%0AW_%7Bmin%7D%20%5Cleq%20W%20%5Cleq%20W_%7Bmax%7D%0A%5Ctag%7B10%7D"></span></p>
<p>So the optimization is converted to a convex optimization problem with a linear performance function and a piecewise-linear convex constraint, given by maximizing return subject to Equation&nbsp;6 through Equation&nbsp;10.</p>
</section>
<section id="cdar-1" class="level3">
<h3 class="anchored" data-anchor-id="cdar-1">CDaR</h3>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Cmax_%7BW%7D%7BW%5ETR%7D%20"></p>
<p>constrained on</p>
<p><img src="https://latex.codecogs.com/png.latex?%20CDaR_%5Calpha(W)%20%5Cleq%20v_2%20C%20"></p>
<p>Rewriting Equation&nbsp;1:</p>
<p><span id="eq-cdar-rewrite"><img src="https://latex.codecogs.com/png.latex?%0ACDaR_%5Calpha(W)%20=%20%5Cmin_%7B%5Czeta%7D%5CBig%5B%5Czeta+%5Cfrac%7B%5Csum_%7Bk%20%5Cin%20(0,T)%7D%5BDD_k-%5Czeta%5D%5E+%7D%7B(1-%5Calpha)T%7D%5CBig%5D%0A%5Ctag%7B11%7D"></span></p>
<p>where the function <img src="https://latex.codecogs.com/png.latex?%5Bx%5D%5E+"> is defined as <img src="https://latex.codecogs.com/png.latex?%5Cmax(0,x)">.</p>
<p>The CDaR constraint function can be linearized as follows:</p>
<p><span id="eq-cdar-linear"><img src="https://latex.codecogs.com/png.latex?%0A%5Czeta+%5Cfrac%7B%5Csum_%7Bk%20%5Cin%20(0,T)%7Dz_k%7D%7B(1-%5Calpha)T%7D%0A%5Ctag%7B12%7D"></span></p>
<p>Defining <img src="https://latex.codecogs.com/png.latex?z_k">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20z_k%20=%20%5BDD_k-%5Czeta%5D%5E+%20"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?z_k"> is the positive part:</p>
<p><span id="eq-zk-geq"><img src="https://latex.codecogs.com/png.latex?%0Az_k%20%5Cgeq%20DD_k%20-%20%5Czeta%20%5Cquad%20%5Cforall%5C%20k%20%5Cin%20%5B0,T%5D%0A%5Ctag%7B13%7D"></span></p>
<p>Using Equation&nbsp;7, <img src="https://latex.codecogs.com/png.latex?DD_k"> can be written in terms of <img src="https://latex.codecogs.com/png.latex?u_k">:</p>
<p><span id="eq-zk-uk"><img src="https://latex.codecogs.com/png.latex?%0Az_k%20%5Cgeq%20u_k%20-%20W%5ETR_k%20-%20%5Czeta%20%5Cquad%20%5Cforall%5C%20k%20%5Cin%20%5B0,T%5D%0A%5Ctag%7B14%7D"></span></p>
<p>And since <img src="https://latex.codecogs.com/png.latex?z_k"> is always positive:</p>
<p><span id="eq-zk-pos"><img src="https://latex.codecogs.com/png.latex?%0Az_k%20%5Cgeq%200%0A%5Ctag%7B15%7D"></span></p>
<p>Similarly to MaxDD, the optimization problem converts to maximizing return subject to Equation&nbsp;14, Equation&nbsp;15, and Equation&nbsp;8 through Equation&nbsp;10.</p>
<p>An important feature of this formulation is that it does not involve the threshold function <img src="https://latex.codecogs.com/png.latex?%5Czeta_%5Calpha"> directly. At the optimal solution, the variables <img src="https://latex.codecogs.com/png.latex?W"> and <img src="https://latex.codecogs.com/png.latex?%5Czeta_%5Calpha"> together give the optimal portfolio and the corresponding value of the threshold function — see <sup>2</sup> for further discussion.</p>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Alexei Chekhlov, Stanislav Uryasev, Michael Zabarankin. <em>Portfolio Optimization With Drawdown Constraints.</em> <a href="https://www.ise.ufl.edu/uryasev/files/2011/11/drawdown.pdf">ise.ufl.edu/uryasev</a>↩︎</p></li>
<li id="fn2"><p>R. Tyrrell Rockafellar, Stanislav Uryasev. <em>Optimization of Conditional Value-at-Risk.</em> <a href="https://www.ise.ufl.edu/uryasev/files/2011/11/CVaR1_JOR.pdf">ise.ufl.edu/uryasev</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Optimization</category>
  <category>Risk</category>
  <guid>https://manurathi1.github.io/posts/2022-drawdown-linearity/</guid>
  <pubDate>Thu, 01 Dec 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Four-Moment Simulation — Mean, Variance, Skew, Kurtosis</title>
  <dc:creator>Manu Rathi</dc:creator>
  <link>https://manurathi1.github.io/posts/2021-four-moment-simulation/</link>
  <description><![CDATA[ 





<p>In the previous article, <a href="../../posts/2021-two-moment-simulation/index.html">Two-Moment Simulation</a>, we discussed how to simulate normally-distributed points for a given mean and standard deviation / covariance matrix.</p>
<p>Here, we discuss how to generate non-normal numbers with a unimodal distribution — i.e. simulate using all four moments (mean, standard deviation, skewness, and kurtosis), ignoring coskewness and cokurtosis.</p>
<p>We refer to the paper by Hao Luo <sup>1</sup>, which uses Fleishman’s power method <sup>2</sup> to simulate non-normal data.</p>
<p>The idea is to first simulate a series <img src="https://latex.codecogs.com/png.latex?Y"> using the power method:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20Y%20%5Csim%20D(0,1,%20%5Cgamma_%7B1%7D,%20%5Cgamma_%7B2%7D)%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cgamma_1">, <img src="https://latex.codecogs.com/png.latex?%5Cgamma_2"> are the skewness and excess kurtosis of the distribution <img src="https://latex.codecogs.com/png.latex?D">, respectively.</p>
<p>Second, we use our understanding from the previous article to convert the first two moments of <img src="https://latex.codecogs.com/png.latex?D"> from <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D"> to <img src="https://latex.codecogs.com/png.latex?%5B%5Cmu,%20%5Csigma%20/%20%5CSigma%5D">.</p>
<section id="algorithm" class="level2">
<h2 class="anchored" data-anchor-id="algorithm">Algorithm</h2>
<p>As per the power method, <img src="https://latex.codecogs.com/png.latex?Y"> can be defined as a third-degree polynomial of <img src="https://latex.codecogs.com/png.latex?X">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20Y%20=%20a%20+%20bX%20+%20cX%5E%7B2%7D%20+%20dX%5E%7B3%7D%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?X%20%5Csim%20N(0,1)">.</p>
<p>We solve for:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20E(Y)%20=%200,%5Cquad%20E(Y%5E%7B2%7D)%20=%201,%5Cquad%20E(Y%5E%7B3%7D)%20=%20%5Cgamma_%7B1%7D,%5Cquad%20E(Y%5E%7B4%7D)%20=%20%5Cgamma_%7B2%7D%20+%203%20"></p>
<p>The following four equations follow from solving the above:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20F_1(b,c,d)%20:%20b%5E2+6bd+%202c%5E2%20+15d%5E2-1%20=0%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?%20F_2(b,c,d)%20:%202c(b%5E2+24bd+105d%5E2+2)-%5Cgamma_1=0"></p>
<p><img src="https://latex.codecogs.com/png.latex?%20F_3(b,c,d)%20:%2024%5Cbig(bd%20+%20c%5E2%5B1%20+%20b%5E2%20+%2028bd%5D%20+%20d%5E2%5B12%20+%2048bd%20+%20141c%5E2%20+%20225d%5E2%5D%5Cbig)-%5Cgamma_2%20=%200%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?%20a%20=%20-c%20"></p>
<p>To solve for <img src="https://latex.codecogs.com/png.latex?a,%20b,%20c,%20d">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Cmin_%7Bb,c,d%7D%20F(b,c,d)%20=%20F_1%5E2(b,c,d)+F_2%5E2(b,c,d)+F_3%5E2(b,c,d)%20"></p>
<p>constrained on</p>
<p><img src="https://latex.codecogs.com/png.latex?%20F_i(b,c,d)%20=0%20%5Cquad%20%5Cforall%5C%20i%20%5Cin%20(1,3)%20"></p>
<p>A solution to the above exists only when</p>
<p><img src="https://latex.codecogs.com/png.latex?%20%5Cgamma_2%20=%20-1.2264489%20+%201.6410373%5C,%20%5Cgamma_1%5E2%20"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://manurathi1.github.io/posts/2021-four-moment-simulation/FourMomentSimulation_Gamma.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<p>Finally, apply the transformation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%20Y%5B%5Cmu,%20%5CSigma,%20%5Cgamma_1,%20%5Cgamma_2%5D%20=%20%5Cmu%20+%20%5Csqrt%7B%5CSigma%7D%20%5Ctimes%20Y%5B0,%201,%20%5Cgamma_1,%20%5Cgamma_2%5D%20"></p>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>Let’s simulate a beta distribution with <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%204.5"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%201">. The left chart shows the PDF of the beta distribution, the simulated beta using the methodology above, and a normal distribution with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu(%5Ctext%7BBeta%7D(4.5,1))"> and standard deviation <img src="https://latex.codecogs.com/png.latex?%5Csigma(%5Ctext%7BBeta%7D(4.5,1))">. The right chart shows the cumulative distribution of the same, excluding the normal.</p>
<div class="quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://manurathi1.github.io/posts/2021-four-moment-simulation/FourMomentSimulation_Pdf.jpg" class="img-fluid"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://manurathi1.github.io/posts/2021-four-moment-simulation/FourMomentSimulation_CumSim.jpg" class="img-fluid"></p>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The PDF and its cumulative distribution are very close to each other by the Kolmogorov–Smirnov statistic.</p>
</blockquote>
<p>First four moment values for all three cases:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: center;">Stat</th>
<th style="text-align: center;">Beta</th>
<th style="text-align: center;">Normal</th>
<th style="text-align: center;">Simulation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">Mean</td>
<td style="text-align: center;">0.82</td>
<td style="text-align: center;">0.82</td>
<td style="text-align: center;">0.81</td>
</tr>
<tr class="even">
<td style="text-align: center;">Std</td>
<td style="text-align: center;">0.15</td>
<td style="text-align: center;">0.15</td>
<td style="text-align: center;">0.15</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Skew</td>
<td style="text-align: center;">-1.13</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">-1.08</td>
</tr>
<tr class="even">
<td style="text-align: center;">Kurt</td>
<td style="text-align: center;">1.02</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0.88</td>
</tr>
</tbody>
</table>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Hao Luo (2011). <em>Generation of Non-normal Data – A Study of Fleishman’s Power Method.</em> <a href="https://www.diva-portal.org/smash/get/diva2:407995/FULLTEXT01.pdf">diva-portal.org</a>↩︎</p></li>
<li id="fn2"><p>Fleishman, A. I. (1978). <em>A method for simulating non-normal distributions.</em> Psychometrika. <a href="https://link.springer.com/article/10.1007/BF02293811">springer.com</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Probability</category>
  <category>Simulation</category>
  <guid>https://manurathi1.github.io/posts/2021-four-moment-simulation/</guid>
  <pubDate>Thu, 14 Oct 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Two-Moment Simulation — Mean and Variance</title>
  <dc:creator>Manu Rathi</dc:creator>
  <link>https://manurathi1.github.io/posts/2021-two-moment-simulation/</link>
  <description><![CDATA[ 





<p>This article looks at how to simulate a series with a normal distribution — i.e.&nbsp;where the higher moments (skew, excess kurtosis) are zero. For simulation with four moments, see <a href="../../posts/2021-four-moment-simulation/index.html">Four-Moment Simulation</a>.</p>
<p>In the first part we look at the univariate case (1 dimension); in the second, we see how to incorporate covariance for more than one dimension.</p>
<section id="univariate-simulation-1-dimension" class="level2">
<h2 class="anchored" data-anchor-id="univariate-simulation-1-dimension">Univariate simulation (1 dimension)</h2>
<p>The idea is to simulate a series</p>
<p><img src="https://latex.codecogs.com/png.latex?%20X%20%5Csim%20N(%5Cmu%20,%20%5Csigma)%20"></p>
<p>given <img src="https://latex.codecogs.com/png.latex?%5Cmu"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma">.</p>
<p><img src="https://manurathi1.github.io/posts/2021-two-moment-simulation/twoMomentNorm.png" class="img-fluid"></p>
<blockquote class="blockquote">
<p>The probability distribution for a random variable that is normally distributed: every point <img src="https://latex.codecogs.com/png.latex?x_i%20=%20%5Cmu%20+%20z%20%5Ctimes%20%5Csigma">, where <img src="https://latex.codecogs.com/png.latex?z%20%5Cin%20(-%5Cinfty,%20%5Cinfty)">.</p>
</blockquote>
<p><img src="https://latex.codecogs.com/png.latex?%20X%20%5Csim%20N(%5Cmu%20,%20%5Csigma)%20%5Cmid%20z%20%5Csim%20N(0,1)%20"></p>
<p><img src="https://latex.codecogs.com/png.latex?%20X%20=%20%5Cmu%20+%20N(0,1)%20%5Ctimes%20%5Csigma%20"></p>
<p>To generate <img src="https://latex.codecogs.com/png.latex?N(0,1)"> we can use the Box–Muller transformation. In Excel:</p>
<pre><code>NORM.S.INV(rand())</code></pre>
<p>where <code>rand()</code> draws from a uniform distribution on (0,1), and <code>NORM.S.INV</code> is the inverse of the cumulative normal distribution.</p>
<div class="quarto-layout-panel" data-layout-ncol="2">
<div class="quarto-layout-row">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://manurathi1.github.io/posts/2021-two-moment-simulation/TwoMomentSimulation_Mean.jpg" class="img-fluid"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="https://manurathi1.github.io/posts/2021-two-moment-simulation/TwoMomentSimulation_Std.jpg" class="img-fluid"></p>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The number of simulated data points is critical to the accuracy of the simulation — more data points lead to higher accuracy.</p>
</blockquote>
<p>Higher accuracy for the expected mean and variance with fewer data points can be achieved using random matrix theory — a topic for a future post.</p>
</section>
<section id="multivariate-simulation-1-dimension" class="level2">
<h2 class="anchored" data-anchor-id="multivariate-simulation-1-dimension">Multivariate simulation (&gt;1 dimension)</h2>
<p>The idea is to simulate a series</p>
<p><img src="https://latex.codecogs.com/png.latex?%20X%20%5Csim%20N(M%20,%20%5CSigma)%20"></p>
<p>given <img src="https://latex.codecogs.com/png.latex?M"> (an array of means) and <img src="https://latex.codecogs.com/png.latex?%5CSigma"> (a covariance matrix).</p>
<p><img src="https://latex.codecogs.com/png.latex?%20X%20=%20M%20+%20N(0,1)%20%5Ctimes%20%5Csqrt%7B%5CSigma%7D%20"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B%5CSigma%7D%20=%20L"> is the Cholesky decomposition of <img src="https://latex.codecogs.com/png.latex?%5CSigma%20=%20LL%5ET">.</p>


</section>

 ]]></description>
  <category>Probability</category>
  <category>Simulation</category>
  <guid>https://manurathi1.github.io/posts/2021-two-moment-simulation/</guid>
  <pubDate>Tue, 12 Oct 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Pinball Maze</title>
  <dc:creator>Manu Rathi</dc:creator>
  <link>https://manurathi1.github.io/posts/2020-pinball-maze/</link>
  <description><![CDATA[ 





<section id="problem-statement" class="level2">
<h2 class="anchored" data-anchor-id="problem-statement">Problem statement</h2>
<p>Imagine a pinball maze like the one below. A ball is dropped into the maze <img src="https://latex.codecogs.com/png.latex?N"> times. A few assumptions: (1) no ball ever leaves the container placed below the maze, and (2) at each pin (each dot in the figure) the ball has only two options — left or right, each equally likely.</p>
<p>The question: as we do this for a large number of throws,</p>
<p><img src="https://latex.codecogs.com/png.latex?%20N%20%5Crightarrow%20%5Cinfty%20"></p>
<p>what does the distribution of balls across the containers (i.e.&nbsp;the frequency of balls in each slot) converge to?</p>
<p><img src="https://manurathi1.github.io/posts/2020-pinball-maze/pinball.png" class="img-fluid"></p>
</section>
<section id="simulation" class="level2">
<h2 class="anchored" data-anchor-id="simulation">Simulation</h2>
<p>Let’s simulate the situation in Python.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># function for simulating one throw</span></span>
<span id="cb1-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> oneThrow(numOfLayers):</span>
<span id="cb1-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generate left(-1) / right(+1) using a binomial distribution, equal probability</span></span>
<span id="cb1-4">    bernoulliNumbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.binomial(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, numOfLayers)</span>
<span id="cb1-5">    decideNumbers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> arr: [<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arr])(bernoulliNumbers)</span>
<span id="cb1-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># final container slot</span></span>
<span id="cb1-7">    finalContainerSlot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> decide <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> decideNumbers:</span>
<span id="cb1-9">        finalContainerSlot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> decide</span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> decideNumbers, finalContainerSlot</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">numberOfLayers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb2-2">numberOfThrows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">finalContainerSlotArr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [oneThrow(numberOfLayers)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(numberOfThrows)]</span></code></pre></div></div>
<p>Each throw is a sum of <code>numberOfLayers</code> independent <img src="https://latex.codecogs.com/png.latex?%5Cpm%201"> steps — by the central limit theorem, as <code>numberOfLayers</code> grows, the distribution of <code>finalContainerSlot</code> across many throws converges to a normal distribution. The pinball maze is, in effect, a physical Galton board.</p>


</section>

 ]]></description>
  <category>Maths</category>
  <category>Probability</category>
  <guid>https://manurathi1.github.io/posts/2020-pinball-maze/</guid>
  <pubDate>Thu, 28 May 2020 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
