-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
Important Review skippedReview was skipped due to path filters Files ignored due to path filters (1)
You can disable this status message by setting the WalkthroughThe 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
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
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this 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
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 usedRemove assignment to unused variable
max_bet
(F841)
234-234: Local variable
confidence
is assigned to but never usedRemove 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.
There was a problem hiding this 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
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.
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py
Outdated
Show resolved
Hide resolved
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py
Outdated
Show resolved
Hide resolved
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- user passes in p_yes, market_p_yes. Returns a tuple of [bet_direction, bet amount]
- user passes in p_yes, market_p_yes. Returns signed 'yes' bet_amount (i.e. -ve amount means 'no' direction)
- 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)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
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 | ||
) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this 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
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
There was a problem hiding this 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
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done
prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py
Outdated
Show resolved
Hide resolved
- 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. |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(#331 to reproduce graph)
…_criterion.py Co-authored-by: Peter Jung <[email protected]>
There was a problem hiding this 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
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
…osis/prediction-market-agent-tooling into evan/add-kelly-bet-simplified
There was a problem hiding this 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
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.
There was a problem hiding this 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
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.
pytest failure unrelated |
No description provided.