-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_baseline.R
More file actions
226 lines (188 loc) · 8 KB
/
compute_baseline.R
File metadata and controls
226 lines (188 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# ============================================================================
# compute_baseline.R — Compute Fixed Baseline Parameters for Deception+
# ----------------------------------------------------------------------------
# Runs N random 50/50 splits on historical data (2023-2025) using per-pitcher
# models to establish stable standardization parameters (μ and σ).
#
# This creates a fixed reference point for Deception+ that:
# - Defines what "100" means (average MLB pitcher unpredictability)
# - Is stable across all future runs
# - Should be recomputed periodically (e.g., annually)
#
# Output: baseline_params.rds containing:
# - mu: mean unpredictability_ratio across all pitchers and runs
# - sd: standard deviation of unpredictability_ratio
# - n_runs: number of random splits performed
# - n_pitchers: total unique pitchers across all runs
# - date_computed: when baseline was computed
# - data_period: date range used for computation
# ============================================================================
source("pitch_ppi.R")
# ============================================================================
# CONFIGURATION
# ============================================================================
# Date range for baseline computation
BASELINE_START <- "2023-03-30" # 2023 Opening Day
BASELINE_END <- "2025-09-28" # End of 2025 regular season (adjust as needed)
# Number of random splits to average
N_RUNS <- 30
# Minimum pitches per pitcher to include in baseline
MIN_PITCHES <- 100
# Output file
OUTPUT_FILE <- "baseline_params.rds"
# Features for per-pitcher models
FEATURE_NAMES <- c(
"balls", "strikes", "two_strikes", "ahead_in_count",
"outs", "is_risp", "stand", "last_pitch_type"
)
# Baseline keys for simple baseline comparison
BASELINE_KEYS <- c("balls", "strikes", "stand", "two_strikes")
# ============================================================================
# PER-PITCHER MODEL FUNCTION
# ============================================================================
# Uses evaluate_per_pitcher() from pitch_ppi.R (shared implementation).
# Thin wrapper to match the interface expected by the baseline computation loop.
compute_per_pitcher_unpredictability <- function(df_train,
df_test,
min_train_pitches = 50,
min_test_pitches = 10,
feature_names = FEATURE_NAMES,
baseline_keys = BASELINE_KEYS,
verbose = TRUE) {
result <- evaluate_per_pitcher(
df_history = df_train,
df_test = df_test,
min_train_pitches = min_train_pitches,
min_test_pitches = min_test_pitches,
feature_names = feature_names,
baseline_keys = baseline_keys,
verbose = verbose
)
# Rename n_history -> n_train to match the baseline computation expectation
if (nrow(result$results) > 0) {
result$results %>%
dplyr::rename(n_train = n_history)
} else {
tibble()
}
}
# ============================================================================
# MAIN EXECUTION
# ============================================================================
cat("\n")
cat("============================================================\n")
cat(" Deception+ Baseline Computation\n")
cat("============================================================\n")
cat("Data Period: ", BASELINE_START, "to", BASELINE_END, "\n")
cat("Number of Runs: ", N_RUNS, "\n")
cat("Min Pitches: ", MIN_PITCHES, "\n")
cat("Output File: ", OUTPUT_FILE, "\n")
cat("============================================================\n\n")
# Ensure directories exist
ensure_directories()
# Load data for the full baseline period
message("Loading baseline data...")
TRAIN_LEVEL <- "MLB"
cachefile <- sprintf("cache/savant_raw_%s_%s_R_MLB.Rds", BASELINE_START, BASELINE_END)
if (file.exists(cachefile)) {
message("Using cached data: ", cachefile)
raw_data <- readRDS(cachefile)
} else {
message("Downloading data (this may take a while)...")
raw_data <- load_statcast_range(BASELINE_START, BASELINE_END,
game_type = "R", level = "MLB", verbose = TRUE)
if (nrow(raw_data) > 0) {
saveRDS(raw_data, cachefile)
message("Cached to: ", cachefile)
} else {
stop("No data found for baseline period")
}
}
message("Engineering features...")
batter_metric_cols <- c("o_swing_pct", "z_contact_pct", "swing_pct", "chase_contact_pct")
df_all <- engineer_features(raw_data, include_batter_metrics = any(batter_metric_cols %in% FEATURE_NAMES))
df_all <- df_all %>% filter(!is.na(pitcher_id))
message("Total: ", nrow(df_all), " pitches from ",
length(unique(df_all$pitcher_id)), " pitchers\n")
# Run N random splits
message("Running ", N_RUNS, " random splits with per-pitcher models...\n")
all_ratios <- vector("list", N_RUNS)
for (run in seq_len(N_RUNS)) {
if (run %% 10 == 1 || run == N_RUNS) {
message(sprintf("Run %d/%d...", run, N_RUNS))
}
# Set seed for reproducibility
set.seed(run)
# Random 50/50 split per pitcher
df_split <- df_all %>%
group_by(pitcher_id) %>%
mutate(
random_order = sample(n()),
is_train = random_order <= ceiling(n() / 2)
) %>%
ungroup()
df_train <- df_split %>% filter(is_train) %>% select(-random_order, -is_train)
df_test <- df_split %>% filter(!is_train) %>% select(-random_order, -is_train)
# Compute per-pitcher unpredictability
run_results <- compute_per_pitcher_unpredictability(
df_train, df_test,
min_train_pitches = MIN_PITCHES / 2, # Half goes to train
min_test_pitches = MIN_PITCHES / 2, # Half goes to test
feature_names = FEATURE_NAMES,
verbose = FALSE
)
if (nrow(run_results) > 0) {
run_results$run <- run
all_ratios[[run]] <- run_results
}
}
# Combine all results
all_results <- bind_rows(all_ratios)
if (nrow(all_results) == 0) {
stop("No valid results from any run")
}
message("\nComputing baseline parameters...")
# Compute stable μ and σ across all runs
baseline_mu <- mean(all_results$unpredictability_ratio, na.rm = TRUE)
baseline_sd <- sd(all_results$unpredictability_ratio, na.rm = TRUE)
# Summary stats
n_observations <- nrow(all_results)
n_unique_pitchers <- length(unique(all_results$pitcher_id))
observations_per_run <- all_results %>%
group_by(run) %>%
summarise(n = n(), .groups = "drop")
cat("\n")
cat("============================================================\n")
cat(" Baseline Parameters Computed\n")
cat("============================================================\n")
cat("μ (mean): ", round(baseline_mu, 6), "\n")
cat("σ (std dev): ", round(baseline_sd, 6), "\n")
cat("Total observations: ", n_observations, "\n")
cat("Unique pitchers: ", n_unique_pitchers, "\n")
cat("Avg pitchers per run: ", round(mean(observations_per_run$n), 1), "\n")
cat("============================================================\n\n")
# Save baseline parameters
baseline_params <- list(
mu = baseline_mu,
sd = baseline_sd,
n_runs = N_RUNS,
n_observations = n_observations,
n_unique_pitchers = n_unique_pitchers,
avg_pitchers_per_run = mean(observations_per_run$n),
date_computed = Sys.time(),
data_period = paste(BASELINE_START, "to", BASELINE_END),
min_pitches = MIN_PITCHES,
feature_names = FEATURE_NAMES,
baseline_keys = BASELINE_KEYS
)
saveRDS(baseline_params, OUTPUT_FILE)
message("Saved baseline parameters to: ", OUTPUT_FILE)
# Also save the full results for analysis
saveRDS(all_results, "baseline_full_results.rds")
message("Saved full results to: baseline_full_results.rds")
cat("\n")
cat("============================================================\n")
cat(" Done! Use these values for Deception+ standardization:\n")
cat("============================================================\n")
cat(" Deception+ = 100 + 10 * ((ratio - ", round(baseline_mu, 4), ") / ", round(baseline_sd, 4), ")\n", sep = "")
cat("============================================================\n\n")