Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab betting on a biased coin

Subject: Matlab betting on a biased coin

From: Kaye

Date: 11 Feb, 2013 12:31:18

Message: 1 of 3

I want to run a biased coin toss simulation, where at each toss i 'bet' a certain fraction of my current wealth. Then i want to run this for 100+ tosses, betting the same fraction each time, and plot how my wealth changes over time
This is what i have so far but i think there is something wrong within the for loop. can anyone help with this? Thanks K
Ntoss = 100;
Wealth(1)=100;
bet(1)=0.5*100; %bet 50% of my current wealth each time
for i=2:Ntoss
bet(i)=0.5*Wealth(i-1);
s = -bet(i)* ones(1, Ntoss);
u = rand(1, Ntoss);
h = find(u < 0.6); % finds where it was heads.
winnings = cumsum(s);
Wealth(i)=Wealth(i-1)+winnings(i);
end
plot(1:Ntoss, Wealth)
xlabel('Toss')
ylabel('S(n)')

Subject: Matlab betting on a biased coin

From: Kaye

Date: 11 Feb, 2013 12:52:09

Message: 2 of 3

***There is a line missing after
h = find(u < 0.6); % finds where it was heads.
it should say
s(h)=2*bet(i); since i double my bet if i win

Subject: Matlab betting on a biased coin

From: anyone

Date: 11 Feb, 2013 18:15:54

Message: 3 of 3

On Mon, 11 Feb 2013 12:31:18 +0000, Kaye wrote:

> I want to run a biased coin toss simulation, where at each toss i 'bet'
> a certain fraction of my current wealth. Then i want to run this for
> 100+ tosses, betting the same fraction each time, and plot how my wealth
> changes over time This is what i have so far but i think there is
> something wrong within the for loop. can anyone help with this?
> Ntoss = 100;
> Wealth(1)=100;
> bet(1)=0.5*100; %bet 50% of my current wealth each time

in a manner of writing, I suppose. there is only one "bet(1)".

> for i=2:Ntoss
> bet(i)=0.5*Wealth(i-1);

start building a vector, first two values are 50.

> s = -bet(i)* ones(1, Ntoss);

create a 100-element vector, all "-50".

> u = rand(1, Ntoss);

on this pass, generate a 100-element vector of rands.

> h = find(u < 0.6); % finds where it was heads.

find indices for arbitrarily-defined and artificially weighted 'heads'.

> s(h)=2*bet(i);

changes elements of 's', at indices of 'h' to 100, on this pass.

> winnings = cumsum(s);

march along the array 's', artificially adding everything up, element by element.

> Wealth(i)=Wealth(i-1)+winnings(i);

on this pass, subtract a very large number from '50'.

> end, [plot stuff]

do you now see how this works?

> Thanks K

for what?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us