# Using Monte Carlo Simulation to Model Dimaggio's hitting streak

Discussion in 'Homework Help' started by ckuylen, Mar 1, 2012.

1. ### ckuylen Thread Starter New Member

Feb 1, 2012
8
0
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.

x=rand(1, Natbats);
BA=(x<=p);
end

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

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!

2. ### ckuylen Thread Starter New Member

Feb 1, 2012
8
0
Sorry this is in MATLAB by the way.

3. ### Georacer Moderator

Nov 25, 2009
5,154
1,281
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.