├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── data ├── SFM_data_byPlayer_clean.csv ├── fastball_spin_rates.csv └── salmon.txt ├── environment.yml ├── notebooks ├── SFM-PAR-pydata.ipynb └── gaussian_processes.ipynb ├── pixi.lock └── pixi.toml /.gitattributes: -------------------------------------------------------------------------------- 1 | # GitHub syntax highlighting 2 | pixi.lock linguist-language=YAML linguist-generated=true 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | # MacOS hidden files 163 | .DS_Store 164 | *.key 165 | 166 | # netcdf files 167 | *.nc# pixi environments 168 | .pixi 169 | *.egg-info 170 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Alexandre Andorra 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastering Gaussian Processes with PyMC 2 | 3 | ## PyData NYC 2024 4 | 5 | ### [Video of the tutorial](https://www.youtube.com/watch?v=u6I5pN_Q6r4&list=PLGVZCDnMOq0ohEIZ-_wM2W_xqSVjyA3dC&index=6) 6 | 7 | Gaussian processes (GPs) are a powerful Bayesian approach for quantifying uncertainty and making probabilistic inferences, especially when dealing with complex, non-linear relationships in regression and classification. 8 | This tutorial will introduce you to the flexibility of GPs in handling diverse data problems, including the [new Hilbert space Gaussian process (HSGP) approximation](https://www.pymc.io/projects/examples/en/latest/gaussian_processes/HSGP-Basic.html), which scales GPs to large datasets. 9 | By the end, you will be equipped to specify, fit, and validate GP models using PyMC, with a special focus on a real-world sports analytics case study. 10 | 11 | --- 12 | 13 | Gaussian processes (GPs) are a versatile and powerful tool in the Bayesian modeling toolkit, allowing users to model complex systems without needing to specify exact functional forms. This tutorial is designed for data scientists and statisticians who are new to GPs or want to deepen their understanding. 14 | 15 | We'll begin with an overview of probabilistic modeling before outlining the key properties of GPs, notably their flexibility in capturing non-linear relationships and their ability to incorporate prior knowledge. We will then focus on the practical aspects of building and fitting GP models in PyMC, highlighting the importance of selecting the right covariance function and demonstrating how to apply the HSGP approximation for scaling to larger datasets. 16 | 17 | To reinforce these concepts, we will demonstrate a Gaussian process modeling workflow using a sports analytics example. Here we will demonstrate how to use GPs to model the data and generate predictions, showcasing the practical application of these concepts in real-world scenarios. 18 | 19 | ## Target Audience 20 | 21 | This tutorial is aimed at data scientists, statisticians, and researchers with a basic understanding of statistics and Python, who are interested in learning how to apply Gaussian Processes to their work. Prior experience with PyMC is not required but will be beneficial. 22 | 23 | ## Takeaway 24 | 25 | Participants will leave with a solid understanding of Gaussian Processes and the skills needed to apply these models using PyMC, including handling large datasets with HSGP. They will also gain insight into practical applications through a detailed sports analytics case study. 26 | 27 | ## Background Knowledge Required 28 | 29 | Basic understanding of probability and statistics, and familiarity with Python. 30 | 31 | ## Materials Distribution 32 | 33 | All tutorial materials, including notebooks and datasets, will be made available via a GitHub repository. 34 | 35 | ## Computer Setup 36 | 37 | We recommend using either [Conda](https://docs.conda.io/en/latest/miniconda.html) or [Pixi](https://pixi.sh/latest/) to install the dependencies for this tutorial; which you choose is a matter of personal preference. Once installed, you can create the environment by running the appropriate command below from within the project directory. 38 | 39 | ### Conda 40 | 41 | ```bash 42 | conda env create -f environment.yml 43 | ``` 44 | 45 | ### Pixi 46 | 47 | ```bash 48 | pixi shell 49 | ``` 50 | 51 | ## Tutorial Outline 52 | 53 | 1. Introduction to Probabilistic Modeling (10 min) 54 | 55 | - Overview of modeling complex systems using Gaussian distributions. 56 | 57 | 2. What is a Gaussian Process? (15 min) 58 | 59 | - Explanation of Gaussian Processes, their features, and properties. 60 | - Discussion of the flexibility of GPs in capturing non-linear relationships. 61 | 62 | 3. Building a Gaussian Process Model in PyMC (10 min) 63 | 64 | - How to choose and customize covariance functions to suit different problems. 65 | 66 | 4. Marginal, Latent, and Sparse GPs (10 min) 67 | 68 | - Different classes of GPs and their properties. 69 | 70 | 5. Fast Gaussian Processes: HSGP Approximation (10 min) 71 | 72 | - Introduction to the HSGP approximation and how it scales GPs to large datasets. 73 | 74 | 6. Sports Analytics Case Study (40 min) 75 | 76 | - Application of GPs to a real-world sports analytics problem. 77 | - Step-by-step walkthrough of modeling and prediction using GPs in PyMC. 78 | -------------------------------------------------------------------------------- /data/salmon.txt: -------------------------------------------------------------------------------- 1 | year recruits spawners 2 | 1 68 56 3 | 2 77 62 4 | 3 299 445 5 | 4 220 279 6 | 5 142 138 7 | 6 287 428 8 | 7 276 319 9 | 8 115 102 10 | 9 64 51 11 | 10 206 289 12 | 11 222 351 13 | 12 205 282 14 | 13 233 310 15 | 14 228 266 16 | 15 188 256 17 | 16 132 144 18 | 17 285 447 19 | 18 188 186 20 | 19 224 389 21 | 20 121 113 22 | 21 311 412 23 | 22 166 176 24 | 23 248 313 25 | 24 161 162 26 | 25 226 368 27 | 26 67 54 28 | 27 201 214 29 | 28 267 429 30 | 29 121 115 31 | 30 301 407 32 | 31 244 265 33 | 32 222 301 34 | 33 195 234 35 | 34 203 229 36 | 35 210 270 37 | 36 275 478 38 | 37 286 419 39 | 38 275 490 40 | 39 304 430 41 | 40 214 235 42 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: gp-pydata24 2 | channels: 3 | - conda-forge 4 | dependencies: 5 | - black 6 | - ipywidgets 7 | - isort 8 | - jupyter 9 | - jupyterlab 10 | - matplotlib 11 | - nbqa 12 | - numba 13 | - numpy 14 | - numpyro 15 | - nutpie 16 | - pandas 17 | - preliz 18 | - pymc>=5.19.0 19 | - scikit-learn 20 | - scipy 21 | - seaborn 22 | - tensorflow 23 | - watermark 24 | -------------------------------------------------------------------------------- /pixi.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | authors = ["Chris Fonnesbeck ", "Alex Andorra "] 3 | channels = ["conda-forge"] 4 | description = "Mastering Gaussian Processes with PyMC" 5 | name = "gp-pydata24" 6 | platforms = ["linux-64"] 7 | version = "0.1.0" 8 | 9 | [tasks] 10 | 11 | [dependencies] 12 | black = "*" 13 | ipywidgets = "*" 14 | isort = "*" 15 | jupyter = "*" 16 | jupyterlab = "*" 17 | matplotlib = "*" 18 | nbqa = "*" 19 | numba = "*" 20 | numpy = "*" 21 | numpyro = "*" 22 | nutpie = "*" 23 | pandas = "*" 24 | preliz = ">=0.11.0,<0.12" 25 | pymc = ">=5.16.0" 26 | scikit-learn = "*" 27 | scipy = "*" 28 | seaborn = "*" 29 | tensorflow = "*" 30 | watermark = ">=2.5.0,<3" 31 | --------------------------------------------------------------------------------