How to perform Matlab programming for the biased coin toss simulation

Let the bias be the probability of turning up a head and denoted by the parameter q. If we use a coin with the bias specified by q to conduct a coin flipping process d times, the outcome will be a sequence of heads and tails.
Then the probability - where nH is the number of heads turned up during d trials. Now using such a simulated coin with q = ½ to conduct the experiments based on a sequence of outcomes generated by the random generator from computer.
For example, if the number generated by the random generator is less than 1/2, it assigned to be 0; otherwise, it will be assigned 1.
I would like to perform the Matlab programming with certain parameters for the Pattern Recognition (Binomial Distribution) as below -
(1) d = 100 and n =100 using a simulated coin with q = ¼ and ½.
(2) d = 10 and n =1000 using a simulated coin with q = ¼ and ½.
(3) d = 100 and n = 1000 using a simulated coin with q = ¼ and ½.
Kindly please provide your opinion and suggestion thus I will be able to improve my computing skills.
( The result of the execution from program will be drawn into histogram on the PowerPoint)

3 Comments

So what is your question here? Surely you know how to test if a value is less than 1/4? Or how to test if every element of an array is less than some constant?
If you want help on MATLAB, make an effort. Post the code you would write if you want an assessment of how to do it better, or want to know what you did wrong.
If you want a suggestion of how to do the above simulations, consider that you can call rand to generate a complete array of numbers in one call, and then test to see if every element of that array is less than some constant in one test. So you can doo virtually everything in very short code.
BUT MAKE THE EFFORT. You won't learn by being given the answers on a silver platter. You will learn by reading the help for these tools. By trying things.

Don't post your attempt as an answer. Moved to a comment, from vokoyo.

Matlab Programming Method - I get some errors for my coding sample as below

 x = rand(0,1);
    if(x<0.25),
    toss = 0; %Head
    else
    toss = 1; %Tail
    end
    n = 100;
    d = 100;
    hist(y,0:n)
        x = rand(0,1);
        if(x<0.5),
        toss = 0; %Head
        else
        toss = 1; %Tail
        end
        n = 100;
        d = 100;
        hist(y,0:n)

But now, tell us what errors you get? If you get an error, then read the error. Think about what it says. If you cannot understand the error message, then post the ENTIRE error message.

So, look at the error. What does it say? In this case, it will be some obscure problem, created because you did not even look at the very first thing you wrote.

x = rand(0,1);
x
x =
  0×1 empty double column vector

Look at the very first line you wrote. EXECUTE IT. LOOK AT THE RESULT. THINK ABOUT WHAT HAPPENED.

What does the help for rand tell you? Does rand(0,1) somehow know that maybe you wanted to generate a random number in the interval [0,1]? Or maybe you wanted it to sample from the integers in that range. How can rand know?

What rand does is go by how it is programmed. Not what you want it to do.

So rand(0,1) generates an empty vector. Not what you wanted of course.

Sign in to comment.

Answers (0)

Asked:

on 14 Apr 2018

Edited:

on 24 Apr 2018

Community Treasure Hunt

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

Start Hunting!