M ardışık olarak verilen N ardışık elde etmek için beklenen madalyon sayısı


10

Interviewstreet, Ocak ayında aşağıdaki soruyu içeren ikinci CodeSprint'lerini yaptı. Programlı cevap yayınlanır ancak istatistiksel bir açıklama içermez.

(Orijinal sorunu ve yayınlanan çözümü Google kredileriyle Interviewstreet web sitesinde oturum açıp bu sayfadan Coin Tosses sorununa giderek görebilirsiniz .)

Bozuk Para Atışları

N ardışık kafa elde edene kadar savurmaya devam etmek istediğiniz tarafsız bir paranız var. Madeni parayı M kez fırlattınız ve şaşırtıcı bir şekilde, tüm fırlatmalar kafalarla sonuçlandı.

N ardışık kafa elde edene kadar beklenen ek fırlatma sayısı nedir?

Giriş:
İlk satır T sayısını içerir. Sonraki T satırlarının her biri iki N ve M numarası içerir.

Çıktı: İlgili
test durumu için cevabı içeren çıktı T çizgileri. Yanıtı tam olarak 2 ondalık basamağa yuvarlayın.

Örnek Giriş:
4
2 0
2 1
3 3
3 2

Örnek Çıktı:
6.00
4.00
0.00
8.00

Örnek Açıklamalar:
N = 2 ve M = 0 ise, art arda 2 kafa alana kadar jetonu atmaya devam etmeniz gerekir. Ortalama 6 madeni paraya ihtiyaç duyulduğunu göstermek zor değil.
N = 2 ve M = 1 ise, arka arkaya 2 kafaya ihtiyacınız vardır ve zaten 1'e sahipsiniz. Ne olursa olsun, bir kez daha atmanız gerekir. Bu ilk atışta, kafa alırsan, bitirdin. Aksi takdirde, ardışık sayaç sıfırlandıkça baştan başlamanız ve N = 2 ardışık kafa elde edene kadar jetonu atmaya devam etmeniz gerekir. Beklenen jeton fırlatma sayısı 1 + (0,5 * 0 + 0,5 * 6) = 4,0 N = 3 ve M = 3 ise, zaten 3 başınız var, bu yüzden daha fazla fırlatmaya ihtiyacınız yok.

Ortaya koyduğum tüm matematiksel denklemler, yukarıda listelenen örnek girdi verileri için doğru cevaplara sahipti, ancak diğer tüm girdi kümeleri (yanlış bilinen) için yanlıştı. Programatik çözümleri sorunu denklemle gelmeye çalış yöntemimden çok farklı çözüyor gibi görünüyor. Birisi bunu çözecek bir denklemin nasıl bulunacağını açıklayabilir mi?


1
Ayrıca Daniel Johnson tarafından verilen sonucunu da bulacağımız buraya bakınız. 2N+12M+1
Dilip Sarwate

Yanıtlar:


8

Bu hesaplamalı bir alıştırmadır, bu yüzden tekrar tekrar düşünün . Para çevirme mevcut durumu sıralı çift ile belirlenir ile . Ardışık kafaya ulaşmak için beklenen saygısızlık :(N,M)NM0Ne(N,M)

(1)% 50 şans bir sonraki flip baş olacak, sizi devlete götürecek ve% 50 şans bir sonraki flip kuyruk olacak ve sizi devlete götürecek . Bu bir seferlik bir maliyet. Bu nedenle (özyinelemeli) beklenti,(N,M+1)(N,0)

e(N,M)=12e(N,M+1)+12e(N,0)+1.

(2) Base conditions: you have already stipulated that

e(N,0)=2N+12

and obviously

e(N,N)=0

(no more flips are needed).

Here's the corresponding Mathematica program (including caching of intermediate results to speed up the recursion, which effectively makes it a dynamic programming solution):

e[n_, m_] /; n > m > 0 := e[n, m] = 1 + (e[n, m + 1] + e[n, 0])/2 // Simplify
e[n_, 0] := 2^(n + 1) - 2
e[n_, n_] := 0

The program would look similar in other programming languages that support recursion. Mathematically, we can verify that e(N,M)=2N+12M+1 simply by checking the recursion, because it obviously holds for the base cases:

2N+12M+1=1+(2N+12M+2+2N+12)/2,

which is true for any M and N, QED.


More generally, the same approach will establish that e(N,M)=pNpM1p when the coin has probability p of heads. The hard part is working out the base condition e(N,0). That is done by chasing the recursion out N steps until finally e(N,0) is expressed in terms of itself and solving:

e(N,0)=1+pe(N,1)+(1p)e(n,0)=1+p(1+pe(N,2)+(1p)e(N,0))+(1p)e(N,0)=1+p+p2++pN1+(1p)[1+p++pN1]e(N,0);e(N,0)=1pN1p+(1pN)e(N,0);e(N,0)=pN11p.

1
Perhaps working iteratively rather than recursively might be better? You have
e(N,M)=12e(N,M+1)+12e(N,0)+1
which gives
e(N,M+1)=2e(N,M)2N+1
and so
e(N,1)=2e(N,0)2N+1=2(2N+12)2N+1=2N+14e(N,2)=2e(N,1)2N+1=2(2N+14)2N+1=2N+18
and so on giving
e(N,M)=2N+12M+1.
Or the difference equation could be solved "theoretically" instead of by iteration.
Dilip Sarwate

@Dilip The inferences you draw both at "which gives" and "and so on" are recursive. What solution method do you have in mind by "solved theoretically"?
whuber

