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:25:22

Message: 1 of 3

I'm trying to run a simluation on a biased coin toss. At each toss i want to 'bet' a specific fraction of my current wealth on the coin toss, then run this for 100+ tosses and plot my wealth over these tosses. This is what i have so far but something in my for loop is not right. Can anyone help with this?
Thanks
K

>>Ntoss = 100;
Wealth(1)=100;
bet(1)=50;
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: someone

Date: 11 Feb, 2013 15:20:25

Message: 2 of 3

"Kaye" wrote in message <kfanvi$do5$1@newscl01ah.mathworks.com>...
> I'm trying to run a simluation on a biased coin toss. At each toss i want to 'bet' a specific fraction of my current wealth on the coin toss, then run this for 100+ tosses and plot my wealth over these tosses. This is what i have so far but something in my for loop is not right. Can anyone help with this?
> Thanks
> K
>
> >>Ntoss = 100;
> Wealth(1)=100;
> bet(1)=50;
> 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)')

Without knowing what the error message is (if any), I have two suggestions:

Change:
s = -bet(i)* ones(1, Ntoss); % matrix multiplication
To:
s = -bet(i).*ones(1, Ntoss); % element-by-element multipl;ication

Change:
Wealth(i)=Wealth(i-1)+winnings(i); % What is the valuie of winning(i)?
To:
Wealth(i)=Wealth(i-1)+winnings;

Subject: Matlab betting on a biased coin

From: someone

Date: 11 Feb, 2013 15:34:17

Message: 3 of 3

"someone" wrote in message <kfb27p$p8i$1@newscl01ah.mathworks.com>...
> "Kaye" wrote in message <kfanvi$do5$1@newscl01ah.mathworks.com>...
> > I'm trying to run a simluation on a biased coin toss. At each toss i want to 'bet' a specific fraction of my current wealth on the coin toss, then run this for 100+ tosses and plot my wealth over these tosses. This is what i have so far but something in my for loop is not right. Can anyone help with this?
> > Thanks
> > K
> >
> > >>Ntoss = 100;
> > Wealth(1)=100;
> > bet(1)=50;
> > 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)')
>
> Without knowing what the error message is (if any), I have two suggestions:
>
> Change:
> s = -bet(i)* ones(1, Ntoss); % matrix multiplication
> To:
> s = -bet(i).*ones(1, Ntoss); % element-by-element multipl;ication
>
> Change:
> Wealth(i)=Wealth(i-1)+winnings(i); % What is the valuie of winning(i)?
> To:
> Wealth(i)=Wealth(i-1)+winnings;

After a little more looking, I suspect something is wrong with:
winnings = cumsum(s);

You are not using h for anything. Perhaps you mean:
winnings = cumsum(h);
 

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