Skip to content

A comprehensive Python-based exploration of Random Walks in 1D, 2D, and 3D. This repository features simulations, statistical analysis, and interactive visualizations (using Plotly) to demonstrate the behavior of random walks, including unbiased and biased scenarios, distribution convergence, and the impact of dimensionality.

License

Notifications You must be signed in to change notification settings

PuspenduPH/Random-Walk-Simulations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧭Random Walk Simulations in 1D, 2D and 3D

License: MIT Python 3.8+ Jupyter Notebook Matplotlib Plotly

A comprehensive collection of random walk simulations in 1D, 2D, and 3D with interactive visualizations, animations, and statistical analysis.

1D Random Walk 2D Random Walk 3D Random Walk

πŸ“š Table of Contents

πŸ“˜ Introduction

A random walk is a mathematical object, known as a stochastic or random process, that describes a path that consists of a succession of random steps on some mathematical space such as the integers. This notebook provides a hands-on approach to understanding random walks by:

  • Simulating paths in 1D, 2D, and 3D.
  • Analyzing statistical properties like mean displacement and standard deviation.
  • Visualizing trajectories and distributions using matplotlib and plotly.
  • Creating interactive animations to observe the walk's evolution over time.
  • Exploring the effects of bias on the walker's path.

✨ Features

  • Multi-Dimensional Simulations: Explore random walks in 1D, 2D, and 3D space.
  • Statistical Analysis: Calculate and visualize mean position, standard deviation, and final position distributions over multiple trials.
  • Biased Walks: Simulate and analyze random walks where steps in certain directions are more probable.
  • Interactive Visualizations: Utilize plotly and ipywidgets for dynamic plots.
  • Animations: Generate .mp4 or .gif animations of random walks using matplotlib.animation.
  • Quadrant Analysis (2D): Track the walker's time in each quadrant.
  • Step Size Comparison (2D): Analyze the effect of different step sizes.
  • Simultaneous Walks (3D): Visualize multiple walkers starting from different positions.

πŸ“ Folder Structure

.
β”œβ”€β”€ 🎞️ ANIMATION/
β”‚   β”œβ”€β”€ 🎞️ 1D/         # Animations for 1D walks (.mp4, .gif)
β”‚   β”œβ”€β”€ 🎞️ 2D/         # Animations for 2D walks (.mp4, .gif)
β”‚   └── 🎞️ 3D/         # Animations for 3D walks (.mp4, .gif)
β”œβ”€β”€ πŸ–ΌοΈ IMAGES/
β”‚   β”œβ”€β”€ πŸ–ΌοΈ 1D/         # Static plots for 1D walks (.png, .jpg)
β”‚   β”œβ”€β”€ πŸ–ΌοΈ 2D/         # Static plots for 2D walks (.png, .jpg)
β”‚   └── πŸ–ΌοΈ 3D/         # Static plots for 3D walks (.png, .jpg)
β”œβ”€β”€ πŸ“Š STATISTICS/     # Optional: CSV or other files with statistical results
β”œβ”€β”€ πŸ§ͺ RandomWalk.ipynb  # The main Jupyter Notebook file
β”œβ”€β”€ πŸ“¦ requirements.txt # List of required Python packages
└── πŸ“„ README.md       # This file

πŸ› οΈ Setup and Installation

You can follow these steps to set up your environment and run the notebook.

βœ… Prerequisites

  • Python 3.7 or higher
  • pip (Python package installer)
  • git (for cloning the repository)

🧬 Cloning the Repository

git clone https://github.com/PuspenduPH/Random-Walk-Simulations
cd Random-Walk-Simulations

🧰 Setting Up a Virtual Environment (Recommended)

Using a virtual environment prevents dependency conflicts.

  1. Create a virtual environment (e.g., named randomwalk_env):
    python -m venv randomwalk_env
  2. Activate the environment:
    • Windows:
      randomwalk_env\Scripts\activate
    • Linux/macOS:
      source randomwalk_env/bin/activate
  3. (Optional) Upgrade pip:
    python -m pip install --upgrade pip

πŸ“¦ Installing Dependencies

Install all required Python libraries using the requirements.txt file:

pip install -r requirements.txt

Alternatively, install them manually:

pip install numpy scipy scikit-learn matplotlib plotly ipywidgets pandas tqdm ipympl

Note: FFmpeg is necessary to save animations in .mp4 format and must be installed manually (see Important Notes below).

πŸš€ Usage

▢️ Running the Notebook

  1. Ensure your virtual environment is activated.
  2. Navigate to the repository directory in your terminal.
  3. Launch Jupyter Notebook or JupyterLab:
    jupyter notebook
    # OR
    jupyter lab
  4. Open the random_walk_notebook.ipynb (or your actual notebook file name) in the Jupyter interface.
  5. Run the cells sequentially or explore specific sections.

⚠️ Important Notes

This notebook contains interactive visualizations, animations, and data manipulation tools. Follow these instructions to avoid common errors and ensure smooth execution:

  • Animation in Jupyter:

    • Install the ipympl package with:
      pip install ipympl
    • Use the following magic commands at the top of your notebook:
      %matplotlib ipympl  # For displaying animations
      %matplotlib widget  # For interactive widgets
    • Tip: Use only one magic command in a cell, depending on your requirements. If you need any help installing ipympl yo can follow this link
  • Saving Animations as .mp4:

    • Install FFmpeg:
      • Windows:
        • Download FFmpeg from ffmpeg.org.
        • Extract and add the bin folder to your system's PATH.
      • macOS/Linux:
        brew install ffmpeg     # For macOS with Homebrew
        sudo apt install ffmpeg # For Ubuntu/Debian
  • General Tips:

    • Use FuncAnimation from matplotlib.animation for creating animations.
    • Utilize PillowWriter if you prefer saving animations as .gif.
    • Use tqdm for progress tracking in loops or simulations.
    • Suppress unnecessary warnings:
      import warnings
      warnings.filterwarnings("ignore")
    • Use Python’s traceback module for inline error logging during debugging.

