python
python
with Plotly
Likith(23d3090)
M.Kushi(23d3091)
Madhumitha M(23d3092)
Mahadev(23d3093)
Mahendra(23d3094)
What is Random walks?
• Random walk is a mathematical concept that describes a sequence of steps or
movements that are randomly determined.
• It can be thought of as a series of events or decisions that cannot be
predicted with certainty.
• It’s commonly used to model physical and financial processes.
-likith
Example for Random walks
Traffic Flow in a City: The movement of vehicles on a road network can be modeled
as a random walk, where vehicles take random turns at intersections and move along
the roads. By modeling traffic as a random walk, transportation planners can make
predictions about traffic flow and congestion in a city
Example to Demonstrate Random walk
• Let's say we flip the coin 5 times and get:Heads, Tails, Heads, Heads, Tails.
• The positions would look like this:
1. Start at 0.
2. Heads → Move to 1.
3. Tails → Move back to 0.
4. Heads → Move to 1.
5. Heads → Move to 2.
6. Tails → Move back to 1. -likith
Plotting one Dimensional(1D)Random Walk
• A 1D random walk is a sequence of steps taken along a one-dimensional line,
where each step is either forward (+1) or backward (-1). In a 1D random walk, the
position changes over time based on a sequence of random steps.
• How It Works
1. Starting Position: Start at an initial position, say 0 on a number line.
2. Random Steps: Flip a coin or use any other random method to decide each step:
If heads, take a step forward (add +1).
If tails, take a step backward (subtract -1).
3. Continue for Many Steps: Repeat this process multiple times, recording the
position after each step.
-Kushi
Example
import random
import matplotlib.pyplot as plt
num_steps = 100 # Number of steps
position = 0 # Initial position
positions = [position] # List to keep track of positions over time
for _ in range(num_steps): # Simulate random walk
step = random.choice([-1, 1]) # Choose -1 (backward) or +1 (forward) randomly
position += step
positions.append(position)
plt.plot(positions) # Plot the random walk
plt.title("1D Random Walk")
plt.xlabel("Step")
plt.ylabel("Position")
plt.show() -Kushi
Output:
Plotting one Dimensional(2D)Random Walk
• A 2D random walk is a process where an object moves in a plane, taking random
steps in one of four possible directions: up, down, left, or right. This concept is
often used to model random movements in a grid or plane, like the movement of
particles in fluids or animals searching for food.
How It Works
1. Initial Position: Start from a starting point (e.g., ) in a 2D plane.
2. Random Movement: At each step, move in one of four directions:
Right (+1,0),Left(-1,0) ,Up(0,+1), Down (0,-1)
3. Continue for Many Steps: Repeat the process for a set number of steps, recording
each position.
-Madhumitha
Example
import random
import matplotlib.pyplot as plt
steps = 100# Number of steps
x, y = 0, 0# Starting position at (0, 0)
x_positions = [x]
y_positions = [y]
for _ in range(steps): # Simulate 2D random walk
dx, dy = random.choice([(1, 0), (-1, 0), (0, 1), (0, -1)])# Randomly choose a direction to move
x += dx
y += dy
x_positions.append(x)
y_positions.append(y)
plt.plot(x_positions, y_positions, marker='o', markersize=3, linestyle='-')# Plot the 2D random walk
plt.xlabel("X Position")
plt.ylabel("Y Position")
plt.title("2D Random Walk")
plt.show() -Madhumitha
Output:
Plotting one Dimensional(3D)Random Walk
A 3D random walk is a path in three-dimensional space where each step is taken in a
random direction along the x, y, or z axis. This concept extends the idea of a random
walk to three dimensions and is used to model complex random processes, such as the
diffusion of particles in a gas or 3D molecular movement.
How It Works
1. Initial Position: Start at an initial point, typically .
2. Random Movement: At each step, randomly choose one of six possible directions:
• Move along the x-axis: right or left
• Move along the y-axis: forward or backward
• Move along the z-axis: up or down
3. Continue for Many Steps: Repeat this process for a set number of steps, recording
each position.
-Mahadev
Example
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
steps = 100
x, y, z = 0, 0, 0# Starting position
x_positions, y_positions, z_positions = [x], [y], [z]
for _ in range(steps): # 3D random walk
dx, dy, dz = random.choice([(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)])
x, y, z = x + dx, y + dy, z + dz
x_positions.append(x)
y_positions.append(y)
z_positions.append(z)
# Plot
plt.figure().add_subplot(111, projection='3d').plot(x_positions, y_positions, z_positions)
plt.show() -Mahadev
Output:
Rolling Dice Using Plotly
• Rolling a dice involves randomly selecting a value from a set of possible
outcomes, typically 1 through 6 for a standard six-sided dice.
• We can simulate rolling a dice multiple times and then visualize the frequency
distribution of the outcomes using a histogram or bar chart with Plotly.
• Installing Plotly:
1. Install Plotly: pip install plotly
2. Verify Installation:
import plotly
print(plotly._version_)
3. Optional for Jupyter Notebooks : pip install "plotly[notebook]"
4. Upgrade Plotly (if needed): pip install --upgrade plotly
-Mahendra
Example
import random
import plotly.express as px
rolls = 500 # Simulate rolling a six-sided dice 500 times
results = [random.randint(1, 6) for _ in range(rolls)]
# Create a histogram of the dice roll results
fig = px.histogram(results, nbins=6, title="Distribution of Dice Rolls", labels={"value": "Dice Outcome"})
# Customize the layout for better appearance
fig.update_layout(
xaxis_title="Dice Outcome",
yaxis_title="Frequency",
bargap=0.2,
template="plotly_dark"
)
fig.show()# Show the plot -Mahendra
Output:
Conclusion
1. Random Walks : Random walks model unpredictable processes (1D, 2D, 3D).
Plotly visualizes paths, showing how randomness evolves over time.
3. Key Takeaways : Random processes (like random walks and dice rolls) are easily
simulated and visualized.
Plotly provides interactive visualizations for better insights into randomness and
probability.