Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/chap04.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 4
Copyright 2017 Allen Downey
Returning values
Here's a simple function that returns a value:
And here's how we call it.
If you run a function on the last line of a cell, Jupyter displays the result:
But that can be a bad habit, because usually if you call a function and don't assign the result in a variable, the result gets discarded.
In the following example, Jupyter shows the second result, but the first result just disappears.
When you call a function that returns a variable, it is generally a good idea to assign the result to a variable.
Exercise: Write a function called make_state
that creates a State
object with the state variables olin=10
and wellesley=2
, and then returns the new State
object.
Write a line of code that calls make_state
and assigns the result to a variable named init
.
Running simulations
Here's the code from the previous notebook.
Here's a modified version of run_simulation
that creates a State
object, runs the simulation, and returns the State
object.
Now run_simulation
doesn't plot anything:
But after the simulation, we can read the metrics from the State
object.
Now we can run simulations with different values for the parameters. When p1
is small, we probably don't run out of bikes at Olin.
When p1
is large, we probably do.
More for loops
linspace
creates a NumPy array of equally spaced numbers.
We can use an array in a for
loop, like this:
This will come in handy in the next section.
linspace
is defined in modsim.py
. You can get the documentation using help
.
linspace
is based on a NumPy function with the same name. Click here to read more about how to use it.
Exercise: Use linspace
to make an array of 10 equally spaced numbers from 1 to 10 (including both).
Exercise: The modsim
library provides a related function called linrange
. You can view the documentation by running the following cell:
Use linrange
to make an array of numbers from 1 to 11 with a step size of 2.
Sweeping parameters
p1_array
contains a range of values for p1
.
The following loop runs a simulation for each value of p1
in p1_array
; after each simulation, it prints the number of unhappy customers at the Olin station:
Now we can do the same thing, but storing the results in a SweepSeries
instead of printing them.
And then we can plot the results.
Exercises
Exercise: Wrap this code in a function named sweep_p1
that takes an array called p1_array
as a parameter. It should create a new SweepSeries
, run a simulation for each value of p1
in p1_array
, store the results in the SweepSeries
, and return the SweepSeries
.
Use your function to plot the number of unhappy customers at Olin as a function of p1
. Label the axes.
Exercise: Write a function called sweep_p2
that runs simulations with p1=0.5
and a range of values for p2
. It should store the results in a SweepSeries
and return the SweepSeries
.
Optional Exercises
The following two exercises are a little more challenging. If you are comfortable with what you have learned so far, you should give them a try. If you feel like you have your hands full, you might want to skip them for now.
Exercise: Because our simulations are random, the results vary from one run to another, and the results of a parameter sweep tend to be noisy. We can get a clearer picture of the relationship between a parameter and a metric by running multiple simulations with the same parameter and taking the average of the results.
Write a function called run_multiple_simulations
that takes as parameters p1
, p2
, num_steps
, and num_runs
.
num_runs
specifies how many times it should call run_simulation
.
After each run, it should store the total number of unhappy customers (at Olin or Wellesley) in a TimeSeries
. At the end, it should return the TimeSeries
.
Test your function with parameters
Display the resulting TimeSeries
and use the mean
function provided by the TimeSeries
object to compute the average number of unhappy customers (see Section 2.7).
Exercise: Continuting the previous exercise, use run_multiple_simulations
to run simulations with a range of values for p1
and
Store the results in a SweepSeries
, then plot the average number of unhappy customers as a function of p1
. Label the axes.
What value of p1
minimizes the average number of unhappy customers?