Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,8 +104,10 @@ def extract_and_reduce_features(data, n_components=3):
|
|
| 104 |
st.set_page_config(page_title="Pattern Recognition", layout="wide")
|
| 105 |
st.title('Pattern Recognition in Stock Prices')
|
| 106 |
|
|
|
|
| 107 |
# Sidebar for method selection
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
# Sidebar for input parameters
|
| 111 |
st.sidebar.header("Input Parameters")
|
|
@@ -292,15 +294,28 @@ if st.sidebar.button('Run'):
|
|
| 292 |
if selected == "DTW Pattern Recognition":
|
| 293 |
st.markdown("""
|
| 294 |
### DTW Pattern Recognition
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
**How to use:**
|
| 299 |
1. Enter the stock ticker, start date, and end date.
|
| 300 |
2. Select the number of subsequent days to forecast.
|
| 301 |
3. Select the number of days to compare.
|
| 302 |
4. Click the 'Run' button.
|
| 303 |
-
|
| 304 |
**Results:**
|
| 305 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 306 |
""")
|
|
@@ -315,8 +330,17 @@ if selected == "DTW Pattern Recognition":
|
|
| 315 |
elif selected == "Correlation Pattern Recognition":
|
| 316 |
st.markdown("""
|
| 317 |
### Correlation Pattern Recognition
|
| 318 |
-
|
| 319 |
-
This method calculates the correlation between the current stock price pattern and historical patterns. Correlation measures
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
**How to use:**
|
| 322 |
1. Enter the stock ticker, start date, and end date.
|
|
@@ -324,7 +348,7 @@ elif selected == "Correlation Pattern Recognition":
|
|
| 324 |
3. Select the number of days to compare.
|
| 325 |
4. Select the number of days prior to the similar series.
|
| 326 |
5. Click the 'Run' button.
|
| 327 |
-
|
| 328 |
**Results:**
|
| 329 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 330 |
""")
|
|
@@ -339,15 +363,25 @@ elif selected == "Correlation Pattern Recognition":
|
|
| 339 |
elif selected == "TA-Enhanced DTW Pattern Recognition":
|
| 340 |
st.markdown("""
|
| 341 |
### TA-Enhanced DTW Pattern Recognition
|
| 342 |
-
|
| 343 |
-
This method combines technical analysis (TA) features with DTW to enhance pattern recognition. It integrates various TA indicators into the time series data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
**How to use:**
|
| 346 |
1. Enter the stock ticker, start date, and end date.
|
| 347 |
2. Select the number of subsequent days to forecast.
|
| 348 |
3. Select the number of days to compare.
|
| 349 |
4. Click the 'Run' button.
|
| 350 |
-
|
| 351 |
**Results:**
|
| 352 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 353 |
""")
|
|
|
|
| 104 |
st.set_page_config(page_title="Pattern Recognition", layout="wide")
|
| 105 |
st.title('Pattern Recognition in Stock Prices')
|
| 106 |
|
| 107 |
+
|
| 108 |
# Sidebar for method selection
|
| 109 |
+
st.sidebar.header("Pattern Recognition Methods")
|
| 110 |
+
selected = st.sidebar.radio("Choose a method:",["DTW Pattern Recognition", "Correlation Pattern Recognition", "TA-Enhanced DTW Pattern Recognition"])
|
| 111 |
|
| 112 |
# Sidebar for input parameters
|
| 113 |
st.sidebar.header("Input Parameters")
|
|
|
|
| 294 |
if selected == "DTW Pattern Recognition":
|
| 295 |
st.markdown("""
|
| 296 |
### DTW Pattern Recognition
|
| 297 |
+
|
| 298 |
+
Dynamic Time Warping (DTW) is a method that measures the similarity between two time series that may vary in time or speed. DTW aligns the time series by warping the time axis to minimize the distance between them. This method identifies historical periods that have similar patterns to the current stock price pattern by comparing their shapes, regardless of possible distortions in the time axis.
|
| 299 |
+
|
| 300 |
+
The DTW distance \( D \) between two time series \( X \) and \( Y \) is calculated as:
|
| 301 |
+
|
| 302 |
+
""")
|
| 303 |
+
st.latex(r'''
|
| 304 |
+
D(i, j) = \text{dist}(X_i, Y_j) + \min \begin{cases}
|
| 305 |
+
D(i-1, j) \\
|
| 306 |
+
D(i, j-1) \\
|
| 307 |
+
D(i-1, j-1)
|
| 308 |
+
\end{cases}
|
| 309 |
+
''')
|
| 310 |
+
st.markdown("""
|
| 311 |
+
where {dist}(X_i, Y_j) is the distance between points (X_i) and (Y_j), and D(i, j) is the accumulated cost.
|
| 312 |
|
| 313 |
**How to use:**
|
| 314 |
1. Enter the stock ticker, start date, and end date.
|
| 315 |
2. Select the number of subsequent days to forecast.
|
| 316 |
3. Select the number of days to compare.
|
| 317 |
4. Click the 'Run' button.
|
| 318 |
+
|
| 319 |
**Results:**
|
| 320 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 321 |
""")
|
|
|
|
| 330 |
elif selected == "Correlation Pattern Recognition":
|
| 331 |
st.markdown("""
|
| 332 |
### Correlation Pattern Recognition
|
| 333 |
+
|
| 334 |
+
This method calculates the correlation between the current stock price pattern and historical patterns. Correlation measures the degree to which two time series move together. A higher correlation value indicates that the patterns are more similar. This method helps identify historical periods where stock prices behaved similarly to the current pattern by looking at how closely the price movements align.
|
| 335 |
+
|
| 336 |
+
The correlation coefficient \( r \) is calculated as:
|
| 337 |
+
|
| 338 |
+
""")
|
| 339 |
+
st.latex(r'''
|
| 340 |
+
r = \frac{ \sum (X_i - \bar{X})(Y_i - \bar{Y}) }{ \sqrt{ \sum (X_i - \bar{X})^2 } \sqrt{ \sum (Y_i - \bar{Y})^2 } }
|
| 341 |
+
''')
|
| 342 |
+
st.markdown("""
|
| 343 |
+
where \( X \) and \( Y \) are the two time series being compared.
|
| 344 |
|
| 345 |
**How to use:**
|
| 346 |
1. Enter the stock ticker, start date, and end date.
|
|
|
|
| 348 |
3. Select the number of days to compare.
|
| 349 |
4. Select the number of days prior to the similar series.
|
| 350 |
5. Click the 'Run' button.
|
| 351 |
+
|
| 352 |
**Results:**
|
| 353 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 354 |
""")
|
|
|
|
| 363 |
elif selected == "TA-Enhanced DTW Pattern Recognition":
|
| 364 |
st.markdown("""
|
| 365 |
### TA-Enhanced DTW Pattern Recognition
|
| 366 |
+
|
| 367 |
+
This method combines technical analysis (TA) features with Dynamic Time Warping (DTW) to enhance pattern recognition. It integrates various TA indicators such as moving averages, momentum indicators, and volatility measures into the time series data. These indicators provide additional context to the price movements, making the pattern recognition process more robust.
|
| 368 |
+
|
| 369 |
+
Additionally, Principal Component Analysis (PCA) is used to reduce the dimensionality of the TA features. PCA transforms the TA indicators into a smaller set of uncorrelated components, capturing the most significant information with fewer variables. This reduction helps in efficiently processing the data and improving the accuracy of pattern recognition.
|
| 370 |
+
|
| 371 |
+
The method uses a weighted mechanism to combine the DTW distance of the price changes and the TA features. The formula is:
|
| 372 |
+
""")
|
| 373 |
+
st.latex(r'''
|
| 374 |
+
\text{Total Distance} = w \cdot \text{DTW distance of price changes} + (1 - w) \cdot \text{DTW distance of TA features}
|
| 375 |
+
''')
|
| 376 |
+
st.markdown("""
|
| 377 |
+
where \( w \) is a weight factor that balances the contribution of price changes and TA features.
|
| 378 |
|
| 379 |
**How to use:**
|
| 380 |
1. Enter the stock ticker, start date, and end date.
|
| 381 |
2. Select the number of subsequent days to forecast.
|
| 382 |
3. Select the number of days to compare.
|
| 383 |
4. Click the 'Run' button.
|
| 384 |
+
|
| 385 |
**Results:**
|
| 386 |
The left chart shows the entire stock price data with the identified patterns highlighted. The right chart shows the reindexed price patterns for comparison.
|
| 387 |
""")
|