This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_match_up_simulation(fastball_surrender_prob, curve_surrender_prob, change_surrender_prob, fastball_hit_prob, | |
curve_hit_prob, change_hit_prob, zero_zero_pitch_probs, zero_zero_hit_surrender_prob, | |
zero_zero_hit_prob, one_zero_pitch_probs, one_zero_hit_surrender_prob, one_zero_hit_prob, | |
zero_one_pitch_probs, zero_one_hit_surrender_prob, zero_one_hit_prob, one_one_pitch_probs, | |
one_one_hit_surrender_prob, one_one_hit_prob, two_zero_pitch_probs, | |
two_zero_hit_surrender_prob, two_zero_hit_prob, zero_two_pitch_probs, | |
zero_two_hit_surrender_prob, zero_two_hit_prob, one_two_pitch_probs, | |
one_two_hit_surrender_prob, one_two_hit_prob, two_one_pitch_probs, | |
two_one_hit_surrender_prob, two_one_hit_prob, three_zero_pitch_probs, | |
three_zero_hit_surrender_prob, three_zero_hi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_pitch_simulation(count, count_swing_prob, pitch_probs, count_surrender_prob, fastball_surrender_prob, | |
count_hit_prob, fastball_hit_prob, curve_surrender_prob, curve_hit_prob, | |
change_surrender_prob, change_hit_prob, df): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if pitch == 'fastball': | |
hit_prob = (count_surrender_prob + fastball_surrender_prob + count_hit_prob + fastball_hit_prob) / 4 | |
if swing == 'yes': | |
outcome = np.random.choice(a=['hit', 'no_hit'], p=[hit_prob, 1 - hit_prob]) | |
elif swing == 'no': | |
outcome = 'no_hit' | |
df = df.append(pd.DataFrame({'count': count, 'pitch': 'fastball', 'swing': swing, 'result': [outcome]})) | |
elif pitch == 'curve': | |
hit_prob = (count_surrender_prob + curve_surrender_prob + count_hit_prob + curve_hit_prob) / 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pitch = np.random.choice(a=['fastball', 'curve', 'change'], p=pitch_probs) | |
swing = np.random.choice(a=['yes', 'no'], p=[count_swing_prob, 1 - count_swing_prob]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
at_bat_results = pd.DataFrame() | |
at_bat_results = run_pitch_simulation(count='0-0', | |
count_swing_prob=zero_zero_swing_prob, | |
pitch_probs=zero_zero_pitch_probs, | |
count_surrender_prob=zero_zero_hit_surrender_prob, | |
fastball_surrender_prob=fastball_surrender_prob, | |
count_hit_prob=zero_zero_hit_prob, | |
fastball_hit_prob=fastball_hit_prob, | |
curve_surrender_prob=curve_surrender_prob, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
last_row_df = at_bat_results.tail(1) | |
if last_row_df['swing'].any() == 'yes': | |
end_of_at_bat = np.random.choice(a=['yes', 'no'], p=[swing_produces_out, 1 - swing_produces_out]) | |
else: | |
end_of_at_bat = 'no' | |
if at_bat_results['result'].any() == 'no_hit' and end_of_at_bat == 'no': | |
new_count = np.random.choice(a=['0-1', '1-0'], p=[0.50, 0.50]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simulation_runs = 100 | |
counter = 0 | |
while counter < simulation_runs: | |
temp_df = run_match_up_simulation(fastball_surrender_prob, curve_surrender_prob, change_surrender_prob, | |
fastball_hit_prob, curve_hit_prob, change_hit_prob, zero_zero_pitch_probs, | |
zero_zero_hit_surrender_prob, zero_zero_hit_prob, one_zero_pitch_probs, | |
one_zero_hit_surrender_prob, one_zero_hit_prob, zero_one_pitch_probs, | |
zero_one_hit_surrender_prob, zero_one_hit_prob, one_one_pitch_probs, | |
one_one_hit_surrender_prob, one_one_hit_prob, two_zero_pitch_probs, | |
two_zero_hit_surrender_prob, two_zero_hit_prob, zero_two_pitch_probs, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import subprocess | |
from sklearn.tree import DecisionTreeClassifier, export_graphviz | |
def main(): | |
df = pd.DataFrame({ | |
'location': ['MO', 'MO', 'MO', 'MO', 'KS', 'KS', 'KS', 'IL', 'IL', 'IL'], | |
'age': [29, 30, 21, 40, 45, 60, 35, 24, 47, 50], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# create directories | |
mkdir data | |
mkdir helpers | |
mkdir modeling | |
mkdir scratch | |
mkdir tests | |
mkdir utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import pandas as pd | |
import numpy as np | |
from time import sleep | |
from datetime import datetime | |
def connect_to_sqlite(db_name): | |
""" |
OlderNewer