Baseball simulator question help

5 views (last 30 days)
bikechain
bikechain on 31 Mar 2015
Answered: Image Analyst on 31 Mar 2015
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
bikechain on 31 Mar 2015
Here is the actual question for clarification...
The function Playball simulates a 9-innings baseball game for one lineup. Your code will do the same. The function input is a 9x5 Matrix of player data probabilities. The function has 5 outputs: the number of runs scored in the 9 inning game (a scalar), the runs scored by each player (9x1 vector), the runs batted in (rbi) of each player (9x1 vector), the total hits of each player (9x1 vector), and the total outs of each player (9x1 vector). Your function should simulate a 9 inning game, with three outs per inning, using nested loops. You will use the player data probabilities and Monte Carlo simulation to simulate a given player when they come to bat (more below). If they get a hit, they advance to whatever base their hit corresponds to (a single to first base, double to second, triple to third, homerun back to home plate). If they get an out, they do not advance and the number of outs in the inning increases by 1. Any baserunners on base when a batter gets a hit advance the number of bases that the hitter gets. If the batter was to hit a double all base runners would advance 2 bases, a triple 3 bases and so on. We will assume no base running errors or advances other than that previously mentioned. The order of the hitters should carry over through innings, so if the 5th batter made the last out, then the 6th batter leads off the next inning. In this simulation you will only worry about one of the teams so once one inning is over, you can go right to the next. A 9x1 matrix will also be needed to keep track of where each player is on base. Remember that any time a player reaches over 3 bases, they have scored. An RBI is defined as the number of runs that score when a player gets a hit. So if there are runners on all bases and a double is hit, the batter
Geoff Hayes
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?

Sign in to comment.

Answers (2)

James Tursa
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
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 Word games 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!