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

Refactor DeployableTraderAgent to 1. predict a probability for a market then 2. place a bet based on its BettingStrategy #352

Conversation

gabrielfior
Copy link
Contributor

No description provided.

Copy link

coderabbitai bot commented Aug 20, 2024

Walkthrough

The recent updates across the prediction_market_agent_tooling codebase implement a refined framework for betting strategies, transitioning to a probabilistic model with the introduction of ProbabilisticAnswer. Key modifications include the establishment of structured betting strategies via the BettingStrategy abstract class, enhancements to agent behavior through the addition of new attributes and methods, and improvements to code organization and clarity.

Changes

Files and Changes
prediction_market_agent_tooling/benchmark/utils.py, tests/test_answer.py: Removed or relocated Answer import, consolidating imports for better organization and aligning with ProbabilisticAnswer.
prediction_market_agent_tooling/deploy/agent.py, prediction_market_agent_tooling/deploy/agent_example.py: Updated to include a strategy attribute in DeployableTraderAgent, introduced a new method for calculating bet amounts, and changed return types to ProbabilisticAnswer.
prediction_market_agent_tooling/deploy/betting_strategy.py: Introduced BettingStrategy as an abstract class with implementations for MaxAccuracyBettingStrategy and KellyBettingStrategy.
tests/test_betting_strategy.py: New tests added for MaxAccuracyBettingStrategy, validating its behavior with parameterized tests.
tests/test_benchmark.py: Removed decision attribute from OutcomePrediction instances in tests, indicating a shift in prediction structure.
prediction_market_agent_tooling/tools/gnosis_rpc.py: Updated GNOSIS_RPC_URL to a new endpoint.
prediction_market_agent_tooling/tools/web3_utils.py: Expanded transaction retry conditions to handle specific error types.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DeployableTraderAgent
    participant BettingStrategy
    participant Market

    User->>DeployableTraderAgent: Place Bet
    DeployableTraderAgent->>BettingStrategy: Calculate Bet Amount and Direction
    BettingStrategy->>Market: Validate Market Conditions
    Market->>BettingStrategy: Return Market Status
    BettingStrategy-->>DeployableTraderAgent: Return Calculated Bet
    DeployableTraderAgent-->>User: Confirm Bet Placement
Loading

Possibly related issues


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.

…ge-default-betting-strategy-from-amount-==-tiny_bet-direction-==-yes-if-p_yes-05-to-use-kelly-bet

# Conflicts:
#	poetry.lock
@gabrielfior gabrielfior marked this pull request as ready for review August 20, 2024 18:10
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

Outside diff range, codebase verification and nitpick comments (3)
prediction_market_agent_tooling/deploy/betting_strategy.py (1)

1-61: New betting strategies file approved.

The new file introduces FixedBetBettingStrategy and KellyBettingStrategy. The use of abstract classes and methods is appropriate and enhances modularity.

Consider adding logging for the calculated bet amounts and directions to aid in debugging and analysis.

+ import logging
+ logger = logging.getLogger(__name__)

class FixedBetBettingStrategy(BettingStrategy, metaclass=ABCMeta):
    ...
    def calculate_bet_amount_and_direction(
        self, answer: Answer, market: AgentMarket
    ) -> TokenAmountAndDirection:
        bet_amount = self.get_bet_amount_for_market(market)
+       logger.debug(f"FixedBet: Market: {market}, Bet Amount: {bet_amount}, Direction: {answer.decision}")
        return TokenAmountAndDirection(
            amount=bet_amount,
            currency=market.currency,
            direction=answer.decision,
        )

class KellyBettingStrategy(BettingStrategy, metaclass=ABCMeta):
    ...
    def calculate_bet_amount_and_direction(
        self, answer: Answer, market: AgentMarket
    ) -> TokenAmountAndDirection:
        max_bet_amount = self.get_max_bet_amount_for_market()
        kelly_bet = get_kelly_bet(
            max_bet_amount, market.current_p_yes, answer.p_yes, answer.confidence
        )
+       logger.debug(f"KellyBet: Market: {market}, Bet Size: {kelly_bet.size}, Direction: {kelly_bet.direction}")
        return TokenAmountAndDirection(
            amount=kelly_bet.size,
            currency=market.currency,
            direction=kelly_bet.direction,
        )
prediction_market_agent_tooling/markets/polymarket/polymarket.py (1)

Line range hint 1-1: Removal of get_tiny_bet_amount approved.

The removal of the unimplemented get_tiny_bet_amount method simplifies the class interface. This change clarifies the current capabilities of PolymarketAgentMarket.

Consider planning the implementation of the place_bet method to complete the class functionality.

Would you like assistance in drafting an implementation plan for the place_bet method?

tests/markets/test_betting_strategies.py (1)

206-206: Simplify the boolean expression.

Use not ... instead of False if ... else True for clarity.

Apply the following change:

-    expected_bet_direction = False if est_p_yes < market_p_yes else True
+    expected_bet_direction = not est_p_yes < market_p_yes
Tools
Ruff

206-206: Use not ... instead of False if ... else True

Replace with not ...

(SIM211)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d9b13c7 and 01ba8f2.

Files ignored due to path filters (2)
  • poetry.lock is excluded by !**/*.lock, !**/*.lock
  • pyproject.toml is excluded by !**/*.toml
Files selected for processing (13)
  • prediction_market_agent_tooling/benchmark/utils.py (1 hunks)
  • prediction_market_agent_tooling/deploy/agent.py (7 hunks)
  • prediction_market_agent_tooling/deploy/agent_example.py (1 hunks)
  • prediction_market_agent_tooling/deploy/betting_strategy.py (1 hunks)
  • prediction_market_agent_tooling/markets/agent_market.py (1 hunks)
  • prediction_market_agent_tooling/markets/data_models.py (2 hunks)
  • prediction_market_agent_tooling/markets/manifold/manifold.py (1 hunks)
  • prediction_market_agent_tooling/markets/omen/omen.py (1 hunks)
  • prediction_market_agent_tooling/markets/polymarket/polymarket.py (1 hunks)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (4 hunks)
  • scripts/sell_all_omen.py (2 hunks)
  • tests/markets/test_betting_strategies.py (2 hunks)
  • tests/test_answer.py (1 hunks)
Files skipped from review due to trivial changes (2)
  • prediction_market_agent_tooling/benchmark/utils.py
  • prediction_market_agent_tooling/markets/omen/omen.py
Additional context used
Ruff
tests/markets/test_betting_strategies.py

206-206: Use not ... instead of False if ... else True

Replace with not ...

(SIM211)

Additional comments not posted (12)
tests/test_answer.py (1)

5-5: Import update approved.

The import path for the Answer class has been updated to reflect its new location. This change is consistent with the restructuring of the codebase.

prediction_market_agent_tooling/deploy/agent_example.py (1)

5-5: Import update approved.

The import path for the Probability type has been updated to reflect its new location. This change is consistent with the restructuring of the codebase.

prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (1)

1-1: Refactor approved with verification suggestion.

The function get_kelly_bet now returns a KellyResult instead of a KellyBet, and the logic for determining bet direction has been simplified. These changes enhance clarity and efficiency.

Ensure that all usages of the get_kelly_bet function are updated to handle the new KellyResult return type.

Run the following script to verify the function usage:

Also applies to: 14-55

Verification successful

All usages of get_kelly_bet correctly handle the KellyResult return type. The function is used in both test and deployment files, where its return value is accessed appropriately. No further changes are needed.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `get_kelly_bet` handle the new return type.

# Test: Search for the function usage. Expect: Only occurrences of handling `KellyResult`.
rg --type python -A 5 $'get_kelly_bet'

Length of output: 68


Script:

#!/bin/bash
# Description: Verify all function calls to `get_kelly_bet` handle the new return type.

# Test: Search for the function usage. Expect: Only occurrences of handling `KellyResult`.
rg 'get_kelly_bet' --glob '*.py' -A 5

Length of output: 2572

scripts/sell_all_omen.py (1)

65-67: LGTM! Verify the new logic for skipping bets.

The introduction of minimum_token_amount_for_selling enhances readability and maintainability.

Ensure that the logic for skipping bets based on this new variable is correctly integrated and doesn't introduce any unintended behavior.

Run the following script to verify the logic:

Verification successful

Verification Successful: Logic for Skipping Bets is Correctly Integrated

The logic for skipping bets using minimum_token_amount_for_selling is correctly implemented. The condition checks if current_token_balance.amount is less than or equal to minimum_token_amount_for_selling.amount, which ensures that bets are skipped appropriately.

  • File: scripts/sell_all_omen.py
    • Line: Conditional check using minimum_token_amount_for_selling.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the logic for skipping bets based on the new variable.

# Test: Search for the usage of `minimum_token_amount_for_selling`. Expect: Consistent logic for skipping bets.
rg --type python 'minimum_token_amount_for_selling'

Length of output: 82


Script:

#!/bin/bash
# Description: Verify the logic for skipping bets based on the new variable.

# Test: Search for the usage of `minimum_token_amount_for_selling`. Expect: Consistent logic for skipping bets.
rg --glob '*.py' 'minimum_token_amount_for_selling'

Length of output: 239

prediction_market_agent_tooling/markets/data_models.py (5)

60-62: LGTM! New class KellyResult is well-defined.

The KellyResult class encapsulates direction and size attributes effectively.


65-66: LGTM! New class TokenAmountAndDirection is well-defined.

The TokenAmountAndDirection class extends TokenAmount with a direction attribute, adding useful context.


89-89: LGTM! The Decision type is well-implemented.

The use of Annotated with BeforeValidator ensures that decisions adhere to the expected boolean format.


92-96: LGTM! The Answer class update is appropriate.

The inclusion of the Decision type in the Answer class emphasizes validated decision-making.


69-86: LGTM! Verify the usage of to_boolean_outcome.

The to_boolean_outcome function is robust and handles various input types effectively.

Ensure that this function is used consistently across the codebase to maintain data integrity.

Run the following script to verify the function usage:

prediction_market_agent_tooling/deploy/agent.py (3)

9-12: Import changes look good.

The imports for BettingStrategy and FixedBetBettingStrategy are necessary for the new betting strategy functionality.


186-186: Strategy attribute addition is appropriate.

The addition of the strategy attribute initialized with FixedBetBettingStrategy enhances the flexibility of the betting logic.


279-289: Method calculate_bet_amount_and_direction is well-implemented.

The method effectively integrates the strategy into the betting process and includes necessary validation for currency consistency.

Ensure that the calculate_bet_amount_and_direction method in the strategy is implemented correctly and handles all edge cases.

Run the following script to verify the method implementation in the strategy:

Verification successful

Multiple Implementations of calculate_bet_amount_and_direction Found

The calculate_bet_amount_and_direction method is implemented multiple times in betting_strategy.py, each with different logic. Ensure that these implementations handle all edge cases and integrate correctly with the agent's method in agent.py.

  • File: prediction_market_agent_tooling/deploy/betting_strategy.py
    • Lines: Multiple implementations found; verify logic and edge case handling.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `calculate_bet_amount_and_direction` in the strategy.

# Test: Search for the method implementation. Expect: Correct handling of edge cases.
ast-grep --lang python --pattern $'class $_ {
  $$$
  def calculate_bet_amount_and_direction($_, $_) {
    $$$
  }
  $$$  
}'

Length of output: 471


Script:

#!/bin/bash
# Description: Refine the search for the `calculate_bet_amount_and_direction` method in any class.

# Corrected pattern to search for the method implementation.
ast-grep --lang python --pattern $'class $_ {
  $$$
  def calculate_bet_amount_and_direction(self, answer, market):
    $$$
  }
  $$$  
}'

Length of output: 664


Script:

#!/bin/bash
# Description: Simplify the search pattern to locate the `calculate_bet_amount_and_direction` method in any class.

# Simplified pattern to search for the method implementation.
ast-grep --lang python --pattern $'def calculate_bet_amount_and_direction(self, $_, $_):'

Length of output: 90


Script:

#!/bin/bash
# Description: Search for any method containing "bet_amount" to find related implementations.

# Use ripgrep to find methods with "bet_amount" in their name.
rg --pcre2 --glob '*.py' -A 5 'def .*bet_amount.*\('

Length of output: 3949

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 01ba8f2 and d6f4b14.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/gnosis_rpc.py (1 hunks)
Additional comments not posted (1)
prediction_market_agent_tooling/tools/gnosis_rpc.py (1)

6-6: Verify the impact of changing the default GNOSIS_RPC_URL.

The default RPC URL for the Gnosis network has been updated. Ensure that this change aligns with the intended functionality and that any dependencies or documentation are updated accordingly.

Run the following script to verify the usage of GNOSIS_RPC_URL across the codebase and ensure that the change is consistent with the system's requirements:

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 d6f4b14 and 1ae480a.

Files selected for processing (1)
  • prediction_market_agent_tooling/tools/web3_utils.py (1 hunks)
Additional context used
Learnings (1)
Common learnings
Learnt from: kongzii
PR: gnosis/prediction-market-agent-tooling#224
File: prediction_market_agent_tooling/markets/markets.py:67-67
Timestamp: 2024-04-29T14:14:38.751Z
Learning: The function `have_bet_on_market_since` is suggested to be refactored into a method on the `AgentMarket` class, potentially reusing code from `DeployedAgent.get_resolved_bets`. This task is planned for future work, not within the scope of the current PR.
Additional comments not posted (1)
prediction_market_agent_tooling/tools/web3_utils.py (1)

160-160: LGTM! Verify the retry logic for new error condition.

The addition of the "ALREADY_EXISTS" error to the retry condition is a robust enhancement. Ensure that this error condition is correctly handled in all relevant scenarios.

To verify the retry logic, consider running tests that simulate the "ALREADY_EXISTS" error condition.

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 1ae480a and f073fb8.

Files ignored due to path filters (3)
  • ape-config.yaml is excluded by !**/*.yaml
  • poetry.lock is excluded by !**/*.lock, !**/*.lock
  • pyproject.toml is excluded by !**/*.toml
Files selected for processing (1)
  • prediction_market_agent_tooling/tools/web3_utils.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/tools/web3_utils.py
Additional context used
Learnings (1)
Common learnings
Learnt from: kongzii
PR: gnosis/prediction-market-agent-tooling#224
File: prediction_market_agent_tooling/markets/markets.py:67-67
Timestamp: 2024-04-29T14:14:38.751Z
Learning: The function `have_bet_on_market_since` is suggested to be refactored into a method on the `AgentMarket` class, potentially reusing code from `DeployedAgent.get_resolved_bets`. This task is planned for future work, not within the scope of the current PR.

@gabrielfior
Copy link
Contributor Author

Closes #309

prediction_market_agent_tooling/deploy/agent.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/deploy/betting_strategy.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/deploy/betting_strategy.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/deploy/betting_strategy.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/markets/data_models.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/tools/web3_utils.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@@ -61,7 +62,10 @@ def sell_all(
outcome = agent_market.outcomes[bet.outcomeIndex]
current_token_balance = agent_market.get_token_balance(better_address, outcome)

if current_token_balance.amount <= OmenAgentMarket.get_tiny_bet_amount().amount:
minimum_token_amount_for_selling = BetAmount(
amount=0.00001, currency=OmenAgentMarket.currency
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can keep the .get_tiny_bet_amount so that 0.0001 is defined on one place instead of two?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

abstracted to a common variable and refactored both usages

Copy link
Contributor

Choose a reason for hiding this comment

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

But why not just keep get_tiny_bet_amount as it is and use that? What was the motivation to remove it? (is there a need for that? from the code it doesn't look like it)

We have AgentMarket as a abstract class that our agents can work with, if something is subclass of AgentMarket, agent should just work with it (if all methods are implemented correctly).

But now, if we introduce a new SomeNewMarket(AgentMarket), it's not clear that there are places in the code where we accept AgentMarket, yet it won't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was an implementation decision. Certainly get_tiny_bet_amount used to work, but in your example, if we have a new market (Polymarket(AgentMarket), we would have to overwrite get_tiny_bet_amount implementation for that subclass - same here, we extend the logic on the FixedBettingStrategy. This is always needed because the bet_amount involves currency, which is different from market type to market type.

But now, if we introduce a new SomeNewMarket(AgentMarket), it's not clear that there are places in the code where we accept AgentMarket, yet it won't work.
The same will happen, only the exception will be different - NotImplementedError vs ValueError (from the Strategy get_max_bet_amount_for_market).

Copy link
Contributor

Choose a reason for hiding this comment

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

This is always needed

It was needed, but the difference is that it was forced. Because AgentMartet class has many such methods:

Screenshot by Dropbox Capture

So, if we add new market support, it should be as hard as possible to forget about something.

That was the idea behind AgentMarket. If we go with this other way of doing things, we will need to keep track of all the places around the code that need an update to make them work with basic agent stuff. And then AgentMarket class could be removed.

Copy link

Choose a reason for hiding this comment

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

@kongzii, I've created the issue as requested. You can track it here: Issue #358. This will help ensure that the AgentMarket class is refactored to use abstract methods, enhancing code robustness and preventing runtime errors. Let me know if there's anything else you'd like to address!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Create a GitHub issue to refactor the `AgentMarket` class to use abstract methods.
gh issue create --title "Refactor AgentMarket class to use abstract methods" --body "Refactor the \`AgentMarket\` class to make it an abstract class and use the \`@abstractmethod\` decorator for methods currently raising \`NotImplementedError\`. This change will enforce the implementation of necessary methods in subclasses, providing compile-time warnings if they are not implemented. This approach will enhance code robustness and prevent runtime errors. See the discussion for more details: https://github.com/gnosis/prediction-market-agent-tooling/pull/352#discussion_r1724570075" --assignee kongzii --repo gnosis/prediction-market-agent-tooling

Length of output: 715


Learnings used
Learnt from: kongzii
PR: gnosis/prediction-market-agent-tooling#224
File: prediction_market_agent_tooling/markets/markets.py:67-67
Timestamp: 2024-04-29T14:14:38.751Z
Learning: The function `have_bet_on_market_since` is suggested to be refactored into a method on the `AgentMarket` class, potentially reusing code from `DeployedAgent.get_resolved_bets`. This task is planned for future work, not within the scope of the current PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ABC pattern is ofc great and new issue makes sense.

Still, I don't see a way where we keep tiny_bet_amount and use kelly, except simply hijacking the method get_tiny_bet_amount with some bool_flag (e.g.use_kelly), which then implements a new logic.
Maybe you have a better suggestion?

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it seems like a misunderstanding.

KellyBettingStrategy is great; no change is needed there from my point of view.

FixedBetBettingStrategy could be renamed to TinyBetStrategy and use the get_tiny_bet_amount method of the market class. It makes sense to me that TinyBetStrategy would be the default strategy, because it's safe, and it will just work with arbitrary AgentMarket.

FixedBetBettingStrategy can exist as well, but it would take a amount argument in its __init__ method, and again, it doesn't have to use any get_tiny_bet_amount-like implementation, just return the fixed value.

wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented a mix, see 683b95f#diff-ebe828b1c77960ecaea62858ada5f1ee0b662b19051aa2ffbf92cd6ea6287110R25

I like the idea of using get_tiny_bet_amount, because this will break (mypy will complain) if another AgentMarket is implemented which does not have the method get_tiny_bet_amount. Together with ABC from the other issue, this is robust enough for me.

@@ -316,12 +302,17 @@ def process_bets(self, market_type: MarketType) -> None:
logger.info(f"Skipping market {market} as no answer was provided")
continue
if self.place_bet:
amount = self.calculate_bet_amount(result, market)
amount_and_direction = self.calculate_bet_amount_and_direction(
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels to me odd that we're computing an Answer above with result = self.answer_binary_market(market) (which already contains a bet direction == result.decision) and then computing a direction again here.

Not sure how much of a can of worms that is to refactor, but I think that should come first (or as part of this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's an important aspect.

It feels to me odd that we're computing an Answer above with result = self.answer_binary_market(market) (which already contains a bet direction == result.decision) and then computing a direction again here.

We need the Answer because it contains estimated_p_yes, required by the Kelly criterion (which can then revert the direction). To compare that with an agent case, the TTA creates an Answer object with a "black-box" logic - if we provide the Kelly strategy, then that's how we want to calculate bet sizes | direction.

Another idea I see is having the agents simply output p_yes, rationale, etc but NOT directions. In this case it's clear how direction was defined.
Problem with that is that FixedBettingStrategy copies direction from Answer - so in this case, we would need a logic for deciding direction (e.g. p_yes > 0.5, go YES) or similar, but also observing the market p_yes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another idea I see is having the agents simply output p_yes, rationale, etc but NOT directions.

Yes, this one!! This is the refactor I meant in the description for the ticket (#338 (comment)) 😁 (sorry if it wasn't very clear).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Removed direction from Answer and adapted the method, will change this in PMA when agents produce Answers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented this, see ProbabilisticAnswer - also adapted PMA to restrain the agent from producing Direction - this will be done by the Strategy itself using estimate_p_yes and market.current_p_yes

prediction_market_agent_tooling/deploy/agent.py Outdated Show resolved Hide resolved
ape-config.yaml 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8dce9ce and 82bd7b9.

Files selected for processing (1)
  • prediction_market_agent_tooling/deploy/betting_strategy.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/deploy/betting_strategy.py

gabrielfior and others added 6 commits August 23, 2024 13:47
Co-authored-by: Evan Griffiths <56087052+evangriffiths@users.noreply.github.com>
Co-authored-by: Evan Griffiths <56087052+evangriffiths@users.noreply.github.com>
…egy-from-amount-==-tiny_bet-direction-==-yes-if-p_yes-05-to-use-kelly-bet' into 338-change-default-betting-strategy-from-amount-==-tiny_bet-direction-==-yes-if-p_yes-05-to-use-kelly-bet
Co-authored-by: Evan Griffiths <56087052+evangriffiths@users.noreply.github.com>
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

Outside diff range, codebase verification and nitpick comments (9)
prediction_market_agent_tooling/deploy/agent_example.py (2)

16-18: Consider improving the randomness explanation.

The reasoning for the decision in DeployableCoinFlipAgent could be more descriptive to clarify the randomness.

Consider updating the reasoning attribute to explain the randomness more explicitly, such as "Randomly decided based on a coin flip."


26-27: Clarify the exception message.

The RuntimeError message in DeployableAlwaysRaiseAgent could be more informative.

Consider updating the message to something like "This agent is configured to always raise an exception."

prediction_market_agent_tooling/deploy/betting_strategy.py (4)

13-18: Ensure proper documentation for the abstract class.

The BettingStrategy class serves as an abstract base class for betting strategies. Consider adding a class-level docstring to describe its purpose and usage.

 class BettingStrategy(ABC):
+    """
+    Abstract base class for defining betting strategies.
+    """

21-27: Consider renaming calculate_direction for clarity.

The method name calculate_direction might be more descriptive if it reflects its purpose, such as determine_bet_direction.

 def calculate_direction(market_p_yes: float, estimate_p_yes: float) -> bool:
-    # If estimate_p_yes >= market.current_p_yes, then bet TRUE, else bet FALSE.
+    # If estimate_p_yes >= market_p_yes, then bet TRUE, else bet FALSE.

29-38: Ensure consistency in method documentation.

The calculate_bet_amount_and_direction method lacks a docstring. Adding one would improve readability and maintainability.

 def calculate_bet_amount_and_direction(
+    """
+    Calculate the bet amount and direction based on the market and answer probabilities.
+    """

47-58: Ensure consistency in method documentation.

The calculate_bet_amount_and_direction method lacks a docstring. Adding one would improve readability and maintainability.

 def calculate_bet_amount_and_direction(
+    """
+    Calculate the bet amount and direction using the Kelly criterion.
+    """
tests/test_benchmark.py (3)

Line range hint 48-54: Ensure comprehensive test coverage.

The test_agent_prediction function correctly tests the updated prediction logic. Consider adding edge cases to ensure robustness.

def test_agent_prediction(dummy_agent: DummyAgent) -> None:
    # Existing assertions...
    # Add edge case tests here

Line range hint 64-73: Ensure cache functionality is robust.

The test_cache function tests the cache save and load functionality. Ensure that it handles edge cases, such as corrupted files or missing data.

def test_cache() -> None:
    # Existing test logic...
    # Add tests for edge cases

Line range hint 76-111: Ensure benchmarker cache functionality is robust.

The test_benchmarker_cache function tests the benchmarker cache functionality. Ensure that it handles edge cases, such as cache misses or invalid data.

def test_benchmarker_cache(dummy_agent: DummyAgent) -> None:
    # Existing test logic...
    # Add tests for edge cases
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 82bd7b9 and 45f9545.

Files ignored due to path filters (2)
  • poetry.lock is excluded by !**/*.lock, !**/*.lock
  • pyproject.toml is excluded by !**/*.toml
Files selected for processing (7)
  • prediction_market_agent_tooling/benchmark/agents.py (2 hunks)
  • prediction_market_agent_tooling/deploy/agent.py (8 hunks)
  • prediction_market_agent_tooling/deploy/agent_example.py (2 hunks)
  • prediction_market_agent_tooling/deploy/betting_strategy.py (1 hunks)
  • prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (2 hunks)
  • tests/test_benchmark.py (3 hunks)
  • tests/test_betting_strategy.py (1 hunks)
Additional comments not posted (13)
tests/test_betting_strategy.py (1)

1-20: Good start with the test cases! Consider adding edge cases.

The test cases for MaxAccuracyBettingStrategy are well-structured and cover basic scenarios.

Consider adding edge cases, such as when estimate_p_yes equals market_p_yes, to ensure comprehensive test coverage.

prediction_market_agent_tooling/deploy/agent_example.py (1)

5-5: Approved: Transition to ProbabilisticAnswer.

The change to use ProbabilisticAnswer enhances the probabilistic modeling approach.

Ensure that all dependent code is updated to handle ProbabilisticAnswer.

prediction_market_agent_tooling/tools/betting_strategies/kelly_criterion.py (3)

4-6: Approved: Probability validation function.

The check_is_valid_probability function effectively ensures that probabilities are within a valid range.

Consider adding a docstring to describe its purpose.


10-10: Approved: Simplification of direction attribute.

The change to use a boolean for direction in KellyBet simplifies the representation.

Ensure that all dependent code is updated to handle the boolean direction.


42-46: Approved: Kelly Criterion implementation.

The get_kelly_bet function correctly implements the Kelly Criterion for calculating bet sizes.

Consider adding comments to explain the logic behind choosing the bet_direction and handling market_prob.

prediction_market_agent_tooling/benchmark/agents.py (1)

Line range hint 50-57: Simplification of prediction logic in FixedAgent.

The predict method now directly assigns p_yes based on fixed_answer. This change simplifies the logic and aligns with the agent's purpose.

tests/test_benchmark.py (3)

Line range hint 57-61: Verify benchmark run logic.

The test_benchmark_run function appears to correctly test the benchmark logic. Ensure that it covers all relevant scenarios, especially with the updated prediction logic.


Line range hint 114-128: Verify handling of cancelled markets.

The test_benchmarker_cancelled_markets function tests the handling of cancelled markets. Ensure that it covers all relevant scenarios and correctly raises exceptions.


Line range hint 131-186: Verify probable resolution logic.

The test_market_probable_resolution function tests the probable resolution logic. Ensure that it covers all relevant scenarios and correctly handles unexpected resolutions.

