Created
April 23, 2024 16:21
-
-
Save qihongl/0f2938e2eff2ce2c025f4a3b7d821ba1 to your computer and use it in GitHub Desktop.
walk-on-a-sphere.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| sns.set(style='white', palette='colorblind', context='talk') | |
| cpal = sns.color_palette('colorblind') | |
| import numpy as np | |
| def random_walk_on_sphere(n, T, epsilon=0.05): | |
| # Initialize the position on the sphere | |
| position = np.random.randn(n) | |
| position /= np.linalg.norm(position) | |
| # Path of the random walk | |
| path = [position.copy()] | |
| for _ in range(T): | |
| # Generate a small random perturbation | |
| perturbation = np.random.randn(n) * epsilon | |
| # Move to the new position | |
| new_position = position + perturbation | |
| # Normalize to stay on the sphere | |
| new_position /= np.linalg.norm(new_position) | |
| position = new_position | |
| # Store the position | |
| path.append(position.copy()) | |
| return np.array(path) | |
| # Example of a random walk on a 3-dimensional sphere with 100 steps | |
| n = 20 | |
| T = 100 | |
| path = random_walk_on_sphere(n, T) | |
| sns.heatmap(np.corrcoef(path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment