You are on page 1of 6

Q2: Select a number randomly with probability proportional to its magnitude

from the given array of n elements


consider an experiment, selecting an element from the list A randomly with probability proportional to its
magnitude. assume we are doing the same experiment for 100 times with replacement, in each experiment
you will print a number that is selected randomly from A.

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]:

from random import uniform


# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input exampl
es

# you can free to change all these codes/structure


def pick_a_number_from_list(A):
A = sorted(A) #Sort the list in ascending order
sum_A = sum(A) # sum of the elements in the list

# Normalise the elements (each element/sum of all elements)


A_norm = []
for x in A:
A_norm.append(x/sum_A)

# Cumulative of Normalised list


A_cum = []
d = A_norm[0] # starting element for cumulative
for x in A_norm: # for each element in normalise list
d += x # cumulative of each elements
A_cum.append(d)

# pick a random no. in the range 0-1


r = uniform(0,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

# get the element based on obtained index list "r_A_idx"


for i in r_A_idx:
return A[i] # probablity of picking up index/random number is prop
ortional to A[i] magnitude

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.

Please correct me, if any mistake in the code!!!!

{100: 64, 79: 11, 45: 8, 28: 5, 27: 4, 13: 3, 10: 2, 6: 2}

{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]:

# freq. of element in dict


In [5]:

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]

{100: 52, 79: 22, 45: 7, 28: 7, 27: 5, 13: 4, 10: 1, 6: 1}

In [ ]:

In [ ]:

You might also like