 # countgain8.py

Aug 20th, 2018
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #! /usr/bin/python
2. import copy as CO
3. import bisect as BI
4. import math as MA
5. '''
6. dice game
8. '''
9.
10. class CountingValueError(ValueError):
11.     pass
12.
13. def arangements_ie( n, l ):
14.     """ arrangements_ie ( n,( k1, k2, k3,...) )
15.    Return the number of possible arrangements of n elements
16.    with k1, k2,.... elements being indistinguishable.
17.    """
18.     a= MA.factorial( n )
19.     K=0
20.     for k in l:
21.         K += k
22.         if K > n:
23.             raise CountingValueError ("Can't have k indistinguishable objects taken from n objects if k > n")
24.         a= a / MA.factorial( k )
25.     return a
26. def result(result):
27.     print "gain\t\tresult"
28.     for r in result:
29.         pass
30.
31.
32. def storegain(trialgain,trialproba):
33.     #print "### trial gain:",trialgain,"|proba:",trialproba*100,"%"
34.     global tp
35.     tp+=trialproba
36.     i = BI.bisect_right(totwin,trialgain) -1
37.     totproba [i] += trialproba
38.
39. def trial(t):
40.     global g_no
41.     global g_N
42.     trialproba = 0.0
43.     trialgain = 0.0
44.     for i  in range (g_no):
45.         k=t[i]
46.         if k!=0:
47.             p = gamewin[i]
48.             gain = gamewin[i]
49.             trialgain += gain * k
50.             proba=MA.pow(p,k)
51.             if trialproba == 0:
52.                 trialproba= proba
53.             else:
54.                trialproba*= proba
55.             #print "-- outcome:",i,"|k:",k,"|p:",p,"|totgain:", gain * k
56.     trialproba*=arangements_ie(g_N,t)
57.
58.     storegain(trialgain, trialproba)
59.
60.
61. def callback ( t):
62.
63.     #print t
64.     #print "."
65.     global cpt
66.     cpt+=1
67.     trial(t)
68.
69. def rec_gen ( co , t ,s ):
70.     '''
71.     N: number of experiment still  available
72.     co: current outcome (position in t )
73.     t : list of occurence number for each outcome
74.    '''
75.
76.     for i in xrange ( 0,g_N-s+1 ):
77.         t[ co ] = i
78.         ss=s+i
79.         if ss == g_N :
80.              callback ( t )
81.              continue
82.
83.         if  co > 0 :
84.             t2 = CO.copy ( t )
85.             rec_gen ( co-1 , t2,ss )
86.
87.     del(t)
88.     return
89.
90. def gen ( no , N ):
91.     '''
92.    no: number outcome
93.    N : Number of subsequent experiment (trial of N experiment)
94.    t : contain the number of occurence for each outcomes
95.    '''
96.     global g_no  # number of outcome
97.     g_no = no
98.     global g_N  # number of experiment
99.     g_N = N
100.     print "===", g_no,g_N
101.     t = [ 0 ] * no
102.     rec_gen ( no-1 , t,0 )
103.
104. if __name__ == "__main__":
105.
106.     # number of expiriment (how many time I throw the dices)
107.     N = 100
108.
109.     #list of outcomes  (proba, gain)
110.     gamewin = [ [1.0/6, 200], [2.0/6, 10], [3.0/6, 0] ]
111.
112.     # game expectation E(X)
113.     gameexpect= 0
114.     for g in gamewin:
115.         gameexpect += g[ 0 ] * g[ 1 ]
116.     print "Game expectation = ", gameexpect
117.     # maxwin after N experiments
118.     maxwin = gameexpect * N
119.
120.     # to have the total gains  we get after N experiments  well distributed in
121.     #  the result  list [(gains,proba), .... ]
122.
123.     totwin = range( 0, int(maxwin), int(maxwin/10))
124.     totproba = [0.0] * len( totwin )
125.
126.     cpt = 0
127.     tp=0
128.     gen( len(gamewin),N )
129.     print "cpt",cpt,"tp",tp
130.     result = zip( totwin , totproba)
131.     print result
132.     # verifying that the calculus are corrects total probability for all final
133.     # gains = 1
134.     print "should get near 1:",sum(totproba)
RAW Paste Data