cyberosa commited on
Commit
a46bb55
·
1 Parent(s): 773f144

adding more graphs

Browse files
app.py CHANGED
@@ -9,7 +9,11 @@ from tabs.tokens_votes_dist import (
9
  get_based_tokens_distribution,
10
  get_based_votes_distribution,
11
  )
12
- from tabs.dist_gap import get_distribution_plot
 
 
 
 
13
 
14
 
15
  def get_logger():
@@ -45,6 +49,10 @@ def prepare_data():
45
  df["sample_datetime"] = df["sample_timestamp"].apply(
46
  lambda x: datetime.fromtimestamp(x)
47
  )
 
 
 
 
48
  return df
49
 
50
 
@@ -74,16 +82,19 @@ with demo:
74
  with gr.Tabs():
75
  with gr.TabItem("💹 Probability distributions of live markets"):
76
  with gr.Row():
77
- gr.Markdown("# Evolution of outcomes probability based on tokens")
78
-
79
- with gr.Row():
80
- gr.Markdown("Best case: a market with a low distribution gap metric")
81
  with gr.Row():
82
  gr.Markdown(f"Market id = {best_market_id}")
83
  with gr.Row():
84
- best_market_tokens_dist = get_based_tokens_distribution(
85
- best_market_id, markets_data
86
- )
 
 
 
 
 
 
87
 
88
  with gr.Row():
89
  gr.Markdown("Worst case: a market with a high distribution gap metric")
@@ -91,36 +102,36 @@ with demo:
91
  gr.Markdown(f"Market id = {worst_market_id}")
92
 
93
  with gr.Row():
94
- worst_market_tokens_dist = get_based_tokens_distribution(
95
- worst_market_id, markets_data
96
- )
 
 
 
 
 
 
97
 
 
98
  with gr.Row():
99
- gr.Markdown("# Evolution of outcomes probability based on votes")
100
- with gr.Row():
101
- gr.Markdown("Best case: a market with a low distribution gap metric")
102
- with gr.Row():
103
- gr.Markdown(f"Market id = {best_market_id}")
104
- with gr.Row():
105
- best_market_votes_dist = get_based_votes_distribution(
106
- best_market_id, markets_data
107
  )
108
  with gr.Row():
109
- gr.Markdown("Worst case: a market with a high distribution gap metric")
110
  with gr.Row():
111
- gr.Markdown(f"Market id = {worst_market_id}")
112
 
113
  with gr.Row():
114
- worst_market_tokens_dist = get_based_votes_distribution(
115
- worst_market_id, markets_data
116
- )
117
 
118
- with gr.TabItem("📏 Distribution gap metric"):
119
  with gr.Row():
120
- gr.Markdown(
121
- "This metric measures the difference between the probability distribution based on the tokens distribution and the one based on the votes distribution"
122
- )
123
 
124
  with gr.Row():
125
- dist_plot = get_distribution_plot(markets_data)
 
 
 
 
126
  demo.queue(default_concurrency_limit=40).launch()
 
9
  get_based_tokens_distribution,
10
  get_based_votes_distribution,
11
  )
12
+ from tabs.dist_gap import (
13
+ get_distribution_plot,
14
+ get_correlation_map,
15
+ get_kde_with_trades,
16
+ )
17
 
18
 
19
  def get_logger():
 
49
  df["sample_datetime"] = df["sample_timestamp"].apply(
50
  lambda x: datetime.fromtimestamp(x)
51
  )
52
+ df["opening_datetime"] = df["openingTimestamp"].apply(
53
+ lambda x: datetime.fromtimestamp(int(x))
54
+ )
55
+ df["days_to_resolution"] = (df["opening_datetime"] - df["sample_datetime"]).dt.days
56
  return df
57
 
58
 
 
82
  with gr.Tabs():
83
  with gr.TabItem("💹 Probability distributions of live markets"):
84
  with gr.Row():
85
+ gr.Markdown("Best case: a market with a low gap between distributions")
 
 
 
86
  with gr.Row():
87
  gr.Markdown(f"Market id = {best_market_id}")
88
  with gr.Row():
89
+ with gr.Column(scale=1, min_width=300):
90
+ # gr.Markdown("# Evolution of outcomes probability based on tokens")
91
+ best_market_tokens_dist = get_based_tokens_distribution(
92
+ best_market_id, markets_data
93
+ )
94
+ with gr.Column(scale=2, min_width=300):
95
+ best_market_votes_dist = get_based_votes_distribution(
96
+ best_market_id, markets_data
97
+ )
98
 
99
  with gr.Row():
100
  gr.Markdown("Worst case: a market with a high distribution gap metric")
 
102
  gr.Markdown(f"Market id = {worst_market_id}")
103
 
104
  with gr.Row():
