File size: 8,122 Bytes
52d1750 12536a4 3f72b8c 577dd09 6298cbe 52d1750 3498a52 52d1750 f81ac63 330cbe3 52d1750 3f72b8c 52d1750 12536a4 3f72b8c 0e538d2 8704528 3035b84 8704528 3035b84 8704528 3035b84 8704528 6298cbe 8a73d91 6298cbe 8a73d91 8704528 3035b84 8704528 3498a52 8704528 3498a52 8704528 6298cbe 8704528 577dd09 3035b84 577dd09 3035b84 577dd09 3035b84 577dd09 3035b84 577dd09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
import pandas as pd
import gradio as gr
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
import seaborn as sns
from tabs.daily_graphs import color_mapping
from datetime import datetime
color_mapping = [
"darkviolet",
"purple",
"goldenrod",
"darkgoldenrod",
"green",
"darkgreen",
]
def plot_kl_div_per_market(closed_markets: pd.DataFrame) -> gr.Plot:
# adding the total
all_markets = closed_markets.copy(deep=True)
all_markets["market_creator"] = "all"
# merging both dataframes
final_markets = pd.concat([closed_markets, all_markets], ignore_index=True)
final_markets = final_markets.sort_values(by="opening_datetime", ascending=True)
fig = px.box(
final_markets,
x="month_year_week",
y="kl_divergence",
color="market_creator",
color_discrete_sequence=["purple", "goldenrod", "darkgreen"],
category_orders={"market_creator": ["pearl", "quickstart", "all"]},
)
fig.update_traces(boxmean=True)
fig.update_layout(
xaxis_title="Markets closing Week",
yaxis_title="Kullback–Leibler divergence",
legend=dict(yanchor="top", y=0.5),
width=800, # Adjusted for better fit on laptop screens
height=600, # Adjusted for better fit on laptop screens
)
fig.update_xaxes(tickformat="%b %d\n%Y")
return gr.Plot(
value=fig,
)
def plot_kl_div_with_off_by(closed_markets: pd.DataFrame) -> gr.Plot:
# adding the total
all_markets = closed_markets.copy(deep=True)
all_markets["market_creator"] = "all"
# merging both dataframes
final_markets = pd.concat([closed_markets, all_markets], ignore_index=True)
final_markets = final_markets.sort_values(by="opening_datetime", ascending=True)
# Create the main figure and axis
fig, ax1 = plt.subplots(figsize=(10, 6))
# Create the boxplot using seaborn
sns.boxplot(
data=final_markets,
x="month_year_week",
y="kl_divergence",
ax=ax1,
hue="market_creator",
)
# Set labels and title for the main axis
ax1.set_xlabel("Week")
ax1.set_ylabel("KL Divergence")
ax1.set_title("KL Divergence Boxplot with Off-by Percentage")
# Create a secondary y-axis
ax2 = ax1.twinx()
# Plot the off_by_perc values on the secondary y-axis
for i, week in enumerate(closed_markets["month_year_week"].unique()):
off_by_perc = closed_markets[closed_markets["month_year_week"] == week][
"off_by_perc"
]
ax2.scatter([i] * len(off_by_perc), off_by_perc, color="red", alpha=0.01)
# Set label for the secondary y-axis
ax2.set_ylabel("Off-by Percentage")
# Adjust the layout and display the plot
plt.tight_layout()
return gr.Plot(
value=fig,
)
def plot_total_bet_amount_per_trader_per_market(
trades_df: pd.DataFrame, trader_filter: str = "all"
) -> gr.Plot:
"""Plots the total bet amount per trader per market."""
traders_all = trades_df.copy(deep=True)
traders_all["market_creator"] = "all"
# merging both dataframes
final_traders = pd.concat([traders_all, trades_df], ignore_index=True)
final_traders = final_traders.sort_values(by="creation_date", ascending=True)
# Create binary staking category
final_traders["trader_type"] = final_traders["staking"].apply(
lambda x: "non_Olas" if x == "non_Olas" else "Olas"
)
final_traders["trader_market"] = final_traders.apply(
lambda x: (x["trader_type"], x["market_creator"]), axis=1
)
color_discrete_sequence = ["purple", "goldenrod", "darkgreen"]
if trader_filter == "Olas":
color_discrete_sequence = ["darkviolet", "goldenrod", "green"]
final_traders = final_traders.loc[final_traders["trader_type"] == "Olas"]
elif trader_filter == "non_Olas":
final_traders = final_traders.loc[final_traders["trader_type"] != "Olas"]
total_bet_amount = (
final_traders.groupby(
["month_year_week", "market_creator", "trader_address", "title"],
sort=False,
)["collateral_amount"]
.sum()
.reset_index(name="total_bet_amount")
)
# Convert string dates to datetime and sort them
all_dates_dt = sorted(
[
datetime.strptime(date, "%b-%d-%Y")
for date in total_bet_amount["month_year_week"].unique()
]
)
# Convert back to string format
all_dates = [date.strftime("%b-%d-%Y") for date in all_dates_dt]
fig = px.box(
total_bet_amount,
x="month_year_week",
y="total_bet_amount",
color="market_creator",
color_discrete_sequence=color_discrete_sequence,
category_orders={
"market_creator": ["pearl", "quickstart", "all"],
"trader_market": [
("Olas", "pearl"),
("non_Olas", "pearl"),
("Olas", "quickstart"),
("non_Olas", "quickstart"),
("Olas", "all"),
("non_Olas", "all"),
],
},
# facet_col="trader_type",
)
fig.update_traces(boxmean=True)
fig.update_layout(
xaxis_title="Week",
yaxis_title="Weekly bet amounts per trader per market",
legend=dict(yanchor="top", y=0.5),
width=1000, # Adjusted for better fit on laptop screens
height=600, # Adjusted for better fit on laptop screens
)
# for axis in fig.layout:
# if axis.startswith("xaxis"):
# fig.layout[axis].update(title="Week")
fig.update_xaxes(tickformat="%b %d\n%Y")
# Update layout to force x-axis category order (hotfix for a sorting issue)
fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": all_dates})
return gr.Plot(
value=fig,
)
def plot_nr_trades_per_trader_per_market(
traders_data: pd.DataFrame, trader_filter: str = "all"
) -> gr.Plot:
"""Function to paint the plot with the metric nr_trades_per_market by trader type and market creator"""
traders_all = traders_data.copy(deep=True)
traders_all["market_creator"] = "all"
# merging both dataframes
final_traders = pd.concat([traders_all, traders_data], ignore_index=True)
final_traders = final_traders.sort_values(by="creation_date", ascending=True)
# Create binary staking category
final_traders["trader_type"] = final_traders["staking"].apply(
lambda x: "non_Olas" if x == "non_Olas" else "Olas"
)
final_traders["trader_market"] = final_traders.apply(
lambda x: (x["trader_type"], x["market_creator"]), axis=1
)
color_discrete_sequence = ["purple", "goldenrod", "darkgreen"]
if trader_filter == "Olas":
color_discrete_sequence = ["darkviolet", "goldenrod", "green"]
final_traders = final_traders.loc[final_traders["trader_type"] == "Olas"]
elif trader_filter == "non_Olas":
final_traders = final_traders.loc[final_traders["trader_type"] != "Olas"]
fig = px.box(
final_traders,
x="month_year_week",
y="nr_trades_per_market",
color="market_creator",
color_discrete_sequence=color_discrete_sequence,
category_orders={
"market_creator": ["pearl", "quickstart", "all"],
"trader_market": [
("Olas", "pearl"),
("non_Olas", "pearl"),
("Olas", "quickstart"),
("non_Olas", "quickstart"),
("Olas", "all"),
("non_Olas", "all"),
],
},
# facet_col="trader_type",
)
fig.update_traces(boxmean=True)
fig.update_layout(
xaxis_title="Week",
yaxis_title="Nr trades per trader per market",
legend=dict(yanchor="top", y=0.5),
width=1000, # Adjusted for better fit on laptop screens
height=600, # Adjusted for better fit on laptop screens
)
fig.update_xaxes(tickformat="%b %d\n%Y")
return gr.Plot(
value=fig,
)
|