Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simplified get_kelly_bet function, remove old #330

Merged
merged 10 commits into from
Jul 31, 2024

Conversation

evangriffiths
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Jul 30, 2024

Important

Review skipped

Review was skipped due to path filters

Files ignored due to path filters (1)
  • pyproject.toml is excluded by !**/*.toml

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The recent changes enhance the betting strategy based on the Kelly Criterion by introducing an enumeration for bet outcomes, improving probability validation, and refining functions for optimal bet size calculations. These modifications increase code readability, robustness, and adaptability to varying market conditions. Additionally, new tests have been implemented to verify functionality under diverse scenarios, ensuring the reliability of the betting logic.

Changes

Files Change Summary
prediction_market_agent_tooling/.../kelly_criterion.py Introduced BetDirection enum for bet outcomes, added check_is_valid_probability for validation, defined get_kelly_bet and get_kelly_criterion_bet for optimal bet calculations. Enhanced clarity and robustness of the betting strategy implementation.
tests/markets/test_betting_strategies.py Added test_kelly_bet to validate the get_kelly_bet function with multiple scenarios, enhancing test coverage for the Kelly Criterion logic.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant KellyCriterion
    participant Validator
    participant Outcome

    User->>KellyCriterion: Request bet calculation
    KellyCriterion->>Validator: Check if probabilities are valid
    Validator-->>KellyCriterion: Return validation result
    KellyCriterion->>Outcome: Calculate optimal bet size
    Outcome-->>KellyCriterion: Return bet size
    KellyCriterion-->>User: Provide bet size
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1f4f58c and c6ece3f.

Files selected for processing (2)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (3 hunks)
  • tests/markets/test_betting_strategies.py (2 hunks)
Additional context used
Ruff
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py

179-179: Undefined name kelly_bet

(F821)

tests/markets/test_betting_strategies.py

233-233: Local variable max_bet is assigned to but never used

Remove assignment to unused variable max_bet

(F841)


234-234: Local variable confidence is assigned to but never used

Remove assignment to unused variable confidence

(F841)

Additional comments not posted (9)
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (4)

17-19: LGTM! Enumeration for bet outcomes is correctly defined.

The BetOutcome class enhances code readability and maintainability by replacing raw string literals with defined values.


22-24: LGTM! Probability validation is correctly implemented.

The check_is_valid_probability function ensures that probability values are within the acceptable range of 0 to 1.


27-64: LGTM! Optimal bet size calculation is well-implemented.

The get_kelly_bet_simplified function correctly calculates the optimal bet size using the Kelly Criterion, with appropriate checks and handling of edge cases.


Line range hint 67-163:
LGTM! Kelly Criterion implementation is well-documented and handles edge cases.

The _get_kelly_criterion_bet function correctly implements the Kelly Criterion for changing market odds, with appropriate mathematical calculations and documentation.

tests/markets/test_betting_strategies.py (5)

Line range hint 65-76:
LGTM! Test for minimum bet to win is well-implemented.

The test_minimum_bet_to_win function correctly validates the minimum bet to win calculation with multiple scenarios.


Line range hint 89-102:
LGTM! Test for minimum bet to win in Manifold market is well-implemented.

The test_minimum_bet_to_win_manifold function correctly validates the minimum bet to win calculation for the Manifold market with multiple scenarios.


Line range hint 114-135:
LGTM! Test for market moving bet is well-implemented.

The test_get_market_moving_bet function correctly validates the market moving bet calculation with multiple scenarios.


Line range hint 147-159:
LGTM! Test for Kelly Criterion bet is well-implemented.

The test_kelly_criterion_bet function correctly validates the Kelly Criterion bet calculation with multiple scenarios.


Line range hint 171-183:
LGTM! Test for stretch bet calculation is well-implemented.

The test_stretch_bet_between function correctly validates the stretch bet calculation with multiple scenarios.

tests/markets/test_betting_strategies.py Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c6ece3f and d2b3f87.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (3 hunks)
Additional context used
Ruff
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py

179-179: Undefined name kelly_bet

(F821)

Additional comments not posted (3)
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (3)

17-20: LGTM!

The BetOutcome enumeration is correctly implemented.


22-24: LGTM!

The check_is_valid_probability function correctly validates probability values.


27-64: LGTM!

The get_kelly_bet_simplified function is well-structured and correctly implements the Kelly Criterion for calculating the optimal bet amount.

kelly_fraction = edge / odds

# Ensure bet size is non-negative does not exceed the wallet balance
bet_size = max(min(kelly_fraction * max_bet, max_bet), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my reading of the Wikipedia article, I was under the impression it is possible that bet size is negative, hence we should bet on the opposite direction.
Is that also possible/desirable here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's one option. I figured there are 3 ways of writing this:

  1. user passes in p_yes, market_p_yes. Returns a tuple of [bet_direction, bet amount]
  2. user passes in p_yes, market_p_yes. Returns signed 'yes' bet_amount (i.e. -ve amount means 'no' direction)
  3. user passes in p_yes, market_p_yes, intended bet_direction. Returns bet_amount if > 0, else 0

I went with (3). Do you prefer one of the others (or something else)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like (1) better because you can simply pass the resulting bet_direction and bet_amount into whatever function is placing bets. Not a strong opinion on this though - if the ability to "switch" bet outcomes is covered here, then I'm happy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, have changed to (1)

Comment on lines 239 to 248
assert (
get_kelly_bet_simplified(
market_p_yes=omen_market.current_p_yes,
estimated_p_yes=est_p_yes,
max_bet=10,
confidence=1.0,
bet_outcome=bet_outcome,
)
> 0
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the formula presented a closed form, thus the test could simply assert the formula was calculated properly for a few conditions, or is it not the case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert the formula was calculated properly

How would that look like, without duplicating the code for the calculation in the test?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d2b3f87 and 02e1818.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 02e1818 and 43b52b6.

Files selected for processing (2)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (2 hunks)
  • tests/markets/test_betting_strategies.py (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py
  • tests/markets/test_betting_strategies.py

@@ -20,6 +80,7 @@ def _get_kelly_criterion_bet(

Taken from https://github.com/valory-xyz/trader/blob/main/strategies/kelly_criterion/kelly_criterion.py

Takes into account how the bet changes the market odds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove _get_kelly_criterion_bet? It's not used anywhere and your approach is well integrated with the whole market classes we have in order to determine a bet size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done

- q = 1-p is the probability of a loss
- b is the proportion of the bet gained with a win

'Simplified' in that it is assumes that the bet does not change the market odds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any significant differences between using simplified and using the original? (in terms of final bet size)

Copy link
Contributor Author

@evangriffiths evangriffiths Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-07-31 at 11 15 58

They're close when the max bet value is <= to market volume, but they diverge after that (tbh I was expecting them to diverge much sooner!)

Copy link
Contributor Author

@evangriffiths evangriffiths Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(#331 to reproduce graph)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 43b52b6 and e2c02d1.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e2c02d1 and c9e70c0.

Files selected for processing (2)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (1 hunks)
  • tests/markets/test_betting_strategies.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/markets/test_betting_strategies.py
Additional comments not posted (7)
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (7)

4-6: LGTM!

The BetOutcome class is well-defined and enhances code readability by replacing raw string literals with a defined set of values.


9-11: LGTM!

The check_is_valid_probability function is well-implemented and ensures that probability values are within the acceptable range.


14-20: Well-documented function.

The get_kelly_bet function is well-documented and provides a clear explanation of the Kelly Criterion formula and its parameters.


39-41: Ensure probability values are valid.

The function correctly validates the probability values using the check_is_valid_probability function.


43-48: Clear differentiation of bet outcomes.

The function correctly differentiates between "Yes" and "No" bet outcomes using the BetOutcome enumeration.


50-57: Handle edge cases and calculate Kelly fraction.

The function handles the edge case where market_prob is zero and correctly calculates the Kelly fraction.


59-61: Ensure non-negative bet size.

The function ensures that the bet size is non-negative and does not exceed the maximum bet.

@evangriffiths evangriffiths changed the title Add get_kelly_bet_simplified Add simplified get_kelly_bet function, remove old Jul 31, 2024
@evangriffiths
Copy link
Contributor Author

evangriffiths commented Jul 31, 2024

How does the bet size change with 'market p_yes'?

import numpy as np
from prediction_market_agent_tooling.tools.betting_strategies.kelly_criterion import (
    BetDirection,
    KellyBet,
    get_kelly_bet,
)

xdai_amounts_simplified = []
market_p_yess = np.linspace(0.01, 0.99, 100)
max_bet = 2
est_p_yes = 0.3
for market_p_yes in market_p_yess:
    kelly_bet: KellyBet = get_kelly_bet(
        market_p_yes=market_p_yes,
        estimated_p_yes=est_p_yes,
        max_bet=max_bet,
        confidence=1,
    )
    kelly_bet_signed = (
        kelly_bet.size if kelly_bet.direction == BetDirection.YES else -kelly_bet.size
    )
    # kelly_bet_signed = kelly_bet.size
    xdai_amounts_simplified.append(kelly_bet_signed)

# Plot the results
from matplotlib import pyplot as plt

plt.plot(
    market_p_yess,
    xdai_amounts_simplified,
)
plt.xlabel("Market p_yes")
plt.ylabel("Bet Amount")
plt.title(f"Kelly Bet ({max_bet=}, est_p_yes={est_p_yes:.3g})")
plt.legend()
plt.show()
Screenshot 2024-07-31 at 12 21 41

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c9e70c0 and 82c9ff7.

Files selected for processing (2)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (1 hunks)
  • tests/markets/test_betting_strategies.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/markets/test_betting_strategies.py
Additional comments not posted (4)
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (4)

6-8: LGTM!

The BetDirection enum is correctly implemented, improving code readability and maintainability.


11-13: LGTM!

The KellyBet class is correctly implemented using Pydantic, enhancing data validation and structure.


16-18: LGTM!

The check_is_valid_probability function is correctly implemented, ensuring probability values are within the valid range.


21-67: LGTM!

The get_kelly_bet function is well-documented and correctly implemented, enhancing the betting strategy based on the Kelly Criterion.

@evangriffiths
Copy link
Contributor Author

How does the bet size change with 'estimated p_yes'?

import numpy as np
from prediction_market_agent_tooling.tools.betting_strategies.kelly_criterion import (
    BetDirection,
    KellyBet,
    get_kelly_bet,
)

bet_amounts = []
est_p_yess = np.linspace(0.01, 0.99, 100)
max_bet = 2
market_p_yes = 0.65
for est_p_yes in est_p_yess:
    kelly_bet: KellyBet = get_kelly_bet(
        market_p_yes=market_p_yes,
        estimated_p_yes=est_p_yes,
        max_bet=max_bet,
        confidence=1,
    )
    kelly_bet_signed = (
        kelly_bet.size if kelly_bet.direction == BetDirection.YES else -kelly_bet.size
    )
    bet_amounts.append(kelly_bet_signed)

# Plot the results
from matplotlib import pyplot as plt

plt.plot(est_p_yess, bet_amounts)
plt.xlabel("Estimated p_yes")
plt.ylabel("Bet Amount")
plt.title(f"Kelly Bet ({max_bet=}, {market_p_yes=})")
plt.show()
Screenshot 2024-07-31 at 12 28 16

@evangriffiths
Copy link
Contributor Author

pytest failure unrelated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants