{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction\n\nHorse races are typically defined by close contests with oftentimes small differences in finish times. Since differences as small as milliseconds can be of utmost importance to the final position of a given horse, racing strategies that produce an edge of tens of seconds are extremely advantageous. During a race, in addition to the horse's intrinsic phisiology, the jockey can also have a fundamental impact in the finish time by employing different strategies. The jockey's ability to correctly assess the capability of its own horse and competitors, and employ an optimal pace, may give him the winning edge. **Here we quantify a metric that analyzes the jockey's performance in applying an optimal pace strategy with respect to his competitors.** For a given race, we determine the best pacing strategies and evaluate possible changes in the final position of the different horses had these strategies been applied.\n\nBy focusing on pacing strategies:\n\n1. **We devise an algorithm for the optimal strategy that the jockey should employ to minimize the finish time. The algorithim is based on our physical model** which depends on the physiological properties of the horses, track geometry, friction and race distance, and drafting conditions. After each race, our method allows to immediately analyze and review the jockey's performance at any given time by comparing the actual velocities and finish times vs simulated values.\n\n2. **We provide actionable metrics and strategy improvements that can be customized to specific horses and jockeys.** Our developed metric allows the determination of which horses/jockeys ran the closest to their optimized race strategy values. This produces a unique performance ranking for each race that is independent of the final position of horses.\n\n3. **By determining the physiological parameters of horses, improvements in equine welfare can be obtained using our algorithims.** In particular, we are able to track the anaerobic energy reservoir of the horse during the race. Anomalies can be identified and possibly taken into account into training regimen and horse selection for future races.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Data Preparation\n\nOur analysis uses data from the 2019 horse-racing season in New York Racing Association's tracks of Acqueduct, Belmont Park and Saratoga. For a given race, we include all tracking data for all horses as produced by the Trakus system. We assume that the data is collected with a 4 Hertz cadence (i.e. every 0.25s), producing around 300-400 sample points per horse. With the position data, we compute the instantaneous speed and displacement vector with the usual kinematics equations. We also compute the longitudinal distance between each pair of horses at all points, which allows the determination of drafting conditions.\n\nDuring the rest of this notebook we use data from the first 15 days of the 2019 horse-racing season, and use the data from the 2019-01-01 race number 2 on the Acqueduct racetrack to illustrate our results. ","metadata":{}},{"cell_type":"markdown","source":"# Part I: Novel Physical Model Including Air Drag and Drafting\n\nThe effects of air resistance ('drag') are brutal on human athletes and animals whenever speeds of the order of 10 m/s are approached. At those velocities, the drag force start to dominate over other resistances such as ground friction$^1$. In multi-player races, any gain from drafting another competitor can be substantial. These effects are well studied in wind-tunnel and fluid-dynamic simulations for cyclists, skiiers and cars $^2,3$. For horse-racing, effects of drafting have been suggested to be substantial based on race-data analysis$^4$. With that in mind, we produce a novel physical model for horse-race dynamics to **quantify the optimal pace strategy of a given race based on horse and track properties, including drafting**. We define 'optimal' as the minimum finish time of the race given the physical talent of the horse and track properties. **We can then compare the actual finish times to the optimal times of each horse in Part II and evaluate which horses/jockeys performed the best**. This is an individual optimal velocity strategy for each horse and race.\n\n## Methods\n\nOur model takes into account Newtonian dynamics and the expected horse physiology, in particular its energetics. We build on earlier models that considered a simple horse energetics$^5$ or neglected drafting $^6$. In Appendix I we describe the technical aspects of our model in detail. In short, we solve an optimal-control problem consisting of coupled differential equations for the velocity, force and anaerobic energy as a function of distance throughout the race, under the condition that the finish time is minimized and assuming a VO2 profile appropriate for horses. Our model can easily incorporate actual VO2 profiles obtained during the race from heart-rate measurements or other methods. **The novelty of our approach is to include the effects of air drag and drafting**. For that, we assume that a horse is drafting if its longitudinal distance is less than 2 horse lengts (4.48m) to any horse that is in front. \n\nWe use the publicly-available Python package Pyomo and IpOpt to solve the optimization problem. Our steps are as follows.\n\nStep 1: For a given race, solve the optimal-control problem using the actual velocity of the horse as a function of distance as an input (i.e. fixed condition) for horse 1. This allows the determination of the initial horse anaerobic energy, force vs distance, maximum force, and track friction for a given horse VO2 profile. We use a VO2 profile as in typical for horses$^6$, with a value that initially goes up, reaches a plateau and decreases towards the end of the race. We also include our own implementation of the effects of drafting in saving anaerobic energy due to the reduced drag (see Appendix). \n\nStep 2: using the values of the initial horse anaerobic energy and maximum force, solve the full optimal control problem for horse 1 to determine the optimal pacing strategy (velocity as a function of distance) and minimum finish time. We assume the same VO2 profile.\n\nStep 3: repeat Steps 1 and 2 for the other horses in this race.\n\nStep 4: repeat the steps aboves for the other races in the dataset.\n\n<!-- ![title](fig1.jpg) -->\n<img src=\"https://i.imgur.com/xAVnCqV.png\" width=\"1000\"> \n    \n## Results\n\nThe figure above illustrates one example of our predictions. In this race, it is clear that this particular horse (blue line) did not follow the optimal pacing strategy (black line). It started faster than it should have, spending valuable energy in the process. This burned energy is then unavailable later in the race, causing the horse to go slower than it could for its physiological talent. **In the end, this particular horse runs about 3.4s slower than its optimal time.** The reasons for that can be various, such as unacounted poor weather conditions, inhomogeneous track friction conditions, poor jockey decision-making, or horse injury.\n\nWe also see that similar to the actual velocity profile, the optimal velocity profile reaches a maximum in the earlier parts of the race. This is not related to the presence of the bend and associated centrifugal force, as it occurs well before the beginning of the curved part of the track. It is instead related to the optimum use of the anaerobic energy of the horse to minimize the final time. Too slow or too fast accelerations will produce longer finish times and impact the final position of the horse.\n\nThe optimal pace varies depending on the horse energetic capabilities. Stronger horses produce larger velocities at the beginning; horses with higher initial anaerobic energies can accelerate longer and reach higher velocities during the race; and horses with high VO2 capabilities can maintain a higher velocity during a larger portion of the race.\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:21:14.597923Z","iopub.execute_input":"2022-11-10T23:21:14.598330Z","iopub.status.idle":"2022-11-10T23:21:14.621720Z","shell.execute_reply.started":"2022-11-10T23:21:14.598300Z","shell.execute_reply":"2022-11-10T23:21:14.620640Z"}}},{"cell_type":"markdown","source":"# Part II: Evaluate Races\n\nIn general, the main metric used to evaluate horse and jockey performances is their positions at the end of the race. This simple metric neglects that the actual capabilities of and strategy opportunities available to each horse/jockey are different. **We produce a novel metric that compares the actual finish time with the optimal finish time of our predictions.** In this way, we can evaluate horse-racing performance in a way that is independent of the position at the end and mostly independent of horse talent to a certain degree.\n\nHaving computed the actual and optimal finish times for each horse and race, we can now investigate a) which jockeys perform closer to the optimal finish time, and b) whether the position at finish is a good indicator of horse/jockey performance. \n\n## A novel metric to rank jockeys\nWe define the following quantity for evaluating jockeys in a given race,\n\n$$\n  P = 100 * \\frac{t_\\mathrm{obs} - t_\\mathrm{opt}}{t_\\mathrm{opt}},\n$$\n\ncorresponding to the percentage increase (or decrease) of the difference between the actual finish time ($t_\\mathrm{obs}$) and the optimal finish time ($t_\\mathrm{opt}$). For context, we note that each 1% in P corresponds to 0.8s in a 6-furlong race that is completed in 80s.\n\nWe then compute the average $P$ values ($P_{avg}$) for a given jockey taking into account all the races that they participated and the 75-percentile value of the distribution of P. This means that in 75% of the races, the jockey achieves a performance better than the value quoted. \n\n## Results\n\nThe following table shows the jockeys that best performed according to our metric, taking into account only jockeys with 100 or more races in the dataset. We can see that Joel Rosario ranks as the top jockey, producing performances that are on average slightly better than the optimal model by 0.252%. This corresponds to performances a few tenths of a second better than the optimal model. Irad Ortiz Jr. and Jose L. Ortiz follow as the 2nd and 3rd best jockeys.\n<center>\n<img src=\"https://i.imgur.com/SXyFw62.png\" width=\"700\"> ","metadata":{}},{"cell_type":"markdown","source":"# How is this innovative and useful to the NYRA?\n\n* **New innovative metrics** for evaluating horse-racing performance including the effects of air drag, drafting, using a physical approach, and independent of final position.\n* **Actionable predictions** by comparing actual velocity profiles and simulations, leading to potential improvements in pace strategies.\n* **Impact on horse welfare** by analyzing anaerobic energy as a function of time and detecting overusage.","metadata":{}},{"cell_type":"markdown","source":"# Appendix: technical details of the physical model\n\nOur approach solves the Newtonian dynamics and energetics of horse racing, following the model from Mercier & Aftalion$^6$. However, we additionally include air drag and drafting. For a race of distance $d$ discretized at spatial indices s, we define the optimal control problem as\n\n$$ \n\\mathrm{min} \\int^d_0 \\frac{1}{v(s)}ds\\\\\nv'(s) = \\frac{1}{v(s)} (-v(s)/\\tau + f(s) - f_\\mathrm{drag}(s) - g \\alpha), \\\\\ne'(s) = \\frac{\\sigma_e(s) - (f(s)-f_\\mathrm{draft}(s))*v(s)) }{v(s)}, \\mathrm{with  }\\,\\,\\, e(s) > 0\\\\\nf'(s) = \\frac{u(s)}{v(s)}, \\mathrm{with  }\\,\\,\\, u_- < u(s) < u_+\\\\\n$$,\n\nassuming the constraint from circular motion that $f(s)^2 + v(s)^4 \\le f^2_M$, that v(0) = $v^0$, $e(0)=e^0$ and e(d) = 0 . The variables in the expression above mean as follows,\n* $v(s)$ = velocity as a function of distance, $e(s)$ = energy as a function of distance, $f(s)$ = force as a function of distance per mass;\n* $f_\\mathrm{drag}(s)$ = air drag force per mass, computed as 0.5 * Cd * rho * Area * v^2. We use Cd = 1.0, rho = 1.28 kg/m^3, and Area = 1m2.\n* $f_\\mathrm{draft}(s)$ = virtual draft force that would have made work when drafting. Computed as drafting_gain_fraction_drag * is_drafting(s) *0.5 * Cd * rho * Area * v^2. We use drafting_gain_fraction_drag=0.3, Cd = 1.0, rho = 1.28 kg/m^3, and Area = 1m2, and is_drafting(s) is obtained from the data and equals to 1 when the distance to any leading horse is less than 1 horse-lengths (2.24m).\n* $\\tau$ = friction due to all sources, including the track, \n* $\\alpha$ = track inclination, ignored here.\n* $\\sigma_e(s)$ = energy/time/mass, or ml of Oxygen/min/kg, i.e. VO2.\n* $u(s)$ = change in force as a function of distance.","metadata":{}},{"cell_type":"markdown","source":"The figure below shows the typical output of our model for different values of VO2 max during the race, showing the behavior of the velocity, anaerobic energy, force, sigma (i.e. VO2), u(s), distance, curvature, and centrifugal force.\n​\n<center>\n<img src=\"https://i.imgur.com/lNn7csf.png\" width=\"1000\">\n​\n    \nBased on our investigations, we find that the curvature of the horsetracks is too small to produce a dynamical effect similar to that seen in 200m human races in curved tracks. Given that negligible effect, our final models do not include the curvature to improve the execution time.","metadata":{}},{"cell_type":"markdown","source":"# References\n\n1. \"The mathematics of breaking away and chasing in cycling\", Olds, T. 1998  Eur. J. Appl. Physiol. 77, p. 492 – 497.\n\n2. \"Drafting effect in cycling: investigation by wind tunnel tests\", Marco Belloli et al. 2016 Procedia Engineering 147 p. 38 – 43\n\n3. \"Cyclist aerodynamics through time: Better, faster, stronger\", F. Malizia and B. Blocken 2021, Journal of Wind Engineering & Industrial Aerodynamics 214 104673\n\n4. \"Speed, pacing strategy and aerodynamic drafting in Thoroughbred horse racing\", Andrew J. Spence, Andrew S. Thurman, Michael J. Maher and Alan M. Wilson Biol. Lett. 2012 8\n\n5. \"Optimal velocity in a race\", Keller JB 1974,  American Mathematical Monthly. 1974; p. 474–480\n\n6. \"Optimal speed in Thoroughbred horse racing\" Mercier Q, Aftalion A 2020, PLoS ONE 15(12)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:24:05.419714Z","iopub.execute_input":"2022-11-10T23:24:05.420272Z","iopub.status.idle":"2022-11-10T23:24:05.429873Z","shell.execute_reply.started":"2022-11-10T23:24:05.420230Z","shell.execute_reply":"2022-11-10T23:24:05.428146Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}