Question: import random import matplotlib.pyplot as plt ###### DO NOT change this function ##### # Function to plot the random walk for each animal def plot

import random
import matplotlib.pyplot as plt
###### DO NOT change this function #####
# Function to plot the random walk for each animal
def plot_walk(animal):
xs, ys = zip(*animal['positions'])
plt.figure(figsize=(10,10))
plt.scatter(xs, ys, color=animal['color'], edgecolor='k', alpha=0.7, s=100, marker=animal['marker'])
plt.plot(xs, ys, lw=1.5, ls='--', color=animal['color'])
plt.grid(True)
plt.title(f'Path of Random Walk for {animal["name"]}')
plt.xlabel('East-West')
plt.ylabel('North-South')
# Save the plot to a file
plt.savefig(f'{animal["name"]}_random_walk.png', dpi=300)
plt.close() # Close the figure
# You need to create the animal dictionary inside the main function where each animal has
# direction, probabilities, positions, marker and color attributes
animal ={}
def main():
chuck ={"name": "Chuck", "directions": ["N","S","E","W"],"probabilities":[0.25,0.25,0.25,0.25],"positions":[(0,0)],"marker":"o","color":"b"}
daisy ={"name": "Daisy","directions":[.5,.167,.167,.167],"positions":[(0,0),marker]:"s","color":"r"}
chester ={ "name": "Chester","directions": [.5,.5],"positions":[(0,0)], "marker": "^", "color":"g"}
for i in range(1000):
walk(Chuck)
walk(Daisy)
walk(Chester)
plot_walk(Chuck)
plot_walk(Daisy)
plot_walk(Chester)
def walk(animal):
direction = random.choices(animal["directions"],weight=animal ["probabilities"])[0]
x,y = animal["positions"[-1]]
if direction =="N":
y +=1
elif direction =="S":
y -=1
elif direction =="E":
x +=1
else:
x -=1
animal["positions"].append(x,y))
if __name__=='main':
main()
1. Random Walk - Description
Read the instructions carefully. Not following the instructions will result in you not getting the credit you want for the assignment.
Learning Outcomes
Use random choices to simulate random behavior in an experiment
Group data in an appropriate collection data structure such as a dictionary or list
Use Python matplotlib to put visual information on a graph and save it to a file
Separate generating numerical results from visualizing results
Use loops to avoid repeated code in your solution when working with dictionaries and lists
Structure
Files:
random_walk.py
dont modify function plot_walk that you are given
Output files:
Chuck_rw.png
Daisy_rw.png
Chester_rw.png
Background
In 1827, the Scottish botanist Robert Brown observed that pollen particles suspended in water seemed to float around at random. He had no plausible explanation for what came to be known as Brownian motion, and made no attempt to model it mathematically. Louis Bachelier presented a clear mathematical model in his doctoral thesis, The Theory of Speculation in 1900. His thesis was largely ignored by respectable academics because it dealt with the then disreputable field of understanding financial markets. In 1905, Albert Einstein used similar stochastic thinking in physics to describe how it could be used to confirm the existence of atoms. People seemed to think that understanding physics was more important than making money, and the world started paying attention.
Brownian motion is an example of a random walk. Today, random walks are widely used to model physical processes like diffusion, biological processes like the kinetics of displacement of RNA from heteroduplexes by DNA, and social processes like movements of the stock market.
We are interested in random walks because of their wide applications to many problems, and for learning more about how to structure simulations nicely in Python.
Problem
Farmer John has three interesting pets: Chuck the chicken, Daisy the dog, and Chester the cat. Each of these animals has a different style of wandering around:
Chuck the chicken loves to wander randomly. Each second, he takes a step in a random direction: North, South, East, or West, with equal probabilities. Chucks marker in a graph is a blue circle.
Daisy the dog loves to explore but she has a slight preference for the North. Each second, she takes a step with the following probabilities: North (50%), South (16.67%), East (16.67%), and West (16.67%). Daisys marker in a graph is a red square.
Chester the cat is a peculiar feline who only moves East or West, never North or South. Chesters marker is a green triangle.
Your task is to simulate the movement of each animal for 1000
steps and plot their steps. Assume they always start at the barn door, which we will label (0,0)
. We give you code for a function plot_walk(animal) that you call to visualize an animals path using matplotlib. Do not modify this function, just call it.
Key Requirements and Gradinig
While you might observe that certain rubrics are being automatically graded, please note that these scores will not be transferred to Canvas until your instructor or TA comprehensively reviews the entire project and manually assigns gra

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!