As a question in an assignment, I have been asked to write an Octave function that simulates 1000 experiments of finding a Random Variable X with alphabet {0, 1, 2, 3} and pmf:
Px(0) = 1/8
Px(1) = 1/4
Px(2) = 1/2
Px(3) = 1/8
by asking a sequence of binary, "yes" or "no" questions.
I have determined that the optimal sequence of binary questions asked to find the value of X is to simply ask "Is X = p?" where p are the possible values, in order of decreasing probabilty.
So the optimal sequence would be:
-
Is X = 2?
If not:
-
Is X = 1?
If not:
-
Is X = 0?
If not then X = 3
This is the function I have written:
function x = guessing_experiment(probabilities, n)
% generates n simulations of finding a random number in an alphabet by asking binary questions,
% where 'probabilities' is a list of the probabilities per number in the order the questions will be asked
num_Qs = zeros(1,n); % allocate array of size n for number of questions asked per experiment
[num_col, alphabet_size] = size(probabilities); % get size of alphabet
for i = 1:n % generate n experiments
Qs = 0; % number of questions asked in this experiment
for j = 1:alphabet_size - 1 % iterate through questions
question = rand; % generate random number in range [0, 1]
Qs++; % incremenet number of questions asked
if (question <= probabilities(j)) % if question produces a "yes" answer
break;
endif
endfor
num_Qs(i) = Qs; % store number of questions asked for this experiment
endfor
x = mean(num_Qs); % calculate mean number of questions asked over the n experiments
end
Which is called as guessing_experiment([1/2, 1/4, 1/8, 1/8], 1000)
where the array is the probability of each question producing a "yes" answer, in order of how they are to be asked, and n is the number of experiments.
Asking these questions should produce an average number of questions of 1.75, but my program is always producing an average of ~1.87. Where is my script in error?
Aucun commentaire:
Enregistrer un commentaire