Try   HackMD

Coins pooling or not

Student ID: F74082125
Student Name: 張議隆
More information in hackmd website

TOC

How to run my code

Most commands are already written in Makefile

SEPERATE_CODE=Seperate_experiment
COMBINE_CODE=Combined_experiment

setup:
        export GSL_RNG_SEED=74082125
run_seperate:
        gcc -o $(SEPERATE_CODE)  $(SEPERATE_CODE).c  GSLfun.c -lgsl -lgslcblas -lm
        ./$(SEPERATE_CODE)

run_combine:
        gcc -o $(COMBINE_CODE)  $(COMBINE_CODE).c  GSLfun.c -lgsl -lgslcblas -lm
        ./$(COMBINE_CODE)

clean:
        rm $(SEPERATE_CODE)
        rm $(COMBINE_CODE)

To set the RNG seed, run make setup or make in short.
This command may not always work(at least in my enviorment). If make setup did not set the RNG seed, run export GSL_RNG_SEED=74082125 manually to set the seed.
Then you could run make run_seperate and run_combine to get the simulation result.
If the simulation took too long to generate the result, modify line 23 to a smaller number.

const unsigned long long int sample_count = 1000 * (1 << sample_power);

Seperate trials

Possible outcomes of 4 trials(s1)

Table entry

(s<4) taken directly from given pdf file
Cii12=(i12)(i32)12i!

s 0 1 2 3 4
Css0.5
1
12
38
516
35128
P[s]
35128
532
964
532
35128
128P[s]
35
20
18
20
35

Possible outcomes of 5 trials(s2)

Extended from the table above, and re-calculate

P[s]

s 0 1 2 3 4 5
Css0.5
1
12
38
516
35128
63256
P[s]
63256
35256
15128
15128
35256
63256
256P[s]
63
35
30
30
35
63

Probability table of s1, s2

s1=0
1 2 3 4
s2=0
6325635128
63256532
63256964
63256532
6325635128
1
3525635128
35256532
35256964
35256532
3525635128
2
1512835128
15128532
15128964
15128532
1512835128
3
1512835128
15128532
15128964
15128532
1512835128
4
3525635128
35256532
35256964
35256532
3525635128
5
6325635128
63256532
63256964
63256532
6325635128

Normalized form of above table

the below comes from above table

215=32768

s1=0
1 2 3 4
s2=0
2205
1260
1134
1260
2205
1
1225
700
630
700
1225
2
1050
600
540
600
1050
3
1050
600
540
600
1050
4
1225
700
630
700
1225
5
2205
1260
1134
1260
2205

Simulation Output

gcc -o Seperate_experiment  Seperate_experiment.c  GSLfun.c -lgsl -lgslcblas -lm
./Seperate_experiment

total sample points: 32768000
Using default random seed
0 2203936 1223708 1050776 1051424 1224938 2206024
1 1257968 701588 599610 601165 699895 1259920
2 1136410 630719 540751 539500 630802 1133816
3 1259445 699621 600675 600773 699740 1259132
4 2204491 1223057 1050313 1050340 1224293 2203170

Combined trials

Possible outcomes of 9 trials

s 0 1 2 3 4 5 6 7 8 9
Css0.5
1
12
38
516
35128
63256
2311024
4292048
643532768
1215565536
P[s]
1215565536
643565536
19305262144
214532768
220532768
220532768
214532768
19305262144
643565536
1215565536
262144P[s]
48620
25740
19305
17160
17640
17640
17160
19305
25740
48620

Combination table

Combination table are calculated using python code

Python Code
# combinations.py
from math import factorial

def nCk(n,k): 
  return factorial(n) / (factorial(k) * factorial(n - k))

S1_element = 4
S2_element = 5
max_number = 3

tuple_list = list()
combination_list = list()
for i in range(max(max_number-S2_element, 0),min(S1_element+1, max_number + 1)):
    tuple_list.append((i, max_number - i))
print(tuple_list)

for item in tuple_list:
    combination_list.append((int(nCk(S1_element, item[0])), int(nCk(S2_element, item[1]))))
