Skip to main content

Python library for advanced time series forecasting using fractal geometry and chaos theory

Project description

FracTime

Fractal-based time series forecasting with ensemble methods, exogenous predictors, and interactive visualizations.

Python 3.10+ License: MIT PyPI Documentation

Installation

pip install fractime

All dependencies (ARIMA, GARCH, Prophet, PyMC, XGBoost) are included.

Quick Start

import fractime as ft
import numpy as np

# Load or create data
prices = np.random.randn(500).cumsum() + 100

# Analyze fractal properties
analyzer = ft.FractalAnalyzer()
hurst = analyzer.compute_hurst(prices)
print(f"Hurst: {hurst:.3f} ({'trending' if hurst > 0.5 else 'mean-reverting'})")

# Forecast
forecaster = ft.FractalForecaster()
forecaster.fit(prices)
result = forecaster.predict(n_steps=30)

print(f"Forecast: {result['weighted_forecast'][-1]:.2f}")
print(f"95% CI: [{result['lower'][-1]:.2f}, {result['upper'][-1]:.2f}]")

# Interactive visualization with path density
fig = ft.plot_forecast(prices, result, colorscale='Viridis')
fig.show()

Features

Core Forecasting

  • Fractal Analysis: Hurst exponent, fractal dimension, long-term memory detection
  • Monte Carlo Simulation: Generate thousands of potential future paths
  • Probability Weighting: Paths weighted by fractal similarity to historical patterns
  • Trading Time Warping: Mandelbrot's concept of market time dilation

Interactive Visualization

  • Path Density Plots: See clusters of high-probability paths
  • Color-coded Density: Purple (low probability) to yellow (high probability)
  • Percentile Overlays: 5th, 25th, 50th, 75th, 95th percentile lines
  • Fully Interactive: Zoom, pan, hover for details (Plotly-based)

Exogenous Predictors

  • External Variables: Condition forecasts on market indicators, economic data
  • Automatic Lag Selection: Finds optimal lag for each predictor
  • Regime Detection: Identifies how exogenous states affect returns
  • Probability Adjustment: Shifts path probabilities based on current conditions

Model Comparison

  • Baseline Models: ARIMA, ETS, GARCH, Prophet, VAR, LSTM
  • ML Models: Random Forest, XGBoost, SVR, KNN
  • Ensemble Methods: Stacking and boosting
  • Backtesting: Walk-forward validation with comprehensive metrics

Interactive Path Density Visualization

The new visualization system shows all simulated paths colored by probability density:

import fractime as ft

# Fit and predict
forecaster = ft.FractalForecaster()
forecaster.fit(prices, dates=dates)
result = forecaster.predict(n_steps=30, n_paths=1000)

# Create interactive density plot
fig = ft.plot_forecast(
    prices[-100:],           # Show last 100 days of history
    result,
    dates=dates[-100:],
    title="Fractal Forecast with Path Density",
    colorscale='Viridis',    # Options: 'Viridis', 'Plasma', 'Inferno', 'Hot'
    show_percentiles=True,   # Show percentile lines
    max_paths=500            # Limit paths for performance
)
fig.show()  # Interactive in Jupyter
fig.write_html("forecast.html")  # Save as HTML

Colorscales:

  • Viridis: Purple → Teal → Yellow (default, perceptually uniform)
  • Plasma: Blue → Pink → Yellow
  • Inferno: Black → Red → Yellow
  • Hot: Blue → Red → Yellow

Exogenous Predictors

Incorporate external variables to improve forecasts:

import fractime as ft
import pandas as pd

# Load target and exogenous data
# Example: SPY with VIX, TLT (bonds), GLD (gold) as predictors
target = spy_prices
exogenous = pd.DataFrame({
    'VIX': vix_prices,
    'TLT': bond_prices,
    'GLD': gold_prices
})

