Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,9 @@ import streamlit as st
|
|
| 9 |
from statsmodels.tsa.seasonal import STL
|
| 10 |
from statsmodels.tsa.stattools import acf, pacf
|
| 11 |
|
| 12 |
-
# Helper function to fetch stock data
|
| 13 |
def fetch_stock_data(ticker: str, start_date: str, end_date: str) -> pd.DataFrame:
|
| 14 |
-
"""Fetch stock data from Yahoo Finance."""
|
| 15 |
data = yf.download(ticker, start=start_date, end=end_date)
|
| 16 |
return data.dropna()
|
| 17 |
|
|
@@ -31,15 +31,9 @@ def plot_candlestick(data: pd.DataFrame, ticker: str) -> go.Figure:
|
|
| 31 |
height=800)
|
| 32 |
return fig
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
resampled_data = data["Adj Close"].resample(freq).last()
|
| 36 |
-
return resampled_data.pct_change().dropna()
|
| 37 |
-
|
| 38 |
-
def plot_returns_distribution(data: pd.DataFrame, ticker: str, return_periods: list) -> go.Figure:
|
| 39 |
"""Plot returns distribution."""
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
returns = {period: data['Close'].resample(freq).last().pct_change().dropna()
|
| 43 |
for period, freq in return_periods.items()}
|
| 44 |
|
| 45 |
fig = make_subplots(rows=3, cols=2, subplot_titles=[f'{ticker} {period} Down Days' for period in returns.keys()] +
|
|
@@ -175,18 +169,49 @@ def plot_rolling_statistics(data: pd.DataFrame, ticker: str, window_size: int, q
|
|
| 175 |
return fig
|
| 176 |
|
| 177 |
# Streamlit app
|
| 178 |
-
st.set_page_config(page_title="Stock Price Descriptive Statistics", layout="wide")
|
| 179 |
-
st.title('Stock Price Descriptive Statistics')
|
| 180 |
-
|
| 181 |
-
#
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
# Fetch data
|
| 192 |
if 'data' not in st.session_state or st.sidebar.button('Fetch Data'):
|
|
@@ -194,299 +219,242 @@ if 'data' not in st.session_state or st.sidebar.button('Fetch Data'):
|
|
| 194 |
|
| 195 |
data = st.session_state.data
|
| 196 |
|
| 197 |
-
# Additional input parameters based on selected method
|
| 198 |
-
if selected == "Returns Distribution":
|
| 199 |
-
return_periods = {
|
| 200 |
-
"Daily": 'D',
|
| 201 |
-
"Weekly": 'W',
|
| 202 |
-
"Monthly": 'M'
|
| 203 |
-
}
|
| 204 |
-
return_periods = {key: st.sidebar.text_input(f'{key} Resampling Frequency', value) for key, value in return_periods.items()}
|
| 205 |
-
|
| 206 |
-
elif selected == "Time Series Analysis":
|
| 207 |
-
period = st.sidebar.number_input('STL Decomposition Period', min_value=1, value=252)
|
| 208 |
-
|
| 209 |
-
elif selected == "ACF and PACF":
|
| 210 |
-
nlags = st.sidebar.number_input('Number of Lags', min_value=1, value=100)
|
| 211 |
-
|
| 212 |
-
elif selected == "Rolling Statistics":
|
| 213 |
-
window_size = st.sidebar.number_input('Rolling Window Size', min_value=1, value=20)
|
| 214 |
-
quantile_value = st.sidebar.number_input('Quantile Value', min_value=0.0, max_value=1.0, value=0.5)
|
| 215 |
-
|
| 216 |
# Display results based on the selected method
|
| 217 |
if selected == "Candlestick Chart":
|
| 218 |
-
st.
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
**Results:**
|
| 243 |
-
The chart shows the candlestick representation of the stock's price movements over time, helping to identify trends, reversals, and patterns such as dojis, hammers, and engulfing patterns.
|
| 244 |
-
""")
|
| 245 |
fig = plot_candlestick(data, ticker)
|
| 246 |
st.plotly_chart(fig)
|
| 247 |
|
| 248 |
elif selected == "Returns Distribution":
|
| 249 |
-
st.
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
**Results:**
|
| 287 |
-
The chart shows the distribution of returns with histograms for daily, weekly, and monthly periods, helping to identify the stock's volatility and return patterns. This analysis can be useful for assessing the risk and performance of the stock.
|
| 288 |
-
""")
|
| 289 |
fig = plot_returns_distribution(data, ticker, return_periods)
|
| 290 |
st.plotly_chart(fig)
|
| 291 |
|
| 292 |
elif selected == "Probability Plots":
|
| 293 |
-
st.
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
1. Enter the stock ticker, start date, and end date.
|
| 324 |
-
2. Click 'Fetch Data' to load the stock data.
|
| 325 |
-
3. The chart will display the probability plots.
|
| 326 |
-
|
| 327 |
-
**Results:**
|
| 328 |
-
The chart shows the probability plots of the stock's daily returns. These plots help determine if the daily returns follow a normal or t-distribution, which can be useful for risk management and modeling.
|
| 329 |
-
""")
|
| 330 |
fig = plot_probability_plots(data, ticker)
|
| 331 |
st.plotly_chart(fig)
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
elif selected == "Time Series Analysis":
|
| 337 |
-
st.
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
4. The chart will display the time series analysis.
|
| 364 |
-
|
| 365 |
-
**Results:**
|
| 366 |
-
The chart shows the original, trend, seasonal, and residual components of the stock price. This helps in understanding the underlying patterns and making informed decisions based on the decomposed components.
|
| 367 |
-
""")
|
| 368 |
fig = plot_time_series_analysis(data, ticker, period)
|
| 369 |
st.plotly_chart(fig)
|
| 370 |
|
| 371 |
-
|
| 372 |
elif selected == "ACF and PACF":
|
| 373 |
-
st.
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
**Results:**
|
| 403 |
-
The chart shows the ACF and PACF of the stock's daily returns. These plots help in identifying significant lags and understanding the autocorrelation structure of the time series.
|
| 404 |
-
""")
|
| 405 |
fig = plot_acf_pacf(data, ticker, nlags)
|
| 406 |
st.plotly_chart(fig)
|
| 407 |
|
| 408 |
-
|
| 409 |
elif selected == "Boxplots":
|
| 410 |
-
st.
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
**Results:**
|
| 429 |
-
The chart shows the boxplots of the stock's returns based on different time periods, helping to identify temporal patterns and trends.
|
| 430 |
-
""")
|
| 431 |
fig = plot_boxplots(data, ticker)
|
| 432 |
st.plotly_chart(fig)
|
| 433 |
|
| 434 |
-
|
| 435 |
elif selected == "Rolling Statistics":
|
| 436 |
-
st.
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
The chart shows the rolling kurtosis, skewness, quantile, and autocorrelation of the stock's price.
|
| 478 |
-
""")
|
| 479 |
fig = plot_rolling_statistics(data, ticker, window_size, quantile_value)
|
| 480 |
st.plotly_chart(fig)
|
| 481 |
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
hide_streamlit_style = """
|
| 487 |
<style>
|
| 488 |
#MainMenu {visibility: hidden;}
|
| 489 |
footer {visibility: hidden;}
|
| 490 |
</style>
|
| 491 |
"""
|
| 492 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
|
|
|
| 9 |
from statsmodels.tsa.seasonal import STL
|
| 10 |
from statsmodels.tsa.stattools import acf, pacf
|
| 11 |
|
| 12 |
+
# Helper function to fetch stock or crypto data
|
| 13 |
def fetch_stock_data(ticker: str, start_date: str, end_date: str) -> pd.DataFrame:
|
| 14 |
+
"""Fetch stock or crypto data from Yahoo Finance."""
|
| 15 |
data = yf.download(ticker, start=start_date, end=end_date)
|
| 16 |
return data.dropna()
|
| 17 |
|
|
|
|
| 31 |
height=800)
|
| 32 |
return fig
|
| 33 |
|
| 34 |
+
def plot_returns_distribution(data: pd.DataFrame, ticker: str, return_periods: dict) -> go.Figure:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""Plot returns distribution."""
|
| 36 |
+
returns = {period: data['Close'].resample(freq).last().pct_change().dropna()
|
|
|
|
|
|
|
| 37 |
for period, freq in return_periods.items()}
|
| 38 |
|
| 39 |
fig = make_subplots(rows=3, cols=2, subplot_titles=[f'{ticker} {period} Down Days' for period in returns.keys()] +
|
|
|
|
| 169 |
return fig
|
| 170 |
|
| 171 |
# Streamlit app
|
| 172 |
+
st.set_page_config(page_title="Stock and Crypto Price Descriptive Statistics", layout="wide")
|
| 173 |
+
st.title('Stock and Crypto Price Descriptive Statistics')
|
| 174 |
+
|
| 175 |
+
# Expander for Ticker and Date Input
|
| 176 |
+
with st.expander("Ticker and Date Input", expanded=True):
|
| 177 |
+
ticker = st.text_input('Enter Stock or Crypto Ticker (e.g., AAPL or BTC-USD)', 'ASML.AS', help="Enter the symbol for the stock or cryptocurrency you want to analyze.")
|
| 178 |
+
start_date = st.date_input('Start Date', pd.to_datetime('2000-01-01'), help="Select the start date for data fetching.")
|
| 179 |
+
end_date = st.date_input('End Date', pd.to_datetime(pd.Timestamp.now().date() + pd.Timedelta(days=1)), help="Select the end date for data fetching.")
|
| 180 |
+
|
| 181 |
+
# Expander for Method Selection
|
| 182 |
+
with st.expander("Method Selection", expanded=True):
|
| 183 |
+
selected = st.radio("Select Method", ["Candlestick Chart", "Returns Distribution", "Probability Plots",
|
| 184 |
+
"Time Series Analysis", "ACF and PACF", "Boxplots", "Rolling Statistics"])
|
| 185 |
+
|
| 186 |
+
# Expander for Method-Specific Parameters
|
| 187 |
+
with st.expander("Parameters", expanded=True):
|
| 188 |
+
if selected == "Returns Distribution":
|
| 189 |
+
return_periods = {
|
| 190 |
+
"Daily": st.text_input('Daily Resampling Frequency', 'D', help="Set the frequency for daily returns calculation."),
|
| 191 |
+
"Weekly": st.text_input('Weekly Resampling Frequency', 'W', help="Set the frequency for weekly returns calculation."),
|
| 192 |
+
"Monthly": st.text_input('Monthly Resampling Frequency', 'M', help="Set the frequency for monthly returns calculation."),
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
elif selected == "Time Series Analysis":
|
| 196 |
+
period = st.number_input('STL Decomposition Period', min_value=1, value=252, help="Enter the period for STL decomposition (e.g., 252 for yearly seasonality).")
|
| 197 |
+
|
| 198 |
+
elif selected == "ACF and PACF":
|
| 199 |
+
nlags = st.number_input('Number of Lags', min_value=1, value=100, help="Enter the number of lags for ACF and PACF plots.")
|
| 200 |
+
|
| 201 |
+
elif selected == "Rolling Statistics":
|
| 202 |
+
window_size = st.number_input('Rolling Window Size', min_value=1, value=20, help="Enter the window size for rolling calculations.")
|
| 203 |
+
quantile_value = st.number_input('Quantile Value', min_value=0.0, max_value=1.0, value=0.5, help="Set the quantile value for rolling quantile calculation.")
|
| 204 |
+
|
| 205 |
+
# Sidebar for "How to Use" instructions
|
| 206 |
+
with st.sidebar.expander("How to Use", expanded=False):
|
| 207 |
+
st.write("""
|
| 208 |
+
### How to Use:
|
| 209 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 210 |
+
2. Choose the desired analysis method from the available options.
|
| 211 |
+
3. Adjust the parameters specific to the selected method if needed.
|
| 212 |
+
4. Click 'Fetch Data' to load the data and run the analysis.
|
| 213 |
+
5. The results and visualizations will appear in the main section of the app.
|
| 214 |
+
""")
|
| 215 |
|
| 216 |
# Fetch data
|
| 217 |
if 'data' not in st.session_state or st.sidebar.button('Fetch Data'):
|
|
|
|
| 219 |
|
| 220 |
data = st.session_state.data
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
# Display results based on the selected method
|
| 223 |
if selected == "Candlestick Chart":
|
| 224 |
+
with st.expander("Method Description", expanded=False):
|
| 225 |
+
st.markdown("""
|
| 226 |
+
### Candlestick Chart
|
| 227 |
+
The candlestick chart visually represents the open, high, low, and close prices of a stock or crypto for each day. This type of chart helps identify patterns in the price movements and provides insights into market sentiment.
|
| 228 |
+
**Components of a Candlestick:**
|
| 229 |
+
- **Open**: The price at which a stock or crypto started trading at the beginning of the time period.
|
| 230 |
+
- **High**: The highest price reached during the time period.
|
| 231 |
+
- **Low**: The lowest price reached during the time period.
|
| 232 |
+
- **Close**: The price at which a stock or crypto stopped trading at the end of the time period.
|
| 233 |
+
**Candlestick Structure:**
|
| 234 |
+
- **Body**: The area between the open and close prices.
|
| 235 |
+
- If the close is higher than the open, the body is typically green or white, indicating a bullish sentiment.
|
| 236 |
+
- If the close is lower than the open, the body is typically red or black, indicating a bearish sentiment.
|
| 237 |
+
- **Wicks (Shadows)**: The lines extending above and below the body.
|
| 238 |
+
- The upper wick represents the range from the high to the close (or open, if it's higher).
|
| 239 |
+
- The lower wick represents the range from the low to the open (or close, if it's lower).
|
| 240 |
+
**How to use:**
|
| 241 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 242 |
+
2. Click 'Fetch Data' to load the data.
|
| 243 |
+
3. The chart will display the candlestick chart for the selected period.
|
| 244 |
+
**Results:**
|
| 245 |
+
The chart shows the candlestick representation of the stock or crypto's price movements over time, helping to identify trends, reversals, and patterns such as dojis, hammers, and engulfing patterns.
|
| 246 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
fig = plot_candlestick(data, ticker)
|
| 248 |
st.plotly_chart(fig)
|
| 249 |
|
| 250 |
elif selected == "Returns Distribution":
|
| 251 |
+
with st.expander("Method Description", expanded=False):
|
| 252 |
+
st.markdown("""
|
| 253 |
+
### Returns Distribution
|
| 254 |
+
This analysis shows the distribution of daily, weekly, and monthly returns. It helps understand the stock's or crypto's return characteristics by visualizing the frequency and magnitude of price changes over different time periods.
|
| 255 |
+
**Components:**
|
| 256 |
+
- **Daily Returns**: Calculated as the percentage change in the closing price from one day to the next.
|
| 257 |
+
- **Weekly Returns**: Calculated as the percentage change in the closing price from the last trading day of one week to the last trading day of the next week.
|
| 258 |
+
- **Monthly Returns**: Calculated as the percentage change in the closing price from the last trading day of one month to the last trading day of the next month.
|
| 259 |
+
""")
|
| 260 |
+
st.latex(r'''
|
| 261 |
+
\text{Daily Return} (R_d) = \frac{P_{\text{close}, t} - P_{\text{close}, t-1}}{P_{\text{close}, t-1}}
|
| 262 |
+
''')
|
| 263 |
+
|
| 264 |
+
st.latex(r'''
|
| 265 |
+
\text{Weekly Return} (R_w) = \frac{P_{\text{close}, \text{week}_t} - P_{\text{close}, \text{week}_{t-1}}}{P_{\text{close}, \text{week}_{t-1}}}
|
| 266 |
+
''')
|
| 267 |
+
|
| 268 |
+
st.latex(r'''
|
| 269 |
+
\text{Monthly Return} (R_m) = \frac{P_{\text{close}, \text{month}_t} - P_{\text{close}, \text{month}_{t-1}}}{P_{\text{close}, \text{month}_{t-1}}}
|
| 270 |
+
''')
|
| 271 |
+
|
| 272 |
+
st.markdown("""
|
| 273 |
+
where:
|
| 274 |
+
- \( P_{\text{close}, t} \) is the closing price at time \( t \)
|
| 275 |
+
- \( P_{\text{close}, t-1} \) is the closing price at time \( t-1 \)
|
| 276 |
+
- \( P_{\text{close}, \text{week}_t} \) is the closing price at the end of the current week
|
| 277 |
+
- \( P_{\text{close}, \text{week}_{t-1}} \) is the closing price at the end of the previous week
|
| 278 |
+
- \( P_{\text{close}, \text{month}_t} \) is the closing price at the end of the current month
|
| 279 |
+
- \( P_{\text{close}, \text{month}_{t-1}} \) is the closing price at the end of the previous month
|
| 280 |
+
**How to use:**
|
| 281 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 282 |
+
2. Click 'Fetch Data' to load the data.
|
| 283 |
+
3. The chart will display the distribution of returns.
|
| 284 |
+
**Results:**
|
| 285 |
+
The chart shows the distribution of returns with histograms for daily, weekly, and monthly periods, helping to identify the stock's or crypto's volatility and return patterns. This analysis can be useful for assessing the risk and performance of the stock or crypto.
|
| 286 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
fig = plot_returns_distribution(data, ticker, return_periods)
|
| 288 |
st.plotly_chart(fig)
|
| 289 |
|
| 290 |
elif selected == "Probability Plots":
|
| 291 |
+
with st.expander("Method Description", expanded=False):
|
| 292 |
+
st.markdown("""
|
| 293 |
+
### Probability Plots
|
| 294 |
+
This analysis shows the normal and t-distribution probability plots of daily returns. It helps check if the returns follow a specific distribution.
|
| 295 |
+
**Components:**
|
| 296 |
+
- **Normal Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a normal distribution.
|
| 297 |
+
- **T-Distribution Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a t-distribution with estimated parameters.
|
| 298 |
+
**Formulas:**
|
| 299 |
+
- **Daily Return** (\( R_d \)):
|
| 300 |
+
""")
|
| 301 |
+
st.latex(r'''
|
| 302 |
+
R_d = \frac{P_{close, t} - P_{close, t-1}}{P_{close, t-1}}
|
| 303 |
+
''')
|
| 304 |
+
st.markdown("""
|
| 305 |
+
where \( P_{close, t} \) is the closing price at time \( t \) and \( P_{close, t-1} \) is the closing price at time \( t-1 \).
|
| 306 |
+
- **Normal Probability Plot**: Compares the ordered sample values of daily returns to the expected values if the data followed a normal distribution.
|
| 307 |
+
- **T-Distribution Probability Plot**: Compares the ordered sample values of daily returns to the expected values if the data followed a t-distribution. The t-distribution is parameterized by degrees of freedom (\( \nu \)), location parameter (\( \mu \)), and scale parameter (\( \sigma \)):
|
| 308 |
+
""")
|
| 309 |
+
st.latex(r'''
|
| 310 |
+
t(x, \nu, \mu, \sigma) = \frac{\Gamma\left(\frac{\nu + 1}{2}\right)}{\sqrt{\nu \pi} \Gamma\left(\frac{\nu}{2}\right)} \left(1 + \frac{(x - \mu)^2}{\nu \sigma^2}\right)^{-\frac{\nu + 1}{2}}
|
| 311 |
+
''')
|
| 312 |
+
st.markdown("""
|
| 313 |
+
where \( \Gamma \) is the gamma function, \( \nu \) is the degrees of freedom, \( \mu \) is the location parameter, and \( \sigma \) is the scale parameter.
|
| 314 |
+
**How to use:**
|
| 315 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 316 |
+
2. Click 'Fetch Data' to load the data.
|
| 317 |
+
3. The chart will display the probability plots.
|
| 318 |
+
**Results:**
|
| 319 |
+
The chart shows the probability plots of the stock or crypto's daily returns. These plots help determine if the daily returns follow a normal or t-distribution, which can be useful for risk management and modeling.
|
| 320 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
fig = plot_probability_plots(data, ticker)
|
| 322 |
st.plotly_chart(fig)
|
| 323 |
|
|
|
|
|
|
|
|
|
|
| 324 |
elif selected == "Time Series Analysis":
|
| 325 |
+
with st.expander("Method Description", expanded=False):
|
| 326 |
+
st.markdown("""
|
| 327 |
+
### Time Series Analysis
|
| 328 |
+
This analysis decomposes the stock or crypto price into trend, seasonal, and residual components. It helps understand the underlying patterns in the time series data.
|
| 329 |
+
**Components:**
|
| 330 |
+
- **Trend** (\( T_t \)): Represents the long-term movement in the time series.
|
| 331 |
+
- **Seasonal** (\( S_t \)): Captures the repeating short-term cycle in the data.
|
| 332 |
+
- **Residual** (\( R_t \)): The remaining component after removing the trend and seasonal effects, representing the noise or irregular component.
|
| 333 |
+
**Formulas:**
|
| 334 |
+
The time series (\( Y_t \)) can be decomposed as:
|
| 335 |
+
""")
|
| 336 |
+
st.latex(r'''
|
| 337 |
+
Y_t = T_t + S_t + R_t
|
| 338 |
+
''')
|
| 339 |
+
st.markdown("""
|
| 340 |
+
where \( Y_t \) is the observed value at time \( t \), \( T_t \) is the trend component, \( S_t \) is the seasonal component, and \( R_t \) is the residual component.
|
| 341 |
+
The Seasonal-Trend decomposition using LOESS (STL) is used for this decomposition:
|
| 342 |
+
- **LOESS** (Locally Estimated Scatterplot Smoothing): A non-parametric method that fits multiple regressions in local neighborhoods.
|
| 343 |
+
**How to use:**
|
| 344 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 345 |
+
2. Enter the STL decomposition period.
|
| 346 |
+
3. Click 'Fetch Data' to load the data.
|
| 347 |
+
4. The chart will display the time series analysis.
|
| 348 |
+
**Results:**
|
| 349 |
+
The chart shows the original, trend, seasonal, and residual components of the stock or crypto price. This helps in understanding the underlying patterns and making informed decisions based on the decomposed components.
|
| 350 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
fig = plot_time_series_analysis(data, ticker, period)
|
| 352 |
st.plotly_chart(fig)
|
| 353 |
|
|
|
|
| 354 |
elif selected == "ACF and PACF":
|
| 355 |
+
with st.expander("Method Description", expanded=False):
|
| 356 |
+
st.markdown("""
|
| 357 |
+
### ACF and PACF
|
| 358 |
+
This analysis shows the autocorrelation and partial autocorrelation functions of the stock or crypto's daily returns. It helps identify the presence of patterns or trends.
|
| 359 |
+
**Autocorrelation Function (ACF):**
|
| 360 |
+
The ACF measures the correlation between a time series and its lagged values. It is defined as:
|
| 361 |
+
""")
|
| 362 |
+
st.latex(r'''
|
| 363 |
+
\rho_k = \frac{\sum_{t=k+1}^{n} (Y_t - \bar{Y})(Y_{t-k} - \bar{Y})}{\sum_{t=1}^{n} (Y_t - \bar{Y})^2}
|
| 364 |
+
''')
|
| 365 |
+
st.markdown("""
|
| 366 |
+
where \( \rho_k \) is the autocorrelation at lag \( k \), \( Y_t \) is the value at time \( t \), and \( \bar{Y} \) is the mean of the series.
|
| 367 |
+
**Partial Autocorrelation Function (PACF):**
|
| 368 |
+
The PACF measures the correlation between a time series and its lagged values, controlling for the values of the intermediate lags. It is defined as the coefficient \( \phi_{kk} \) in the linear regression:
|
| 369 |
+
""")
|
| 370 |
+
st.latex(r'''
|
| 371 |
+
Y_t = \phi_{k1} Y_{t-1} + \phi_{k2} Y_{t-2} + \cdots + \phi_{kk} Y_{t-k} + \epsilon_t
|
| 372 |
+
''')
|
| 373 |
+
st.markdown("""
|
| 374 |
+
where \( \phi_{kk} \) is the partial autocorrelation at lag \( k \), and \( \epsilon_t \) is the white noise error term.
|
| 375 |
+
**How to use:**
|
| 376 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 377 |
+
2. Enter the number of lags for ACF and PACF.
|
| 378 |
+
3. Click 'Fetch Data' to load the data.
|
| 379 |
+
4. The chart will display the ACF and PACF plots.
|
| 380 |
+
**Results:**
|
| 381 |
+
The chart shows the ACF and PACF of the stock or crypto's daily returns. These plots help in identifying significant lags and understanding the autocorrelation structure of the time series.
|
| 382 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
fig = plot_acf_pacf(data, ticker, nlags)
|
| 384 |
st.plotly_chart(fig)
|
| 385 |
|
|
|
|
| 386 |
elif selected == "Boxplots":
|
| 387 |
+
with st.expander("Method Description", expanded=False):
|
| 388 |
+
st.markdown("""
|
| 389 |
+
### Boxplots
|
| 390 |
+
This analysis shows the effect of the day of the week, month of the year, and year on the stock or crypto's returns. It helps identify patterns based on time periods.
|
| 391 |
+
**How it Works:**
|
| 392 |
+
- **Day of the Week Effect:**
|
| 393 |
+
The boxplot groups returns by each day of the week (e.g., Monday, Tuesday) to identify any patterns or anomalies specific to particular days.
|
| 394 |
+
- **Month of the Year Effect:**
|
| 395 |
+
The boxplot groups returns by each month to highlight any seasonal effects in the stock or crypto returns.
|
| 396 |
+
- **Year Effect:**
|
| 397 |
+
The boxplot groups returns by year to observe any long-term trends or changes in performance over the years.
|
| 398 |
+
**How to use:**
|
| 399 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 400 |
+
2. Click 'Fetch Data' to load the data.
|
| 401 |
+
3. The chart will display the boxplots.
|
| 402 |
+
**Results:**
|
| 403 |
+
The chart shows the boxplots of the stock or crypto's returns based on different time periods, helping to identify temporal patterns and trends.
|
| 404 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 405 |
fig = plot_boxplots(data, ticker)
|
| 406 |
st.plotly_chart(fig)
|
| 407 |
|
|
|
|
| 408 |
elif selected == "Rolling Statistics":
|
| 409 |
+
with st.expander("Method Description", expanded=False):
|
| 410 |
+
st.markdown("""
|
| 411 |
+
### Rolling Statistics
|
| 412 |
+
This analysis shows the rolling kurtosis, skewness, quantile, and autocorrelation of the stock or crypto's price. It helps understand the dynamic changes in the characteristics.
|
| 413 |
+
**How it Works:**
|
| 414 |
+
- **Rolling Kurtosis:**
|
| 415 |
+
Kurtosis measures the tails' heaviness of the distribution. It indicates the presence of outliers.
|
| 416 |
+
""")
|
| 417 |
+
st.latex(r'''
|
| 418 |
+
\text{Kurtosis}(X) = \frac{E[(X - \mu)^4]}{\sigma^4}
|
| 419 |
+
''')
|
| 420 |
+
st.markdown("""
|
| 421 |
+
- **Rolling Skewness:**
|
| 422 |
+
Skewness measures the asymmetry of the distribution of returns.
|
| 423 |
+
""")
|
| 424 |
+
st.latex(r'''
|
| 425 |
+
\text{Skewness}(X) = \frac{E[(X - \mu)^3]}{\sigma^3}
|
| 426 |
+
''')
|
| 427 |
+
st.markdown("""
|
| 428 |
+
- **Rolling Quantile:**
|
| 429 |
+
Quantile indicates the value below which a given percentage of observations fall. For the 50th percentile (median):
|
| 430 |
+
""")
|
| 431 |
+
st.latex(r'''
|
| 432 |
+
Q_p(X) = \inf \{ x \in \mathbb{R} : F_X(x) \geq p \}
|
| 433 |
+
''')
|
| 434 |
+
st.markdown("""
|
| 435 |
+
- **Rolling Autocorrelation:**
|
| 436 |
+
Autocorrelation measures the correlation of the series with its lagged values.
|
| 437 |
+
""")
|
| 438 |
+
st.latex(r'''
|
| 439 |
+
\text{Autocorrelation}(k) = \frac{E[(X_t - \mu)(X_{t-k} - \mu)]}{\sigma^2}
|
| 440 |
+
''')
|
| 441 |
+
st.markdown("""
|
| 442 |
+
**How to use:**
|
| 443 |
+
1. Enter the stock or crypto ticker, start date, and end date.
|
| 444 |
+
2. Enter the rolling window size and quantile value.
|
| 445 |
+
3. Click 'Fetch Data' to load the data.
|
| 446 |
+
4. The chart will display the rolling statistics.
|
| 447 |
+
**Results:**
|
| 448 |
+
The chart shows the rolling kurtosis, skewness, quantile, and autocorrelation of the stock or crypto's price.
|
| 449 |
+
""")
|
|
|
|
|
|
|
| 450 |
fig = plot_rolling_statistics(data, ticker, window_size, quantile_value)
|
| 451 |
st.plotly_chart(fig)
|
| 452 |
|
| 453 |
+
# Hide Streamlit's default footer and menu
|
|
|
|
|
|
|
|
|
|
| 454 |
hide_streamlit_style = """
|
| 455 |
<style>
|
| 456 |
#MainMenu {visibility: hidden;}
|
| 457 |
footer {visibility: hidden;}
|
| 458 |
</style>
|
| 459 |
"""
|
| 460 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|