File size: 6,641 Bytes
08a409c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 streamlit as st
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px

import plotly.graph_objects as go
from plotly.subplots import make_subplots

st.set_page_config(layout="wide")


@st.cache_data
def create_datafram(start_date, num_prices, base_price, price_ratio):

    # Generate timestamps (assuming daily data)
    dates = pd.date_range(start=start_date, periods=num_prices)

    # Generate random prices 
    prices = [base_price]  # Initial value
    for _ in range(num_prices - 1):
        new_value = prices[-1] + (np.random.randn()* price_ratio)  # AR(1) process
        prices.append(new_value)

    # Create the DataFrame
    data = {
        'timestamp': dates,
        'price': prices
    }

    return pd.DataFrame(data)





st.header('Create Price Data', divider=True)

col1, col2, col3, col4 = st.columns(4)

with col1:
    date = st.date_input("Date Start", datetime.date(2019, 7, 6))
    
with col2:
    steps = st.number_input("Time steps (days)", value=365)
    
with col3:
    price = st.number_input("Base Price", value = 100)

with col4:
    ratio = st.number_input("Fluctuation factor [0 - 1]", value=0.9)
 
 
if 'data' not in st.session_state:
    st.session_state['data'] = create_datafram(date, steps, price, ratio)


if st.button('Create Time-series data'):
    st.session_state.data = create_datafram(date, steps, price, ratio)



st.header('Show Price Data', divider=True)


col5, col6 = st.columns((1,2))

with col5:
        
    st.dataframe(st.session_state.data, use_container_width=True)
            
with col6:
    
    fig = px.line(st.session_state.data, x="timestamp", y="price", title='Time Series Price Data')
    
    st.plotly_chart(fig, use_container_width=True)
    #st.line_chart(st.session_state.data, x="timestamp", y="price", use_container_width=True)
    



def cal_ipa(df_in: pd.DataFrame, window_size: int = 120, std_adjust : float = 2.0, ipa_col: str='IPA', adj_price: bool= False):

    if adj_price:
        
        df_in['price'] = df_in['price'] - (1 - df_in['price'].rolling(window=window_size).std())
        
    df_in[ipa_col] = abs((df_in['price'] - df_in['price'].rolling(window=window_size).mean()) / (df_in['price'].rolling(window=window_size).std()*std_adjust))
    
    return df_in

def cal_anomaly(df_in):
    
    return ...

    
st.header('Anomaly IPA Analysis', divider=True)

a_col1, a_col2, a_col3, a_col4 = st.columns(4)

with a_col1:
    window_quater = st.number_input("Quater window (days)", value=120)

with a_col2:
    window_month = st.number_input("Monthly window (days)", value=30)
    
with a_col3:
    std_adj = st.number_input("Standard Deviation Adjustment", value=2.5)
    
with a_col4:
    adj_price = st.toggle("Adjust Price")


b_col1, b_col2 = st.columns(2)

with b_col1:
    gamma = st.number_input("Gamma adjustment", value=0.8)   
    


if 'ipa_df' not in st.session_state:
    st.session_state['ipa_df'] = st.session_state.data.copy()
else:
    st.session_state.ipa_df = st.session_state.data.copy()
    
if not adj_price:
    st.session_state.ipa_df = st.session_state.data.copy()

st.session_state.ipa_df = cal_ipa(st.session_state.ipa_df, window_size=window_month, std_adjust=std_adj, ipa_col='IPA_m', adj_price=adj_price)
st.session_state.ipa_df = cal_ipa(st.session_state.ipa_df, window_size=window_quater, std_adjust=std_adj, ipa_col='IPA_q', adj_price=adj_price)

w_adj = np.random.uniform(low=0.9, high=1.0, size=steps)

st.session_state.ipa_df['IPA_q'] = st.session_state.ipa_df['IPA_q'] * w_adj
st.session_state.ipa_df['IPA_m'] = st.session_state.ipa_df['IPA_m'] * w_adj

st.session_state.ipa_df['ipa_adj'] = gamma* st.session_state.ipa_df['IPA_m'] + (1-gamma)*st.session_state.ipa_df['IPA_q']

st.session_state.ipa_df['Warning'] = 0
#df.loc[df['IPA'] < 0.5, 'Warning'] = 'Normal'
st.session_state.ipa_df.loc[st.session_state.ipa_df['ipa_adj'] >= 0.5, 'Warning'] = 1
st.session_state.ipa_df.loc[st.session_state.ipa_df['ipa_adj'] >= 1.0, 'Warning'] = 2


c_col1, c_col2 = st.columns((1,2))

with c_col1:
    st.dataframe(st.session_state.ipa_df)

with c_col2:
    # fig, ax1 = plt.subplots(figsize=(14, 5), dpi=160)
    # ax2 = ax1.twinx()

    # ax1.plot(st.session_state.ipa_df['price'])
    # #ax1.set_ylim(90, 150)
    # ax1.set_xlim(120, 365)

    # #ax2.plot(df['IPA'], c='red')
    # ax2.fill_between(st.session_state.ipa_df.index, st.session_state.ipa_df['ipa_adj'], color='blue', alpha=0.1)
    # ax2.fill_between(st.session_state.ipa_df.index, st.session_state.ipa_df['Warning'], color='orange', alpha=0.3)
    # ax2.set_xlim(120, 365)
    # ax2.set_ylim(0, 2)

    # st.pyplot(fig)
    
# =================================================================

    fig = go.Figure()

    fig.add_trace(go.Scatter(
        x=st.session_state.ipa_df['timestamp'], 
        y=st.session_state.ipa_df['price'],
        hoverinfo='x+y',
        mode='lines',
        line=dict(width=1.0, color='rgb(100, 100, 100)'),
        name='Price'
    ))

    fig.add_trace(
        go.Scatter(
            x=st.session_state.ipa_df['timestamp'], 
            y=st.session_state.ipa_df['ipa_adj'],
            hoverinfo='x+y',
            mode='lines',
            line=dict(width=1.0, color='rgb(0, 230, 230)'),
            stackgroup='one',
            name='IPA Value',
            yaxis='y2'
            )
        ,)

    fig.add_trace(
        go.Scatter(
            x=st.session_state.ipa_df['timestamp'], 
            y=st.session_state.ipa_df['Warning'],
            hoverinfo='x+y',
            mode='lines',
            line=dict(width=1.0, color='rgb(230, 172, 0)'),
            stackgroup='one',
            name='Anomaly Index',
            yaxis='y3'
            )
        ,)


    fig.update_layout(
        
        legend=dict(orientation="h"),
        
        yaxis=dict(
            title=dict(text="Price"),
            side="left",
            range=(min(st.session_state.ipa_df['price']*0.99), max(st.session_state.ipa_df['price']*1.01)),
        ),
        yaxis2=dict(
            title='',
            side="right",
            range=(0, 2),
            overlaying="y",
            showgrid=False,
        ),
        yaxis3=dict(
            title='Anomaly Index',
            side="right",
            range=(0, 2),
            overlaying="y",
            showgrid=False,
        ),
    )

    #fig.update_yaxes(title_text="<b>primary</b> yaxis title")
    #fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)

    fig.update_layout(
        title_text="IPA Anomaly detection chart",
    )
    st.plotly_chart(fig)