Skip to main content

A Python toolkit for simulating intermittent processes and Levy flights.

Project description

Documentation Status License PyPI - Python Version PyPI Downloads GitHub Watchers GitHub Stars

IntLevPy: Intermittent Lévy Processes Package

IntLevPy is a Python package for simulating and analyzing intermittent and Lévy processes. It provides tools for process simulation, moments calculation, optimization, and classification methods, making it ideal for researchers and practitioners in fields like statistical physics and complex systems.

Overview

  • Process Simulation: Generate synthetic intermittent and Lévy flight trajectories.
  • Moments Calculation: Compute theoretical and empirical moments of trajectories.
  • Optimization: Fit model parameters to empirical data using optimization techniques.
  • Classification: Differentiate between intermittent and Lévy processes using statistical methods.

For detailed documentation, visit the IntLevPy Documentation.

Model

Intermittent Lévy Process Model

Figure: Schematic representation of the intermittent Lévy process model.

Installation

Install IntLevPy directly from PyPI:

pip install IntLevPy

Usage

Simulating and Analyzing an Intermittent Process

import numpy as np
import matplotlib.pyplot as plt
from intermittent_levy.processes import intermittent3
from intermittent_levy.moments import mom2_serg_log, mom4_serg_log
from intermittent_levy.optimization import to_optimize_mom4_and_2_serg_log
from intermittent_levy.utils import adjusted_r_square
from scipy import optimize

# Simulation parameters
N = 300000                   # Number of steps
integration_factor = 3
tau = 1.0 / integration_factor
v0 = 10.0                    # Mean velocity
D = 0.5                      # Diffusion coefficient
lambda_B = 0.1               # Transition rate from ballistic to diffusive
lambda_D = 0.05              # Transition rate from diffusive to ballistic

# Simulate intermittent process
x_traj, y_traj = intermittent3(
    N * integration_factor,
    tau,
    v0,
    D,
    lambda_B,
    lambda_D
)

# Time intervals for analysis
tau_list = np.arange(1, 100, 5)

# Calculate empirical moments
dx2 = []
dx4 = []
for tau_i in tau_list:
    dx = x_traj[int(tau_i):] - x_traj[:-int(tau_i)]
    dy = y_traj[int(tau_i):] - y_traj[:-int(tau_i)]
    displacement = dx**2 + dy**2
    dx2.append(np.mean(displacement))
    dx4.append(np.mean(displacement**2))

dx2_log = np.log(dx2)
dx4_log = np.log(dx4)

# Parameter estimation using optimization
def objective_function(params, tau_list, dx2_log, dx4_log):
    v0_opt, D_opt, lambda_B_opt, lambda_D_opt = params
    model_dx2_log = mom2_serg_log(tau_list, v0_opt, D_opt, lambda_B_opt, lambda_D_opt)
    model_dx4_log = mom4_serg_log(tau_list, v0_opt, D_opt, lambda_B_opt, lambda_D_opt)
    error = np.sum((dx2_log - model_dx2_log)**2 + (dx4_log - model_dx4_log)**2)
    return error

initial_guess = [v0, D, lambda_B, lambda_D]
bounds = [
    (v0 * 0.5, v0 * 1.5),
    (D * 0.5, D * 1.5),
    (lambda_B * 0.5, lambda_B * 1.5),
    (lambda_D * 0.5, lambda_D * 1.5)
]

result = optimize.minimize(
    objective_function,
    initial_guess,
    args=(tau_list, dx2_log, dx4_log),
    bounds=bounds
)

estimated_params = result.x
print("Estimated Parameters:")
print(f"v0 = {estimated_params[0]:.4f}")
print(f"D = {estimated_params[1]:.4f}")
print(f"lambda_B = {estimated_params[2]:.4f}")
print(f"lambda_D = {estimated_params[3]:.4f}")

# Plot empirical and fitted moments
fitted_dx2_log = mom2_serg_log(tau_list, *estimated_params)
fitted_dx4_log = mom4_serg_log(tau_list, *estimated_params)

plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(np.log(tau_list), dx2_log, 'o', label='Empirical log M2')
plt.plot(np.log(tau_list), fitted_dx2_log, '-', label='Fitted log M2')
plt.xlabel('log(tau)')
plt.ylabel('log(M2)')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(np.log(tau_list), dx4_log, 'o', label='Empirical log M4')
plt.plot(np.log(tau_list), fitted_dx4_log, '-', label='Fitted log M4')
plt.xlabel('log(tau)')
plt.ylabel('log(M4)')
plt.legend()

