├── docker ├── __init__.py ├── core │ ├── __init__.py │ ├── util.py │ ├── log.py │ ├── kelly.py │ ├── portfolio.py │ └── simulate.py ├── requirements.txt ├── Makefile └── Dockerfile ├── README.md ├── .gitignore └── run.sh /docker/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docker/core/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | -------------------------------------------------------------------------------- /docker/requirements.txt: -------------------------------------------------------------------------------- 1 | flake8==3.4.1 2 | matplotlib==2.0.2 3 | numpy==1.14.0 4 | numpydoc==0.6.0 5 | scipy==0.19.0 6 | -------------------------------------------------------------------------------- /docker/core/util.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | def cgr(first, last, periods): 5 | return (last / first) ** (1 / float(periods)) * 100.0 - 100.0 6 | -------------------------------------------------------------------------------- /docker/core/log.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import logging 3 | import sys 4 | 5 | 6 | def create_logger(): 7 | log = logging.getLogger() 8 | log.setLevel(logging.INFO) 9 | 10 | ch = logging.StreamHandler(sys.stdout) 11 | ch.setLevel(logging.INFO) 12 | formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") 13 | ch.setFormatter(formatter) 14 | log.addHandler(ch) 15 | return log 16 | -------------------------------------------------------------------------------- /docker/Makefile: -------------------------------------------------------------------------------- 1 | all: deps clean style test 2 | 3 | clean: 4 | rm -rf dist/* 5 | find . -name '*pyc' -delete 6 | find . -name '.coverage' -delete 7 | 8 | deps: 9 | pip3 install -U pip 10 | pip3 install wheel 11 | pip3 install -r requirements.txt 12 | pip3 install -e . 13 | 14 | style: 15 | flake8 app 16 | 17 | package: clean style test 18 | python3.6 setup.py bdist_wheel 19 | cp dist/kelly-multiarm-`cat VERSION`-py3-none-any.whl dist/kelly-multiarm-latest-py3-none-any.whl 20 | -------------------------------------------------------------------------------- /docker/core/kelly.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | from scipy.optimize import minimize 4 | 5 | 6 | def objective(frac, probs, odds, eps=1e-20): 7 | return -np.dot(probs, np.log2(frac[0] + frac[1:] * odds + eps)) 8 | 9 | 10 | def compute_optimal_allocation(portfolio, odds, probs): 11 | current_allocation = np.asarray(portfolio.allocation) 12 | nonnegative_constraints = tuple([(0.0, 1.0) for _ in range(len(current_allocation))]) 13 | optimum = minimize( 14 | objective, 15 | current_allocation, 16 | args=(probs, odds), 17 | bounds=nonnegative_constraints, 18 | constraints=({"type": "eq", "fun": lambda x: np.sum(x) - 1.0}), 19 | ) 20 | return optimum.x, -optimum.fun 21 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM jupyter/scipy-notebook 2 | 3 | USER root 4 | RUN apt-get update && \ 5 | apt-get install -y --no-install-recommends pybind11-dev && \ 6 | apt-get clean && \ 7 | rm -rf /var/lib/apt/lists/* 8 | 9 | USER $NB_UID 10 | 11 | COPY requirements.txt requirements.txt 12 | 13 | # Upgrade pip and wheel. 14 | # Install requirements using pip, but they get installed to conda's space :D 15 | # Generate a jupyter config then unceremoniously change it to increase the kernel activity timeout. 16 | # Enable notebook extensions. 17 | RUN pip install -U pip wheel && \ 18 | pip install -r requirements.txt && \ 19 | yes | jupyter notebook --generate-config && \ 20 | echo "\nc.MappingKernelManager.kernel_info_timeout = 600\n" >> /home/jovyan/.jupyter/jupyter_notebook_config.py && \ 21 | echo "\nc.NotebookApp.allow_remote_access = True\n" >> /home/jovyan/.jupyter/jupyter_notebook_config.py && \ 22 | jupyter nbextension enable --py widgetsnbextension 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kelly Allocation with Multi-armed Bandits 2 | 3 | Simulating the combination of a solution of the multi-armed bandits problem, Thompson sampling, with the Kelly criterion 4 | for portfolio allocation. 5 | 6 | ## Brief overview 7 | 8 | The [Kelly criterion](http://www.herrold.com/brokerage/kelly.pdf), developed by John L. Kelly Jr. 9 | at Bell Labs, is an optimal sizing of bets, given an initial pool of wealth, to maximize 10 | the doubling rate of wealth in a repeated bets scenario. This has been applied to various games, 11 | including horse racing. 12 | 13 | However, the major difficulty in applying the criterion is that it assumes that the true probabilities of 14 | an event occurring, say for instance, a horse winning a race, is known to the bettor. In reality, 15 | these probabilities have to be estimated by the bettor. The criterion is sensitive to the estimated probabilities, 16 | and since the criterion maximizes the wealth doubling exponent, mistakes made in the estimated probabilities can 17 | easily ruin the bettor over time. 18 | 19 | For concreteness, we cast the problem to the specific problem of betting on horses at a race track. 20 | In order to estimate the probabilities, we update the probabilities by using Bayesian updating. 21 | 22 | ## Using the notebook 23 | 24 | For convenience, the simulation is packaged into an IPython notebook. To run the notebook, at the 25 | root directory, run 26 | ```$xslt 27 | ./run.sh 28 | ``` 29 | -------------------------------------------------------------------------------- /.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 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | .idea/ 107 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Runs the notebook server. 4 | set -eu 5 | set -o pipefail 6 | 7 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 8 | IMAGE_NAME="kelly-multiarm" 9 | # Note that this user is the default as defined in jupyter/base-notebook. Using anything else here 10 | # will probably break things. 11 | NB_USER="jovyan" 12 | 13 | # Map the code into the container, so that restarting kernels will reload externally updated code. 14 | HOST_CODE="${SCRIPT_DIR}/docker" 15 | CONTAINER_CODE="/opt/conda/lib/python3.6/site-packages/app" 16 | 17 | 18 | error() { 19 | local prog 20 | prog=$(basename "$0") 21 | echo "$(date '+[%Y-%m-%d %H:%M:%S]') ${prog} ERROR: $*" >&2 22 | exit 1 23 | } 24 | 25 | 26 | usage() { 27 | local prog 28 | prog=$(basename "$0") 29 | cat < burn_in_period: 49 | bayesian_update_portfolio.update_wealth(win_index, odds, log) 50 | optimal_portfolio.update_wealth(win_index, odds, log) 51 | uniform_portfolio.update_wealth(win_index, odds, log) 52 | 53 | bayesian_update_portfolio_history.append(bayesian_update_portfolio.wealth) 54 | optimal_portfolio_history.append(optimal_portfolio.wealth) 55 | uniform_portfolio_history.append(uniform_portfolio.wealth) 56 | 57 | log.info("------------------------------------------------") 58 | log.info("estimate, true, odds") 59 | estimates = bayesian_update_portfolio.mean() 60 | for i, estimate in enumerate(estimates): 61 | log.info("%i: %f, %f, %f" % (i, estimate, probabilities[i], odds[i])) 62 | 63 | return ( 64 | bayesian_update_portfolio_history, 65 | optimal_portfolio_history, 66 | uniform_portfolio_history, 67 | bayesian_update_portfolio, 68 | optimal_portfolio, 69 | uniform_portfolio, 70 | ) 71 | 72 | 73 | def race_result(probabilities): 74 | return choice(len(probabilities), p=probabilities) 75 | --------------------------------------------------------------------------------