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
    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);

    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

    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,

    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

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

    Thread Starter New Member

    Feb 1, 2012
    Sorry this is in MATLAB by the way.
  3. Georacer


    Nov 25, 2009
    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.
    ckuylen likes this.