Skip to content

Commit

Permalink
Save more detailed bet details in strategy simulation script (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Sep 24, 2024
1 parent 92c4a89 commit 909d9fe
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions examples/monitor/match_bets_with_langfuse_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def get_outcome_for_trace(
MaxExpectedValueBettingStrategy(bet_amount=25),
]

overall_md = ""

print("# Agent Bet vs Simulated Bet Comparison")
for agent_name, private_key in agent_pkey_map.items():
print(f"\n## {agent_name}\n")
Expand Down Expand Up @@ -152,11 +154,12 @@ def get_outcome_for_trace(

print(f"Number of bets since {start_time}: {len(bets_with_traces)}\n")
if len(bets_with_traces) != len(bets):
raise ValueError(
f"{len(bets) - len(bets_with_traces)} bets do not have a corresponding trace"
print(
f"{len(bets) - len(bets_with_traces)} bets do not have a corresponding trace, ignoring them."
)

simulations: list[dict[str, Any]] = []
details = []

for strategy_idx, strategy in enumerate(strategies):
# "Born" agent with initial funding, simulate as if he was doing bets one by one.
Expand All @@ -175,6 +178,26 @@ def get_outcome_for_trace(
simulated_outcomes.append(simulated_outcome)
agent_balance += simulated_outcome.profit

details.append(
{
"url": trace.market.url,
"market_p_yes": round(trace.market.current_p_yes, 4),
"agent_p_yes": round(trace.answer.p_yes, 4),
"agent_conf": round(trace.answer.confidence, 4),
"org_bet": round(bet.amount.amount, 4),
"sim_bet": round(simulated_outcome.size, 4),
"org_dir": bet.outcome,
"sim_dir": simulated_outcome.direction,
"org_profit": round(bet.profit.amount, 4),
"sim_profit": round(simulated_outcome.profit, 4),
}
)

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
)

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 Down Expand Up @@ -207,4 +230,10 @@ def get_outcome_for_trace(
}
)

print(pd.DataFrame.from_records(simulations).to_markdown(index=False))
overall_md += (
f"\n\n## {agent_name}\n\n{len(simulations)} bets\n\n"
+ pd.DataFrame.from_records(simulations).to_markdown(index=False)
)

with open("match_bets_with_langfuse_traces_overall.md", "w") as overall_f:
overall_f.write(overall_md)

0 comments on commit 909d9fe

Please sign in to comment.