Professional Documents
Culture Documents
Ex 1: A = [0 5 27 6 13 28 100 45 10 79]
let f(x) denote the number of times x getting selected in 100 experiments.
f(100) > f(79) > f(45) > f(28) > f(27) > f(13) > f(10) > f(6) > f(5) > f(0)
In [1]:
# get the index of cumulative element,if the value >= random number.
r_A=[] # propablity of 'r' lies between these index.(#initi
ally empty list)
for i in range(len(A_cum)): # for each index of cumulaitive list
if r <= A_cum[i]: # if random no. is less than or equal to each elemen
t of cumul. list
r_A.append(i) # append that index in a empty list "r_A"
# get the index of Original list 'A' value based on cumulative index
if len(r_A)> 1: # if the cum. indexes are more than 1
r_A_idx =r_A[1:] # then original list index are except the 1st cum. in
dex's
else:
r_A_idx = r_A # else original list index is same as cumul. index
In [2]:
A = [0,5,27,6,13,28,100,45,10,79]
In [3]:
def sampling_based_on_magnitued():
for i in range(1,100):
number = pick_a_number_from_list(A)
print(number)
sampling_based_on_magnitued()
27
27
100
79
100
79
79
100
28
100
100
100
100
100
100
100
45
10
13
13
100
100
100
27
28
100
79
100
100
100
100
100
100
28
100
100
100
100
45
79
100
79
100
79
45
79
27
100
100
100
79
100
100
100
100
28
100
79
100
100
100
45
10
100
45
100
100
100
28
100
100
10
100
45
27
13
28
45
100
100
10
45
100
27
100
100
79
79
100
100
100
100
79
100
28
100
100
100
28
Doubt:
When i run the program, I am getting the frequencies a mention below:
Maximum number of times,the probablity of a number is not proportional to its magnitues.
{100: 50, 79: 15, 45: 7, 28: 10, 27: 3, 13: 8, 10: 4, 6: 2}
{100: 48, 79: 19, 45: 14, 28: 11, 27: 4, 13: 1, 10: 1, 6: 1}
In [4]:
def sampling_based_on_magnitued():
prob_num=[]
for i in range(1,100):
number = pick_a_number_from_list(A)
prob_num.append(number)
prob_num = sorted(prob_num,reverse=True)
print(prob_num)
print(" ")
# Compute frequency
count_ = {}
for e in prob_num:
count =prob_num.count(e)
count_[e] = count
print(count_)
sampling_based_on_magnitued()
[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 10
0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 1
00, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 79, 79, 79, 79, 79, 79, 79, 79, 7
9, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 45, 45, 45, 45, 45,
45, 45, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 13, 13, 13, 13, 1
0, 6]
In [ ]:
In [ ]: