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 max_price_impact parameter to KellyBettingStrategy #433

Merged
merged 44 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
4bfc702
WIP
gabrielfior Sep 24, 2024
2c8389a
Added Slippage-based Kelly strategy
gabrielfior Sep 25, 2024
c9c198a
Fixing PR
gabrielfior Sep 25, 2024
5333d1d
Removed file
gabrielfior Sep 25, 2024
195f019
Removed another file
gabrielfior Sep 25, 2024
fd741d4
Removed omen corrections
gabrielfior Sep 25, 2024
f7dc54b
WIP
gabrielfior Sep 26, 2024
9b6aaa7
Payouts increased
gabrielfior Sep 27, 2024
337df5e
Merge remote-tracking branch 'refs/remotes/origin/main' into gabriel/…
gabrielfior Sep 27, 2024
c17790d
Added tests for kelly slippage | PR comments
gabrielfior Sep 27, 2024
a6804a3
WIP
gabrielfior Sep 30, 2024
ac99ce1
Fetching bets with more error checking
gabrielfior Sep 30, 2024
bda388b
Fix examples/monitor/match_bets_with_langfuse_traces.py
evangriffiths Oct 1, 2024
752c3be
tidy
evangriffiths Oct 1, 2024
6c67f15
Review comment
evangriffiths Oct 1, 2024
f6a7499
Change match_bets_with_langfuse_traces.py to calculate p_yes MSE for …
evangriffiths Oct 1, 2024
0a01259
Removed dotenv
gabrielfior Oct 1, 2024
7a04ea6
Merge remote-tracking branch 'refs/remotes/origin/evan/fix-examples/m…
gabrielfior Oct 1, 2024
d13f235
Print correlation of p_yes mse with profit
evangriffiths Oct 1, 2024
55d90a6
Merge remote-tracking branch 'refs/remotes/origin/main' into gabriel/…
gabrielfior Oct 1, 2024
4b1a6d2
Sorting output file
gabrielfior Oct 1, 2024
297a0bf
Update prediction_market_agent_tooling/deploy/betting_strategy.py
gabrielfior Oct 1, 2024
5ba66b5
Update prediction_market_agent_tooling/deploy/betting_strategy.py
gabrielfior Oct 1, 2024
572b943
Merge remote-tracking branch 'refs/remotes/origin/evan/get_p_yes_mse'…
gabrielfior Oct 1, 2024
ba2afcd
Avoided price_impact warnings
gabrielfior Oct 1, 2024
dabcc23
Update prediction_market_agent_tooling/deploy/betting_strategy.py
gabrielfior Oct 1, 2024
1a3f362
Removed load_dotenv
gabrielfior Oct 1, 2024
625d012
Merge remote-tracking branch 'origin/gabriel/track-experiments-kelly'…
gabrielfior Oct 1, 2024
5e04adf
Tidy
gabrielfior Oct 1, 2024
3ccb430
Update .env.example
gabrielfior Oct 2, 2024
b7cd215
Implemented PR comments
gabrielfior Oct 2, 2024
321cff8
Switched back betting strategy to agreed upon impl
gabrielfior Oct 2, 2024
efa1909
Fixed mypy
gabrielfior Oct 2, 2024
06be0d6
Improved mypy removing ifs
gabrielfior Oct 2, 2024
79fca0c
Final mypy improvements
gabrielfior Oct 2, 2024
04ec90d
Merge remote-tracking branch 'refs/remotes/origin/main' into gabriel/…
gabrielfior Oct 2, 2024
b8884de
Moved test | refactored Yes,No
gabrielfior Oct 3, 2024
6b7b6a7
Importing logger from loggers and not loguru
gabrielfior Oct 3, 2024
83349d4
Made minimize_scalar bounds more robust
gabrielfior Oct 3, 2024
22e4028
Merge remote-tracking branch 'refs/remotes/origin/main' into gabriel/…
gabrielfior Oct 3, 2024
6f014a6
Small changes after merge
gabrielfior Oct 3, 2024
d3f5c04
Removed incorrect slippage mentions
gabrielfior Oct 3, 2024
74d524e
Refactored assert from betting stategy into integration test
gabrielfior Oct 3, 2024
8cd4818
Small fix test
gabrielfior Oct 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion examples/monitor/match_bets_with_langfuse_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from prediction_market_agent_tooling.markets.data_models import ResolvedBet
from prediction_market_agent_tooling.markets.omen.omen import OmenAgentMarket
from prediction_market_agent_tooling.tools.httpx_cached_client import HttpxCachedClient
from prediction_market_agent_tooling.tools.langfuse_client_utils import (
ProcessMarketTrace,
ResolvedBetWithTrace,
Expand All @@ -33,6 +34,11 @@ class SimulatedOutcome(BaseModel):
profit: float


class MSEProfit(BaseModel):
p_yes_mse: list[float]
total_profit: list[float]


def get_outcome_for_trace(
strategy: BettingStrategy,
trace: ProcessMarketTrace,
Expand Down Expand Up @@ -93,27 +99,61 @@ def get_outcome_for_trace(
"DeployableThinkThoroughlyProphetResearchAgent": "pma-think-thoroughly-prophet-research",
"DeployableKnownOutcomeAgent": "pma-knownoutcome",
}

agent_pkey_map = {
k: get_private_key_from_gcp_secret(v) for k, v in agent_gcp_secret_map.items()
}

# Define strategies we want to test out
strategies = [
MaxAccuracyBettingStrategy(bet_amount=1),
MaxAccuracyBettingStrategy(bet_amount=2),
MaxAccuracyBettingStrategy(bet_amount=25),
KellyBettingStrategy(max_bet_amount=1),
KellyBettingStrategy(max_bet_amount=2),
KellyBettingStrategy(max_bet_amount=5),
KellyBettingStrategy(max_bet_amount=25),
MaxAccuracyWithKellyScaledBetsStrategy(max_bet_amount=1),
MaxAccuracyWithKellyScaledBetsStrategy(max_bet_amount=2),
MaxAccuracyWithKellyScaledBetsStrategy(max_bet_amount=25),
MaxExpectedValueBettingStrategy(bet_amount=1),
MaxExpectedValueBettingStrategy(bet_amount=2),
MaxExpectedValueBettingStrategy(bet_amount=5),
MaxExpectedValueBettingStrategy(bet_amount=25),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.01),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.05),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.1),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.15),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.2),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.25),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.3),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.4),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.5),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.6),
KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.7),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.1),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.15),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.2),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.3),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.4),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.5),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.6),
KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.7),
KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.1),
KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.2),
KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.3),
KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.5),
KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.7),
]

