File size: 4,630 Bytes
6e7ee88
 
 
3c32096
3af04c7
 
 
 
dcdc7b7
3af04c7
 
 
 
 
 
 
 
 
 
 
 
9823ef6
 
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
from src.data.utils import *
from src.features.utils import *
from src.visualization.visualize import *
from app_utils import *


def main():

    st.title("Time Series Autocorrelation Demo")

    st.write("""
    Autocorrelation is the correlation of a single time series with a lag copy of itself.\n
    In the discrete case autocorrelation is also referred to as serial correlation.\n
    In general autocorrelation usually refers to the lag-one autocorrelation.
    """)

    st.title("Data")

    sample_data_selected = st.selectbox(
        'Select sample data:', data_set_options)

    data, graph_data = import_sample_data(
        sample_data_selected, data_set_options)

    with st.expander("Line Plot:"):
        time_series_line_plot(data)

    with st.expander("Box Plot:"):
        time_series_box_plot(graph_data)

    with st.expander("Dist Plot (histogram and violin plot):"):
        time_series_violin_and_box_plot(data)

    st.title("Time Series Autocorrelation")

    data_array = data.values.squeeze()

    data_series = pd.Series(data_array).dropna()

    st.header("Auto-Correlation Function (ACF)")

    st.write("""
    ACF shows the entire autocorrelation function for different lags (not just lag-one).\n
    Given the autocorrelation is a function of the lag any significant non-zero correlation imply the series can be forecast from the past.\n
    Lag 0 autocorrelation will always be 1 since the values (y-axis) are the same at the same time (x-axis) for the same time series.
    """)

    acf_type = st.radio(
        'Default ACF:', ('True', 'False'), key='acf_type')

    default_acf_selected = acf_type == 'True'

    if default_acf_selected:
        acf_array = create_standard_acf_array(data_series)

    if not default_acf_selected:
        [confidence_level,
         acf_nlags_selected,
         acf_fft_selected,
         acf_adjust_selected] = acf_settings()

        alpha_selected = (100-confidence_level)/100

        acf_array = acf(data_series,
                        alpha=alpha_selected,
                        nlags=acf_nlags_selected,
                        fft=acf_fft_selected,
                        adjusted=acf_adjust_selected)

    corr_presentation(acf_array)

    st.subheader("ACF Plot")

    if default_acf_selected:

        st.write('Given a confidence inverval of 95% (significance level of 0.05) there is a 5% chance that if true autocorrelation is zero, it will fall outside blue band.')

        create_standard_corr_plot(data_series, plot_pacf=False)

    if not default_acf_selected:

        st.write(
            f'Given a confidence inverval of {confidence_level}% (significance level of {alpha_selected}) there is a {alpha_selected*100}% chance that if true autocorrelation is zero, it will fall outside blue band.')

        create_acf_plot(data_series,
                        alpha_selected,
                        acf_nlags_selected,
                        acf_fft_selected)

    st.header("Partial Auto-Correlation Function (PACF)")

    st.write("Unlike ACF, PACF controls for other lags.")
    st.write(
        "PACF represents how significant adding lag n is when you already have lag n-1.")

    pacf_type = st.radio(
        'Default PACF:', ('True', 'False'), key='pacf_type')

    default_pacf_selected = pacf_type == 'True'

    if default_pacf_selected:
        pacf_array = create_standard_pacf_array(data_series)

    if not default_pacf_selected:
        [confidence_level,
         pacf_nlags_selected,
         pacf_calculation_method] = pacf_settings()

        alpha_selected = (100-confidence_level)/100

        pacf_array = pacf(data_series,
                          alpha=alpha_selected,
                          nlags=pacf_nlags_selected,
                          method=pacf_calculation_method)

    corr_presentation(pacf_array)

    st.subheader("PACF Plot")

    if default_pacf_selected:
        st.write('Given a confidence inverval of 95% (significance level of 0.05) there is a 5% chance that if true autocorrelation is zero, it will fall outside blue band.')
        create_standard_corr_plot(data_series, plot_pacf=True)

    if not default_pacf_selected:
        st.write(
            f'Given a confidence inverval of {confidence_level}% (significance level of {alpha_selected}) there is a {alpha_selected*100}% chance that if true autocorrelation is zero, it will fall outside blue band.')
        create_pacf_plot(data_series,
                         alpha_selected,
                         pacf_nlags_selected,
                         pacf_calculation_method)


if __name__ == "__main__":
    main()