Baseball Season Simulation Help
8 views (last 30 days)
Show older comments
If someone could give me the basic idea and code to start this, i just have no idea where to begin
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 ets 2 RBI. If they hit a homerun, they get 4 RBI (you get an RBI for a homerun for driving in yourself) You should create a sub-function that uses a Monte Carlo simulation to determine what each player hits when they are up to bat. Then in you main function you should call your subfunction for every at bat.
0 Comments
Answers (1)
John D'Errico
on 30 Mar 2015
So you need to make an effort.
Score = [0,0];
for inning = 1:9
for team = 1:2
onbase = [0 0 0];
for outs = 1:3
DO STUFF HERE! Start writing!
end
end
end
1 Comment
John D'Errico
on 31 Mar 2015
Edited: John D'Errico
on 31 Mar 2015
You can read in a text file using load, or you can use textread. For example,
load hits.txt
will create a variable named hits in your workspace.
hits
hits =
0.1 0.1 0.05 0.05 0.7
0.2 0.1 0.05 0.05 0.6
0.3 0.1 0.05 0.05 0.5
0.1 0.1 0.05 0.05 0.7
0.2 0.1 0.05 0.05 0.6
0.3 0.1 0.05 0.05 0.5
0.1 0.1 0.05 0.05 0.7
0.2 0.1 0.05 0.05 0.6
0.3 0.1 0.05 0.05 0.5
So for batter k, you can see what happens in one at bat as simply as
1+sum(rand >= cumsum(hits(k,:)))
This will be a random integer from 1 to 5, based on the probabilities for batter k.
k
k =
1
1 + sum(rand >= cumsum(hits(k,:)))
ans =
5
1 + sum(rand >= cumsum(hits(k,:)))
ans =
2
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!