<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-probability-to-win-is-hard-to-estimate/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://freakonometrics.hypotheses.org/56264"> R-english – Freakonometrics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Real-time computation (or estimation) of the “probability to win” is difficult. We’ve seem that in soccer games, in elections… but actually, as a professor, I see that frequently when I grade my students.</p>
<p>Consider a classical multiple choice exam. After each question, imagine that you try to compute the probability that the student will pass. Consider here the case where we have 50 questions. Students pass when they have 25 correct answers, or more. Just for simulations, I will assume that students just flip a coin at each question… I have <img alt="n" title="n" src="https://s0.wp.com/latex.php?latex=n&bg=ffffff&fg=000&s=0" class="latex"> students, and 50 questions</p>
<div><table><tr><td><pre>1
2
3
</pre></td><td><pre><span>set.<span>seed</span></span><span>(</span><span>1</span><span>)</span>
n<span>=</span><span>10</span>
M<span>=</span><span>matrix</span><span>(</span><span>sample</span><span>(</span><span>0</span><span>:</span><span>1</span>,size<span>=</span>n<span>*</span><span>50</span>,<span>replace</span><span>=</span>TRUE<span>)</span>,<span>50</span>,n<span>)</span></pre></td></tr></table></div>
<p>Let <img alt="X_{i,j}" title="X_{i,j}" src="https://s0.wp.com/latex.php?latex=X_%7Bi%2Cj%7D&bg=ffffff&fg=000&s=0" class="latex"> denote the score of student <img alt="i" title="i" src="https://s0.wp.com/latex.php?latex=i&bg=ffffff&fg=000&s=0" class="latex"> at question <img alt="j" title="j" src="https://s0.wp.com/latex.php?latex=j&bg=ffffff&fg=000&s=0" class="latex">. Let <img alt="S_{i,j}" title="S_{i,j}" src="https://s0.wp.com/latex.php?latex=S_%7Bi%2Cj%7D&bg=ffffff&fg=000&s=0" class="latex"> denote the cumulated score, i.e. <img alt="S_{i,j}=X_{i,1}+\cdots+X_{i,j}" title="S_{i,j}=X_{i,1}+\cdots+X_{i,j}" src="https://s0.wp.com/latex.php?latex=S_%7Bi%2Cj%7D%3DX_%7Bi%2C1%7D%2B%5Ccdots%2BX_%7Bi%2Cj%7D&bg=ffffff&fg=000&s=0" class="latex">. At step <img alt="j" title="j" src="https://s0.wp.com/latex.php?latex=j&bg=ffffff&fg=000&s=0" class="latex">, I can get some sort of prediction of the final score, using <img alt="\hat{T}_{i,j}=50\times S_{i,j}/j" title="\hat{T}_{i,j}=50\times S_{i,j}/j" src="https://s0.wp.com/latex.php?latex=%5Chat%7BT%7D_%7Bi%2Cj%7D%3D50%5Ctimes+S_%7Bi%2Cj%7D%2Fj&bg=ffffff&fg=000&s=0" class="latex">. Here is the code</p>
<div><table><tr><td><pre>1
2
</pre></td><td><pre>SM<span>=</span><span>apply</span><span>(</span>M,<span>2</span>,<span>cumsum</span><span>)</span>
NB<span>=</span>SM<span>*</span><span>50</span><span>/</span><span>(</span><span>1</span><span>:</span><span>50</span><span>)</span></pre></td></tr></table></div>
<p>We can actually plot it</p>
<div><table><tr><td><pre>1
2
3
4
</pre></td><td><pre><span>plot</span><span>(</span>NB<span>[</span>,<span>1</span><span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,ylim<span>=</span><span>c</span><span>(</span><span>0</span>,<span>50</span><span>)</span><span>)</span>
<span>abline</span><span>(</span>h<span>=</span><span>25</span>,<span>col</span><span>=</span><span>&quot;blue&quot;</span><span>)</span>
<span>for</span><span>(</span>i <span>in</span> <span>2</span><span>:</span>n<span>)</span> <span>lines</span><span>(</span>NB<span>[</span>,i<span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,<span>col</span><span>=</span><span>&quot;light blue&quot;</span><span>)</span>
<span>lines</span><span>(</span>NB<span>[</span>,<span>3</span><span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,<span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span></pre></td></tr></table></div>
<p><img alt="" src="https://i0.wp.com/f-origin.hypotheses.org/wp-content/blogs.dir/253/files/2018/11/Capture-d%E2%80%99e%CC%81cran-2018-11-06-a%CC%80-21.23.39.png?w=456&ssl=1" data-recalc-dims="1"><br>
But that’s <em>simply</em> the prediction of the final score, at each step. That’s not the computation of the probability to pass !</p>
<p>Let’s try to see how we can do it… If after <img alt="j" title="j" src="https://s0.wp.com/latex.php?latex=j&bg=ffffff&fg=000&s=0" class="latex"> questions, the students has 25 correct answer, the probability should be 1 – i.e. if <img alt="S_{i,j}\geq 25" title="S_{i,j}\geq 25" src="https://s0.wp.com/latex.php?latex=S_%7Bi%2Cj%7D%5Cgeq+25&bg=ffffff&fg=000&s=0" class="latex"> – since he cannot fail. Another simple case is the following : if after <img alt="j" title="j" src="https://s0.wp.com/latex.php?latex=j&bg=ffffff&fg=000&s=0" class="latex"> questions, the number of points he can get with all correct answers until the end is not sufficient, he will fail. That means if <img alt="" title="" src="https://s0.wp.com/latex.php?latex=&bg=ffffff&fg=000&s=0" class="latex">S_{i,j}+(50-i+1)&lt; 25[/latex] the probability should be 0. Otherwise, to compute the probability to sucess, it is quite straightforward. It is the probability to obtain at least [latex]25-S_{i,j}[/latex] correct answers, out of [latex]50-j[/latex] questions, when the probability of success is actually [latex]S_{i,j}/j[/latex]. We recognize the survival probability of a binomial distribution. The code is then simply</p>
<div><table><tr><td><pre>1
2
3
4
5
6
7
</pre></td><td><pre>PB<span>=</span>NB<span>*</span>NA
<span>for</span><span>(</span>i <span>in</span> <span>1</span><span>:</span><span>50</span><span>)</span><span>{</span>
  <span>for</span><span>(</span>j <span>in</span> <span>1</span><span>:</span>n<span>)</span><span>{</span>
    <span>if</span><span>(</span>SM<span>[</span>i,j<span>]</span><span>&amp;</span>gt<span>;=</span><span>25</span><span>)</span> PB<span>[</span>i,j<span>]</span><span>=</span><span>1</span>
    <span>if</span><span>(</span>SM<span>[</span>i,j<span>]</span><span>+</span><span>(</span><span>50</span><span>-</span>i<span>+</span><span>1</span><span>)</span><span>&amp;</span>lt<span>;</span><span>25</span><span>)</span>   PB<span>[</span>i,j<span>]</span><span>=</span><span>0</span>
    <span>if</span><span>(</span><span>(</span>SM<span>[</span>i,j<span>]</span><span>&amp;</span>lt<span>;</span><span>25</span><span>)</span><span>&amp;</span>amp<span>;</span><span>(</span>SM<span>[</span>i,j<span>]</span><span>+</span><span>(</span><span>50</span><span>-</span>i<span>+</span><span>1</span><span>)</span><span>&amp;</span>gt<span>;=</span><span>25</span><span>)</span><span>)</span> PB<span>[</span>i,j<span>]</span><span>=</span><span>1</span><span>-</span><span>pbinom</span><span>(</span><span>25</span><span>-</span>SM<span>[</span>i,j<span>]</span>,size<span>=</span><span>(</span><span>50</span><span>-</span>i<span>)</span>,prob<span>=</span>SM<span>[</span>i,j<span>]</span><span>/</span>i<span>)</span>
  <span>}</span><span>}</span></pre></td></tr></table></div>
<p>So if we plot it, we get</p>
<div><table><tr><td><pre>1
2
3
4
</pre></td><td><pre><span>plot</span><span>(</span>PB<span>[</span>,<span>1</span><span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,ylim<span>=</span><span>c</span><span>(</span><span>0</span>,<span>1</span><span>)</span><span>)</span>
<span>abline</span><span>(</span>h<span>=</span><span>25</span>,<span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span>
<span>for</span><span>(</span>i <span>in</span> <span>2</span><span>:</span>n<span>)</span> <span>lines</span><span>(</span>PB<span>[</span>,i<span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,<span>col</span><span>=</span><span>&quot;light blue&quot;</span><span>)</span>
<span>lines</span><span>(</span>PB<span>[</span>,<span>3</span><span>]</span>,type<span>=</span><span>&quot;s&quot;</span>,<span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span></pre></td></tr></table></div>
<p><img alt="" src="https://i1.wp.com/f-origin.hypotheses.org/wp-content/blogs.dir/253/files/2018/11/Capture-d%E2%80%99e%CC%81cran-2018-11-06-a%CC%80-21.23.49.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>which is much more volatile than the previous curves we obtained ! So yes, computing the “probability to win” is a complicated exercice ! Don’t blame those who try to find it hard to do !</p>
<p>Of course, things are slightly different if my students don’t flip a coin… this is what we obtain if half of the students are good (2/3 probability to get a question correct) and half is not good (1/3 chance),</p>
<p><img alt="" src="https://i1.wp.com/f-origin.hypotheses.org/wp-content/blogs.dir/253/files/2018/11/Capture-d%E2%80%99e%CC%81cran-2018-11-06-a%CC%80-21.55.44.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>If we look at the probability to pass, we usually do not have to wait until the end (the 50 questions) to know who passed and who failed</p>
<p><img alt="" src="https://i2.wp.com/f-origin.hypotheses.org/wp-content/blogs.dir/253/files/2018/11/Capture-d%E2%80%99e%CC%81cran-2018-11-06-a%CC%80-21.55.35.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>PS : I guess a less volatile solution can be obtained with a Bayesian approach… if I find some spare time this week, I will try to code it…</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://freakonometrics.hypotheses.org/56264"> R-english – Freakonometrics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/0ea1Xczg3qo" height="1"></a></div>