httpx_client = HttpxCachedClient().get_client()

overall_md = ""

strat_mse_profits: dict[str, MSEProfit] = {}
for strategy in strategies:
strat_mse_profits[repr(strategy)] = MSEProfit(p_yes_mse=[], total_profit=[])

evangriffiths marked this conversation as resolved.
Show resolved Hide resolved
print("# Agent Bet vs Simulated Bet Comparison")
for agent_name, private_key in agent_pkey_map.items():
print(f"\n## {agent_name}\n")
Expand All @@ -126,6 +166,7 @@ def get_outcome_for_trace(
secret_key=api_keys.langfuse_secret_key.get_secret_value(),
public_key=api_keys.langfuse_public_key,
host=api_keys.langfuse_host,
httpx_client=httpx_client,
evangriffiths marked this conversation as resolved.
Show resolved Hide resolved
)

traces = get_traces_for_agent(
Expand Down Expand Up @@ -196,7 +237,18 @@ def get_outcome_for_trace(
)

details.sort(key=lambda x: x["sim_profit"], reverse=True)
pd.DataFrame.from_records(details).to_csv(
f"{agent_name} - {strategy} - all bets.csv", index=False
Copy link
Contributor

Choose a reason for hiding this comment

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

I think @evangriffiths removed this in the other PR because it was bloating disk too much? (btw sorry for spamming your disk Evan 😄 )

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. What's the use of saving them? I don't mind having them if they're useful, but maybe in a subdirectory which is .gitignored

Copy link
Contributor

Choose a reason for hiding this comment

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

The markdown file shows it aggregated per strategy (total profit, total ROI, etc.). These per-strategy files are good for finding out which are the bets that made it unprofitable.

For example, these sheets are imported out of these CSVs, it was eye-opening for me to see that the agent is profitable up to last 4 bets that have catastrophic effect.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see - reckless of me to just remove then. Well now they're back!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, will keep them here then.

)

sum_squared_errors = 0.0
for bet_with_trace in bets_with_traces:
bet = bet_with_trace.bet
trace = bet_with_trace.trace
estimated_p_yes = trace.answer.p_yes
actual_answer = float(bet.market_outcome)
sum_squared_errors += (estimated_p_yes - actual_answer) ** 2
p_yes_mse = sum_squared_errors / len(bets_with_traces)
evangriffiths marked this conversation as resolved.
Show resolved Hide resolved
total_bet_amount = sum([bt.bet.amount.amount for bt in bets_with_traces])
total_bet_profit = sum([bt.bet.profit.amount for bt in bets_with_traces])
total_simulated_amount = sum([so.size for so in simulated_outcomes])
Expand All @@ -212,27 +264,45 @@ def get_outcome_for_trace(
"bet_amount": total_bet_amount,
"bet_profit": total_bet_profit,
"roi": roi,
"p_yes mse": p_yes_mse,
# We don't know these for the original run.
"start_balance": None,
"end_balance": None,
}
)
else:
strat_mse_profits[repr(strategy)].p_yes_mse.append(p_yes_mse)
strat_mse_profits[repr(strategy)].total_profit.append(
total_simulated_profit
)
evangriffiths marked this conversation as resolved.
Show resolved Hide resolved

simulations.append(
{
"strategy": repr(strategy),
"bet_amount": total_simulated_amount,
"bet_profit": total_simulated_profit,
"roi": simulated_roi,
"p_yes mse": p_yes_mse,
"start_balance": starting_balance,
"end_balance": agent_balance,
}
)

simulations_df = pd.DataFrame.from_records(simulations)
simulations_df.sort_values(by="bet_profit", ascending=False, inplace=True)
overall_md += (
f"\n\n## {agent_name}\n\n{len(bets_with_traces)} bets\n\n"
+ pd.DataFrame.from_records(simulations).to_markdown(index=False)
+ simulations_df.to_markdown(index=False)
)
# export details per agent
pd.DataFrame.from_records(details).to_csv(f"{agent_name}_details.csv")

print(f"Correlation between p_yes mse and total profit:")
evangriffiths marked this conversation as resolved.
Show resolved Hide resolved
for strategy_name, mse_profit in strat_mse_profits.items():
mse = mse_profit.p_yes_mse
profit = mse_profit.total_profit
correlation = pd.Series(mse).corr(pd.Series(profit))
print(f"{strategy_name}: {correlation=}")

with open("match_bets_with_langfuse_traces_overall.md", "w") as overall_f:
overall_f.write(overall_md)
48 changes: 40 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading