{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":37644,"databundleVersionId":4235210,"sourceType":"competition"},{"sourceId":4451198,"sourceType":"datasetVersion","datasetId":2405994},{"sourceId":4559566,"sourceType":"datasetVersion","datasetId":2622395}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Horse Racing &mdash; Welcome to the Machine\n## An analysis of equine performance and health\n\n\n\n<img src=\"https://i.imgur.com/SYKb6Um.jpg\" width=\"520\">\n\n*A historic moment at Saratoga as Arrogate wins the 2016 Travers Stakes in track record time*","metadata":{}},{"cell_type":"code","source":"%%HTML\n<style type=\"text/css\">\n\ndiv.h1 {\n    font-size: 32px; \n    margin-bottom:2px;\n}\ndiv.h2 {\n    background-color: steelblue; \n    color: white; \n    padding: 8px; \n    padding-right: 300px; \n    font-size: 24px; \n    max-width: 1500px; \n    margin-top: 50px;\n    margin-bottom:4px;\n}\ndiv.h3 {\n    color: steelblue; \n    font-size: 18px; \n    margin-top: 4px; \n    margin-bottom:8px;\n}\ndiv.h4 {\n    font-size: 15px; \n    margin-top: 20px; \n    margin-bottom: 8px;\n}\nspan.note {\n    font-size: 5; \n    color: gray; \n    font-style: italic;\n}\nspan.lists {\n    font-size: 16; \n    color: dimgray; \n    font-style: bold;\n    vertical-align: top;\n}\nspan.captions {\n    font-size: 5; \n    color: dimgray; \n    font-style: italic;\n    margin-left: 130px;\n    vertical-align: top;\n}\nhr {\n    display: block; \n    color: gray;\n    height: 1px; \n    border: 0; \n    border-top: 1px solid;\n}\nhr.light {\n    display: block; \n    color: lightgray;\n    height: 1px; \n    border: 0; \n    border-top: 1px solid;\n}\ntable.dataframe th \n{\n    border: 1px darkgray solid;\n    color: black;\n    background-color: white;\n}\ntable.dataframe td \n{\n    border: 1px darkgray solid;\n    color: black;\n    background-color: white;\n    font-size: 14px;\n    text-align: center;\n} \ntable.rules th \n{\n    border: 1px darkgray solid;\n    color: black;\n    background-color: white;\n    font-size: 14px;\n}\ntable.rules td \n{\n    border: 1px darkgray solid;\n    color: black;\n    background-color: white;\n    font-size: 13px;\n    text-align: center;\n} \ntable.rules tr.best\n{\n    color: green;\n}\n\n</style>","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-11-17T01:21:37.630760Z","iopub.execute_input":"2022-11-17T01:21:37.631444Z","iopub.status.idle":"2022-11-17T01:21:37.642248Z","shell.execute_reply.started":"2022-11-17T01:21:37.631405Z","shell.execute_reply":"2022-11-17T01:21:37.641066Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The NYRA and NYTHA have asked the data science community to join them in improving equine welfare and performance. We accepted the challenge and teamed up to combine data science skills and racing knowledge. Our approach was to identify the factors affecting racing performance and provide practical applications to assist racing teams. We used a series of state-of-the-art Machine Learning models as the basis for our analysis.\n\nSix factors surfaced as being the most predictive of teams’ finishing positions. We explore the first two factors in greater detail later in the report.\n\n- **Lateral acceleration**\n- **Pedigree**\n- **Jockey**\n- **Path efficiency**\n- **Post position**\n- **Race distance and course type**\n\nMany of these factors are correlated with each other; for instance, a jockey’s style and decision-making can affect path efficiency and acceleration. One opportunity then is for a jockey to modify their actions to be more consistent with the patterns of jockeys who win a higher percentage of races. This report includes an augmented racing video designed to supplement the video with metrics based on the tracking data.\n\n<img src=\"https://i.imgur.com/zFImBeP.png\" width=\"550\">\n\nAlso, we chose these factors because they can be extended to predictive modeling for horse injuries. Accelerative forces on the joints, breeding characteristics, jockey style, course condition, etc. have all been linked to thoroughbred health over time. *(1)*\n\nOur report contains the following sections:\n- Background\n- Methodology\n- Analysis and Insights\n- Practical Application\n- Appendix\n\nThank you for this opportunity! -- JohnM and Steve Ragone\n","metadata":{}},{"cell_type":"markdown","source":"# Background\n","metadata":{}},{"cell_type":"markdown","source":"Horse racing in New York has a long and distinguished history, starting with America's first recognized racing event in 1665 at the Newmarket course in Salisbury, New York. Today, the New York Racing Association (NYRA) oversees and operates the pre-eminent thoroughbred horse racing circuit in North America. Races are conducted at three historic venues: Aqueduct Race Track, Belmont Park, and Saratoga Race Course.\n\n- **Aqueduct Race Track** is a 1-1/8 mile oval track located in New York City. It operates primarily from fall to spring each year. \n- **Belmont Park**, at 1-1/2 miles, is the largest track in the United States, and is located on Long Island in New York. It is home to the third jewel of racing’s Triple Crown, the Belmont Stakes.\n- **Saratoga Race Course** is a 1-1/8 mile track in Saratoga Springs, New York. Established in 1863, it is one of the oldest, continually run major sports venues in the United States. The 40-day summer meet is considered to be the highest quality race meeting in the country. The Travers Stakes for three-year-olds highlights the racing each year.\n\nLike other sports, the focus on safety in thoroughbred racing has increased over the last several years. Thoroughbred injuries are costly in many ways and can carry a heavy toll. Jockey injury, veterinary care for the horse, lost prize winnings, and stress for the horses, trainers, and owners are among the negative effects. But we can all agree, most recently, public perception has also made its way to the forefront in this regard. As the sport seeks to attract a new generation of fans, higher safety will translate to more fans and more revenue.","metadata":{}},{"cell_type":"markdown","source":"# Methodology","metadata":{}},{"cell_type":"markdown","source":"We used the following methodology for our analysis. \n- **Data Collection.** We relied mostly on the racing and coordinate data provided by the hosts. We also collected external data, including pedigree data available online.\n- **Analysis and Insight.** We created a Machine Learning model as the basis for our analysis. The model is designed to detect patterns across multiple variables and correlate the patterns with the finishing positions of the horses. We also conducted deeper analysis into two factors where we thought we could be the most helpful. \n- **Practical Application.** We considered several ideas and created an overlay for racing videos with calculated metrics. The augmented video could help trainers and jockeys as they review their performance.","metadata":{}},{"cell_type":"markdown","source":"# Analysis and Insights","metadata":{}},{"cell_type":"code","source":"!pip install hvplot\n\nimport re\n\nimport colorcet as cc\nimport holoviews as hv\nimport hvplot.pandas\nimport numpy as np\nimport pandas as pd\n","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-11-17T01:21:37.649193Z","iopub.execute_input":"2022-11-17T01:21:37.649653Z","iopub.status.idle":"2022-11-17T01:21:55.831142Z","shell.execute_reply.started":"2022-11-17T01:21:37.649616Z","shell.execute_reply":"2022-11-17T01:21:55.829042Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Predictive Modeling\n\nThe first part of our analysis focused on the creation of a predictive machine learning model to identify important factors. We used Driverless AI to perform our machine learning experiments. Driverless AI is a software tool that creates best-in-class models using a point-and-click interface. It uses advanced techniques under the hood first developed on Kaggle by data scientists who have won countless competitions. The winning techniques are embedded and automatically executed during model building. \n\nThe appendix contains details on the final model architecture, feature engineering, and parameters. The model can be completely replicated using open-source software and the information specified in the model’s document.\n\nThe combined model identified several key factors contributing to racing performance as seen in the chart below. Note we did not use velocity as a factor. Mean velocity, aka average speed, is the number one determinant in finishing position. However, using it in a model would not add meaningful insight and would overshadow the contributions of other factors.\n\nThe highlighted bars in the chart show which factors we chose for a deeper analysis.\n","metadata":{}},{"cell_type":"code","source":"\nshaps = pd.read_csv(\"../input/equine-safety/shaps.csv\")\n\nold_labels = [\n    \"jockey\",\n    \"dist_from_center_mean\",\n    \"accel_lateral_var\",\n    \"distance_id\",\n    \"program_number\",\n    \"course_type\",\n    \"post_time\",\n    \"accel_lateral_mean\",\n    \"ggs\",\n    \"gggs\",\n]\nnew_labels = [\n    \"Jockey\",\n    \"Path Efficiency\",\n    \"Variance in Lateral Acceleration\",\n    \"Race Distance\",\n    \"Post Position\",\n    \"Course Type\",\n    \"Post Time\",\n    \"Mean Lateral Acceleration\",\n    \"3rd Generation Sires\",\n    \"4th Generation Sires\",\n]\nlabel_change = dict(zip(old_labels, new_labels))\nshaps[\"label\"] = shaps[\"label\"].map(label_change)\n\nshaps = shaps[:10]\nshaps_in = shaps.iloc[np.r_[2, 7:10], :]\n\ngrid_style = {\n    \"xgrid_line_color\": \"gray\",\n    \"ygrid_line_color\": \"white\",\n    \"grid_line_width\": 0.5,\n}\n\ndef make_bar(df, color):\n    lb = df.hvplot.bar(\n        x=\"label\",\n        y=\"value\",\n        c=color,\n    #     title=\"\",\n        invert=True,\n        flip_yaxis=True,\n        xlabel=\"\",\n        ylabel=\"Relative Importance\",\n        xticks=np.arange(0, 0.36, 0.05).tolist(),\n        line_alpha=0,\n        bar_width=0.2,\n    ).opts(\n        show_grid=True,\n        gridstyle=grid_style,\n        show_frame=False,\n        width=800,\n        height=400,\n        fontscale=1.3\n        # padding=0.5\n    )\n    return lb\n\ndef make_scatter(df, color):\n    rd = df.hvplot.scatter(\n        x=\"label\",\n        y=\"value\",\n        c=color,\n        title=\"Feature Importance\",\n        invert=True,\n        flip_yaxis=True,\n        xlabel=\"\",\n        ylabel=\"Relative Importance\",\n        xticks=np.arange(0, 0.36, 0.05).tolist(),\n        size=100,\n    ).opts(padding=0.5)\n    return rd\n\nbars = make_bar(shaps, \"#888888\") * make_bar(shaps_in, \"#ffa43d\")\nends = make_scatter(shaps, \"#888888\") * make_scatter(shaps_in, \"#ffa43d\")\ndisplay(bars * ends)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:21:55.835303Z","iopub.execute_input":"2022-11-17T01:21:55.835926Z","iopub.status.idle":"2022-11-17T01:21:56.254230Z","shell.execute_reply.started":"2022-11-17T01:21:55.835868Z","shell.execute_reply":"2022-11-17T01:21:56.253048Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Most of the factors here make intuitive sense although their relative importance may challenge common knowledge. Sometimes though a machine learning model latches onto features that help segregate data without being of individual importance. Post Time may be such a factor as the time of day for a race doesn't seem like it would matter. On the other hand, it could be correlated with track condition - a deeper analysis would be needed.\n\nThe model is directionally correct and does relatively well at the high and low ends. In other words, it typically  identifies winning horses as being in the top 3 and the bottom few as being at the bottom. The model predicts the actual position to +- 2 places. Although it wouldn't be useful for placing winning bets, models at this level are often good at indicating relevant factors. We expect that more data over a longer period of time would improve results.\n\nAdditional data could also allow extension of the model to assess health and safety. Looking at racing data and also aggregate data for each horse would bring to light both acute and chronic conditions that contribute to injuries. \n\nAfter completing our predictive model and identifying key factors we performed deeper analysis in two areas: Lateral Acceleration and Pedigree.\n\n\n### Lateral Acceleration\n\nWe first looked at the lateral acceleration experienced by horses, particularly through the turns. Lateral acceleration in a race is a function of the horse’s speed and change in direction. Increasing speed or tightening the turn radius results in higher lateral acceleration. The picture below shows the geometry of lateral acceleration.\n\n\n<img src=\"https://i.imgur.com/hBIbWlP.png\" width=\"500\">\n\n*Justify, winning the 2018 Belmont Stakes to become the 13th Triple Crown champion*\n\nThe second chart below confirms the importance of the mean and variance of lateral acceleration. It  shows the distribution of lateral acceleration for the 5th race on 9/2/2019 at Saratoga, which was a 9 furlong race on the dirt track. Only the top 2 and bottom 2 finishers are shown for clarity.\n\nThese numbers are for the final turn as shown in the first chart below *(added 11/16)*. ","metadata":{}},{"cell_type":"code","source":"df = pd.read_parquet(\"../input/equine-safety/trakus_accel.parquet\")\nhorses = pd.read_csv(\"../input/equine-safety/horse_ids.csv\")\n\nmerge_cols = [\"track_id\", \"race_date\", \"race_number\", \"program_number\"]\n\nmatched = horses.merge(df, on=merge_cols, how=\"left\")\nmatched[\"dist_from_center\"] = matched.dist_from_center**0.5\nmatched[\"time_index\"] = matched.trakus_index * 3 / 10\n\nbase = matched[\n    matched.track_id.eq(\"SAR\")\n    & matched.race_date.eq(\"2019-09-02\")\n    & matched.race_number.eq(4)\n]\nbase_mean = base.groupby(\"trakus_index\", as_index=False, sort=False).mean(\n    numeric_only=True\n)\none = matched[\n    matched.track_id.eq(\"SAR\")\n    & matched.race_date.eq(\"2019-09-02\")\n    & matched.race_number.eq(5)\n]\nrace_mean = (\n    one[[\"x_coord\", \"y_coord\", \"accel_lateral\", \"trakus_index\"]]\n    .groupby(\"trakus_index\")\n    .mean()\n)\nrace_mean_chart = race_mean[race_mean.x_coord<=800]\n\nr = race_mean_chart.hvplot.scatter(\n    x=\"x_coord\",\n    y=\"y_coord\",\n    c=\"accel_lateral\",\n    cmap=cc.blues[40:],\n    xticks = list(range(0, 2600, 200)),\n    yticks=list(range(0, 1100, 200)),\n    xlabel=\"X Distance (ft)\",\n    ylabel=\"Y Distance (ft)\",\n    size=10,\n    clabel=\"Lateral Acceleration (ft / s^2)\",\n    title=\"Mean Lateral Acceleration at Final Turn\",\n)\nb = base_mean.hvplot.line(\n    x=\"x_coord\", y=\"y_coord\", color=\"gray\", alpha=0.2, \n        line_width=18, padding=0.2\n).opts(show_frame=False, fontscale=1.2,\n         )\n\ndisplay(r * b)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:21:56.255674Z","iopub.execute_input":"2022-11-17T01:21:56.256049Z","iopub.status.idle":"2022-11-17T01:22:08.347471Z","shell.execute_reply.started":"2022-11-17T01:21:56.256015Z","shell.execute_reply":"2022-11-17T01:22:08.346102Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%opts Distribution [tools=['save', 'pan', 'wheel_zoom', 'box_zoom', 'reset']]\n\nhv.renderer('bokeh').theme = None\none = pd.read_csv(\"../input/equine-safety/one.csv\")\n\ngrid_style = {\n    \"xgrid_line_color\": \"white\",\n    \"ygrid_line_color\": \"gray\",\n    \"grid_line_width\": 0.5,\n    \"grid_line_alpha\": 0.5,\n}\nlateral = (\n    one[one.accel_lateral.gt(3) & one.finishing_place.isin([1, 2, 5, 6])]\n    .sort_values(\"finishing_place\")\n    .hvplot.kde(\n        y=\"accel_lateral\",\n        by=\"finishing_place\",\n        alpha=0.1,\n        xlabel=\"Lateral Acceleration (ft / s^2)\",\n        ylabel=\"Relative Frequency\",\n        title=\"Distribution of Lateral Acceleration for Final Turn\",\n        yticks=np.arange(0, 1.1, 0.2).tolist(),\n        height=400\n    )\n    .opts(show_grid=True, gridstyle=grid_style, show_frame=False, fontscale=1.2,\n         )\n)\nlateral.get_dimension('finishing_place').label='Position at Finish'\n\ndisplay(lateral)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:22:44.498844Z","iopub.execute_input":"2022-11-17T01:22:44.499306Z","iopub.status.idle":"2022-11-17T01:22:44.882918Z","shell.execute_reply.started":"2022-11-17T01:22:44.499271Z","shell.execute_reply":"2022-11-17T01:22:44.881541Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The chart shows the differences in the lateral acceleration profile between the top two and bottom two finishers. \n\n- **Mean level of acceleration** The top finishes have a higher average level of acceleration. This can come from higher speed through the turn, or from a tighter turn radius and shorter path through the turn. Both are advantageous.\n\n- **Variance, or spread, of acceleration** The top finishers we looked at often had a tighter range, indicating a smoother passage through the turn. Abrupt changes to the high side typically indicate a decrease in forward velocity as a jockey attempts to cut into the turn. Changes to the low side typically indicate a decrease in velocity or a widening of the turn radius. Changes in either direction can reduce overall performance through a turn.\n\nThe chart above is typical for the races we examined, although not every race shows a clear difference. Horses win or lose for any number of reasons, including average velocity as we mentioned before. In the case shown here, it might indicate to the jockey in 6th place that execution during the final turn contributed in part to their loss.\n\nThe next chart shows the profile of lateral acceleration during the same race. As with any high-frequency sensor data, the raw numbers contain a certain amount of noise. We smoothed the raw data using a combination of short-term rolling mean and a function called a Kalman filter. This smoothing method reduced a fair amount of noise while still capturing the variations.\n","metadata":{}},{"cell_type":"code","source":"onet = one[one.finishing_place.isin([1,2,5,6]) & one.time_index.between(70, 87)]\n\nline_plot = onet.hvplot.line(\n    x=\"time_index\",\n    y=\"accel_lateral\",\n    by=\"finishing_place\",\n    alpha=1,\n    xlabel=\"Time (s)\",\n    ylabel=\"Lateral Acceleration (ft / s^2)\",\n    title=\"Lateral Acceleration through the Final Turn\",\n    xticks = list(range(70,87, 2)),\n    yticks = np.arange(5.5, 8.2, 0.5).tolist(),\n    grid=True,\n    height=400,\n    width=800\n).opts(fontscale=1.2)\n\nline_plot.get_dimension('finishing_place').label='Position at Finish'\ndisplay(line_plot)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:22:09.271441Z","iopub.execute_input":"2022-11-17T01:22:09.272705Z","iopub.status.idle":"2022-11-17T01:22:09.549799Z","shell.execute_reply.started":"2022-11-17T01:22:09.272659Z","shell.execute_reply":"2022-11-17T01:22:09.548446Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Here it becomes clear that horse 1 (Rucksack) and horse 2 (Super Silver) maintained higher acceleration through the turn than the other horses. Super Silver accelerated faster out of the turn than the other horses, which actually might have helped. A review of the video along with velocity and acceleration charts are needed to draw a conclusion for this specific race.","metadata":{}},{"cell_type":"markdown","source":"### Pedigree\n\nWe next took a closer look at pedigreee, the second of our two deep dives. The pedigree of a thoroughbred receives a great deal of attention, both from the perspective of the foal’s potential and the sire’s previous success. We analyzed sire lines for each horse from the 2nd generation through the 5th generation. For this analysis, we used Natural Language Processing (NLP) to look directly at the names of the sires and identified which sires appeared to have the most influence on racing performance. We used Bidirectional Encoder Representations from Transformers (BERT) in combination with a tree-based model. BERT was developed by Google in 2018 and, along with derivative transformers, has proven to be the highest performing method for NLP tasks.\n\nWe included sires from both the paternal and maternal sides of each generation. A 2015 study on the role of maternal lineage found that the heritability of performance between dams and their foals (r=0.141) was significantly higher than between sires and their foals (r=0.035). They further found that a good stallion could generally not negate the heredity effect of a poor race mare but that a good race mare could negate the hereditary effect of a poor sire. *[2]*\n\nThe chart below shows the most influential sires relative to a horse’s racing results. Overall, the 3rd generation sires on both sides show the most influence. The result is expected because the lineage is less diluted at that level, and more unique than older generations. First and 2nd generation sires would intuitively be even more important, but the model is limited by the small sample size. There is not enough commonality in those generations across the horses in our sample to discover a pattern.\n","metadata":{}},{"cell_type":"code","source":"\nlocos = pd.read_csv(\"../input/equine-safety/locotable.csv\")\nlocos = locos[:8]\n\nlocos.label.tolist()\npretty_labels = [\n    \"3rd gen: Seattle Slew\",\n    \"3rd gen: Mr Prospector\",\n    \"3rd gen: A P Indy\",\n    \"3rd gen: Unbridled\",\n    \"3rd gen: Stormcat\",\n    \"4th gen: Mr Prospector\",\n    \"4th gen: Raise a Native\",\n    \"4th gen: Northern Dancer\"\n]\nlocos[\"label\"] = pretty_labels\n\ngrid_style = {\n    \"xgrid_line_color\": \"gray\",\n    \"ygrid_line_color\": \"white\",\n    \"grid_line_width\": 0.5,\n}\n\nlb = locos.hvplot.bar(\n    x=\"label\",\n    y=\"value\",\n#     title=\"\",\n    invert=True,\n    flip_yaxis=True,\n    xlabel=\"\",\n    ylabel=\"Relative Importance\",\n    xticks=np.arange(0, 0.36, 0.05).tolist(),\n    line_alpha=0,\n    bar_width=0.2,\n).opts(\n    show_grid=True,\n    gridstyle=grid_style,\n    show_frame=False,\n    width=800,\n    height=400,\n    fontscale=1.3\n    # padding=0.5\n)\n\ns = locos.hvplot.scatter(\n    x=\"label\",\n    y=\"value\",\n    title=\"Sires of High Performing Horses\",\n    invert=True,\n    flip_yaxis=True,\n    xlabel=\"\",\n    ylabel=\"Relative Importance\",\n    xticks=np.arange(0, 0.36, 0.05).tolist(),\n    size=150,\n).opts(padding=0.5)\n\ndisplay(lb * s)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:22:09.551651Z","iopub.execute_input":"2022-11-17T01:22:09.552084Z","iopub.status.idle":"2022-11-17T01:22:09.793792Z","shell.execute_reply.started":"2022-11-17T01:22:09.552040Z","shell.execute_reply":"2022-11-17T01:22:09.792652Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Extending this study to all current thoroughbreds and including detailed maternal lineage could likely be used also for identifying correlations between pedigree and horses' susceptibility to chronic health conditions. The findings could supplement research in equine genetics such as that underway at The School of Veterinary Medicine at UC Davis.*[4]*\n\nRaw data for this part of the analysis was gathered from the Pedigree Online Thoroughbred Database. The linked dataset contains the file used.","metadata":{}},{"cell_type":"markdown","source":"# Practical Application","metadata":{}},{"cell_type":"markdown","source":"There are several ways the acceleration, velocities, and positions from sensor data can be used together. Televised horse racing already uses the data in several ways to provide on-screen information throughout a race. \n\nWe propose an on-screen overlay that trainers, jockeys, and other stakeholders can use to assess performance in certain areas. As mentioned before, videos of the races provide context for charts of the metrics. And the charts provide a quantitative view, based on data, to supplement the video. It's likely that a seasoned jockey or horse enthusiast will be able to assess most of this information most of the time. For younger jockeys and new fans to racing, it would be an addition to interpreting one aspect of a race's outcome. The cumulative view showing distributions of acceleration can also be useful in showing the characteristics associated with high performance of jockey and horse. \n\nHere is one version of combining key metrics with video replays. Here we show lateral and longitudinal acceleration for a single horse. We chose Super Silver in the race mentioned previously due to his strong exit from the final turn and the final push at the finish line.You can see how longitudinal and lateral acceleration typically move in opposing directions.","metadata":{}},{"cell_type":"markdown","source":"The video shown here is meant to be a proof of concept. We would expect that further evolution from the horse racing community would lead to new and exciting ways to build on the concept.\n\n\n\n\nThe blue line shows lateral acceleration for Super Silver.\nThe red line shows his longitudinal acceleration.\n\n**Follow the link on the video to YouTube to watch full screen and hear the announcer.**","metadata":{}},{"cell_type":"code","source":"from IPython.display import YouTubeVideo\n\nYouTubeVideo('NuOlFT6CdV0', width=600)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:22:09.795463Z","iopub.execute_input":"2022-11-17T01:22:09.795878Z","iopub.status.idle":"2022-11-17T01:22:09.917824Z","shell.execute_reply.started":"2022-11-17T01:22:09.795842Z","shell.execute_reply":"2022-11-17T01:22:09.916705Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Conclusion","metadata":{}},{"cell_type":"markdown","source":"This is the first time to our knowledge that Trakus coordinate data has been made publicly available. We utilized that data as well as other publicly available information for our analysis. The limitations of the data were apparent. A larger, multi-year dataset would allow for more accurate diagnosis of racing best practices and would better detect telltale signs of impending racehorse injury.\n\n\n\nEven so,we believe that additional metrics and insights based on current sensor data are a useful addition to the NYRA's current efforts. It would provide jockeys and trainers  with additional information in context when reviewing game video, and also reinforce the importance of choices made during turns. The information can also contribute to fan engagement and provide additional understanding when viewing on-air broadcasts.\n\n\n\nFinally, further analysis of key factors based on sensor data and factors such as pedigree will likely provide insights beyond our findings. Such analysis will provide data-based evidence that will either reinforce or challenge the collective body of knowledge gained from over 300 years of horse racing in New York and elsewhere.\n","metadata":{}},{"cell_type":"markdown","source":"<img src=\"https://i.imgur.com/p05TMgT.png\" width=\"550\">\n\n*One of the rarest occurrences in racing, a triple dead heat. The most famous one took place at Aqueduct Race Track, in the 1944 Carter Handicap.*","metadata":{}},{"cell_type":"markdown","source":"# Appendix","metadata":{"execution":{"iopub.status.busy":"2021-12-21T00:18:12.613663Z","iopub.execute_input":"2021-12-21T00:18:12.614808Z","iopub.status.idle":"2021-12-21T00:18:12.620876Z","shell.execute_reply.started":"2021-12-21T00:18:12.614732Z","shell.execute_reply":"2021-12-21T00:18:12.619244Z"}}},{"cell_type":"markdown","source":"#### About the authors\n\nJohnM works as a Principal Data Scientist at H2O.ai. He is currently helping a large hospital system use AI to provide better healthcare. John has also contributed in the past to sports safety for football players. The NFL used his recommendations to support their efforts for reducing player concussions and leg injuries.\n\nSteve Ragone is retired from a career in the public sector, most recently serving as a Health Program Director. A lifelong horse racing aficionado, he has attended the races at Saratoga for over 50 years. Steve has seen Triple Crown champions Secretariat, Seattle Slew, Affirmed, and American Pharoah compete in person.\n","metadata":{}},{"cell_type":"markdown","source":"#### Model Details\n\nThe final machine learning model is a combination of two tree-based models. The autodoc.docx word document in the attached dataset contains 75 pages describing details of the model, including the full set of factors used.","metadata":{}},{"cell_type":"markdown","source":"#### Other Ideas that might improve safety\n\nWe considered a few other ideas during the course of our analysis based on one author's knowledge of horse racing gained over many years. Data in these areas was not available so we left them as potential areas of interest.\n\n**Temporary Rail for Dirt Courses**\n\nAdmittedly an outside the box suggestion - would be a temporary rail on dirt courses, especially during stretches of inclement weather. This could allow for a more fair and safe racetrack by taking the inside, oversaturated portion of the track out of the equation. Sedimentation during and after heavy rains no doubt occurs, and alters the makeup of the track, particularly that part closest to the rail. A temporary rail may provide a good opportunity for the track superintendent to restore the lowest portion of the track to proper condition while allowing for the continuation of racing on a more uniform track.\n\n<img src=\"https://i.imgur.com/JqIOs0C.jpg\" width=\"550\">\n\n\n**Analyzing Warm-ups**\n\nTrainers rightfully often wonder, especially before big races, if a horse’s final workout was too slow or too fast. Did a blazing work take too much out of the runner? Did a slow work not ensure ultimate fitness? \n\nThese workouts usually occur a week before the race. If there is concern about taking too much out of a horse a week in advance, what about aggressive pre-race warm-ups? Should there be any consideration given for effects on endurance in the moments preceding a race? What effect, if any, do aggressive or non-aggressive warm-ups have on performance?\n\nPer its literature, Trakus monitors horses as soon as they enter the track. It might be beneficial to examine the impact warm-ups have on performance and injury. By analyzing the distance and speed of pre-race activity, one could identify the positive and negative aspects of various types of warm-ups. Is there a relationship between warm-ups and performance? Is there a relationship between warm-ups and injury? \n\n\n**Enhanced Presentation / Identifying Prospective Injuries**\n\nWe believe the Trakus data can be used further to enhance the presentation of the NYRA racing product, and at the same time identify horses that may tipping off lameness or some other ailment. By using a model to factor in, among other things, pace, distance, surface, class level, position and velocity, the NYRA could develop something similar to what the NFL uses to show Win Probability during games. However, for horse races, the percentages will be in a state of flux as the race progresses and a horse’s position and speed is measured against competitor’s positions and speed, as well as distance remaining in the race. \n\nWe can also use the data to identify, in real-time, idiosyncrasies\nin a horse’s deceleration when viewed against what is typical for the class\nlevel of the race for a particular surface and distance. Any horse that is\nidentified to be significantly outside the expected values of deceleration,\nwhich may be the harbinger of an impending health issue, can be immediately\nsent to the post-race test barn for veterinary examination or assigned for\nfurther evaluation prior to entry in another race.\n\nProduct improvements gained through technological innovation\nis a hallmark of sporting event presentation, and these enhanced graphics\nshould appeal to NYRA. Injury prevention/identification is certainly first and\nforemost on the minds of horse racing’s curators, so any tool that can aid in\nthis endeavor should be welcome assistance.\n\nThe following series of graphics are from the 2019 Diana Stakes at Saratoga Win percentages change as the race develops and converge toward the end of the race. Note: this is a prototype and is not based on any AI currently available.\n\n\n<img src=\"https://i.imgur.com/xwLuFjx.jpg\" width=\"550\">\n\n","metadata":{}},{"cell_type":"markdown","source":"*A 5-second glimpse into the future...*","metadata":{}},{"cell_type":"code","source":"YouTubeVideo('Fi9UVbf6pz8', width=600, height=300)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-17T01:22:09.919820Z","iopub.execute_input":"2022-11-17T01:22:09.920415Z","iopub.status.idle":"2022-11-17T01:22:10.024940Z","shell.execute_reply.started":"2022-11-17T01:22:09.920368Z","shell.execute_reply":"2022-11-17T01:22:10.023723Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**If the Shoe Doesn’t Fit...**\n\nIs it inconceivable that shoes lost on the track are responsible for some racing injuries? Shoes are lost all the time, along with nails. Are competitors checking for lost shoes after every race and initiating recovery prior to subsequent races?\n\n<img src=\"https://i.imgur.com/nPCXmR7.jpg\" width=\"580\">\n\n\n*This horse will need help finding his shoe.*","metadata":{}},{"cell_type":"markdown","source":"#### Citations\n\n[1] Meta-analysis of risk factors for racehorse catastrophic musculoskeletal injury in flat racing. The Veterinary Journal 245 (2019) 29–40\n\n[2] Potential role of maternal lineage in the thoroughbred breeding strategy. Reproduction, Fertility, and Development\n (2015 May 5). doi: 10.1071/RD15063\n\n[3] Pedigree Online Thoroughbred database.\n\n[4] Horse Report, Summer 2020. Center for Equine Health. School of Veterinary Medicine, University of California, Davis","metadata":{}}]}