Reminder: Always restart the kernel after switching %matplotlib backends or installing new Jupyter-related packages to avoid rendering issues.

πŸ““ Notebook Content Overview

The notebook is divided into sections exploring random walks in increasing dimensions:

1. One-Dimensional Random Walk

  • Simulation: Basic simulation of a single 1D random walk.

  • Analysis: Calculating displacement and distance from the origin.

  • Multiple Trials: Running simulations multiple times to observe average behaviour.

  • Biased Random Walk: Introducing probabilities for moving left or right.

Probability p=0.7 Probability p=0.3
1DRandomWalk_trials_p_0.7 1DRandomWalk_trials_p_0.3
  • Mean and Standard Deviation Analysis: Examining how mean position and standard deviation evolve.

  • Distribution of Walker's Position: Visualizing the probability distribution of the walker's position after N steps.

  • Animation: Animating the 1D random walk.

  • Animation with Position Distribution: Animating the walk alongside its evolving position distribution.

    rdwalk_1D_dist_s50_t40_p0.5.mp4

2. Two-Dimensional Random Walk

  • Simulation & Plotting: Simulating and plotting a 2D walk using matplotlib and plotly.

  • Contour Plot: Visualizing the density of visited locations.

  • Quadrant Analysis: Tracking the proportion of time spent in each quadrant.

  • Quadrant Analysis with Animation: Animating the walk and the evolving quadrant distribution.

  • Analysis with Contour Plot and Heatmap: Combined visualisation of path and visit frequency.

  • Distribution of Walker's Position: Analyzing the 2D distribution of positions over multiple trials.

  • Distribution of Final Position: Visualising where walkers end up after N steps.

  • Animation: Basic animation of the 2D random walk path.

  • Animation with Position Frequency: Animating the walk alongside a heatmap of visited positions.

  • Step Size Comparison: Analysing walks with different step lengths.

  • 2D Biased Random Walk: Implementing bias in movement directions (e.g., tendency to move North-East).

    2D Random Walk with Angular Bias 2D Random Walk with Target Bias
  • Comparison with/without Bias: Visual comparison of biased vs. unbiased walks.

  • Biased Walk with Different Probabilities: Exploring various bias configurations.

    rdwalk_2D_with_counts_s100_t20_p0.4_q0.1_r0.25_s0.25.mp4

3. Three-Dimensional Random Walk

  • Simulation (Single Trial): Simulating and visualizing a single walk in 3D space using matplotlib or plotly.

    random_walk_3d_s100.mp4
  • Simulation (Multiple Trials): Running and analyzing multiple 3D walks.

    random_walk_3d_multi_s50_t20_1.1.mp4
  • Multiple Trials with Different Initial Positions: Simulating walkers starting from various points.

    random_ip_walk_3d_multi_s50_t20.mp4
  • Distribution of Walker's Final Position: Visualizing the 3D distribution of endpoints.

  • Simultaneous Random Walk: Animating multiple walks concurrently.

  • Biased Random Walk: Introducing directional bias in 3D steps.

    • 3D Random Walk with Target Bias

      distance_biased_rdwalk_3d_s50_t15.mp4
    • 3D Random Walk with Angular Bias

      angular_biased_rdwalk_3d_s50_t15.mp4
  • Visualise Biased Walks with IpyWidgets: Using widgets to explore different bias parameters interactively.

  • Comparison with/without Bias: Comparing trajectories of biased and unbiased 3D walks.

    final_walk_comparison_3d_s50_t15.mp4

🎞️ Animation & Visual Assets

Animations and visual outputs are organized into separate directories by dimensionality:

  • ANIMATION/1D/ – Animations for 1D random walk.
  • ANIMATION/2D/ – Animations for 2D random walk.
  • ANIMATION/3D/ – Animations for 3D random walk.
  • IMAGES/1D/, IMAGES/2D/, IMAGES/3D/ – Supporting images and figures.

Please add, update, or replace assets as your experiments change.

πŸ“Š Statistics and Analysis

The STATISTICS folder is intended for:

  • Storing detailed statistical reports and datasets related to random walk experiments.
  • Tracking parameters such as mean, variance, skewness, and other performance metrics of the simulations.

This modular organization makes maintaining a historical record of simulation outcomes and analysis easier.

🧩 Interactive Features

The notebook includes several interactive components:

  • Adjustable parameters for walk length, bias, and step size
  • Real-time visualization updates
  • Animated simulations with playback controls
  • Parameter exploration with ipywidgets
  • Statistical analysis with immediate feedback

πŸ”¬ Advanced Analysis

Statistical investigations included in the notebook:

  • Mean square displacement analysis
  • First return times
  • Probability distributions
  • Central limit theorem demonstrations
  • Quadrant occupation statistics
  • Bias effects on displacement

πŸ”— References

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b amazing-feature).
  3. Commit your changes (git commit -m 'Add some amazing feature').
  4. Push to the branch (git push origin amazing-feature).
  5. Open a Pull Request.

πŸ“„ License

Distributed under the MIT License. See LICENSE for more information.

About

A comprehensive Python-based exploration of Random Walks in 1D, 2D, and 3D. This repository features simulations, statistical analysis, and interactive visualizations (using Plotly) to demonstrate the behavior of random walks, including unbiased and biased scenarios, distribution convergence, and the impact of dimensionality.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published