File size: 5,015 Bytes
c9eef1d
 
 
 
 
 
 
309bf92
 
11a5d2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9eef1d
0e4b180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46bb55
c9eef1d
 
 
 
 
 
773f144
 
 
 
a46bb55
 
 
 
17301f4
a46bb55
773f144
 
 
 
a46bb55
 
 
 
4fd6aee
17301f4
a46bb55
 
 
309bf92
 
 
 
 
 
 
79bdafd
1c548c3
1b041d1
79bdafd
 
 
 
a46bb55
 
 
79bdafd
 
 
 
 
11a5d2b
79bdafd
a46bb55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e22f08e
a46bb55
 
 
 
 
 
 
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
import pandas as pd
import gradio as gr
import matplotlib.pyplot as plt
import seaborn as sns
from seaborn import FacetGrid
import plotly.express as px

HEIGHT = 300
WIDTH = 600


def get_dist_gap_time_evolution(market_id: str, all_markets: pd.DataFrame):
    """Function to paint the evolution in time of the distance gap between the tokens and the price weighted distributions"""
    sns.set_style("darkgrid")
    selected_market = all_markets.loc[all_markets["id"] == market_id]
    selected_market["sample_datetime"] = selected_market["sample_datetime"].astype(str)
    selected_market.columns = selected_market.columns.astype(str)

    return gr.LinePlot(
        value=selected_market,
        x="sample_datetime",
        y="dist_gap_perc",
        y_title="Distribution gap in %",
        interactive=True,
        show_actions_button=True,
        tooltip=[
            "sample_datetime",
            "dist_gap_perc",
            "total_trades",
            "total_bet_amount",
        ],
        height=HEIGHT,
        width=WIDTH,
    )


def get_avg_gap_time_evolution(all_markets: pd.DataFrame):
    avg_dist_gap_perc = (
        all_markets.groupby("sample_datetime")["dist_gap_perc"].mean().reset_index()
    )
    avg_dist_gap_perc["sample_datetime"] = avg_dist_gap_perc["sample_datetime"].astype(
        str
    )
    avg_dist_gap_perc.rename(
        columns={"dist_gap_perc": "mean_dist_gap_perc"}, inplace=True
    )
    avg_dist_gap_perc.columns = avg_dist_gap_perc.columns.astype(str)
    return gr.LinePlot(
        value=avg_dist_gap_perc,
        x="sample_datetime",
        y="mean_dist_gap_perc",
        y_title="Mean dist gap percentage (%)",
        interactive=True,
        show_actions_button=True,
        tooltip=[
            "sample_datetime",
            "mean_dist_gap_perc",
        ],
        height=HEIGHT,
        width=WIDTH,
    )


def get_top_best_behaviour_markets(markets_data: pd.DataFrame):
    """Function to paint the top markets with the lowest metric of distribution gap"""
    sorted_data = markets_data.sort_values(by="dist_gap_perc", ascending=False)
    top_best_markets = sorted_data[["title", "sample_datetime", "dist_gap_perc"]].head(
        5
    )
    return gr.DataFrame(top_best_markets)


def get_distribution_plot(markets_data: pd.DataFrame):
    """Function to paint the density plot of the metric distribution gap percentage"""
    # A kernel density estimate (KDE) plot is a method for visualizing the distribution of
    # observations in a dataset, analogous to a histogram. KDE represents the data using a
    # continuous probability density curve in one or more dimensions.
    sns.set_theme(palette="viridis")
    plt.figure(figsize=(10, 5))

    plot = sns.kdeplot(markets_data, x="dist_gap_perc", fill=True)
    # TODO Add title and labels
    # Display the plot using gr.Plot
    return gr.Plot(value=plot.get_figure())


def get_kde_with_trades(markets_data: pd.DataFrame):
    """Function to paint the density plot of the metric in terms of the number of trades"""
    plot = sns.kdeplot(markets_data, x="dist_gap_perc", y="total_trades", fill=True)
    plt.ylabel("Total number of trades per market")
    return gr.Plot(value=plot.get_figure())


def get_kde_with_total_bet_amount(markets_data: pd.DataFrame):
    """Function to paint the density plot of the metric in terms of the total bet amount"""
    plot = sns.kdeplot(markets_data, x="dist_gap_perc", y="total_bet_amount", fill=True)
    plt.ylabel("Total bet amount per market")
    return gr.Plot(value=plot.get_figure())


def get_regplot_with_mean_trade_size(markets_data: pd.DataFrame):
    """Function to Plot data and a linear regression model fit between the metric and the mean trade size"""
    regplot = sns.regplot(markets_data, x="dist_gap_perc", y="mean_trade_size")
    plt.ylabel("Mean trade size in USD")
    return gr.Plot(value=regplot.get_figure())


def get_correlation_map(markets_data: pd.DataFrame):
    """Function to paint the correlation between different variables"""

    columns_of_interest = [
        "total_trades",
        "dist_gap_perc",
        "liquidityMeasure",
        "mean_trade_size",
        "total_bet_amount",
    ]
    data = markets_data[columns_of_interest]

    # Compute the correlation matrix
    correlation_matrix = data.corr()

    # Create the heatmap
    heatmap = sns.heatmap(
        correlation_matrix,
        annot=True,  # Show the correlation values
        cmap="coolwarm",  # Color scheme
        vmin=-1,
        vmax=1,  # Set the range of values
        center=0,  # Center the colormap at 0
        square=True,  # Make each cell square-shaped
        linewidths=0.5,  # Add lines between cells
        cbar_kws={"shrink": 0.8},
    )  # Adjust the size of the colorbar

    # Set the title
    plt.title("Correlation Heatmap")

    # Rotate the y-axis labels for better readability
    plt.yticks(rotation=0)

    # Show the plot
    plt.tight_layout()
    return gr.Plot(value=heatmap.get_figure())