Baseball Simulation

Created in January and February of 2019 as Part 1 of a sport simulation project. GitHub Link


Methodology

This was created in the 2019 Pre-Season so I used combined WAR (Wins Above Replacement) of teams and added 150 to that value in order to create and adjusted "ranking". The reason 150 was added is because, due to the nature of WAR, some teams will have negative WAR, and I needed some kind of way to implement a compete method between two teams.

Implementation of Compete method

The following code is in the Team.java file.
 
 
  public void compete(Team rival) {
    double result = (Math.random()*(this.ranking + rival.ranking));
    if (result < ranking ) {
      this.wins++;
      rival.losses++;
      this.ranking += .2;
      rival.ranking += -.2;
    }
    else {
      this.losses++;
      rival.wins++;
      this.ranking += -.2;
      rival.ranking +=.2;
    }
  }

Here, the ranking instance variable is exactly what is described above (WAR + 150). A random number is generated within the range from 0 to the sum of the two team rankings. Then, if this random number is less than the current team's ranking, then they are awarded the win. Otherwise, they are awarded the loss. Each game, a team's ranking is adjusted by ±.2 to allow for mid season shifts in ability. Given the compete method, pre-season the Yankees would have a 54% chance of beating the Twins in any individual game.

Implementation of Season Class

In the MLB, each time is assigned a league and a division. There are 2 leagues, and each league has 3 divisions, each with 5 teams. Each team plays 19 games against every other team in their division (76 total), 6/7 games against each of the other 10 opponents in their league but not their division (66 total), and 20 inter-league games. Division and League games were easy to program because they just required looping through and doing some randomization. However, for inter-league play, I had to develop 6 different schedules and then randomly pick one from those. This is because of the existence of rival teams and some teams have multiple rivals which rotate by season.

Future Improvements + Criticism

Home Field Advantage

One aspect of baseball which is not implemented in my program is the existence of home field advantage. Home field advantage is certainly present in baseball, especially so in the postseason. Unfortunately, my program did not account for home field advantage, but this is an improvement I would want to make in the future.

Win Streaks

As with home field advantage, win streaks have the potential to dramatically change a team's probability of winning any given game. Momentum is the driving factor here, and I am looking into a way to implement this in the future.

Example Output

AL East AL Central AL West NL East NL Central NL West
Yankees: 100-62 Indians: 89-73 Athletics: 98-64 Nationals: 93-69 Brewers: 93-69 Dodgers: 87-75
Rays: 92-70 Tigers: 80-82 Astros: 87-75 Braves: 87-75 Cubs: 87-75 Giants: 80-82
Red Sox: 83-79 Twins: 79-83 Angels: 72-90 Mets: 85-77 Cardinals: 81-81 Rockies: 79-83
Blue Jays: 81-81 Royals: 78-84 Rangers: 71-91 Marlins: 72-90 Pirates: 78-84 Diamondbacks: 78-84
Orioles: 63-99 White Sox: 75-87 Mariners: 69-93 Phillies: 69-93 Reds: 76-86 Padres: 68-94

AL Wild Card: Rays vs Astros
Astros Win 1-0
NL Wild Card: Braves vs Cubs
Cubs Win 1-0

ALDS #1: Yankees vs Astros
Astros Win 3-0
ALDS #2: Athletics vs Indians
Indians Win 3-1
NLDS #1: Nationals vs Cubs
Nationals Win 3-2
NLDS #2: Brewers vs Dodgers
Dodgers Win 3-2

ALCS: Astros vs Indians
Astros Win 4-2
NLCS: Nationals vs Dodgers
Dodgers Win 4-1

World Series: Astros vs Dodgers
Astros Win 4-3

Astros Win the MLB Season!