- if true is pointless - you can leave it off
- The function should be in a separate file, not inside a for loop.
- In your for loop, you need to call your function (e.g.:
simulating an unbias coin with a bias coin flip
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Andrew Newell
2014-4-28
That's not an error, just a suggestion. You can preallocate outcome by adding the line
outcome = zeros(size(p));
before the loop.
采纳的回答
Andrew Newell
2014-4-28
编辑:Andrew Newell
2014-5-1
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 个评论
Andrew Newell
2014-5-2
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!