105
+ with gr.Column(scale=1, min_width=300):
106
+ # gr.Markdown("# Evolution of outcomes probability based on tokens")
107
+ worst_market_tokens_dist = get_based_tokens_distribution(
108
+ worst_market_id, markets_data
109
+ )
110
+ with gr.Column(scale=2, min_width=300):
111
+ worst_market_votes_dist = get_based_votes_distribution(
112
+ worst_market_id, markets_data
113
+ )
114
 
115
+ with gr.TabItem("📏 Distribution gap metric"):
116
  with gr.Row():
117
+ gr.Markdown(
118
+ "This metric measures the difference between the probability distribution based on the tokens distribution and the one based on the votes distribution"
 
 
 
 
 
 
119
  )
120
  with gr.Row():
121
+ gr.Markdown("# Density distribution")
122
  with gr.Row():
123
+ kde_plot = get_distribution_plot(markets_data)
124
 
125
  with gr.Row():
126
+ gr.Markdown("# Relationship with number of trades")
 
 
127
 
 
128
  with gr.Row():
129
+ kde_trades_plot = get_kde_with_trades(markets_data)
 
 
130
 
131
  with gr.Row():
132
+ gr.Markdown("# Correlation analysis between variables")
133
+
134
+ with gr.Row():
135
+ correlation_plot = get_correlation_map(markets_data)
136
+
137
  demo.queue(default_concurrency_limit=40).launch()
notebooks/analysis_of_markets_data.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
tabs/dist_gap.py CHANGED
@@ -6,7 +6,7 @@ from seaborn import FacetGrid
6
  import plotly.express as px
7
 
8
 
9
- def plot_top_best_behaviour_markets(markets_data: pd.DataFrame):
10
  """Function to paint the top markets with the lowest metric of distribution gap"""
11
  sorted_data = markets_data.sort_values(by="dist_gap_perc", ascending=False)
12
  top_best_markets = sorted_data[["title", "sample_datetime", "dist_gap_perc"]].head(
@@ -17,8 +17,56 @@ def plot_top_best_behaviour_markets(markets_data: pd.DataFrame):
17
 
18
  def get_distribution_plot(markets_data: pd.DataFrame):
19
  """Function to paint the density plot of the metric distribution gap percentage"""
 
 
 
 
20
  plt.figure(figsize=(25, 10))
 
21
  plot = sns.kdeplot(markets_data, x="dist_gap_perc", fill=True)
22
  # TODO Add title and labels
23
  # Display the plot using gr.Plot
24
  return gr.Plot(value=plot.get_figure())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import plotly.express as px
7
 
8
 
9
+ def get_top_best_behaviour_markets(markets_data: pd.DataFrame):
10
  """Function to paint the top markets with the lowest metric of distribution gap"""
11
  sorted_data = markets_data.sort_values(by="dist_gap_perc", ascending=False)
12
  top_best_markets = sorted_data[["title", "sample_datetime", "dist_gap_perc"]].head(
 
17
 
18
  def get_distribution_plot(markets_data: pd.DataFrame):
19
  """Function to paint the density plot of the metric distribution gap percentage"""
20
+ # A kernel density estimate (KDE) plot is a method for visualizing the distribution of
21
+ # observations in a dataset, analogous to a histogram. KDE represents the data using a
22
+ # continuous probability density curve in one or more dimensions.
23
+ sns.set_theme(palette="viridis")
24
  plt.figure(figsize=(25, 10))
25
+
26
  plot = sns.kdeplot(markets_data, x="dist_gap_perc", fill=True)
27
  # TODO Add title and labels
28
  # Display the plot using gr.Plot
29
  return gr.Plot(value=plot.get_figure())
30
+
31
+
32
+ def get_kde_with_trades(markets_data: pd.DataFrame):
33
+ """Function to paint the density plot of the metric in terms of the number of trades"""
34
+ plot = sns.kdeplot(markets_data, x="dist_gap_perc", y="total_trades", fill=True)
35
+
36
+ return gr.Plot(value=plot.get_figure())
37
+
38
+
39
+ def get_correlation_map(markets_data: pd.DataFrame):
40
+ """Function to paint the correlation between different variables"""
41
+
42
+ columns_of_interest = ["total_trades", "dist_gap_perc", "days_to_resolution"]
43
+ data = markets_data[columns_of_interest]
44
+
45
+ # Compute the correlation matrix
46
+ correlation_matrix = data.corr()
47
+
48
+ # Create a figure and axis
49
+ plt.figure(figsize=(10, 8))
50
+
51
+ # Create the heatmap
52
+ heatmap = sns.heatmap(
53
+ correlation_matrix,
54
+ annot=True, # Show the correlation values
55
+ cmap="coolwarm", # Color scheme
56
+ vmin=-1,
57
+ vmax=1, # Set the range of values
58
+ center=0, # Center the colormap at 0
59
+ square=True, # Make each cell square-shaped
60
+ linewidths=0.5, # Add lines between cells
61
+ cbar_kws={"shrink": 0.8},
62
+ ) # Adjust the size of the colorbar
63
+
64
+ # Set the title
65
+ plt.title("Correlation Heatmap", fontsize=)
66
+
67
+ # Rotate the y-axis labels for better readability
68
+ plt.yticks(rotation=0)
69
+
70
+ # Show the plot
71
+ plt.tight_layout()
72
+ return gr.Plot(value=heatmap.get_figure())
tabs/tokens_votes_dist.py CHANGED
@@ -9,7 +9,6 @@ import plotly.express as px
9
  def get_based_tokens_distribution(market_id: str, all_markets: pd.DataFrame):
10
  """Function to paint the evolution of the probability of the outcomes based on the tokens distributions over time"""
11
  sns.set_style("darkgrid")
12
- sns.set_theme(palette="viridis")
13
  selected_market = all_markets.loc[all_markets["id"] == market_id]
14
  ax = selected_market.plot(
15
  x="sample_datetime",
@@ -19,9 +18,9 @@ def get_based_tokens_distribution(market_id: str, all_markets: pd.DataFrame):
19
  stacked=True,
20
  )
21
  # add overall title
22
- plt.title(
23
- "Outcomes probability over time based on tokens distributions", fontsize=8
24
- )
25
 
26
  # add axis titles
27
  plt.xlabel("Sample date")
@@ -32,13 +31,13 @@ def get_based_tokens_distribution(market_id: str, all_markets: pd.DataFrame):
32
  loc="upper left",
33
  labels=[first_outcome, second_outcome],
34
  )
 
35
  return gr.Plot(value=ax.figure)
36
 
37
 
38
  def get_based_votes_distribution(market_id: str, all_markets: pd.DataFrame):
39
  """Function to paint the evolution of the probability of the outcomes based on the votes distributions over time"""
40
  sns.set_style("darkgrid")
41
- sns.set_theme(palette="viridis")
42
  selected_market = all_markets.loc[all_markets["id"] == market_id]
43
  ax = selected_market.plot(
44
  x="sample_datetime",
@@ -47,9 +46,8 @@ def get_based_votes_distribution(market_id: str, all_markets: pd.DataFrame):
47
  rot=0,
48
  stacked=True,
49
  )
50
- plt.figure(figsize=(25, 10))
51
  # add overall title
52
- plt.title("Outcomes probability over time based on votes distributions", fontsize=8)
53
 
54
  # add axis titles
55
  plt.xlabel("Sample date")
@@ -60,4 +58,5 @@ def get_based_votes_distribution(market_id: str, all_markets: pd.DataFrame):
60
  loc="upper left",
61
  labels=[first_outcome, second_outcome],
62
  )
 
63
  return gr.Plot(value=ax.figure)
 
9
  def get_based_tokens_distribution(market_id: str, all_markets: pd.DataFrame):
10
  """Function to paint the evolution of the probability of the outcomes based on the tokens distributions over time"""
11
  sns.set_style("darkgrid")
 
12
  selected_market = all_markets.loc[all_markets["id"] == market_id]
13
  ax = selected_market.plot(
14
  x="sample_datetime",
 
18
  stacked=True,
19
  )
20
  # add overall title
21
+ # plt.title(
22
+ # "Outcomes probability over time based on tokens distributions", fontsize=8
23
+ # )
24
 
25
  # add axis titles
26
  plt.xlabel("Sample date")
 
31
  loc="upper left",
32
  labels=[first_outcome, second_outcome],
33
  )
34
+ ax.title = "Outcomes probability over time based on tokens distributions"
35
  return gr.Plot(value=ax.figure)
36
 
37
 
38
  def get_based_votes_distribution(market_id: str, all_markets: pd.DataFrame):
39
  """Function to paint the evolution of the probability of the outcomes based on the votes distributions over time"""
40
  sns.set_style("darkgrid")
 
41
  selected_market = all_markets.loc[all_markets["id"] == market_id]
42
  ax = selected_market.plot(
43
  x="sample_datetime",
 
46
  rot=0,
47
  stacked=True,
48
  )
 
49
  # add overall title
50
+ # plt.title("Outcomes probability over time based on votes distributions", fontsize=8)
51
 
52
  # add axis titles
53
  plt.xlabel("Sample date")
 
58
  loc="upper left",
59
  labels=[first_outcome, second_outcome],
60
  )
61
+ ax.title = "Outcomes probability over time based on votes distributions"
62
  return gr.Plot(value=ax.figure)