Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -253,8 +253,7 @@ if selected == "Candlestick Chart":
|
|
| 253 |
elif selected == "Returns Distribution":
|
| 254 |
|
| 255 |
st.markdown("""### Returns Distribution
|
| 256 |
-
This analysis shows the distribution of daily, weekly, and monthly returns.
|
| 257 |
-
It helps understand the asset return characteristics by visualizing the frequency and magnitude of price changes over different time periods.
|
| 258 |
""")
|
| 259 |
with st.expander("Method Description", expanded=False):
|
| 260 |
st.markdown("""
|
|
@@ -276,19 +275,13 @@ elif selected == "Returns Distribution":
|
|
| 276 |
''')
|
| 277 |
|
| 278 |
st.markdown("""
|
| 279 |
-
|
| 280 |
-
-
|
| 281 |
-
-
|
| 282 |
-
-
|
| 283 |
-
-
|
| 284 |
-
-
|
| 285 |
-
-
|
| 286 |
-
**How to use:**
|
| 287 |
-
1. Enter the stock or crypto ticker, start date, and end date.
|
| 288 |
-
2. Click 'Fetch Data' to load the data.
|
| 289 |
-
3. The chart will display the distribution of returns.
|
| 290 |
-
**Results:**
|
| 291 |
-
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.
|
| 292 |
""")
|
| 293 |
fig = plot_returns_distribution(data, ticker, return_periods)
|
| 294 |
st.plotly_chart(fig)
|
|
@@ -298,70 +291,75 @@ elif selected == "Probability Plots":
|
|
| 298 |
st.markdown("""
|
| 299 |
### Probability Plots
|
| 300 |
This analysis shows the normal and t-distribution probability plots of daily returns. It helps check if the returns follow a specific distribution.
|
|
|
|
|
|
|
|
|
|
| 301 |
**Components:**
|
| 302 |
- **Normal Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a normal distribution.
|
| 303 |
- **T-Distribution Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a t-distribution with estimated parameters.
|
| 304 |
**Formulas:**
|
| 305 |
-
- **Daily Return** (
|
| 306 |
""")
|
| 307 |
st.latex(r'''
|
| 308 |
R_d = \frac{P_{close, t} - P_{close, t-1}}{P_{close, t-1}}
|
| 309 |
''')
|
| 310 |
st.markdown("""
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
| 312 |
- **Normal Probability Plot**: Compares the ordered sample values of daily returns to the expected values if the data followed a normal distribution.
|
| 313 |
-
- **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 (
|
| 314 |
""")
|
| 315 |
st.latex(r'''
|
| 316 |
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}}
|
| 317 |
''')
|
| 318 |
st.markdown("""
|
| 319 |
-
where
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
**Results:**
|
| 325 |
-
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.
|
| 326 |
""")
|
| 327 |
fig = plot_probability_plots(data, ticker)
|
| 328 |
st.plotly_chart(fig)
|
| 329 |
|
| 330 |
elif selected == "Time Series Analysis":
|
|
|
|
|
|
|
|
|
|
| 331 |
with st.expander("Method Description", expanded=False):
|
| 332 |
st.markdown("""
|
| 333 |
-
### Time Series Analysis
|
| 334 |
-
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.
|
| 335 |
**Components:**
|
| 336 |
-
- **Trend** (
|
| 337 |
-
- **Seasonal** (
|
| 338 |
-
- **Residual** (
|
| 339 |
**Formulas:**
|
| 340 |
-
The time series (
|
| 341 |
""")
|
| 342 |
st.latex(r'''
|
| 343 |
Y_t = T_t + S_t + R_t
|
| 344 |
''')
|
| 345 |
st.markdown("""
|
| 346 |
-
where
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
- **LOESS** (Locally Estimated Scatterplot Smoothing): A non-parametric method that fits multiple regressions in local neighborhoods.
|
| 349 |
-
|
| 350 |
-
1. Enter the stock or crypto ticker, start date, and end date.
|
| 351 |
-
2. Enter the STL decomposition period.
|
| 352 |
-
3. Click 'Fetch Data' to load the data.
|
| 353 |
-
4. The chart will display the time series analysis.
|
| 354 |
-
**Results:**
|
| 355 |
-
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.
|
| 356 |
""")
|
| 357 |
fig = plot_time_series_analysis(data, ticker, period)
|
| 358 |
st.plotly_chart(fig)
|
| 359 |
|
| 360 |
elif selected == "ACF and PACF":
|
|
|
|
|
|
|
|
|
|
| 361 |
with st.expander("Method Description", expanded=False):
|
| 362 |
st.markdown("""
|
| 363 |
-
### ACF and PACF
|
| 364 |
-
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.
|
| 365 |
**Autocorrelation Function (ACF):**
|
| 366 |
The ACF measures the correlation between a time series and its lagged values. It is defined as:
|
| 367 |
""")
|
|
@@ -369,22 +367,21 @@ elif selected == "ACF and PACF":
|
|
| 369 |
\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}
|
| 370 |
''')
|
| 371 |
st.markdown("""
|
| 372 |
-
where
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
**Partial Autocorrelation Function (PACF):**
|
| 374 |
-
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
|
| 375 |
""")
|
| 376 |
st.latex(r'''
|
| 377 |
Y_t = \phi_{k1} Y_{t-1} + \phi_{k2} Y_{t-2} + \cdots + \phi_{kk} Y_{t-k} + \epsilon_t
|
| 378 |
''')
|
| 379 |
st.markdown("""
|
| 380 |
-
where
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
2. Enter the number of lags for ACF and PACF.
|
| 384 |
-
3. Click 'Fetch Data' to load the data.
|
| 385 |
-
4. The chart will display the ACF and PACF plots.
|
| 386 |
-
**Results:**
|
| 387 |
-
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.
|
| 388 |
""")
|
| 389 |
fig = plot_acf_pacf(data, ticker, nlags)
|
| 390 |
st.plotly_chart(fig)
|
|
@@ -401,12 +398,6 @@ elif selected == "Boxplots":
|
|
| 401 |
The boxplot groups returns by each month to highlight any seasonal effects in the stock or crypto returns.
|
| 402 |
- **Year Effect:**
|
| 403 |
The boxplot groups returns by year to observe any long-term trends or changes in performance over the years.
|
| 404 |
-
**How to use:**
|
| 405 |
-
1. Enter the stock or crypto ticker, start date, and end date.
|
| 406 |
-
2. Click 'Fetch Data' to load the data.
|
| 407 |
-
3. The chart will display the boxplots.
|
| 408 |
-
**Results:**
|
| 409 |
-
The chart shows the boxplots of the stock or crypto's returns based on different time periods, helping to identify temporal patterns and trends.
|
| 410 |
""")
|
| 411 |
fig = plot_boxplots(data, ticker)
|
| 412 |
st.plotly_chart(fig)
|
|
@@ -444,15 +435,6 @@ elif selected == "Rolling Statistics":
|
|
| 444 |
st.latex(r'''
|
| 445 |
\text{Autocorrelation}(k) = \frac{E[(X_t - \mu)(X_{t-k} - \mu)]}{\sigma^2}
|
| 446 |
''')
|
| 447 |
-
st.markdown("""
|
| 448 |
-
**How to use:**
|
| 449 |
-
1. Enter the stock or crypto ticker, start date, and end date.
|
| 450 |
-
2. Enter the rolling window size and quantile value.
|
| 451 |
-
3. Click 'Fetch Data' to load the data.
|
| 452 |
-
4. The chart will display the rolling statistics.
|
| 453 |
-
**Results:**
|
| 454 |
-
The chart shows the rolling kurtosis, skewness, quantile, and autocorrelation of the stock or crypto's price.
|
| 455 |
-
""")
|
| 456 |
fig = plot_rolling_statistics(data, ticker, window_size, quantile_value)
|
| 457 |
st.plotly_chart(fig)
|
| 458 |
|
|
|
|
| 253 |
elif selected == "Returns Distribution":
|
| 254 |
|
| 255 |
st.markdown("""### Returns Distribution
|
| 256 |
+
This analysis shows the distribution of daily, weekly, and monthly returns. It helps understand the asset return characteristics by visualizing the frequency and magnitude of price changes over different time periods.
|
|
|
|
| 257 |
""")
|
| 258 |
with st.expander("Method Description", expanded=False):
|
| 259 |
st.markdown("""
|
|
|
|
| 275 |
''')
|
| 276 |
|
| 277 |
st.markdown("""
|
| 278 |
+
Where:
|
| 279 |
+
- `P_close_t` is the closing price at time `t`.
|
| 280 |
+
- `P_close_(t-1)` is the closing price at time `t-1`.
|
| 281 |
+
- `P_close_week_t` is the closing price at the end of the current week.
|
| 282 |
+
- `P_close_week_(t-1)` is the closing price at the end of the previous week.
|
| 283 |
+
- `P_close_month_t` is the closing price at the end of the current month.
|
| 284 |
+
- `P_close_month_(t-1)` is the closing price at the end of the previous month
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
""")
|
| 286 |
fig = plot_returns_distribution(data, ticker, return_periods)
|
| 287 |
st.plotly_chart(fig)
|
|
|
|
| 291 |
st.markdown("""
|
| 292 |
### Probability Plots
|
| 293 |
This analysis shows the normal and t-distribution probability plots of daily returns. It helps check if the returns follow a specific distribution.
|
| 294 |
+
""")
|
| 295 |
+
st.markdown("""
|
| 296 |
+
|
| 297 |
**Components:**
|
| 298 |
- **Normal Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a normal distribution.
|
| 299 |
- **T-Distribution Probability Plot**: Plots the quantiles of the daily returns against the theoretical quantiles of a t-distribution with estimated parameters.
|
| 300 |
**Formulas:**
|
| 301 |
+
- **Daily Return** (R_d):
|
| 302 |
""")
|
| 303 |
st.latex(r'''
|
| 304 |
R_d = \frac{P_{close, t} - P_{close, t-1}}{P_{close, t-1}}
|
| 305 |
''')
|
| 306 |
st.markdown("""
|
| 307 |
+
where:
|
| 308 |
+
- `P_close_t` is the closing price at time `t`.
|
| 309 |
+
- `P_close_(t-1)` is the closing price at time `t-1`.
|
| 310 |
+
|
| 311 |
- **Normal Probability Plot**: Compares the ordered sample values of daily returns to the expected values if the data followed a normal distribution.
|
| 312 |
+
- **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 (ν), location parameter (μ), and scale parameter (σ):
|
| 313 |
""")
|
| 314 |
st.latex(r'''
|
| 315 |
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}}
|
| 316 |
''')
|
| 317 |
st.markdown("""
|
| 318 |
+
where:
|
| 319 |
+
- `Γ` is the gamma function.
|
| 320 |
+
- `ν` is the degrees of freedom.
|
| 321 |
+
- `μ` is the location parameter.
|
| 322 |
+
- `σ` is the scale parameter.
|
|
|
|
|
|
|
| 323 |
""")
|
| 324 |
fig = plot_probability_plots(data, ticker)
|
| 325 |
st.plotly_chart(fig)
|
| 326 |
|
| 327 |
elif selected == "Time Series Analysis":
|
| 328 |
+
st.markdown("""### Time Series Analysis
|
| 329 |
+
This analysis decomposes the stock or crypto price into trend, seasonal, and residual components.
|
| 330 |
+
""")
|
| 331 |
with st.expander("Method Description", expanded=False):
|
| 332 |
st.markdown("""
|
|
|
|
|
|
|
| 333 |
**Components:**
|
| 334 |
+
- **Trend** (T_t): Represents the long-term movement in the time series.
|
| 335 |
+
- **Seasonal** (S_t): Captures the repeating short-term cycle in the data.
|
| 336 |
+
- **Residual** (R_t): The remaining component after removing the trend and seasonal effects, representing the noise or irregular component.
|
| 337 |
**Formulas:**
|
| 338 |
+
The time series (Y_t) can be decomposed as:
|
| 339 |
""")
|
| 340 |
st.latex(r'''
|
| 341 |
Y_t = T_t + S_t + R_t
|
| 342 |
''')
|
| 343 |
st.markdown("""
|
| 344 |
+
where:
|
| 345 |
+
- `Y_t` is the observed value at time `t`.
|
| 346 |
+
- `T_t` is the trend component.
|
| 347 |
+
- `S_t` is the seasonal component.
|
| 348 |
+
- `R_t` is the residual component.
|
| 349 |
+
|
| 350 |
+
The Seasonal-Trend decomposition using LOESS is used for this decomposition:
|
| 351 |
- **LOESS** (Locally Estimated Scatterplot Smoothing): A non-parametric method that fits multiple regressions in local neighborhoods.
|
| 352 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
""")
|
| 354 |
fig = plot_time_series_analysis(data, ticker, period)
|
| 355 |
st.plotly_chart(fig)
|
| 356 |
|
| 357 |
elif selected == "ACF and PACF":
|
| 358 |
+
st.markdown("""### ACF and PACF
|
| 359 |
+
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.
|
| 360 |
+
""")
|
| 361 |
with st.expander("Method Description", expanded=False):
|
| 362 |
st.markdown("""
|
|
|
|
|
|
|
| 363 |
**Autocorrelation Function (ACF):**
|
| 364 |
The ACF measures the correlation between a time series and its lagged values. It is defined as:
|
| 365 |
""")
|
|
|
|
| 367 |
\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}
|
| 368 |
''')
|
| 369 |
st.markdown("""
|
| 370 |
+
where:
|
| 371 |
+
- `ρ_k` is the autocorrelation at lag `k`.
|
| 372 |
+
- `Y_t` is the value at time `t`.
|
| 373 |
+
- `Ȳ` is the mean of the series.
|
| 374 |
+
|
| 375 |
**Partial Autocorrelation Function (PACF):**
|
| 376 |
+
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 `φ_kk` in the linear regression:
|
| 377 |
""")
|
| 378 |
st.latex(r'''
|
| 379 |
Y_t = \phi_{k1} Y_{t-1} + \phi_{k2} Y_{t-2} + \cdots + \phi_{kk} Y_{t-k} + \epsilon_t
|
| 380 |
''')
|
| 381 |
st.markdown("""
|
| 382 |
+
where:
|
| 383 |
+
- `φ_kk` is the partial autocorrelation at lag `k`.
|
| 384 |
+
- `ε_t` is the white noise error term.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
""")
|
| 386 |
fig = plot_acf_pacf(data, ticker, nlags)
|
| 387 |
st.plotly_chart(fig)
|
|
|
|
| 398 |
The boxplot groups returns by each month to highlight any seasonal effects in the stock or crypto returns.
|
| 399 |
- **Year Effect:**
|
| 400 |
The boxplot groups returns by year to observe any long-term trends or changes in performance over the years.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
""")
|
| 402 |
fig = plot_boxplots(data, ticker)
|
| 403 |
st.plotly_chart(fig)
|
|
|
|
| 435 |
st.latex(r'''
|
| 436 |
\text{Autocorrelation}(k) = \frac{E[(X_t - \mu)(X_{t-k} - \mu)]}{\sigma^2}
|
| 437 |
''')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
fig = plot_rolling_statistics(data, ticker, window_size, quantile_value)
|
| 439 |
st.plotly_chart(fig)
|
| 440 |
|