Using Monte Carlo Simulation to Model Dimaggio's hitting streak

Thread Starter

ckuylen

Joined Feb 1, 2012
8
Hey guys, this is due at midnight so not sure how much input I'll be able to get, but I appreciate it in advance. Here is the situation: We were asked to model Joe Dimaggio's (JD) hit streak using Monte Carlo simulation to determine how rare this streak actually is. A quick rundown of what I have done so far.

1A) JD had a lifetime batting average of .325 so if less than or equal to .3225 we assign our variable as a hit(1) greater than (0). Write a code that will simulate 10 at bats.

Answer: function BA=hits2(Natbats, p)
x=rand(1, Natbats);
BA=(x<=p);
end

1B) Write a line of code that simulates an entire season of at bats.

Answer: function BA2=hits2(p)
x=rand (4, 154); %four at bats per game for 154 games
BA2=(x<=p);
end

1C) Find out if each game had at least 1 hit in it.

Answer: stored the above output (1B) as season. Used any(season) to get new output and stored this output as B.

1D)What was the longest streak in the season?

Answer: created a function called runLength:

function out=runLength(binaryData)
%function out=runLength(binaryData)

if size(binaryData,2)==1,
binaryData=binaryData';
end

out = diff([0 find(binaryData(1:end-1)~=binaryData(2:end)) length(binaryData)]);

Then---> streak=runLength(B) (provides a list of consecutive 1s)
Then---->max (streak) provides us with the longest hitting game streak in a season ans=15.

1E)Now right a for loop that simulates 10,000 seasons. I.E. right a loop that will repeat steps B-D 10,000 times and store the biggest streak for each season in a a vector called streaks.

THIS IS WHERE I AM CONFUSED. I'm terrible with for loops.

my attempt: for i=1:10000
streaks(i)=max(runLength(any(rand(4,154)<.325)));

Any suggestions? I know its long, but all help would be appreciated!
 

Georacer

Joined Nov 25, 2009
5,182
I 'm afraid this is too specific for a general electronics forum. We do microcontrollers and electric projects pretty in-depth, but software isn't AAC's strong suit.

I 'd try in a math forum if I were you. But your deadline is pretty dead by now, sadly.

Have a nice day.
 
Top