simulating an unbias coin with a bias coin flip
Ältere Kommentare anzeigen
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 Kommentare
Andrew Newell
am 28 Apr. 2014
Bearbeitet: Andrew Newell
am 28 Apr. 2014
Let's see if you can get some code that actually runs first:
- 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.:
for i = 1:length(p)
outcome(i) = mysim(p,N);
end
Genus
am 28 Apr. 2014
Andrew Newell
am 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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Repeated Measures and MANOVA finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!