To my mind, the difference between recursive and iterative is the method of working. For the relation
x(n)=2x(n1),  x(0)=1,
recursion calculates x(n) as
x(n)=2x(n1)=2(2x(n2))=2(2(2x(n3)))==2(2(2x(0))=2n
while iteration says
x(0)=1x(1)=2x(0)=2x(2)=2x(1)=22x(n)=2x(n1)=22n1=2n.
"theoretically" meant solving a difference equation by finding the characteristic polynomial, determining its roots, and then fitting the general solution to the initial conditions, instead of simple calculation as above.
Dilip Sarwate

From a programming viewpoint, a program find_x(n) is recursive if it says something like if n=0 return 1 else return 2*find_x(n-1) in which find_x calls itself n times, whereas an iterative program would say something like y = 1; while n > 0 do begin y=2*y; n=n-1 end; return y
Dilip Sarwate

If you look at how those programs are actually implemented on the computer, @Dilip, in many environments (such as R) they scarcely differ at all. (In one case you create and then process a vector 1:n and in the other you will discover that n:1 has been placed on the stack and processed in reverse.) But part of my point was conceptual: your initial comment spoke of "working iteratively." That referred to the analysis and not to any computer program. But these are trivial, tangential points whose discussion doesn't warrant extending this comment thread.
whuber

5

To solve this problem, I will use stochastic processes, stopping times, and dynamic programming.

First, some definitions:

Xn#(of consecutive heads after the nth flip)
We also allow a value for X0 to mean the number of consecutive heads before we start. So, for X0=0 and the sequence of flips HHTHHHTHTTHH, the corresponding values of X are 120123010012. If we had X0=M, the values of X would be (M+1)(M+2)0123010012.

Then define the following stopping times:

τNmin{k:Xk=N} and τ0min{k>1:Xk=0}

The value we are looking for is the expected value of τN, the number of flips it takes to observe N consecutive flips (XτN=N), given that we have already observed M consecutive flips (X0=M). Assume MN as the answer is trivially 0 otherwise. We compute:

E[τN|X0=M]=E[τN1{τN<τ0}+τN1{τN>τ0}|X0=M]
=(NM)(12)NM+E[τ0|τN>τ0,X0=M]+(1(12)NM)E[τN|X0=0]
This leaves us to compute the last two conditional expectations.

The first corresponds to the expected number of flips before getting a tail assuming a tail is flipped before N consecutive heads are observed assuming we start with M consecutive heads. It is not too difficult to see that

E[τ0|τN>τ0,X0=M]=j=1NM(j)(12)j=2(NM+2)(12)NM

Now all we have to do is compute the second conditional expectation which corresponds to the expected number of flips it takes to observe N consecutive heads starting from 0. With a similar calculations, we see that

E[τN|X0=0]=E[τN1{τN<τ0}+τN1{τN>τ0}|X0=0]
=N(12)N+E[τ0|τN>τ0,X0=0]+(1(12)N)E[τN|X0=0]
=2N{N(12)N+(2(N+2)(12)N)}
=2N+12

This gives a final answer of:

E[τN|X0=M]=(NM)(12)NM+2(NM+2)(12)NM+(1(12)NM)(2N+12)
=2N+12M+1

This agrees with the four test cases you've listed. With such a simple answer, there may be an easier way to compute this.


1
This is a harder way to solve it than the recursive idea listed above, but it's extremely useful to see both approaches laid out together. Most people don't appreciate the way that stopping time methods can be used for small, practical problems too.
ely

2

Warning: the following may not be considered as a proper answer in that it does not provide a closed form solution to the question, esp. when compared with the previous answers. I however found the approach sufficiently interesting to work out the conditional distribution.

Consider the preliminary question of getting a sequence of N heads out of k throws, with probability 1p(N,k). This is given by the recurrence formula

p(N,k)={1if k<Nm=1N12mp(N,km)else
Indeed, my reasoning is that no consecutive N heads out of k draws can be decomposed according to the first occurrence of a tail out of the first N throws. Conditioning on whether this first tail occurs at the first, second, ..., Nth draw leads to this recurrence relation.

Next, the probability of getting the first consecutive N heads in mN throws is $$ q(N,m) =\begin{cases} \dfrac{1}{2^N} &\text{if }m=N\

     p(N,m-N-1) \dfrac{1}{2^{N+1}} &\text{if } N<m<2N+1
     \end{cases}

$$ The first case is self-explanatory. the second case corresponds to a tail occuring at the mN1th draw, followed by N heads, and the last case prohibits N consecutive heads prior to the mN1th draw. (The two last cases could be condensed into one, granted!)

Now, the probability to get M heads first and the first consecutive N heads in exactly mN throws (and no less) is $$ r(M,N,m) = \begin{cases} 1/2^N &\text{if }m=N\

     0 &\text{if } N<m\le N+M\\

      \dfrac{1}{2^{M}}\sum_{r=M+1}^{N}\dfrac{1}{2^{r-M}}q(N,m-r)&\text{if } N+M<m

\end{cases}

Hencetheconditionalprobabilityofwaiting$m$stepstoget$N$consecutiveheadsgiventhefirst$M$consecutiveheadsis
s(M,N,m) = \begin{cases} 1/{2^{N-M}} &\text{if }m=N\ 0 &\text{if } N \sum_{r=M+1}^{N}\dfrac{q(N,m-r)}{2^{r-M}}&\text{if } N+M

\end{cases}

Theexpectednumberofdrawscanthenbederivedby
\mathfrak{E}(M,N)= \sum_{m=N}^\infty m\, s(M,N,m) $$ or E(M,N)M for the number of additional steps...
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.