plt.tight_layout()
plt.show()

Simulating and Analyzing a Lévy Flight Process

import numpy as np
import matplotlib.pyplot as plt
from intermittent_levy.processes import levy_flight_2D_Simplified
from intermittent_levy.moments import levy_moments_log
from intermittent_levy.optimization import to_optimize_levy
from intermittent_levy.utils import adjusted_r_square
from scipy import optimize

# Simulation parameters
N = 300000               # Number of steps
alpha = 1.7              # Lévy exponent (1 < alpha < 2)
tmin = 0.01              # Minimum time between steps
v_mean = 10.0            # Mean velocity
dt = 1.0                 # Time increment

# Simulate Lévy flight
x_traj, y_traj = levy_flight_2D_Simplified(N, alpha, tmin, v_mean, dt)

# Time intervals for analysis
tau_list = np.arange(1, 100, 5)

# Calculate empirical moments
dx2 = []
dx4 = []
for tau_i in tau_list:
    dx = x_traj[int(tau_i):] - x_traj[:-int(tau_i)]
    dy = y_traj[int(tau_i):] - y_traj[:-int(tau_i)]
    displacement = dx**2 + dy**2
    dx2.append(np.mean(displacement))
    dx4.append(np.mean(displacement**2))

dx2_log = np.log(dx2)
dx4_log = np.log(dx4)

# Parameter estimation using optimization
def objective_function(params, tau_list, dx2_log, dx4_log, tmin):
    alpha_opt, v_mean_opt = params
    model_dx2_log = levy_moments_log(2, alpha_opt, v_mean_opt, tau_list, tmin)
    model_dx4_log = levy_moments_log(4, alpha_opt, v_mean_opt, tau_list, tmin)
    error = np.sum((dx2_log - model_dx2_log)**2 + (dx4_log - model_dx4_log)**2)
    return error

initial_guess = [alpha, v_mean]
bounds = [(1.1, 2.0), (v_mean * 0.5, v_mean * 1.5)]

result = optimize.minimize(
    objective_function,
    initial_guess,
    args=(tau_list, dx2_log, dx4_log, tmin),
    bounds=bounds
)

estimated_alpha, estimated_v_mean = result.x
print("Estimated Parameters:")
print(f"alpha = {estimated_alpha:.4f}")
print(f"v_mean = {estimated_v_mean:.4f}")

# Plot empirical and fitted moments
fitted_dx2_log = levy_moments_log(2, estimated_alpha, estimated_v_mean, tau_list, tmin)
fitted_dx4_log = levy_moments_log(4, estimated_alpha, estimated_v_mean, tau_list, tmin)

plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(np.log(tau_list), dx2_log, 'o', label='Empirical log M2')
plt.plot(np.log(tau_list), fitted_dx2_log, '-', label='Fitted log M2')
plt.xlabel('log(tau)')
plt.ylabel('log(M2)')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(np.log(tau_list), dx4_log, 'o', label='Empirical log M4')
plt.plot(np.log(tau_list), fitted_dx4_log, '-', label='Fitted log M4')
plt.xlabel('log(tau)')
plt.ylabel('log(M4)')
plt.legend()

plt.tight_layout()
plt.show()

Applying to Real-Life Data

IntLevPy can also be applied to real-life datasets, such as eye-tracking data or financial time series, for classification and analysis using the provided statistical methods.

Contact

For questions or inquiries, please contact:


This package is licensed under the MIT License.


GitHub Repository: IntLevPy on GitHub

PyPI Package: IntLevPy on PyPI

Documentation: IntLevPy Documentation

No AUTHORS information provided.

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

intlevpy-0.0.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

IntLevPy-0.0.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file intlevpy-0.0.2.tar.gz.

File metadata

  • Download URL: intlevpy-0.0.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for intlevpy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 ee2538f3d28e748615c99208023f2611425fd561ff763dc8436eaf85d9af0ae8
MD5 cb1fecc71b7150609f7718e523994e17
BLAKE2b-256 684327b72bb8a91e99656801e53c55802c6ae75c2bc3150c80e9e703422ad8e9

See more details on using hashes here.

File details

Details for the file IntLevPy-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: IntLevPy-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for IntLevPy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6660a99724f54fb5d9f39293a9b5fcfdef2c4c463a08353c298c91a6cc35f1bf
MD5 caeea673338127e0ef088bbcb45d7845
BLAKE2b-256 42d5604b1d5994944edef226537262a64d6c71e3ff19aa0de21186910a807299

See more details on using hashes here.

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