{"word_count": 767, "character_count": 5464}
Yesterday, I was uploading some old posts to complete the migration (I get back to my old posts, one by one, to check links of pictures, reformating R codes, etc). And I re-discovered a post published amost 2 years ago, on nuns and Hell's Angels in an airplaine.It reminded me an old probability problem (that might be known as one on Feymann's problems): suppose that you have a prescription to take half pills for 6 days. Unfortunately the pharmacist was a bit lazy (or just wanted to help me to write a mathematical problem), and he gives 3 (full) pills in a small box. Day 1, you take a pill, break it in two parts, eat one, and return the other half in the box. Day 2, you draw randomly 'something' from the box, i.e. either half a pill, or a pill. If it's a half one, then you eat it. If it is a fill one, you break it in two, eat one half, and return the other half in the box. Etc.On Day 6, if my story was well explained, you should know that there can only be one half pill. So far, so good. But what about Day 5 ? There were either two half pills, or one full pill. But what was the probability that there was a fill pill in the box on Day 5 ?Nice problem, isn't it ?The good thing is that it can be modeled as a Markovian model. Assume that we do have  pills. After  days, the box will be empty. Consider the pair  denoting the number of half pills, and complete pills.  can take all values, from 0 to , and  will be positive, with . Thus, the number of states - possible pairs from Day 1 till Day  - will be , i.e. . More precisely, define those states in a dataframe,&#62; n=3&#62; COMPLETE=HALF=NULL&#62; for(i in n:0){+ HALF=c(0:(n-i),HALF)+ COMPLETE=c(rep(i,length(0:(n-i))),COMPLETE)+ }&#62; k=length(COMPLETE)&#62; state=data.frame(s=1:k,nc=rev(COMPLETE),nh=rev(HALF))&#62; states nc nh1   1  3  02   2  2  13   3  2  04   4  1  25   5  1  16   6  1  07   7  0  38   8  0  29   9  0  110 10  0  0Now, we can play to derive the transition matrix of the Markov chain.&#62; attach(state)&#62; P=matrix(0,k,k)&#62; for(i in 1:k){+ C=state$nc[i]+ H=state$nh[i]+ if((C&#62;0)&#38;(H&#62;0)){+ P[i,state[(nc==C-1)&#38;(nh==H+1),"s"]]= C/(C+H)+ P[i,state[(nc==C)&#38;(nh==H-1),"s"]]= H/(C+H)}+ if((C&#62;0)&#38;(H==0)){+ P[i,state[(nc==C-1)&#38;(nh==H+1),"s"]]=1}+ if((C==0)&#38;(H&#62;0)){+ P[i,state[(nc==C)&#38;(nh==H-1),"s"]]=1}+ if((C==0)&#38;(H==0)){+ P[i,state[(nc==C)&#38;(nh==H),"s"]]=1}+ }We do have a transition matrix (or a probability matrix) since all elements are positive, and the sum per line is 1,&#62; apply(P,1,sum)[1] 1 1 1 1 1 1 1 1 1 1Here, the transition matrix is the following&#62; P[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,]    0    1 0.00 0.00 0.00  0.0 0.00  0.0    0     0[2,]    0    0 0.33 0.66 0.00  0.0 0.00  0.0    0     0[3,]    0    0 0.00 0.00 1.00  0.0 0.00  0.0    0     0[4,]    0    0 0.00 0.00 0.66  0.0 0.33  0.0    0     0[5,]    0    0 0.00 0.00 0.00  0.5 0.00  0.5    0     0[6,]    0    0 0.00 0.00 0.00  0.0 0.00  0.0    1     0[7,]    0    0 0.00 0.00 0.00  0.0 0.00  1.0    0     0[8,]    0    0 0.00 0.00 0.00  0.0 0.00  0.0    1     0[9,]    0    0 0.00 0.00 0.00  0.0 0.00  0.0    0     1[10,]   0    0 0.00 0.00 0.00  0.0 0.00  0.0    0     1In order to get our probability, let us start from state 1 - or  - with probability 1, and let us look at the distribution at different periods,&#62; dist=c(1,rep(0,k-1))&#62; MatDist=matrix(NA,2*n+1,k)&#62; MatDist[1,]=dist&#62; for(i in 1:(2*n)){dist=as.vector(t(dist)%*%P)+ MatDist[i+1,]=dist+ }(one can check that after  days, the box is empty). The probability is given in row , and we just have to check which column corresponds to the pair ,&#62; vs=state[which(MatDist[2*n-1,]&#62;0),]&#62; proba=MatDist[2*n-1,vs[vs$nc==1,"s"]]&#62; proba[1] 0.3888889Here the probability of having a full pair on Day 5 is 38.89%.Actually, it is possible to study the evolution of this probability as a function of ,&#62; computeproba=function(n=3){+ COMPLETE=HALF=NULL+ for(i in n:0){+ HALF=c(0:(n-i),HALF)+ COMPLETE=c(rep(i,length(0:(n-i))),COMPLETE)+ }+ k=length(COMPLETE)+ state=data.frame(s=1:k,nc=rev(COMPLETE),nh=rev(HALF))+ P=matrix(0,k,k)+ for(i in 1:k){+ C=state$nc[i]+ H=state$nh[i]+ if((C&#62;0)&#38;(H&#62;0)){+ P[i,state[(state$nc==C-1)&#38;(state$nh==H+1),"s"]]= C/(C+H)+ P[i,state[(state$nc==C)&#38;(state$nh==H-1),"s"]]= H/(C+H)}+ if((C&#62;0)&#38;(H==0)){+ P[i,state[(state$nc==C-1)&#38;(state$nh==H+1),"s"]]=1}+ if((C==0)&#38;(H&#62;0)){+ P[i,state[(state$nc==C)&#38;(state$nh==H-1),"s"]]=1}+ if((C==0)&#38;(H==0)){+ P[i,state[(state$nc==C)&#38;(state$nh==H),"s"]]=1}+ }+ dist=c(1,rep(0,k-1))+ MatDist=matrix(NA,2*n+1,k)+ MatDist[1,]=dist+ for(i in 1:(2*n)){dist=as.vector(t(dist)%*%P)+ MatDist[i+1,]=dist+ }+ vs=state[which(MatDist[2*n-1,]&#62;0),]+ proba=MatDist[2*n-1,vs[vs$nc==1,"s"]]+ return(proba)+ }If we plot the probability as a function of , we get&#62; P=Vectorize(computeproba)(2:40)&#62; plot(2:40,P,ylim=c(0,.5))One can observe that the probability is decreasing. But slowly, extremely slowly. With a log scale on the y-axis, we have&#62; plot(2:40,P,ylim=c(0,.5),log="y")If we look for 'high' values, we can get&#62; computeproba(100)[1] 0.14218I do not know if this limit goes to 0 as  goes to infinity. Actually, since we do have to compute a matrix with   entries i.e. roughly ,  cannot be that large... Too bad. If anyone knows how this probability behaves as a function of , when  is large, I'd be glad to know...