Gambler's Ruin(賭徒破產問題概率論) - 台部落
文章推薦指數: 80 %
Problem Statement · Definition · Limits · Notes · Constraints · Examples.
請輸入正確的登錄賬號或密碼
註冊
忘記密碼
首頁
概率DP
正文
Gambler'sRuin(賭徒破產問題概率論)
原創
solotzg
2018-08-2406:25
賭徒破產問題,做tc時遇到,順便拿來好好研究下
英文原版地址爲:Gambler'sRuin
問題如下:
一個賭徒有h枚金幣,每次有概率a獲得一枚金幣或者概率(1-a)丟掉一枚金幣,直到其所有的金幣總數達到N或0則遊戲結束,求賭徒最終贏得N枚金幣的概率P(N|h)。
對於兩個狀態我們可以確定,即P(N|N)=1、P(N|0)=0。
同時得出狀態轉移公式(概率的推導和普通的DP還是很不一樣的,好好體會下):
P(N|h)=a*P(N|h+1)+(1-a)*P(N|h-1)
這類公式可以表示爲二階線性遞歸關係,其特徵多項式爲(自行百度):
x^2-1/a*x+(1-a)/a=0
求出特徵方程的根爲1和r=(1-a)/a,針對a==1/2的情況需要特殊處理。
得到公式的通解爲:
P(N|h)=A*(1^h)+B*(r^h)
根據已知條件P(N|N)=1、P(N|0)=0得:
1=A+B*(r^N)
0=A+B
A=-1/(r^N-1)、B=1/(r^N-1)
得到最終解P(N|h)=(r^h-1)/(r^N-1)
但是當a==1/2時,特徵方程有重根,因此這種情況下通解爲
P(N|h)=A+B*h
A=0、B=1/N
即 P(N|h)=h/N
再來看topcodersrm667div1500的題
ProblemStatement
ThereareNcatssittingaroundacircle.Thecatsarenumbered0throughN-1inclockwiseorder.Notethatastheysitaroundacircle,catN-1isadjacenttocat0.Thecatsareplayingagameandthewinner
willgetaprize!
Thegamelooksasfollows:
Thereisasingleball.Initially,cat0holdstheball.Ineachroundofthegame,thecatwhocurrentlyholdsaballflipsabiasedcoin.Thecoinwillcomeupheadswithprobabilityp/1,000,000,000andtailswithprobability1-(p/1,000,000,000).Ifthecoincameupheads,thecurrentcatwillhandtheballtothenextcatclockwise,otherwisethecurrentcatwillhandtheballtothenextcatcounterclockwise.Formally,ifthecurrentcatiscatj,headsmeansthattheballgoestocat(j+1)modN
andtailsmeansthatitgoestocat(j-1)modN.Thegameisplayeduntileachcatheldtheballatleastonce.Thecatwhoholdstheballattheendofthegameisthewinner.Inotherwords,thewinneristhelastcattotouchtheball.Notethatcat0holdstheballatthebeginning,andthisdoescountasholdingtheball.Hence,ifthereismorethanonecat,cat0canneverwinthegame.
CatKwonderswhatistheprobabilitythatshewillwintheprize.YouaregiventheintsN,K,and
p.ReturntheprobabilitythatcatKwins.
Definition
Class:
CatsOnTheCircle
Method:
getProb
Parameters:
int,int,int
Returns:
double
Methodsignature:
doublegetProb(intN,intK,intp)
(besureyourmethodispublic)
Limits
Timelimit(s):
2.000
Memorylimit(MB):
256
Stacklimit(MB):
256
Notes
-
Yourreturnvaluemusthaveanabsoluteorrelativeerrorsmallerthanorequalto1e-6
Constraints
-
Nwillbebetween3and1,000,000,000,inclusive.
-
Kwillbebetween1andN-1,inclusive.
-
pwillbebetween1and999,999,999,inclusive.
Examples
0)
3
1
300000000
Returns:0.6999999999999985
ThisgamehasN=3cats,labeled0,1,2.Wehavep=30,000,000,hencethecoinwillcomeupheadswithprobability30,000,000/1,000,000,000=0.3andtailswithprobability0.7.Thegamecanlookasfollows:
Cat0isgiventheball.Cat0flipsthecoin.Thecoincomesuptails.Cat0handstheballtocat(0-1)mod3=cat2.Cat2flipsthecoin.Thecomesuptailsagain.Cat2handstheballtocat(2-1)mod3=cat1.Atthismoment,eachcathasheldtheball.Thegameendsandcat1getstheprize.
Thisparticularsequenceofeventshasprobability0.7*0.7ofoccuring.Itcanbeshownthattheprobabilitythatcat1winsthegameis0.7.
1)
6
2
500000000
Returns:0.2
Thecointhatisflippedwillcomeupheadswithprobability1/2,andtailswithprobability1/2.
2)
6
5
500000000
Returns:0.2
3)
10
2
666666666
Returns:0.00391389439551009
4)
999999999
999999996
777777777
Returns:0.05830903870125612
5)
1000000000
4
300000000
Returns:0.044981259448371
6)
534428790
459947197
500000000
Returns:1.871156682766205E-9
題意:
N只貓圍成一圈玩遊戲,順時針編號0~N-1,N-1與0相鄰。
遊戲規則如下:
、一開始編號0的貓拿着一個球
、每個回合中手裏拿球的貓拋硬幣,該硬幣有P/1000000000的概率正面朝上,(1-P/1000000000)的概率反面朝上
、如果硬幣正面朝上,則該貓j把球傳給編號爲(1+j)%N的貓,否則傳給編號爲(j-1+N)%N的貓
、該遊戲持續進行直到每隻貓至少拿到一次球。
且最終拿球的貓贏得遊戲
現在給定NKP,求出編號爲K的貓贏得遊戲的概率。
分析:
1.如果最終貓K拿到球並結束遊戲,那麼之前一回合必然是貓K-1或K+1拿球,且除K外的貓都至少拿過一次球。
則最終的結果爲P(K+1,K-1)+P(K-1,K+1),既貓K+1先拿到球的前提下K-1拿到球的概率加上貓K-1先拿到球的前提下K+1拿到球的概率。
這樣就可以了,因爲當全局只剩下K沒有拿過球,K必然是最後一個拿到球的。
2.這種情況和賭徒破產問題有什麼類似之處呢?再來回顧下賭徒破產問題,該問題求的是當前有h枚金幣的情況下,贏得N枚金幣的概率。
不如我們換一種表述方式,即該賭徒一開始最多能連續輸掉h枚金幣。
放到這題的環境中,我們假設順時針走等於金幣加一,逆時針走等於金幣減一。
3.以求解P(K-1,K+1)爲例,需要將其拆分爲兩種概率的乘積:P(a)=從0出發向左走最多到達K+2,且向右走必然到達K-1;P(b)=從K-1出發向右最多到達K-1,且向左走必然到達K+1;這樣一來就可以套賭徒破產問題了。
4.大於1.0的浮點數求冪可能會爆,需要控制一下
總結:
概率真是tm的神奇
#include
延伸文章資訊
- 1Gambler's Ruin(賭徒破產問題概率論) - 台部落
Problem Statement · Definition · Limits · Notes · Constraints · Examples.
- 2賭徒破產理論(Gambler's ruin)機率公式證明 - Quanist理財智
賭徒破產理論(Gambler's ruin)指指的是兩位賭徒,每局賭1元,A賭徒有i元,B賭徒有n-i元,兩人不斷的賭,直到一人輸光為止。 回到「機率統計」頁面 ...
- 3概率的应用Ⅱ:赌徒破产问题(Gambler's Ruin Problem) 和赛车 ...
- 4Gambler's Ruin Problem(赌徒破产问题)研究总结 - YorkFish ...
- 5The Gambler's Ruin Problem 賭徒破產@ 我只是愛按快門的人..