Baseball simulator question help
Show older comments
This is my code so far.... i have a text file that i will attach with the probabilities of each batter getting a (single, double, triple, homerun, and out) respectively. I need help on how to use those probabilites and plug it into the outcome for each batter. Then how to get the RBIs Runs Hits and Outs for each batter throughout the game. Using the monte Carlo method for the data in the attachment to figue out what each person will do at bat.
Lineup=[1 2 3 4 5 6 7 8 9];
outcome=
FirstBase=0;
SecondBase=0;
ThirdBase=0;
CurrentBatter=0;
Score=0;
NumOuts=0;
while NumOuts < 3
CurrentBatter=CurrentBatter + 1;
if CurrentBatter > length(Lineup)
CurrentBatter = 1;
end
ShouldKeepBatting= true;
while ShouldKeepBatting
if outcome=single
if ThirdBase ~= 0
Score= Score+1;
end
ThirdBase=SecondBase;
SecondBase=FirstBase;
FirstBase=Lineup(CurrentBatter);
ShouldKeepBatting= false;
elseif outcome=double
if ThirdBase~=0
Score=Score+1;
end
if SecondBase~=0
Score=Score+1;
end
ThirdBase = FirstBase;
SecondBase = Lineup(CurrentBatter);
FirstBase = 0;
ShouldKeepBatting = false;
elseif outcome=triple
if ThirdBase~=0
Score=Score+1;
end
if SecondBase~=0
Score=Score+1;
end
if FirstBase~=0
Score=Score+1;
end
ThirdBase = Lineup(CurrentBatter);
SecondBase = 0;
FirstBase = 0;
ShouldKeepBatting = false;
elseif outcome=homerun
if ThirdBase ~= 0
Score=Score+1;
end
if SecondBase ~= 0
Score=Score+1;
end
if FirstBase ~= 0
Score=Score+1;
end
Score=Score+1;
FirstBase = 0;
SecondBase = 0;
ThirdBase = 0;
ShouldKeepBatting = false;
end
end
end
2 Comments
bikechain
on 31 Mar 2015
Geoff Hayes
on 31 Mar 2015
bikechain - while you have provided the homework assignment (as given in class?) and some code, you may want to start with first reading in the data from the text file. What have you tried so far to read in this data?
Answers (2)
James Tursa
on 31 Mar 2015
For the outcome of a single at-bat, there are various ways to do it. Suppose the 9x5 matrix is called "probabilities". Then the probabilities associated with the current batter will be probabilities(CurrentBatter,:), where it is assumed that the sum of these probabilities for a single player adds up to 1. In other words,
CurrentBatterProbabilitites = probabilities(CurrentBatter,:);
Then to pick a random outcome from these values, one way is to use cumsum to get a cumulative sum vector of the above and then compare a uniform random number against those partial sums. E.g.,
outcome = sum(rand>cumsum(CurrentBatterProbabilitites))+1;
Then outcome will be a number from 1-5 indicating single, double, tripe, homerun, or out. These steps need to be done inside the loop after you have selected the current batter.
To get the statistics, simply have vectors that count them. E.g., have a 9-element RBI vector, a 9-element RUNS vector, etc. And then inside each outcome block (single, double, etc) increment the appropriate vectors depending on who got the hit, who scored, etc.
Also, I don't see the point of your while( ShouldKeepBatting ) loop. This isn't Cricket, so once a batter bats he never keeps batting. Get rid of this loop and instead add another branch for the out possibility.
And, you are going to need to add some logic for innings.
Image Analyst
on 31 Mar 2015
There is a lot to like about this code. It's easy to follow because you use descriptive variable names. One thing to note however is that you should not use single and double as variable names because those are built-in reserved keywords. Use something like aSingle, aDouble, and aTriple instead.
You need to make a function called outcome() where you take the player number, CurrentBatter, and your player data and generate an outcome (ball, strike, strikeout, out, aSingle, aDouble, aTriple, or aHomeRun).
You need to use double equals in comparisons, not single equals:
elseif outcome == homerun
The code you have is just for one team while batting in one inning. You'll need to do both teams and for all 9 innings.
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!