Original author is Magnus Erik Hvass Pedersen
This is a collection of source-code recipes for solving small problems that I could not find solutions for on the internet. These are implemented in Python unless otherwise noted.
-
Rate Limiter (Notebook) (Google Colab)
Throttle events that are too rapid using the simple and efficient "wait-until" algorithm, which also works well in multi-threaded / distributed environments. (Python and MongoDB) -
Rejection Sampling (Notebook) (Google Colab)
Generate random numbers using Rejection Sampling which works for most Probability Density Functions (PDF). -
Inverse Transform Sampling (Notebook) (Google Colab)
Generate random numbers using Inverse Transform Sampling of a Cumulative Distribution Function (CDF). -
Resample KDE (Notebook) (Google Colab)
Resample a Probability Density Function (PDF) estimator such as the Kernel Density Estimation (KDE) that has been fitted to some data. This is useful if you want to save and load the KDE without saving all the raw data. We also show how to draw random samples from a histogram.
If you do not want to install anything on your own computer, then the Notebooks can be viewed, edited and run entirely on the internet by using Google Colab.
You click the "Google Colab"-link next to the recipes listed above. You can view the Notebook on Colab but in order to run it you need to login using your Google account.
Most of the required Python packages should already be installed on Google
Colab, but there is a %pip install
command near the top of each Notebook
which will run automatically on Google Colab, so as to install the required
Python packages for that particular Notebook.
If you want to run these recipes on your own computer, then it is easiest if you download the whole repository from GitHub, instead of just downloading the individual Python Notebooks.
The easiest way to download and install it is by using git from the command-line:
git clone https://github.com/Hvass-Labs/Code-Recipes.git
This creates the directory Code-Recipes
and downloads all the files to it.
This also makes it easy to update the files, simply by executing this command inside that directory:
git pull
You can also download the contents of the GitHub repository as a Zip-file and extract it manually.
If you want to run these tutorials on your own computer, then it is best to use a virtual environment when installing the required packages, so you can easily delete the environment again. You can use Anaconda for this:
conda create --name code-recipes python=3
Then you switch to the virtual environment and install the required packages.
conda activate code-recipes
pip install -r requirements.txt
When you are done working on the project you can deactivate the virtualenv:
conda deactivate
Once you have installed the required Python packages in a virtual environment,
you run the following command from the Code-Recipes
directory to view,
edit and run the Notebooks:
conda activate code-recipes
jupyter notebook
This is published under the MIT License which allows very broad use for both academic and commercial purposes.
You are very welcome to modify and use this source-code in your own project. Please keep a link to the original repository.