Here is a model to forcast an election between two candidates A and B. There are $N$ people who will vote. After $n$ of them have voted, if $k$ of them voted for A we predict that the $n+1$-th person will vote for $A$ with probability $k/n$, and vote for $B$ with probability $1-k/n$.
Given the vote distribution at time $n_0$, find the probability that a particular candidate wins.
%pylab inline
import tqdm
%precision 4
# Total number of people
N = 10000
n0 = N//10 # Number of people we know have voted
fN = ones(N+1)
fN[0:N//2] = 0
fn = fN
for n in tqdm.tqdm_notebook( range(N-1, n0-1, -1) ):
# Probability next person votes for A is an array.
# If k people vote for A, pn[k] = k/n
pn = arange(n+1)/n # Compact way of writing pn[k] = k/n
# Solve the recurrance relation
fn = pn*fn[1:] + (1-pn)*fn[:-1]
# fn[k] is now the chance A wins given that at time n0, k people voted for him.
fn[n0//2+1]
nn = range( int(.4*n0), int(.6*n0) )
#nn = range(n0+1)
plot( nn, fn[nn] )
title( f'Probability A wins after {N} votes are cast')
ylabel( 'Probability A wins' )
xlabel( f'Number of votes for A after {n0} votes were cast')