Tonic commited on
Commit
ae7bc70
·
verified ·
1 Parent(s): 8e8f1fa

add continuity smoothing

Browse files
Files changed (1) hide show
  1. app.py +272 -18
app.py CHANGED
@@ -1230,24 +1230,56 @@ def make_prediction_enhanced(symbol: str, timeframe: str = "1d", prediction_days
1230
  print(f"Warning: Discontinuity detected between last actual ({last_actual:.4f}) and first prediction ({first_pred:.4f})")
1231
  print(f"Discontinuity magnitude: {abs(first_pred - last_actual):.4f} ({abs(first_pred - last_actual)/last_actual*100:.2f}%)")
1232
 
1233
- # Apply smooth continuity correction
1234
  if len(mean_pred) > 1:
1235
- # Calculate the trend from the original prediction
1236
- original_trend = mean_pred[1] - first_pred
 
 
 
 
 
 
 
 
 
 
 
1237
 
1238
- # Apply a smooth transition over the first few predictions
1239
- transition_length = min(3, len(mean_pred))
1240
  for i in range(transition_length):
1241
- # Gradually transition from last actual to predicted trend
1242
- transition_factor = i / transition_length
1243
- mean_pred[i] = last_actual + original_trend * i * transition_factor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1244
 
1245
  # Apply financial smoothing if enabled
1246
  if use_smoothing:
1247
  mean_pred = apply_financial_smoothing(mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
1248
  else:
1249
- # Single prediction case
1250
  mean_pred[0] = last_actual
 
1251
 
1252
  # If we had to limit the prediction length, extend the prediction recursively
1253
  if actual_prediction_length < trim_length:
@@ -1327,20 +1359,51 @@ def make_prediction_enhanced(symbol: str, timeframe: str = "1d", prediction_days
1327
  print(f"Warning: Discontinuity detected between last prediction ({extended_mean_pred[-1]:.4f}) and next prediction ({next_mean_pred[0]:.4f})")
1328
  print(f"Extension discontinuity magnitude: {abs(next_mean_pred[0] - extended_mean_pred[-1]):.4f}")
1329
 
1330
- # Apply smooth continuity correction
1331
  if len(next_mean_pred) > 1:
1332
- # Calculate the trend from the original prediction
1333
- original_trend = next_mean_pred[1] - next_mean_pred[0]
 
 
 
 
 
 
 
 
 
 
1334
 
1335
- # Apply a smooth transition over the first few predictions
1336
- transition_length = min(3, len(next_mean_pred))
1337
  for i in range(transition_length):
1338
- # Gradually transition from last prediction to predicted trend
1339
- transition_factor = i / transition_length
1340
- next_mean_pred[i] = extended_mean_pred[-1] + original_trend * i * transition_factor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1341
  else:
1342
- # Single prediction case
1343
  next_mean_pred[0] = extended_mean_pred[-1]
 
1344
 
1345
  # Apply financial smoothing if enabled
1346
  if use_smoothing and len(next_mean_pred) > 1:
@@ -1407,6 +1470,71 @@ def make_prediction_enhanced(symbol: str, timeframe: str = "1d", prediction_days
1407
  final_pred = mean_pred
1408
  final_uncertainty = std_pred
1409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1410
  except Exception as e:
1411
  print(f"Chronos prediction error: {str(e)}")
1412
  raise
@@ -1530,6 +1658,71 @@ def make_prediction_enhanced(symbol: str, timeframe: str = "1d", prediction_days
1530
 
1531
  print(f"Technical strategy completed: {len(final_pred)} predictions generated")
1532
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1533
  except Exception as e:
1534
  print(f"Technical strategy error: {str(e)}")
1535
  # Fallback to simple moving average prediction
@@ -1555,6 +1748,67 @@ def make_prediction_enhanced(symbol: str, timeframe: str = "1d", prediction_days
1555
  dummy_quantiles, volatility, market_conditions
1556
  )
1557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1558
  except Exception as fallback_error:
1559
  print(f"Fallback prediction error: {str(fallback_error)}")
1560
  raise
 
1230
  print(f"Warning: Discontinuity detected between last actual ({last_actual:.4f}) and first prediction ({first_pred:.4f})")
1231
  print(f"Discontinuity magnitude: {abs(first_pred - last_actual):.4f} ({abs(first_pred - last_actual)/last_actual*100:.2f}%)")
1232
 
1233
+ # Apply improved continuity correction
1234
  if len(mean_pred) > 1:
1235
+ # Calculate the overall trend from the original predictions
1236
+ original_trend = mean_pred[-1] - first_pred
1237
+ total_steps = len(mean_pred) - 1
1238
+
1239
+ # Calculate the desired trend per step to reach the final prediction
1240
+ if total_steps > 0:
1241
+ trend_per_step = original_trend / total_steps
1242
+ else:
1243
+ trend_per_step = 0
1244
+
1245
+ # Apply smooth transition starting from last actual
1246
+ # Use a gradual transition that preserves the overall trend
1247
+ transition_length = min(5, len(mean_pred)) # Longer transition for smoother curve
1248
 
 
 
1249
  for i in range(transition_length):
1250
+ if i == 0:
1251
+ # First prediction should be very close to last actual
1252
+ mean_pred[i] = last_actual + trend_per_step * 0.1
1253
+ else:
1254
+ # Gradually increase the trend contribution
1255
+ transition_factor = min(1.0, i / transition_length)
1256
+ trend_contribution = trend_per_step * i * transition_factor
1257
+ mean_pred[i] = last_actual + trend_contribution
1258
+
1259
+ # For remaining predictions, maintain the original relative differences
1260
+ if len(mean_pred) > transition_length:
1261
+ # Calculate the scale factor to maintain relative relationships
1262
+ original_diff = mean_pred[transition_length] - mean_pred[transition_length-1]
1263
+ if original_diff != 0:
1264
+ # Scale the remaining predictions to maintain continuity
1265
+ for i in range(transition_length, len(mean_pred)):
1266
+ if i == transition_length:
1267
+ # Ensure smooth transition at the boundary
1268
+ mean_pred[i] = mean_pred[i-1] + original_diff * 0.5
1269
+ else:
1270
+ # Maintain the original relative differences
1271
+ original_diff_i = mean_pred[i] - mean_pred[i-1]
1272
+ mean_pred[i] = mean_pred[i-1] + original_diff_i
1273
+
1274
+ print(f"Applied continuity correction: First prediction adjusted from {first_pred:.4f} to {mean_pred[0]:.4f}")
1275
 
1276
  # Apply financial smoothing if enabled
1277
  if use_smoothing:
1278
  mean_pred = apply_financial_smoothing(mean_pred, smoothing_type, smoothing_window, smoothing_alpha, 3, use_smoothing)
1279
  else:
1280
+ # Single prediction case - set to last actual
1281
  mean_pred[0] = last_actual
1282
+ print(f"Single prediction case: Set to last actual value {last_actual:.4f}")
1283
 
1284
  # If we had to limit the prediction length, extend the prediction recursively
1285
  if actual_prediction_length < trim_length:
 
1359
  print(f"Warning: Discontinuity detected between last prediction ({extended_mean_pred[-1]:.4f}) and next prediction ({next_mean_pred[0]:.4f})")
1360
  print(f"Extension discontinuity magnitude: {abs(next_mean_pred[0] - extended_mean_pred[-1]):.4f}")
1361
 
1362
+ # Apply improved continuity correction for extensions
1363
  if len(next_mean_pred) > 1:
1364
+ # Calculate the overall trend from the original predictions
1365
+ original_trend = next_mean_pred[-1] - next_mean_pred[0]
1366
+ total_steps = len(next_mean_pred) - 1
1367
+
1368
+ # Calculate the desired trend per step
1369
+ if total_steps > 0:
1370
+ trend_per_step = original_trend / total_steps
1371
+ else:
1372
+ trend_per_step = 0
1373
+
1374
+ # Apply smooth transition starting from last extended prediction
1375
+ transition_length = min(5, len(next_mean_pred)) # Longer transition for smoother curve
1376
 
 
 
1377
  for i in range(transition_length):
1378
+ if i == 0:
1379
+ # First prediction should be very close to last extended prediction
1380
+ next_mean_pred[i] = extended_mean_pred[-1] + trend_per_step * 0.1
1381
+ else:
1382
+ # Gradually increase the trend contribution
1383
+ transition_factor = min(1.0, i / transition_length)
1384
+ trend_contribution = trend_per_step * i * transition_factor
1385
+ next_mean_pred[i] = extended_mean_pred[-1] + trend_contribution
1386
+
1387
+ # For remaining predictions, maintain the original relative differences
1388
+ if len(next_mean_pred) > transition_length:
1389
+ # Calculate the scale factor to maintain relative relationships
1390
+ original_diff = next_mean_pred[transition_length] - next_mean_pred[transition_length-1]
1391
+ if original_diff != 0:
1392
+ # Scale the remaining predictions to maintain continuity
1393
+ for i in range(transition_length, len(next_mean_pred)):
1394
+ if i == transition_length:
1395
+ # Ensure smooth transition at the boundary
1396
+ next_mean_pred[i] = next_mean_pred[i-1] + original_diff * 0.5
1397
+ else:
1398
+ # Maintain the original relative differences
1399
+ original_diff_i = next_mean_pred[i] - next_mean_pred[i-1]
1400
+ next_mean_pred[i] = next_mean_pred[i-1] + original_diff_i
1401
+
1402
+ print(f"Applied extension continuity correction: First extension prediction adjusted from {next_mean_pred[0]:.4f} to {next_mean_pred[0]:.4f}")
1403
  else:
1404
+ # Single prediction case - set to last extended prediction
1405
  next_mean_pred[0] = extended_mean_pred[-1]
1406
+ print(f"Single extension prediction case: Set to last extended prediction value {extended_mean_pred[-1]:.4f}")
1407
 
1408
  # Apply financial smoothing if enabled
1409
  if use_smoothing and len(next_mean_pred) > 1:
 
1470
  final_pred = mean_pred
1471
  final_uncertainty = std_pred
1472
 
1473
+ # Final continuity validation and correction
1474
+ print("Performing final continuity validation...")
1475
+ last_actual = df['Close'].iloc[-1]
1476
+ first_pred = final_pred[0]
1477
+ discontinuity_threshold = max(1e-6, 0.01 * abs(last_actual)) # Stricter 1% threshold for final check
1478
+
1479
+ if abs(first_pred - last_actual) > discontinuity_threshold:
1480
+ print(f"Final check: Discontinuity detected between last actual ({last_actual:.4f}) and first prediction ({first_pred:.4f})")
1481
+ print(f"Final discontinuity magnitude: {abs(first_pred - last_actual):.4f} ({abs(first_pred - last_actual)/last_actual*100:.2f}%)")
1482
+
1483
+ # Apply final continuity correction
1484
+ if len(final_pred) > 1:
1485
+ # Calculate the overall trend
1486
+ overall_trend = final_pred[-1] - first_pred
1487
+ total_steps = len(final_pred) - 1
1488
+
1489
+ if total_steps > 0:
1490
+ trend_per_step = overall_trend / total_steps
1491
+ else:
1492
+ trend_per_step = 0
1493
+
1494
+ # Apply very smooth transition
1495
+ transition_length = min(8, len(final_pred)) # Longer transition for final correction
1496
+
1497
+ for i in range(transition_length):
1498
+ if i == 0:
1499
+ # First prediction should be extremely close to last actual
1500
+ final_pred[i] = last_actual + trend_per_step * 0.05
1501
+ else:
1502
+ # Very gradual transition
1503
+ transition_factor = min(1.0, (i / transition_length) ** 2) # Quadratic easing
1504
+ trend_contribution = trend_per_step * i * transition_factor
1505
+ final_pred[i] = last_actual + trend_contribution
1506
+
1507
+ # Maintain relative relationships for remaining predictions
1508
+ if len(final_pred) > transition_length:
1509
+ for i in range(transition_length, len(final_pred)):
1510
+ if i == transition_length:
1511
+ # Smooth transition at boundary
1512
+ final_pred[i] = final_pred[i-1] + trend_per_step * 0.8
1513
+ else:
1514
+ # Maintain original relative differences
1515
+ original_diff = final_pred[i] - final_pred[i-1]
1516
+ final_pred[i] = final_pred[i-1] + original_diff * 0.9 # Slightly dampened
1517
+
1518
+ print(f"Final continuity correction applied: First prediction adjusted from {first_pred:.4f} to {final_pred[0]:.4f}")
1519
+
1520
+ # Apply additional smoothing for final correction
1521
+ if use_smoothing:
1522
+ final_pred = apply_financial_smoothing(final_pred, smoothing_type, smoothing_window, smoothing_alpha * 0.5, 3, use_smoothing)
1523
+ else:
1524
+ # Single prediction case
1525
+ final_pred[0] = last_actual
1526
+ print(f"Final single prediction correction: Set to last actual value {last_actual:.4f}")
1527
+
1528
+ # Verify final continuity
1529
+ final_first_pred = final_pred[0]
1530
+ final_discontinuity = abs(final_first_pred - last_actual) / last_actual * 100
1531
+ print(f"Final continuity check: Discontinuity = {final_discontinuity:.3f}% (threshold: 1.0%)")
1532
+
1533
+ if final_discontinuity <= 1.0:
1534
+ print("✓ Continuity validation passed - predictions are smooth")
1535
+ else:
1536
+ print(f"⚠ Continuity validation warning - discontinuity of {final_discontinuity:.3f}% remains")
1537
+
1538
  except Exception as e:
1539
  print(f"Chronos prediction error: {str(e)}")
1540
  raise
 
1658
 
1659
  print(f"Technical strategy completed: {len(final_pred)} predictions generated")
1660
 
1661
+ # Final continuity validation for technical strategy
1662
+ print("Performing final continuity validation for technical strategy...")
1663
+ last_actual = df['Close'].iloc[-1]
1664
+ first_pred = final_pred[0]
1665
+ discontinuity_threshold = max(1e-6, 0.01 * abs(last_actual)) # Stricter 1% threshold for final check
1666
+
1667
+ if abs(first_pred - last_actual) > discontinuity_threshold:
1668
+ print(f"Technical strategy final check: Discontinuity detected between last actual ({last_actual:.4f}) and first prediction ({first_pred:.4f})")
1669
+ print(f"Technical strategy final discontinuity magnitude: {abs(first_pred - last_actual):.4f} ({abs(first_pred - last_actual)/last_actual*100:.2f}%)")
1670
+
1671
+ # Apply final continuity correction for technical strategy
1672
+ if len(final_pred) > 1:
1673
+ # Calculate the overall trend
1674
+ overall_trend = final_pred[-1] - first_pred
1675
+ total_steps = len(final_pred) - 1
1676
+
1677
+ if total_steps > 0:
1678
+ trend_per_step = overall_trend / total_steps
1679
+ else:
1680
+ trend_per_step = 0
1681
+
1682
+ # Apply very smooth transition
1683
+ transition_length = min(8, len(final_pred)) # Longer transition for final correction
1684
+
1685
+ for i in range(transition_length):
1686
+ if i == 0:
1687
+ # First prediction should be extremely close to last actual
1688
+ final_pred[i] = last_actual + trend_per_step * 0.05
1689
+ else:
1690
+ # Very gradual transition
1691
+ transition_factor = min(1.0, (i / transition_length) ** 2) # Quadratic easing
1692
+ trend_contribution = trend_per_step * i * transition_factor
1693
+ final_pred[i] = last_actual + trend_contribution
1694
+
1695
+ # Maintain relative relationships for remaining predictions
1696
+ if len(final_pred) > transition_length:
1697
+ for i in range(transition_length, len(final_pred)):
1698
+ if i == transition_length:
1699
+ # Smooth transition at boundary
1700
+ final_pred[i] = final_pred[i-1] + trend_per_step * 0.8
1701
+ else:
1702
+ # Maintain original relative differences
1703
+ original_diff = final_pred[i] - final_pred[i-1]
1704
+ final_pred[i] = final_pred[i-1] + original_diff * 0.9 # Slightly dampened
1705
+
1706
+ print(f"Technical strategy final continuity correction applied: First prediction adjusted from {first_pred:.4f} to {final_pred[0]:.4f}")
1707
+
1708
+ # Apply additional smoothing for final correction
1709
+ if use_smoothing:
1710
+ final_pred = apply_financial_smoothing(final_pred, smoothing_type, smoothing_window, smoothing_alpha * 0.5, 3, use_smoothing)
1711
+ else:
1712
+ # Single prediction case
1713
+ final_pred[0] = last_actual
1714
+ print(f"Technical strategy final single prediction correction: Set to last actual value {last_actual:.4f}")
1715
+
1716
+ # Verify final continuity for technical strategy
1717
+ final_first_pred = final_pred[0]
1718
+ final_discontinuity = abs(final_first_pred - last_actual) / last_actual * 100
1719
+ print(f"Technical strategy final continuity check: Discontinuity = {final_discontinuity:.3f}% (threshold: 1.0%)")
1720
+
1721
+ if final_discontinuity <= 1.0:
1722
+ print("✓ Technical strategy continuity validation passed - predictions are smooth")
1723
+ else:
1724
+ print(f"⚠ Technical strategy continuity validation warning - discontinuity of {final_discontinuity:.3f}% remains")
1725
+
1726
  except Exception as e:
1727
  print(f"Technical strategy error: {str(e)}")
1728
  # Fallback to simple moving average prediction
 
1748
  dummy_quantiles, volatility, market_conditions
1749
  )
1750
 
1751
+ # Final continuity validation for fallback case
1752
+ print("Performing final continuity validation for fallback prediction...")
1753
+ last_actual = df['Close'].iloc[-1]
1754
+ first_pred = final_pred[0]
1755
+ discontinuity_threshold = max(1e-6, 0.01 * abs(last_actual)) # Stricter 1% threshold for final check
1756
+
1757
+ if abs(first_pred - last_actual) > discontinuity_threshold:
1758
+ print(f"Fallback final check: Discontinuity detected between last actual ({last_actual:.4f}) and first prediction ({first_pred:.4f})")
1759
+ print(f"Fallback final discontinuity magnitude: {abs(first_pred - last_actual):.4f} ({abs(first_pred - last_actual)/last_actual*100:.2f}%)")
1760
+
1761
+ # Apply final continuity correction for fallback case
1762
+ if len(final_pred) > 1:
1763
+ # Calculate the overall trend
1764
+ overall_trend = final_pred[-1] - first_pred
1765
+ total_steps = len(final_pred) - 1
1766
+
1767
+ if total_steps > 0:
1768
+ trend_per_step = overall_trend / total_steps
1769
+ else:
1770
+ trend_per_step = 0
1771
+
1772
+ # Apply very smooth transition
1773
+ transition_length = min(8, len(final_pred)) # Longer transition for final correction
1774
+
1775
+ for i in range(transition_length):
1776
+ if i == 0:
1777
+ # First prediction should be extremely close to last actual
1778
+ final_pred[i] = last_actual + trend_per_step * 0.05
1779
+ else:
1780
+ # Very gradual transition
1781
+ transition_factor = min(1.0, (i / transition_length) ** 2) # Quadratic easing
1782
+ trend_contribution = trend_per_step * i * transition_factor
1783
+ final_pred[i] = last_actual + trend_contribution
1784
+
1785
+ # Maintain relative relationships for remaining predictions
1786
+ if len(final_pred) > transition_length:
1787
+ for i in range(transition_length, len(final_pred)):
1788
+ if i == transition_length:
1789
+ # Smooth transition at boundary
1790
+ final_pred[i] = final_pred[i-1] + trend_per_step * 0.8
1791
+ else:
1792
+ # Maintain original relative differences
1793
+ original_diff = final_pred[i] - final_pred[i-1]
1794
+ final_pred[i] = final_pred[i-1] + original_diff * 0.9 # Slightly dampened
1795
+
1796
+ print(f"Fallback final continuity correction applied: First prediction adjusted from {first_pred:.4f} to {final_pred[0]:.4f}")
1797
+ else:
1798
+ # Single prediction case
1799
+ final_pred[0] = last_actual
1800
+ print(f"Fallback final single prediction correction: Set to last actual value {last_actual:.4f}")
1801
+
1802
+ # Verify final continuity for fallback case
1803
+ final_first_pred = final_pred[0]
1804
+ final_discontinuity = abs(final_first_pred - last_actual) / last_actual * 100
1805
+ print(f"Fallback final continuity check: Discontinuity = {final_discontinuity:.3f}% (threshold: 1.0%)")
1806
+
1807
+ if final_discontinuity <= 1.0:
1808
+ print("✓ Fallback continuity validation passed - predictions are smooth")
1809
+ else:
1810
+ print(f"⚠ Fallback continuity validation warning - discontinuity of {final_discontinuity:.3f}% remains")
1811
+
1812
  except Exception as fallback_error:
1813
  print(f"Fallback prediction error: {str(fallback_error)}")
1814
  raise