# Create forecaster with exogenous support
forecaster = ft.FractalForecaster(
    use_exogenous=True,
    exog_max_lags=10,              # Search up to 10 lags
    exog_min_correlation=0.05,     # Include vars with |corr| > 0.05
    exog_adjustment_strength=0.4   # How strongly exog affects probabilities
)

# Fit with exogenous data
forecaster.fit(target, dates=dates, exogenous=exogenous)

# View exogenous analysis
summary = forecaster.get_exogenous_summary()
for name, info in summary['variables'].items():
    status = "INCLUDED" if info['included'] else "excluded"
    print(f"{name}: lag={info['best_lag']}, corr={info['correlation']:.3f} [{status}]")

# Predict (automatically uses exogenous adjustments)
result = forecaster.predict(n_steps=30)

Exogenous Classes

from fractime import (
    ExogenousHandler,           # Preprocessing and lag selection
    ExogenousRegimeModifier,    # Regime-based probability adjustment
    ExogenousForecastAdjuster,  # Incorporate external forecasts
    compute_exogenous_fractal_coherence  # Analyze fractal alignment
)

# Analyze fractal coherence between series
coherence = compute_exogenous_fractal_coherence(
    target=spy_prices,
    exogenous=vix_prices,
    window_sizes=[21, 63, 126]
)
print(f"Fractal coherence: {coherence['overall_coherence']:.3f}")
print(f"Hurst correlation: {coherence['hurst_correlation']:.3f}")

Model Comparison Example

import fractime as ft
from fractime.baselines.arima import ARIMAForecaster
from fractime.baselines.ets import ETSForecaster

# Prepare data
train, test = prices[:-30], prices[-30:]

# Fractal with exogenous
fc_exog = ft.FractalForecaster(use_exogenous=True)
fc_exog.fit(train, exogenous=exog_train)
pred_exog = fc_exog.predict(n_steps=30)

# ARIMA baseline
arima = ARIMAForecaster()
arima.fit(train)
pred_arima = arima.predict(n_steps=30)

# Compare
from sklearn.metrics import mean_squared_error
print(f"Fractal RMSE: {mean_squared_error(test, pred_exog['weighted_forecast'], squared=False):.4f}")
print(f"ARIMA RMSE: {mean_squared_error(test, pred_arima['forecast'], squared=False):.4f}")

Documentation

Full documentation: https://wayy-research.github.io/fracTime

What's New in v0.2.0

Interactive Path Density Visualization

  • All plots now use Plotly for interactivity
  • Path density coloring shows probability clusters
  • Multiple colorscale options
  • Dark theme optimized for density visualization

Exogenous Predictors

  • New exogenous parameter in FractalForecaster.fit()
  • Automatic lag selection and correlation analysis
  • Regime-based probability adjustment
  • Fractal coherence analysis between series

Improved API

  • get_exogenous_summary() for analysis results
  • Enhanced plot_forecast() with density options
  • New utility functions for exogenous analysis

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fractime-0.2.0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fractime-0.2.0-py3-none-any.whl (182.7 kB view details)

Uploaded Python 3

File details

Details for the file fractime-0.2.0.tar.gz.

File metadata

  • Download URL: fractime-0.2.0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fractime-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d6372e30716eeae70c53bb83e38c5d60031ed4cca6663ab277b47d4afd57fbce
MD5 b7dcd16b93a54fd1934db4fd98e28459
BLAKE2b-256 b8fd92e8739f414f4f061dfd472f1859209c130e771d538476122182149b19b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for fractime-0.2.0.tar.gz:

Publisher: publish.yml on Wayy-Research/fracTime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fractime-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fractime-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 182.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fractime-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e8daf1d21a735e1b3f2aeb2144a3fa23e6254f05b08943bb7bf0d936cf1c534
MD5 d473f83022a4992023ffab47b0f7c322
BLAKE2b-256 5091d497573ada0a28bc42d1c96ad957509d1dcfa5a99c19541651503443aaf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for fractime-0.2.0-py3-none-any.whl:

Publisher: publish.yml on Wayy-Research/fracTime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page