prediction_market_agent_tooling/deploy/agent.py (4)

281-281: LGTM!

The initialization of the strategy attribute with MaxAccuracyBettingStrategy is consistent with the PR's objectives.


314-324: LGTM!

The calculate_bet_amount_and_direction method is well-structured and includes appropriate error handling for currency mismatches.


375-377: LGTM!

The change in return type to ProbabilisticAnswer | None aligns with the new strategy implementation.


425-443: LGTM!

The use of calculate_bet_amount_and_direction and the added logging enhance the clarity and robustness of the betting process.

prediction_market_agent_tooling/deploy/betting_strategy.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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 45f9545 and 0e9386a.

Files selected for processing (1)
  • prediction_market_agent_tooling/deploy/betting_strategy.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/deploy/betting_strategy.py

@evangriffiths evangriffiths self-requested a review August 23, 2024 21:40
Copy link
Contributor

@evangriffiths evangriffiths left a comment

Choose a reason for hiding this comment

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

Nice one. Have left a couple things up to you. Oo also the title needs updating now!

prediction_market_agent_tooling/deploy/betting_strategy.py Outdated Show resolved Hide resolved
prediction_market_agent_tooling/deploy/betting_strategy.py Outdated Show resolved Hide resolved
@@ -47,10 +40,10 @@ def get_kelly_bet(
check_is_valid_probability(confidence)

if estimated_p_yes > market_p_yes:
bet_direction = BetDirection.YES
bet_direction = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this change from a YES enum to a boolean required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was discussed in another thread (can't remember which one from the 80 comments) - if you have specific concerns, happy to open a new PR to discuss having "YES" or True.

pyproject.toml Outdated Show resolved Hide resolved
prediction_market_agent_tooling/deploy/agent.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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0e9386a and 9eb4140.

Files ignored due to path filters (1)
  • poetry.lock is excluded by !**/*.lock, !**/*.lock
Files selected for processing (2)
  • prediction_market_agent_tooling/deploy/agent.py (8 hunks)
  • prediction_market_agent_tooling/deploy/betting_strategy.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • prediction_market_agent_tooling/deploy/agent.py
  • prediction_market_agent_tooling/deploy/betting_strategy.py

@gabrielfior gabrielfior merged commit 5a57423 into main Aug 27, 2024
7 checks passed
@gabrielfior gabrielfior deleted the 338-change-default-betting-strategy-from-amount-==-tiny_bet-direction-==-yes-if-p_yes-05-to-use-kelly-bet branch August 27, 2024 12:17
@evangriffiths evangriffiths changed the title 338 change default betting strategy from amount == tiny bet direction == yes if p yes 05 to use kelly bet Refactor DeployableTraderAgent to 1. predict a probability for a market then 2. place a bet based on its BettingStrategy Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants