From 378f631d18bcf5d909c1d23b6914237ee8de38d5 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 07:51:38 +0900 Subject: [PATCH 01/10] Add unit tests --- requirements-test.txt | 1 + tests/test_kelly.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 requirements-test.txt create mode 100644 tests/test_kelly.py diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..f9ddc40 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1 @@ +pytest==8.3.3 \ No newline at end of file diff --git a/tests/test_kelly.py b/tests/test_kelly.py new file mode 100644 index 0000000..8b113bd --- /dev/null +++ b/tests/test_kelly.py @@ -0,0 +1,18 @@ +import pytest + +import kelly + + +def test_calculate_kelly_stake(): + stake = kelly.calculate_kelly_stake( + price=100, + is_back=False, + probability=1 / 100, + other_probabilities=[99 / 100], + position=0.0, + other_positions=[0.0], + bankroll=100.0, + kelly_fraction=1.0, + verbose=True, + ) + assert stake == pytest.approx(0.0) From 42fbb2b1e62b5a377f77bcc70bdf4b42cb614820 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 07:56:50 +0900 Subject: [PATCH 02/10] Fix assertions --- tests/test_kelly.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_kelly.py b/tests/test_kelly.py index 8b113bd..13ec684 100644 --- a/tests/test_kelly.py +++ b/tests/test_kelly.py @@ -1,5 +1,3 @@ -import pytest - import kelly @@ -15,4 +13,5 @@ def test_calculate_kelly_stake(): kelly_fraction=1.0, verbose=True, ) - assert stake == pytest.approx(0.0) + assert stake > 0 + assert round(stake, 2) == 0.0 From 94456ab92ad2c6fc07854ca30aff0a7291cc65be Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 07:57:06 +0900 Subject: [PATCH 03/10] Fix upper bound when laying --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index bc22f2a..a18ccac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,6 +222,11 @@ fn calculate_kelly_stake( kelly_fraction: f64, verbose: bool, ) -> PyResult { + let upper_bound = if is_back { + bankroll + } else { + bankroll / (price - 1.0) + }; let kelly_stake = bounded_minimisation( |stake| { -calculate_log_expected_wealth( @@ -236,7 +241,7 @@ fn calculate_kelly_stake( ) }, 0.0, - bankroll, // TODO: Fix upper bound; this should depend on current position and might even exceed the bankroll if green on all selections + upper_bound, // TODO: Fix upper bound; this should depend on current position and might even exceed the bankroll if green on all selections verbose, ); Ok(kelly_stake * kelly_fraction) From f6a045b4bb39a50b11c2dd55469b889a657ae8a7 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:00:33 +0900 Subject: [PATCH 04/10] Add test.yml --- .github/workflows/test.yml | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2cd7613 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: test + +on: + push: + branches: [ master, "release-*" ] + pull_request: + branches: [ master, "release-*" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.9", "3.10", "3.11", "3.12"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Run maturin build + uses: PyO3/maturin-action@v1 + with: + manylinux: auto + command: build + args: --release --sdist -o dist --interpreter python${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements-test.txt + - name: Install package + run: python3 -m pip install --user dist/kelly*.tar.gz + - name: Lint with black + run: | + black . --check + - name: Test with pytest + run: | + pytest \ No newline at end of file From d89a05befca9e466bff8450a8ce465290b0d0a69 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:00:42 +0900 Subject: [PATCH 05/10] Rewrite CI.yml --- .github/workflows/CI.yml | 126 ++++++++++++--------------------------- 1 file changed, 38 insertions(+), 88 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 8268eaf..5b600c1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,120 +1,70 @@ -# This file is autogenerated by maturin v1.1.0 -# To update, run -# -# maturin generate-ci github -# name: CI on: push: - branches: - - main - - master - tags: - - '*' pull_request: - workflow_dispatch: - -permissions: - contents: read jobs: linux: - runs-on: ubuntu-latest strategy: matrix: - target: [x86_64, x86, aarch64, armv7, s390x, ppc64le] + target: [x86_64, aarch64] + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - target: ${{ matrix.target }} - args: --release --out dist --find-interpreter - sccache: 'true' - manylinux: auto - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist + - uses: actions/checkout@v3 + - uses: messense/maturin-action@v1 + with: + manylinux: auto + command: build + target: ${{ matrix.target }} + args: --release --sdist -o dist --find-interpreter + - name: Upload wheels + uses: actions/upload-artifact@v2 + with: + name: wheels + path: dist windows: runs-on: windows-latest - strategy: - matrix: - target: [x64, x86] steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version: '3.10' - architecture: ${{ matrix.target }} - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - target: ${{ matrix.target }} - args: --release --out dist --find-interpreter - sccache: 'true' - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist + - uses: actions/checkout@v3 + - uses: messense/maturin-action@v1 + with: + command: build + args: --release -o dist --find-interpreter + - name: Upload wheels + uses: actions/upload-artifact@v2 + with: + name: wheels + path: dist macos: runs-on: macos-latest - strategy: - matrix: - target: [x86_64, aarch64] steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version: '3.10' - - name: Build wheels - uses: PyO3/maturin-action@v1 - with: - target: ${{ matrix.target }} - args: --release --out dist --find-interpreter - sccache: 'true' - - name: Upload wheels - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist - - sdist: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Build sdist - uses: PyO3/maturin-action@v1 - with: - command: sdist - args: --out dist - - name: Upload sdist - uses: actions/upload-artifact@v3 - with: - name: wheels - path: dist + - uses: actions/checkout@v3 + - uses: messense/maturin-action@v1 + with: + command: build + args: --release -o dist --universal2 --find-interpreter + - name: Upload wheels + uses: actions/upload-artifact@v2 + with: + name: wheels + path: dist release: name: Release runs-on: ubuntu-latest if: "startsWith(github.ref, 'refs/tags/')" - needs: [linux, windows, macos, sdist] + needs: [ macos, windows, linux ] steps: - - uses: actions/download-artifact@v3 + - uses: actions/download-artifact@v2 with: name: wheels - name: Publish to PyPI - uses: PyO3/maturin-action@v1 + uses: messense/maturin-action@v1 env: MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} with: command: upload - args: --skip-existing * + args: --skip-existing * \ No newline at end of file From 86b0d3c5dc1b444353779f6e9a1898ea5ef0a5b8 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:01:23 +0900 Subject: [PATCH 06/10] Add black to requirements-test.txt --- requirements-test.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-test.txt b/requirements-test.txt index f9ddc40..1ee5da5 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1 +1,2 @@ +black==24.8.0 pytest==8.3.3 \ No newline at end of file From 1a35a34acaa9d4b98f198f1b58c256a5cd61df99 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:09:11 +0900 Subject: [PATCH 07/10] Bump upload-artifact --- .github/workflows/CI.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5b600c1..34e259b 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -19,7 +19,7 @@ jobs: target: ${{ matrix.target }} args: --release --sdist -o dist --find-interpreter - name: Upload wheels - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: wheels path: dist @@ -33,7 +33,7 @@ jobs: command: build args: --release -o dist --find-interpreter - name: Upload wheels - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: wheels path: dist @@ -47,7 +47,7 @@ jobs: command: build args: --release -o dist --universal2 --find-interpreter - name: Upload wheels - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: wheels path: dist From a3917bb1327fc438703f842d1b3a329d073591ae Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:09:49 +0900 Subject: [PATCH 08/10] Bump minimum Python version --- README.md | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e42db8..d7f40f3 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Scenarios to be covered in the near future include: ## Installation -Requires Python 3.7 or above +Requires Python 3.9 or above ``` pip install git+https://github.com/mberk/kelly.git diff --git a/pyproject.toml b/pyproject.toml index f91b457..59b2baa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "kelly" -requires-python = ">=3.7" +requires-python = ">=3.9" classifiers = [ "Programming Language :: Rust", "Programming Language :: Python :: Implementation :: CPython", From 80409d537f4a2500bc4c7d1381f336b97efd568f Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:16:23 +0900 Subject: [PATCH 09/10] Fix removed cli option --- .github/workflows/CI.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 34e259b..a95ee58 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -45,7 +45,7 @@ jobs: - uses: messense/maturin-action@v1 with: command: build - args: --release -o dist --universal2 --find-interpreter + args: --release -o dist --target universal2-apple-darwin --find-interpreter - name: Upload wheels uses: actions/upload-artifact@v4 with: From 86e902baa5e60ae786a712df7e7228dbed889257 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Mon, 30 Sep 2024 08:25:54 +0900 Subject: [PATCH 10/10] Regenerate CI.yml --- .github/workflows/CI.yml | 195 ++++++++++++++++++++++++++++++--------- 1 file changed, 153 insertions(+), 42 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a95ee58..ad3ec43 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,70 +1,181 @@ +# This file is autogenerated by maturin v1.7.4 +# To update, run +# +# maturin generate-ci github +# name: CI on: push: + branches: + - main + - master + tags: + - '*' pull_request: + workflow_dispatch: + +permissions: + contents: read jobs: linux: + runs-on: ${{ matrix.platform.runner }} strategy: matrix: - target: [x86_64, aarch64] - runs-on: ubuntu-latest + platform: + - runner: ubuntu-latest + target: x86_64 + - runner: ubuntu-latest + target: x86 + - runner: ubuntu-latest + target: aarch64 + - runner: ubuntu-latest + target: armv7 + - runner: ubuntu-latest + target: s390x + - runner: ubuntu-latest + target: ppc64le + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + manylinux: auto + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-linux-${{ matrix.platform.target }} + path: dist + + musllinux: + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: ubuntu-latest + target: x86_64 + - runner: ubuntu-latest + target: x86 + - runner: ubuntu-latest + target: aarch64 + - runner: ubuntu-latest + target: armv7 steps: - - uses: actions/checkout@v3 - - uses: messense/maturin-action@v1 - with: - manylinux: auto - command: build - target: ${{ matrix.target }} - args: --release --sdist -o dist --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels - path: dist + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + manylinux: musllinux_1_2 + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-musllinux-${{ matrix.platform.target }} + path: dist windows: - runs-on: windows-latest + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: windows-latest + target: x64 + - runner: windows-latest + target: x86 steps: - - uses: actions/checkout@v3 - - uses: messense/maturin-action@v1 - with: - command: build - args: --release -o dist --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels - path: dist + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + architecture: ${{ matrix.platform.target }} + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-windows-${{ matrix.platform.target }} + path: dist macos: - runs-on: macos-latest + runs-on: ${{ matrix.platform.runner }} + strategy: + matrix: + platform: + - runner: macos-12 + target: x86_64 + - runner: macos-14 + target: aarch64 steps: - - uses: actions/checkout@v3 - - uses: messense/maturin-action@v1 - with: - command: build - args: --release -o dist --target universal2-apple-darwin --find-interpreter - - name: Upload wheels - uses: actions/upload-artifact@v4 - with: - name: wheels - path: dist + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.platform.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + - name: Upload wheels + uses: actions/upload-artifact@v4 + with: + name: wheels-macos-${{ matrix.platform.target }} + path: dist + + sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build sdist + uses: PyO3/maturin-action@v1 + with: + command: sdist + args: --out dist + - name: Upload sdist + uses: actions/upload-artifact@v4 + with: + name: wheels-sdist + path: dist release: name: Release runs-on: ubuntu-latest - if: "startsWith(github.ref, 'refs/tags/')" - needs: [ macos, windows, linux ] + if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }} + needs: [linux, musllinux, windows, macos, sdist] + permissions: + # Use to sign the release artifacts + id-token: write + # Used to upload release artifacts + contents: write + # Used to generate artifact attestation + attestations: write steps: - - uses: actions/download-artifact@v2 + - uses: actions/download-artifact@v4 + - name: Generate artifact attestation + uses: actions/attest-build-provenance@v1 with: - name: wheels + subject-path: 'wheels-*/*' - name: Publish to PyPI - uses: messense/maturin-action@v1 + if: "startsWith(github.ref, 'refs/tags/')" + uses: PyO3/maturin-action@v1 env: MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} with: command: upload - args: --skip-existing * \ No newline at end of file + args: --non-interactive --skip-existing wheels-*/*