print(combination_list)

ratio_list = list()
for item in combination_list:
   ratio_list.append(item[0] * item[1])
ratio_sum = sum(ratio_list)
print(ratio_list)
print(ratio_sum)
s combinations(
s1,s2
)
ratio probability
0
(0,0)
1
1
(0,1),(1,0)
5:4
59,49
2
(0,2),(1,1),(2,0)
10:20:6
518,59,16
3
(0,3),(1,2),(2,1),(3,0)
10:40:30:4
542,1021,1542,121
4
(0,4),(1,3),(2,2),(3,1),(4,0)
5:40:60:20:1
5126,2063,1021,1063,1126
5
(0,5),(1,4),(2,3),(3,2),(4,1)
1:20:60:40:5
1126,1063,1021,2063,5126
6
(1,5),(2,4),(3,3),(4,2)
4:30:40:10
121,1542,1021,542
7
(2,5),(3,4),(4,3)
6:20:10
16,59,518
8
(3,5),(4,4)
4:5
49,59
9
(4,5)
1

Probability Table

The following table was generated using the code below, some part was from modified version of code above.

Code
# probability.py
from math import factorial

def nCk(n,k): 
  return factorial(n) / (factorial(k) * factorial(n - k))

S1_element = 4
S2_element = 5
max_length = S1_element + S2_element

tuple_matrix = list()
ratio_matrix = list()
sum_list = list()
for current_index in range(max_length + 1):
  tuple_list = list()
  combination_list = list()
  for i in range(max(current_index-S2_element, 0),min(S1_element+1, current_index + 1)):
      tuple_list.append((i, current_index - i))
  print(tuple_list)

  for item in tuple_list:
      combination_list.append((int(nCk(S1_element, item[0])), int(nCk(S2_element, item[1]))))
  print(combination_list)

  ratio_list = list()
  for item in combination_list:
    ratio_list.append(item[0] * item[1])
  ratio_sum = sum(ratio_list)
  print(ratio_list)
  print(ratio_sum)

  tuple_matrix.append(tuple_list)
  ratio_matrix.append(ratio_list)
  sum_list.append(ratio_sum)

# I am too lazy to write a code to generate list of probability
import numpy as np
# P_list = [6435, 3432, 2772, 2520, 2450, 2520, 2772, 3432, 6435]
P_list = [48620, 25740, 19305, 17160, 17640, 17640, 17160, 19305, 25740, 48620]

P_matrix = np.zeros((S1_element + 1, S2_element + 1))

for item_list in tuple_matrix:
    for item, index in zip(item_list, range(len(item_list))):
        first_index = item[0]
        second_index = item[1]
        print(first_index, second_index)

        probability = P_list[first_index + second_index]
        ratio = ratio_matrix[first_index + second_index][index]
        denominator = sum_list[first_index + second_index]
        P_matrix[first_index][second_index] = probability * ratio / denominator

# print(P_matrix) 
for item_list in P_matrix:
    print("|", end="")
    for item in item_list:
        print(item, "|", end="")
    print()
s2=0
1 2 3 4 5
s1=0
48620.0 14300.0 5362.5 2042.86 700.0 140.0
1 11440.0 10725.0 8171.43 5600.0 2800.0 817.14
2 3217.5 6128.57 8400.0 8400.0 6128.57 3217.5
3 817.14 2800.0 5600.0 8171.43 10725.0 11440.0
4 140.0 700.0 2042.86 5362.5 14300.0 48620.0

Simulation Output

gcc -o Combined_experiment  Combined_experiment.c  GSLfun.c -lgsl -lgslcblas -lm
./Combined_experiment

This could take a while

total sample points: 262144000
Using default random seed
0 48622130 14298062 5716439 2200221 698170 139973
1 11438522 11441474 8802979 5596545 2799594 880475
2 3430081 6598164 8400003 8403540 6604028 3428588
3 879872 2800758 5600501 8800023 11443106 11438750
4 140201 701729 2203215 5719598 14292911 48624348

tags: 1112_courses probability models