File size: 5,563 Bytes
3af04c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9823ef6
 
 
 
 
 
 
3af04c7
 
 
 
 
 
 
 
 
9823ef6
 
3af04c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import streamlit as st
import plotly.express as px
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.graphics.tsaplots import acf, pacf
import numpy as np
import plotly.graph_objects as go


def streamlit_2columns_metrics_df_shape(df: pd.DataFrame):
    (
        column1name,
        column2name,
    ) = st.columns(2)

    with column1name:
        st.metric(
            label="Rows",
            value=df.shape[0],
            delta=None,
            delta_color="normal",
        )

    with column2name:
        st.metric(
            label="Columns",
            value=df.shape[1],
            delta=None,
            delta_color="normal",
        )


def show_inputted_dataframe(data):
    with st.expander("Input Dataframe (X and y)"):
        st.dataframe(data)
        streamlit_2columns_metrics_df_shape(data)


def time_series_line_plot(data):
    fig = px.line(
        data
    )
    st.plotly_chart(fig, use_container_width=True)


def time_series_scatter_plot(data):
    fig = px.scatter(data, trendline="ols")
    st.plotly_chart(fig, use_container_width=True)


def time_series_box_plot(data):
    fig = px.box(data, hover_data=['Date'], points="all")
    st.plotly_chart(fig, use_container_width=True)


def time_series_violin_and_box_plot(graph_data):
    fig = px.histogram(graph_data,
                       marginal="violin")
    st.plotly_chart(fig, use_container_width=True)


def streamlit_chart_setting_height_width(
    title: str,
    default_widthvalue: int,
    default_heightvalue: int,
    widthkey: str,
    heightkey: str,


):
    with st.expander(title):

        lbarx_col, lbary_col = st.columns(2)

        with lbarx_col:
            width_size = st.number_input(
                label="Width in inches:",
                value=default_widthvalue,
                key=widthkey,
            )

        with lbary_col:
            height_size = st.number_input(
                label="Height in inches:",
                value=default_heightvalue,
                key=heightkey,
            )
    return width_size, height_size


# zero 0-lag autocorrelation = True
# fft


def streamlit_autocorrelation_plot_settings():
    with st.expander('Autocorrelation Plot Settings:'):
        lag_col, alpha_col = st.columns(2)

        with lag_col:
            lags_selected = st.number_input(
                label="Lags:",
                value=15)

        with alpha_col:
            alpha_selected = st.number_input(
                label="Alpha:",
                value=0.05)

        zero_include_selected = st.radio(
            label="Include the 0-lag autocorrelation:",
            options=('True', 'False'))

        zero_include_selected = zero_include_selected == 'True'

        return [lags_selected,
                alpha_selected,
                zero_include_selected]


def streamlit_acf_plot_settings():
    fft_compute_selected = st.radio(
        label="Compute the ACF via FFT:",
        options=('False', 'True'))

    fft_compute_selected = fft_compute_selected == 'True'

    return fft_compute_selected


def plotly_corr(corr_array, upper_y, lower_y):
    fig = go.Figure()
    [fig.add_scatter(x=(x, x), y=(0, corr_array[0][x]), mode='lines', line_color='#3f3f3f')
     for x in range(len(corr_array[0]))]
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=corr_array[0], mode='markers', marker_color='#1f77b4',
                    marker_size=12)
    fig.add_scatter(x=np.arange(
        len(corr_array[0])), y=upper_y, mode='lines', line_color='rgba(255,255,255,0)')
    fig.add_scatter(x=np.arange(len(corr_array[0])), y=lower_y, mode='lines', fillcolor='rgba(32, 146, 230,0.3)',
                    fill='tonexty', line_color='rgba(255,255,255,0)')
    fig.update_traces(showlegend=False)
    fig.update_yaxes(zerolinecolor='#000000')
    return fig


def create_standard_corr_plot(series, plot_pacf=False):
    corr_array = pacf(series.dropna(), alpha=0.05) if plot_pacf else acf(
        series.dropna(), alpha=0.05)
    lower_y = corr_array[1][:, 0] - corr_array[0]
    upper_y = corr_array[1][:, 1] - corr_array[0]

    fig = plotly_corr(corr_array, upper_y, lower_y)

    title = 'Partial Autocorrelation' if plot_pacf else 'Autocorrelation'
    fig.update_layout(title=title)
    st.plotly_chart(fig, use_container_width=True)


def create_acf_plot(data_series,
                    alpha_selected,
                    acf_nlags_selected_plot,
                    acf_fft_selected_plot):

    corr_array = acf(data_series,
                     alpha=alpha_selected,
                     nlags=acf_nlags_selected_plot,
                     fft=acf_fft_selected_plot)

    lower = corr_array[1][:, 0] - corr_array[0]
    upper = corr_array[1][:, 1] - corr_array[0]
    fig = plotly_corr(corr_array, upper, lower)
    title = 'Autocorrelation'
    fig.update_layout(title=title)
    st.plotly_chart(fig, use_container_width=True)


def create_pacf_plot(data_series,
                     alpha_selected,
                     acf_nlags_selected,
                     pacf_calculation_method):

    corr_array = pacf(data_series,
                      alpha=alpha_selected,
                      nlags=acf_nlags_selected,
                      method=pacf_calculation_method)
    lower = corr_array[1][:, 0] - corr_array[0]
    upper = corr_array[1][:, 1] - corr_array[0]
    fig = plotly_corr(corr_array, upper, lower)
    title = 'Partial Autocorrelation'
    fig.update_layout(title=title)
    st.plotly_chart(fig, use_container_width=True)