A Bit of Probability with Matlab

NaOH

Diamond Member
Mar 2, 2006
5,015
0
0
Prompt: Suppose you have two coins , one biased one fair, but you don't know which. Coin 1 is biased. It comes up heads with probability 3/4, while coin 2 will flip heads with probability 1/2. Suppose you pick a coin at random and flip it. Let Ci denote the event that coin i is picked (Ci = 0 coin two, Ci = 1 coin one). Let Hi denote whether it was heads or tails (Hi = 1 heads, Hi = 0 tails)


My code:

C = rand(1,50)<.5 %Generates 50 trials of picking each coin (50%)
i = 1;

while (i <= 50) %Repeat 50 times

if (C(i) == 0) %If coin two is picked, then generate one flip of fair coin
H(i) = rand(1)<.5 %
i = i + 1; %Increment vector location in C and H
else
H(i) = rand(1)>.25 %Else, generate one flip of biased coin (75%)
i = i + 1; %Increment vector location in C and H
end

end


Look good? The purpose is to be able to simulate the experiment 50 times (or more if I would like to).

edit* Comments ignore the spacing of comments, can't fix it and too lazy too. It looks fine in my original m file
 

Safeway

Lifer
Jun 22, 2004
12,075
11
81
Matlab code sucks. Why don't they stick with traditional programming languages?!
 

NaOH

Diamond Member
Mar 2, 2006
5,015
0
0
Originally posted by: chuckywang
What's the function definition of the rand function?

generates a vector with random numbers ranging from 0 to 1. For example, rand(1,100) generates a vector with one row and 100 columns. Equivalent to one experiment of one hundred trials.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: NaOH
Originally posted by: chuckywang
What's the function definition of the rand function?

generates a vector with random numbers ranging from 0 to 1. For example, rand(1,100) generates a vector with one row and 100 columns. Equivalent to one experiment of one hundred trials.

Forgive me, but I'm not familiar with Matlab.

I can't see where you declare H. And H is a vector of vectors? I'm basing it on this:

H(i) = rand(1)>.25
 

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
Originally posted by: BigJ
Originally posted by: NaOH
Originally posted by: chuckywang
What's the function definition of the rand function?

generates a vector with random numbers ranging from 0 to 1. For example, rand(1,100) generates a vector with one row and 100 columns. Equivalent to one experiment of one hundred trials.

Forgive me, but I'm not familiar with Matlab.

I can't see where you declare H. And H is a vector of vectors? I'm basing it on this:

H(i) = rand(1)>.25

No variable declarations are needed in MATLAB. I just wasn't familiar with the syntax rand(1,50) < .5

I guess it makes sense that it is a vector of fifty boolean values 0 or 1, depending if the original random vector at that index is < .5 or not.

If that is the case, then your script looks good.
 

NaOH

Diamond Member
Mar 2, 2006
5,015
0
0
Originally posted by: BigJ
Originally posted by: NaOH
Originally posted by: chuckywang
What's the function definition of the rand function?

generates a vector with random numbers ranging from 0 to 1. For example, rand(1,100) generates a vector with one row and 100 columns. Equivalent to one experiment of one hundred trials.

Forgive me, but I'm not familiar with Matlab.

I can't see where you declare H. And H is a vector of vectors? I'm basing it on this:

H(i) = rand(1)>.25

You pretty much don't have to declare anything with Matlab. If I set a variable equal to rand, it will automatically turn it into a vector pretty much.

From that line, I set the value of H(i), a position in the vector, to a single value generated by rand. Hence the 1 there, which should be equivalent to 1 row, 1 column. So I'm basically using the loop to fill H with 50 values (Heads or tails) depending on which coin was picked.
 

BigJ

Lifer
Nov 18, 2001
21,330
1
81
Originally posted by: NaOH
Originally posted by: BigJ
Originally posted by: NaOH
Originally posted by: chuckywang
What's the function definition of the rand function?

generates a vector with random numbers ranging from 0 to 1. For example, rand(1,100) generates a vector with one row and 100 columns. Equivalent to one experiment of one hundred trials.

Forgive me, but I'm not familiar with Matlab.

I can't see where you declare H. And H is a vector of vectors? I'm basing it on this:

H(i) = rand(1)>.25

You pretty much don't have to declare anything with Matlab. If I set a variable equal to rand, it will automatically turn it into a vector pretty much.

From that line, I set the value of H(i), a position in the vector, to a single value generated by rand. Hence the 1 there, which should be equivalent to 1 row, 1 column. So I'm basically using the loop to fill H with 50 values (Heads or tails) depending on which coin was picked.

Gotcha. Silly Matlab ;) I figured that's what you were doing. Looks fine to me then.
 

NaOH

Diamond Member
Mar 2, 2006
5,015
0
0
Hehe yeah, It's tricky switching back and forth from C and Matlab. Anyone actually use Matlab in industry? It seems slightly easier to use it for this purpose than C.
 

chuckywang

Lifer
Jan 12, 2004
20,133
1
0
Originally posted by: NaOH
Hehe yeah, It's tricky switching back and forth from C and Matlab. Anyone actually use Matlab in industry? It seems slightly easier to use it for this purpose than C.

Yeah, but MATLAB and C are completely different languages. MATLAB is an interpretative language: meaning it just runs one line after the other. There's no compiler since all you do is write scripts.
 

jiggahertz

Golden Member
Apr 7, 2005
1,532
0
76
Originally posted by: NaOH
Hehe yeah, It's tricky switching back and forth from C and Matlab. Anyone actually use Matlab in industry? It seems slightly easier to use it for this purpose than C.

Yup, I've been at a DoD research lab for the past 3 years and use Matlab 99% of the time. Can't imagine systems analysis without it.
 

oboeguy

Diamond Member
Dec 7, 1999
3,907
0
76
We use Matlab at my job too. Can't be beat for rapid prototyping and it runs our stuff pretty well too. Also, your code is way too complicated. You're not vectorizing nearly enough. Even if the loop were necessary you should have used the for loop, too. Here:

n = 50;
H = rand(1,n) > (0.5*ones(1,n) - 0.25*(rand(1,n)<0.5));

At least I think that does what you want.

Edit: typo in English (not Matlab code!)
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Originally posted by: chuckywang
Originally posted by: NaOH
Hehe yeah, It's tricky switching back and forth from C and Matlab. Anyone actually use Matlab in industry? It seems slightly easier to use it for this purpose than C.

Yeah, but MATLAB and C are completely different languages. MATLAB is an interpretative language: meaning it just runs one line after the other. There's no compiler since all you do is write scripts.

Actually MATLAB, like Java, has a JIT compiler--well, since v6 I think. So while it isn't true everything->machine code compilation, MATLAB is more than just an interpreter.

Additionally, since the MATLAB core (not the gui--that's Java) is built largely on C, a lot of the built-ins are just wrapper functions that call compiled code in Fortran or C. This has actually led to some hilarious fvck-ups... like a few versions ago, sort() was a call to qsort.c. issorted() was written in .m code. It was actually FASTER to sort() an array than to call issorted() on it--kind of eliminates the point of issorted right? Though since then, issorted() changed to a C function.

NaOH: You won't notice it on such a small problem, but you -should- at least declare all of your vector & matrix variables in MATLAB. It speeds up performance significantly if you pre-allocate. Else MATLAB is calling a resize() type of function everytime you do H(1) = blah, H(2) = blahblah, H(3) = foo, etc. Of course, just because you declare H() as a 100-element array doesn't mean you can't resize it later, but at least H isn't resized every time you add to it. And moreover, its just good practice to declare everything--readability ftw.