simulating an unbias coin with a bias coin flip

7 views (last 30 days)
Hello, I have a question about achieving an unbias coin with a bias coin flip for N = 1000 tosses, for a set of I have written code for unknown bias probabilities p = [ 0.5 0.4 0.3 0.2 0.1].I would like to choose these arbitrarily to achieve an unbias coin for each. I visited this site to read about the process http://www.pcoder.net/change-an-unfairly-biased-coin-to-a-fair-coin-a-classical-randomized-algorithm/#axzz308a9TpBx but im confuse about the implementation in matlab. thanks in advance for any feedback. thanks
if true
% calling the function
p = [ 0.5 0.4 0.3 0.2 0.1];
N = 1000;
for i = 1:length(p)
function outcome = mysim(p, N)
P = cumsum(p);
u = rand(1, N);
outcome = zeros(1, N); % A blank array for holding the outcomes
for n=100:100:N,
h = find(u(n)<P, 1 );
outcome(n) = h;
end % code
end
  3 Comments
Genus
Genus on 28 Apr 2014
I implemented your recommendation however there is an error at outcome(i) = mysim(p, N); it states the variable appears to change size on every loop iteration (within a script) consider preallocating for speed. What are your reccomendations for rectifying this issue, thanks
Andrew Newell
Andrew Newell on 28 Apr 2014
That's not an error, just a suggestion. You can preallocate outcome by adding the line
outcome = zeros(size(p));
before the loop.

Sign in to comment.

Accepted Answer

Andrew Newell
Andrew Newell on 28 Apr 2014
Edited: Andrew Newell on 1 May 2014
I think that the simplest approach is to write a little function to simulate a single toss, and then build up your simulation from that. Here is that function:
function side = simulateOneToss
% Make two tosses (outcomes 0 and 1 could stand for heads and tails)
twoTosses = round(rand(1,2));
% If outcome is not HT or TH (both of which sum to 1), try again.
while sum(twoTosses) ~= 1
twoTosses = round(rand(1,2));
end
% Take first of the two tosses as the answer.
side = twoTosses(1);
Based on the comments below, here is a version that allows a probability other than 0.5:
function side = simulateOneToss(p)
% Make two tosses (outcomes 0 and 1 could stand for heads and tails)
twoTosses = rand(1,2)<p;
% If outcome is not HT or TH (both of which sum to 1), try again.
while sum(twoTosses) ~= 1
twoTosses = rand(1,2)<p;
end
% Take first of the two tosses as the answer.
side = twoTosses(1);
Note that I don't round before comparing with the probability (otherwise you'll always get p=0.5 in effect). Now this can be used in a simulator:
function outcomes = simulateTosses(p,nTosses,nTrials)
tosses = zeros(nTosses,nTrials);
for i=1:numel(tosses)
tosses(i) = simulateOneToss(p);
end
outcomes = sum(tosses>p);
Finally, you can run it and plot the results as follows:
nTosses = 100;
nTrials = 10;
p = 0.6;
outcomes = simulateTosses(p,nTosses,nTrials);
bar(1:nTrials,outcomes)
  11 Comments
Genus
Genus on 2 May 2014
excellent, it works. I have a question about descision or hypothesis testing. suppose I have a conditional probability defined by the binomial probability
function [p] = calculateInputProbability(N, T)
inputP = [];
for i=1:N+1
combo = nchoosek(N,i-1);
temp = combo * power(T , i-1);
temp1 = temp * power((1-T), N-(i-1));
inputP(i) = temp1;
end
p = inputP;
end
im hoping to do hypothesis testing with H0 vs H1 the same specifications as before. but now with theta is in[0,1];. Ho having the conditional probability and theta = 1/2 and for H1 defined by the same conditional probability but theta =[ 0.5 0.4 0.3 0.2 0.1];
1) suppose we have 5 bias coins corresponding to each bias element theta =[ 0.5 0.4 0.3 0.2 0.1] and unbias coin corresponding to theta = 1/2. 2) we would like to choose an arbitrary bias single coin from the 5 coins and flip it 100 times and determine if the coin is bias or unbias. if its unbias probability is 1/2 otherwise bias. 3) we would like for each selected coin to simulate it from 100 to 1000 in steps of 100. . how do i rate you on this site. you have helped me greatly. here is my code so far. thanks
%% Input Values n0 = 0.5; n1 = [0.4 0.3 0.2 0.1]; nTosses = [100 200 300 400 500 600 700 800 900 1000]; nTrials = 1; for i = 1:length(n1) for j = 1: length(nToses) outcomes = simulateTosses(n1(i),nTosses(j),nTrials); end %% Calculate the prbability [p] = calculateInputProbability(nTosses(j), n1(i)); % code end
Andrew Newell
Andrew Newell on 2 May 2014
Genus, now you're on to a question on hypothesis testing, which should be posted separately. But first, I would recommend reading up on the relevant statistical methods. For example, there is a Wikipedia page Checking whether a coin is fair.
I'm glad I have been able to help you. We don't have ratings as such, but you can click on the "Accept this answer" button.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!